Files
ytplayer/frontend/lazy.test.js

126 lines
9.1 KiB
JavaScript

const { test } = require('node:test');
const assert = require('node:assert/strict');
const { readFileSync } = require('node:fs');
const vm = require('node:vm');
function fixture(current, held = [], appCache = false, userAgent = 'AppleWebKit/605.1 Safari/605.1', controlled = false) {
const inserted = [], listeners = {}, writes = [], fetched = [];
const manifest = { buildTag: 'own', appCache, groups: { core: { files: [] }, 'layout:classic': { files: ['/classic.css'] }, 'layout:glass-stage': { files: ['/glass.css', '/one.js', '/two.js'] }, 'feature:test': { contract:1, files: ['/one.js', '/two.js'] } }, files: { '/app.js':{h:'a'}, '/classic.css': {h:'c'}, '/glass.css':{h:'g'}, '/one.js':{h:'1'}, '/two.js':{h:'2'} } };
const doc = { readyState:'loading', documentElement: { dataset:{} }, getElementById: () => ({ textContent:JSON.stringify(manifest) }), querySelectorAll: () => [], createElement: tag => ({ tagName:tag.toUpperCase() }), write: value => writes.push(value), addEventListener: (t,f) => listeners[t]=f };
doc.head = { append: node => { inserted.push(node); queueMicrotask(() => node.onload?.()); } };
const root = { document:doc, localStorage:{ getItem:() => '{"settings":{"layout":"glass-stage"}}' }, console, Promise, URL, crypto:{subtle:{}}, fetch:async key=>{fetched.push(key);return new Response('app');}, setTimeout, clearTimeout, navigator:{userAgent,serviceWorker:{ready:Promise.resolve({}),controller:controlled?{}:null,addEventListener(){}}}, addEventListener(){} };
if (current) root.caches = { open: async () => ({ match: async key => key === '/__ytp_asset_state' ? new Response(JSON.stringify({current})) : held.includes(key) ? new Response('', {headers:{'X-Asset-Hash':key.split('=')[1]}}) : undefined }) };
root.window=root; root.globalThis=root;
vm.runInNewContext(readFileSync(require.resolve('./lazy.js'),'utf8'), root);
return { root, inserted, writes, listeners, manifest, fetched };
}
test('external head bootstrap selects remembered CSS before paint without fetching a manifest', () => {
const {root,writes}=fixture();
assert.equal(root.document.documentElement.dataset.layout,'glass-stage');
assert.deepEqual(writes,['<link rel="stylesheet" href="/glass.css?v=g" data-lazy-css="/glass.css">']);
});
test('lazy groups execute ordered classic scripts once, sharing concurrent calls and dependencies', async () => {
const {root,inserted}=fixture();
const one=root.Lazy.load('feature:test'), two=root.Lazy.load('feature:test');
await Promise.all([one,two]);
assert.deepEqual(inserted.map(n=>n.src),['/one.js?v=1','/two.js?v=2']);
assert.equal(root.Lazy.loaded('feature:test'),true);
await root.Lazy.load('layout:glass-stage');
assert.equal(inserted.filter(n=>n.tagName==='SCRIPT').length,2);
});
test('failed injection can retry and proxy dispatches only after execution', async () => {
const {root,inserted}=fixture();
const append=root.document.head.append;
root.document.head.append=n=>{inserted.push(n);queueMicrotask(()=>n.onerror?.());};
await assert.rejects(root.Lazy.load('feature:test'),/one.js/);
root.document.head.append=append;
root.TestApi={open:value=>value+1};
const proxy=root.Lazy.proxy('feature:test',['open'],'TestApi');
assert.equal(await proxy.open(4),5);
assert.equal(root.Lazy.url('/two.js'),'/two.js?v=2');
await assert.rejects(root.Lazy.load('feature:missing'),/Unknown/);
});
test('executing groups are pinned even after a newer compatible manifest arrives', async () => {
const f=fixture(); await f.root.Lazy.load('feature:test');
const newer=JSON.parse(JSON.stringify(f.manifest)); newer.files['/one.js'].h='new';
f.root.caches={open:async()=>({match:async key=>key==='/__ytp_asset_state'?new Response(JSON.stringify({current:newer})):new Response('',{headers:{'X-Asset-Hash':'new'}})})};
await f.root.Lazy.load('feature:test');
assert.deepEqual(f.inserted.map(n=>n.src),['/one.js?v=1','/two.js?v=2']);
});
test('unexecuted compatible group immediately uses newly cached file URLs', async () => {
const next=fixture().manifest; next.buildTag='new'; next.files['/one.js'].h='new';
const f=fixture(next,['/one.js?v=new','/two.js?v=2']);
await f.root.Lazy.load('feature:test');
assert.deepEqual(f.inserted.map(n=>n.src),['/one.js?v=new','/two.js?v=2']);
assert.equal(f.root.Lazy.reloadRequired,false);
});
test('unexecuted incompatible group keeps its running-build N-1 URLs and requires reload', async () => {
const next=fixture().manifest; next.buildTag='new'; next.files['/one.js'].h='new'; next.groups['feature:test'].contract=2;
const f=fixture(next,['/one.js?v=new','/two.js?v=2']);
const events=[]; f.root.CustomEvent=class {constructor(type,opts){this.type=type;this.detail=opts.detail}}; f.root.dispatchEvent=e=>events.push(e.type);
await f.root.Lazy.load('feature:test');
assert.deepEqual(f.inserted.map(n=>n.src),['/one.js?v=1','/two.js?v=2']);
assert.equal(f.root.Lazy.reloadRequired,true);
assert.ok(events.includes('ytp-reload-required'));
});
test('workers receive the pinned import URL map in their first message', () => {
const f=fixture(), sent=[]; f.root.Worker=class { constructor(url){this.url=url;} postMessage(message,transfer){sent.push({message,transfer});} };
f.manifest.files['/worker.js']={h:'w'}; f.root.Lazy.manifest.files['/worker.js']={h:'w'};
const worker=f.root.Lazy.worker('/worker.js');worker.postMessage({op:'open'},['transfer']);worker.postMessage({op:'finish'});
assert.equal(worker.url,'/worker.js?v=w');assert.equal(sent[0].message.assetUrls.sha256,f.root.Lazy.url('/sha256.js'));assert.equal(sent[0].message.assetUrls.wasm,f.root.Lazy.url('/sha256-wasm.js'));assert.deepEqual(sent[0].transfer,['transfer']);assert.equal(sent[1].message.assetUrls,undefined);
});
test('new group files with a changed dependency contract cannot run against the old dependency',async()=>{
const next=fixture().manifest;next.buildTag='next';next.groups['feature:test'].dependencies=['feature:dep'];next.groups['feature:dep']={contract:2,files:[]};next.files['/one.js'].h='new';
const f=fixture(next,['/one.js?v=new','/two.js?v=2']);f.root.Lazy.manifest.groups['feature:test'].dependencies=['feature:dep'];f.root.Lazy.manifest.groups['feature:dep']={contract:1,files:[]};
await f.root.Lazy.load('feature:test');assert.deepEqual(f.inserted.map(n=>n.src),['/one.js?v=1','/two.js?v=2']);assert.equal(f.root.Lazy.reloadRequired,true);
});
test('classic chunk registry proxies use window.YT and keep the executed singleton pinned',async()=>{
const {root,inserted}=fixture();root.YT={TestApi:{open:n=>n+2}};
const proxy=root.Lazy.proxy('feature:test',['open'],'TestApi');
assert.equal(await proxy.open(3),5);
const singleton=root.YT.TestApi;await root.Lazy.load('feature:test');
assert.equal(root.YT.TestApi,singleton);assert.equal(inserted.length,2);
});
test('online launches request worker completion even with Save-Data and skip offline messages',async()=>{
const f=fixture(),sent=[];
const active={postMessage:m=>sent.push(m)},waiting={postMessage:m=>sent.push(m)};
f.root.navigator.connection={saveData:true};f.root.navigator.onLine=true;
f.root.navigator.serviceWorker={ready:Promise.resolve({active,waiting})};
f.root.Lazy.warm();await Promise.resolve();
assert.deepEqual(sent.map(m=>m.type),['COMPLETE_ASSETS','COMPLETE_ASSETS']);
f.root.navigator.onLine=false;f.root.Lazy.warm();await Promise.resolve();assert.equal(sent.length,2);
});
test('server-enabled head capture owns exactly one app response; native and rollback do not fetch',async()=>{
const modern=fixture(null,[],true);await modern.root.Lazy.appResponse;
assert.deepEqual(modern.fetched,['/app.js?v=a']);
const legacy=fixture();assert.deepEqual(legacy.fetched,[]);assert.equal(legacy.root.Lazy.appResponse,undefined);
});
test('head capture is cache-first even before a worker controls the page',async()=>{
const f=fixture(fixture().manifest,['/app.js?v=a'],true);const response=await f.root.Lazy.appResponse;
assert.equal(response.headers.get('X-Asset-Hash'),'a');assert.deepEqual(f.fetched,[]);
});
test('Chromium retains native script loading and only preloads the current app URL',async()=>{
const f=fixture(null,[],true,'AppleWebKit/537.36 Chrome/140.0 Safari/537.36');
assert.equal(f.root.Lazy.captureApp,false);assert.equal(f.root.Lazy.appResponse,undefined);assert.deepEqual(f.fetched,[]);
assert.equal(f.inserted.length,1);assert.equal(f.inserted[0].rel,'preload');assert.equal(f.inserted[0].as,'script');assert.equal(f.inserted[0].href,'/app.js?v=a');assert.equal(f.inserted[0].fetchPriority,'low');
});
test('iOS Chromium-branded browsers still use their WebKit response capture',async()=>{
const f=fixture(null,[],true,'AppleWebKit/605.1 CriOS/140.0 Mobile Safari/605.1');await f.root.Lazy.appResponse;
assert.equal(f.root.Lazy.captureApp,true);assert.deepEqual(f.fetched,['/app.js?v=a']);
});
test('controlled WebKit visits use worker-cached native app scripts without a response handoff',async()=>{
const f=fixture(null,[],true,'AppleWebKit/605.1 Safari/605.1',true);
assert.equal(f.root.Lazy.captureApp,false);assert.equal(f.root.Lazy.appResponse,undefined);assert.deepEqual(f.fetched,[]);
assert.equal(f.inserted[0].as,'script');assert.equal(f.inserted[0].href,'/app.js?v=a');assert.equal(f.inserted[0].fetchPriority,'low');
});