This commit is contained in:
MeghdadFadaee committed 2026-10-18 14:28:53 +03:30
commit 584a04500f
21 files changed
+1014

No files matched your search

+92
View File
@@ -0,0 +1,92 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { battleDetails, renderBattleCards } from '../scripts/render-battles.mjs';
import { directPreview, discoverPreview } from '../scripts/battle-previews.mjs';
const translations=JSON.parse(await readFile(new URL('../content/translations.json',import.meta.url),'utf8'));
const single={title:{en:'A single fight',fa:'یک نبرد'},url:'https://europe.albionbb.com/battles/448776222'};
const combined={title:{en:'A campaign',fa:'مجموعه نبردها'},url:'https://europe.albionbb.com/battles/multi?ids=448776222,441001581,440997498'};
const render=(battles,lang='en',previews={})=>renderBattleCards({battles,lang,previews,base:lang==='fa'?'../':'./',t:translations[lang],icon:()=>'<svg aria-hidden="true"></svg>'});
test('reports use direct images without a cache and keep artwork for unavailable previews',()=>{
for(const lang of ['en','fa']){
const html=render([single,combined],lang);
assert.equal((html.match(/class="battle-art"/g)||[]).length,2);
assert.equal((html.match(/class="battle-terrain"/g)||[]).length,2);
assert.match(html,/battle-card--single/);
assert.match(html,/battle-card--campaign/);
assert.ok(html.includes(single.title[lang]));
assert.ok(html.includes(combined.title[lang]));
assert.match(html,/#441001581/);
assert.equal((html.match(/class="battle-report-image"/g)||[]).length,2);
assert.match(html,/src="https:\/\/api.albionbb.com\/eu\/preview\/448776222"/);
assert.match(html,/src="https:\/\/api.albionbb.com\/eu\/preview\/multi\?ids=448776222,441001581,440997498"/);
assert.ok(!html.includes('battle-card-stats'));
assert.ok(!html.includes('<time'));
assert.ok(!html.includes('undefined'));
}
});
test('Europe single and combined reports resolve to their respective preview routes',()=>{
assert.equal(directPreview(single.url),'https://api.albionbb.com/eu/preview/448776222');
assert.equal(directPreview(single.url+'/'),'https://api.albionbb.com/eu/preview/448776222');
assert.equal(directPreview(combined.url),'https://api.albionbb.com/eu/preview/multi?ids=448776222,441001581,440997498');
assert.equal(directPreview('https://europe.albionbb.com/battles/multi?ids=441002770%2C441001581%2C440997498'),'https://api.albionbb.com/eu/preview/multi?ids=441002770,441001581,440997498');
assert.equal(directPreview('https://europe.albionbb.com/battles/multi/?ids=441002770,441001581'),'https://api.albionbb.com/eu/preview/multi?ids=441002770,441001581');
assert.equal(directPreview('https://europe.albionbb.com/battles/multi'),null);
assert.equal(directPreview('https://europe.albionbb.com/battles/multi?ids=441002770,invalid'),null);
assert.equal(directPreview('https://albionbb.com/battles/448776222'),null);
assert.equal(directPreview('https://europe.albionbb.com.evil.test/battles/448776222'),null);
assert.equal(directPreview('invalid'),null);
});
test('automatic discovery prefers report metadata over a derived endpoint',async()=>{
const image='https://api.albionbb.com/eu/preview/new-format';
const fetchPage=async url=>{
assert.equal(url,single.url);
return new Response(`<meta name="twitter:image" content="${image}">`);
};
assert.equal(await discoverPreview(single,fetchPage),image);
});
test('combined reports discover their own preview and overrides skip metadata fetching',async()=>{
const fetchPage=async()=>new Response('<meta property="og:image" content="/combined-preview.png">');
assert.equal(await discoverPreview(combined,fetchPage),'https://europe.albionbb.com/combined-preview.png');
assert.equal(await discoverPreview({...single,preview:'https://example.com/override.png'},()=>{throw Error('Should not fetch metadata');}),'https://example.com/override.png');
});
test('discovery falls back to single and combined endpoints when report metadata is unavailable',async()=>{
const offline=async()=>{throw Error('Offline');};
assert.equal(await discoverPreview(single,offline),'https://api.albionbb.com/eu/preview/448776222');
assert.equal(await discoverPreview(single,async()=>new Response('<html>No image metadata</html>')),'https://api.albionbb.com/eu/preview/448776222');
assert.equal(await discoverPreview(combined,offline),'https://api.albionbb.com/eu/preview/multi?ids=448776222,441001581,440997498');
assert.equal(await discoverPreview(combined,async()=>new Response('<html>No metadata</html>')),'https://api.albionbb.com/eu/preview/multi?ids=448776222,441001581,440997498');
await assert.rejects(discoverPreview({url:'https://albionbb.com/battles/multi?ids=1,2'},offline),/Offline/);
});
test('combined reports deduplicate battle IDs when counting recorded engagements',()=>{
assert.deepEqual(battleDetails(combined.url).ids,['448776222','441001581','440997498']);
const html=render([single,combined]);
assert.match(html,/<strong>2<\/strong><span>Archived reports/);
assert.match(html,/<strong>3<\/strong><span>Recorded engagements/);
});
test('previews retain underlying artwork and verified zero values remain visible',()=>{
const battle={...single,preview:'https://example.com/preview.png',kills:0,deaths:7,date:'2026-10-18'};
const html=render([battle],'fa',{[battle.url]:'assets/battles/cached.png'});
assert.match(html,/src="\.\.\/assets\/battles\/cached.png"/);
assert.match(html,/data-battle-preview/);
assert.match(html,/class="battle-terrain"/);
assert.match(html,/<dd>۰<\/dd>/);
assert.match(html,/<dd>۷<\/dd>/);
assert.match(html,/datetime="2026-10-18"/);
assert.match(render([battle]),/https:\/\/example.com\/preview.png/);
});
test('empty archives stay empty and report titles are escaped',()=>{
assert.equal(render([]),'');
const html=render([{...single,title:'<script>alert("x")</script>'}]);
assert.ok(!html.includes('<script>'));
assert.match(html,/&lt;script&gt;/);
});
+79
View File
@@ -0,0 +1,79 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile, mkdtemp, cp, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { spawnSync } from 'node:child_process';
import { escapeHtml, youtubeId, validate } from '../scripts/lib.mjs';
import { socialImage, battleKey } from '../scripts/battle-previews.mjs';
const site=JSON.parse(await readFile(new URL('../content/site.json',import.meta.url)));
const rewards=JSON.parse(await readFile(new URL('../content/rewards.json',import.meta.url)));
test('editable guild and reward content passes publication validation',()=>{
validate(site,rewards);
assert.equal(new Set(rewards.categories.map(c=>c.id)).size,rewards.categories.length);
});
test('YouTube parsing accepts supported URLs and rejects lookalike domains',()=>{
for(const u of ['https://youtu.be/dQw4w9WgXcQ','https://www.youtube.com/watch?v=dQw4w9WgXcQ','https://youtube.com/shorts/dQw4w9WgXcQ','https://youtube.com/live/dQw4w9WgXcQ']) assert.equal(youtubeId(u),'dQw4w9WgXcQ');
for(const u of ['javascript:alert(1)','https://youtube.com.evil.test/watch?v=dQw4w9WgXcQ','https://youtube.com/watch?v=bad']) assert.equal(youtubeId(u),null);
assert.equal(escapeHtml('<script>"&'), '&lt;script&gt;&quot;&amp;');
});
test('content validation rejects unsafe links and image paths',()=>{
for(const url of ['javascript:alert(1)','http://example.com']) assert.throws(()=>validate({...site,socials:[{name:'bad',url}]},rewards));
assert.throws(()=>validate({...site,battles:[{url:'https://albionbb.com.evil.test/battles/1'}]},rewards));
assert.throws(()=>validate({...site,clips:[{title:'test',type:'direct',url:'https://example.com/test.mp4',poster:'assets/../../private'}]},rewards));
});
test('battle preview metadata supports Twitter, Open Graph, attribute order and relative URLs',()=>{
const page='https://europe.albionbb.com/battles/448776222';
assert.equal(socialImage('<meta content="/image.png?a=1&amp;b=2" name="twitter:image">',page),'https://europe.albionbb.com/image.png?a=1&b=2');
assert.equal(socialImage("<meta property='og:image' content='https://example.com/battle.jpg'>",page),'https://example.com/battle.jpg');
assert.equal(socialImage('<meta name="twitter:image" content="javascript:alert(1)">',page),null);
assert.equal(socialImage('<html>No image</html>',page),null);
assert.equal(battleKey(page),'europe.albionbb.com-448776222');
const combined='https://europe.albionbb.com/battles/multi?ids=441002770,441001581,440997498';
const otherCombined='https://europe.albionbb.com/battles/multi?ids=441002770,441001581';
assert.notEqual(battleKey(combined),battleKey(otherCombined));
assert.equal(battleKey(combined),battleKey(combined));
});
test('static output includes indexable bilingual content, metadata, and domain files',async()=>{
const result=spawnSync(process.execPath,['scripts/build.mjs'],{cwd:new URL('../',import.meta.url),encoding:'utf8'});
assert.equal(result.status,0,result.stderr);
const read=p=>readFile(new URL(`../dist/${p}`,import.meta.url),'utf8');
const [en,fa,sitemap,cname,robots]=await Promise.all(['index.html','fa/index.html','sitemap.xml','CNAME','robots.txt'].map(read));
assert.match(en,/<html lang="en" dir="ltr">/);
assert.match(fa,/<html lang="fa" dir="rtl">/);
for(const html of [en,fa]){
assert.match(html,/application\/ld\+json/);assert.match(html,/hreflang="fa"/);assert.match(html,/rel="canonical"/);
assert.match(html,/og:image/);assert.match(html,/twitter:image/);
assert.equal((html.match(/<h1 /g)||[]).length,1);
assert.match(html,/id="rewards"/);
assert.ok(!html.includes('undefined'));assert.ok(!html.includes('javascript:'));
const ids=[...html.matchAll(/\bid="([^"]+)"/g)].map(m=>m[1]);
assert.equal(ids.length,new Set(ids).size);
}
assert.match(fa,/\.\.\/assets\/styles\.css/);
assert.match(sitemap,/\/fa\//);assert.equal(cname.trim(),new URL(site.siteUrl).hostname);
assert.match(robots,/Sitemap:/);
});
test('populated archive builds YouTube and direct clips, battle previews, and verified stats in both languages',async()=>{
const fixture=await mkdtemp(join(tmpdir(),'oneup-content-'));
try {
for(const file of ['scripts','content','src','public','package.json']) await cp(new URL(`../${file}`,import.meta.url),join(fixture,file),{recursive:true});
const configured=structuredClone(site);
configured.socials[0].url='https://discord.gg/example';
configured.clips=[
{title:{en:'YouTube fight',fa:'نبرد یوتیوب'},type:'youtube',url:'https://youtu.be/dQw4w9WgXcQ'},
{title:'Direct fight',type:'direct',url:'https://example.com/fight.mp4',poster:'assets/logo.jpg'}
];
configured.battles=[{title:{en:'Test battle',fa:'نبرد آزمایشی'},date:'2026-10-18',url:'https://europe.albionbb.com/battles/448776222',preview:'assets/logo.jpg',kills:42,deaths:7,outcome:'victory'}];
await writeFile(join(fixture,'content/site.json'),JSON.stringify(configured));
const result=spawnSync(process.execPath,['scripts/build.mjs'],{cwd:fixture,encoding:'utf8'});
assert.equal(result.status,0,result.stderr);
for(const path of ['index.html','fa/index.html']){
const html=await readFile(join(fixture,'dist',path),'utf8');
assert.match(html,/battle-card-grid/);assert.match(html,/battle-table/);assert.match(html,/data-video-type="youtube"/);
assert.match(html,/data-video-type="direct"/);assert.match(html,/discord.gg\/example/);assert.match(html,/2026-10-18/);
assert.ok(!html.includes('undefined'));
assert.match(html,/hqdefault.jpg/);
}
}finally{await rm(fixture,{recursive:true,force:true});}
});