Files
ytplayer/frontend/shell-consistency.test.js
2026-10-08 04:12:16 +08:00

91 lines
5.1 KiB
JavaScript

const { test } = require('node:test');
const assert = require('node:assert/strict');
const { readFileSync, existsSync } = require('node:fs');
const { join } = require('node:path');
const root = join(__dirname, '..');
const html = readFileSync(join(__dirname, 'index.html'), 'utf8');
const sw = readFileSync(join(__dirname, 'sw.js'), 'utf8');
const shellMatch = sw.match(/const SHELL = \[([\s\S]*?)\n\];/);
assert.ok(shellMatch, 'service worker SHELL list is present');
const shell = [...shellMatch[1].matchAll(/['"](\/[^'"]+)['"]/g)].map((match) => match[1]);
const listed = new Set(shell);
test('every local script and stylesheet in index.html is precached by the service worker', () => {
const localAssets = [];
for (const match of html.matchAll(/<(?:script\b[^>]*\bsrc|link\b[^>]*\bhref)="([^"]+)"/gi)) {
const url = match[1];
if (/^(?:[a-z]+:|\/|#)/i.test(url)) continue;
if (!/\.(?:js|css)(?:[?#]|$)/i.test(url)) continue;
localAssets.push(`/${url.split(/[?#]/, 1)[0]}`);
}
assert.ok(localAssets.length > 0, 'found local script/style assets');
for (const asset of localAssets) assert.ok(listed.has(asset), `${asset} appears in SHELL`);
});
test('every SHELL asset exists on disk and there are no duplicate entries', () => {
assert.equal(new Set(shell).size, shell.length, 'SHELL has no duplicate entries');
for (const asset of shell) assert.ok(existsSync(join(__dirname, asset.slice(1))), `${asset} exists`);
});
test('assets.json describes the complete existing shell exactly once with eager core features', () => {
const { groups } = JSON.parse(readFileSync(join(__dirname, 'assets.json'), 'utf8'));
const described = new Set();
for (const [name, group] of Object.entries(groups)) {
assert.equal(group.contract, 1, `${name} initial contract`);
assert.equal(typeof group.eager, 'boolean');
for (const asset of group.files) {
assert.ok(!described.has(asset), `${asset} has one group`);
described.add(asset);
assert.ok(existsSync(join(__dirname, asset.slice(1))), `${asset} exists`);
}
}
for (const asset of shell) if (asset !== '/') assert.ok(described.has(asset), `${asset} is described`);
// piano-engine is dynamically imported and historically absent from SHELL.
for (const asset of described) assert.ok(listed.has(asset) || asset === '/piano-engine.mjs', `${asset} shell or documented dynamic import`);
assert.equal(groups.core.eager, true);
for (const name of ['playlist', 'settings', 'service-mode', 'lyrics-editor']) {
assert.equal(groups[`feature:${name}`].eager, true);
for (const asset of groups[`feature:${name}`].embeddedIn) assert.ok(groups.core.files.includes(asset), `${name} remains in core`);
}
});
test('the selected glass-stage layout includes its controls and panel dependencies', () => {
const { groups } = JSON.parse(readFileSync(join(__dirname, 'assets.json'), 'utf8'));
assert.deepEqual(groups['layout:glass-stage'].files, [
'/theme-glass.css', '/theme-glass-panel.css', '/glass-panel-layout.js',
]);
assert.deepEqual(groups['layout:glass-stage'].dependencies,['feature:layout-controls']);
assert.ok(groups['feature:layout-controls'].files.includes('/glass-controls.js'));
});
test('the shared sync core is eager and loads before the update helper',()=>{
const {groups}=JSON.parse(readFileSync(join(__dirname,'assets.json'),'utf8'));
assert.ok(groups.core.files.includes('/asset-sync-core.js'));
assert.ok(html.indexOf('src="asset-sync-core.js"')<html.indexOf('src="sw-update.js"'));
assert.ok(listed.has('/asset-sync-core.js'));
});
test('only core scripts and styles remain eager; native fallback and active layout bootstrap agree', () => {
const {groups}=JSON.parse(readFileSync(join(__dirname,'assets.json'),'utf8'));
const background=new Set(Object.values(groups).filter(g=>g.background).flatMap(g=>g.files));
for(const match of html.matchAll(/<(?:script\b[^>]*\bsrc|link\b[^>]*\bhref)="([^"]+)"/gi)) {
const path='/'+match[1].replace(/^\//,'').split(/[?#]/)[0];
assert.ok(!background.has(path),`${path} is not eager`);
}
assert.ok(html.indexOf('id="ytp-assets"')<html.indexOf('src="lazy.js"'));
assert.ok(html.indexOf('src="lazy.js"')<html.indexOf('</head>'));
assert.ok(html.indexOf('src="lazy.js"')<html.indexOf('rel="stylesheet"'),'head bootstrap must not wait for common CSS before requesting the selected layout');
assert.ok(html.indexOf('rel="preload"')<html.indexOf('src="lazy.js"'),'font preloads must start before the render-blocking bootstrap');
assert.deepEqual(JSON.parse(html.match(/id="ytp-assets">([^<]+)<\/script>/)[1]).groups,groups);
for(const name of ['playlist','settings','service-mode','lyrics-editor'])assert.ok(groups['feature:'+name].eager);
});
test('layout-independent visibility and transport defaults stay in eager CSS',()=>{
const {groups}=JSON.parse(readFileSync(join(__dirname,'assets.json'),'utf8'));
assert.ok(groups.core.files.includes('/layout-base.css'));
const shared=readFileSync(join(__dirname,'layout-base.css'),'utf8');
for(const selector of ['.stg-overlay','.deck-chips','.np-btn.done .i-save','.sleep-status .sl-ic','.ab-group','.mc-tune'])assert.ok(shared.includes(selector));
assert.ok(html.includes('href="layout-base.css"'));
});