Files
leadboard/tests/deploy-layout.test.mjs
2026-11-04 03:12:05 +00:00

86 lines
5.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
const read = (name) => readFile(new URL(`../${name}`, import.meta.url), 'utf8');
// 計算函式在 calc-core.js(網頁與伺服器共用),畫面在 index.html;檢查時兩份一起看。
const readPage = async () => `${await read('index.html')}\n${await read('calc-core.js')}`;
test('seed data is not hidden by the persistent volume mount', async () => {
const [vibe, dockerfile, server] = await Promise.all([read('vibe.yaml'), read('Dockerfile'), read('server.js')]);
const mount = vibe.match(/mount_path:\s*(\S+)/)?.[1];
assert.ok(mount, 'vibe.yaml must declare a persistence mount_path');
const demoRelative = server.match(/const demoPath = resolve\(root, '([^']+)'\)/)?.[1];
assert.ok(demoRelative, 'server.js must define demoPath');
const demoInImage = `/app/${demoRelative}`;
assert.ok(!demoInImage.startsWith(`${mount}/`), `${demoInImage} would be hidden by the volume at ${mount}`);
const copied = [...dockerfile.matchAll(/^COPY\s+(?:--\S+\s+)*(.+)\s+(\S+)$/gm)].map(m => m[2]);
for (const dest of copied) {
const absolute = dest.startsWith('/') ? dest : `/app/${dest.replace(/^\.\//, '')}`;
assert.ok(!absolute.startsWith(mount), `Dockerfile copies into ${absolute}, which the volume at ${mount} hides`);
}
});
test('removed features (製程部, raise events) are gone from seed parameters', async () => {
const seed = JSON.parse(await read('seed/demo.json'));
for (const key of ['pcDept', 'supportConversionRate', 'rdIndividuals', 'raiseEvents']) assert.equal(key in seed.globalParams, false, key);
assert.equal(seed.baseData.some(person => person.dept === '製程部'), false);
});
test('Production Coordinator is recognised as PC for the PC cost share', async () => {
const html = await readPage();
const source = html.match(/function isProductionCoordinator\(person\) \{[\s\S]*?\n \}/)?.[0];
assert.ok(source, 'isProductionCoordinator helper must exist');
const isPC = new Function(`${source}; return isProductionCoordinator;`)();
assert.equal(isPC({ title: 'Production Coordinator', role: 'P - 製作' }), true);
assert.equal(isPC({ title: 'PC', role: 'P - 製作' }), true);
assert.equal(isPC({ title: 'Production Director', role: 'P - 製作' }), false);
assert.equal(isPC({ title: 'Concept Artist', role: 'A - 藝術家' }), false);
assert.equal((html.match(/isProductionCoordinator\(p\)/g) || []).length, 2);
});
test('interns are excluded from cost but still counted as A/P headcount', async () => {
const html = await readPage();
const source = html.match(/function isCostExcludedIntern\(person\) \{[\s\S]*?\n \}/)?.[0];
assert.ok(source);
const isIntern = new Function(`${source}; return isCostExcludedIntern;`)();
assert.equal(isIntern({ title: 'Intern', role: 'A - 藝術家' }), true);
assert.equal(isIntern({ title: '實習工程師', role: 'A - 藝術家' }), false);
assert.equal(isIntern({ title: 'Internal Tools Engineer', role: 'A - 藝術家' }), false);
assert.equal(isIntern({ title: 'Intern', role: 'O - 外包' }), false);
// cost is zeroed in calculateAll, while calculateDeptAllocations counts heads by role only
assert.match(html, /if \(isCostExcludedIntern\(p\)\) \{\s*p\.realSalaryTotal = 0;\s*p\.totalCost = 0;/);
const allocations = html.slice(html.indexOf('function calculateDeptAllocations'), html.indexOf('let totalWeight3'));
assert.equal(allocations.includes('isCostExcludedIntern'), false);
assert.match(allocations, /deptData\[d\]\.aNum \+= fte;/);
});
test('A:P headcount is month-weighted and counts 常態型外包 as A without cost', async () => {
const html = await readPage();
const source = html.match(/function activeMonths\(p\) \{[\s\S]*?\n \}/)?.[0];
assert.ok(source);
const activeMonths = new Function(`${source}; return activeMonths;`)();
assert.equal(activeMonths({ h1Start: 1, h1End: 6, h2Start: 7, h2End: 12 }), 12);
assert.equal(activeMonths({ h1Start: 1, h1End: 6, h2Start: '', h2End: '' }), 6);
assert.equal(activeMonths({ h1Start: 3, h1End: 6, h2Start: 7, h2End: 9 }), 7);
assert.equal(activeMonths({}), 0);
const allocations = html.slice(html.indexOf('function calculateDeptAllocations'), html.indexOf('let totalWeight3'));
assert.match(allocations, /const fte = activeMonths\(person\) \/ 12;/);
assert.match(allocations, /startsWith\('O'\)\) \{\s*deptData\[d\]\.aNum \+= fte;/);
assert.equal(/startsWith\('O'\)\) \{[^}]*aCost/.test(allocations), false);
assert.match(allocations, /isProductionCoordinator\(person\) \? pcHeadcountWeight : 1/);
assert.match(allocations, /pNum \+= fte \* pWeight/);
});
test('the page loads the shared calculation core and the image ships it', async () => {
const [html, dockerfile, server] = await Promise.all([read('index.html'), read('Dockerfile'), read('server.js')]);
const coreTag = html.indexOf('<script src="/calc-core.js"></script>');
assert.ok(coreTag > 0 && coreTag < html.indexOf('// --- CONFIGURATION LAYER ---'), 'calc-core.js must load before the main script');
assert.match(dockerfile, /\bcalc-core\.js\b/);
assert.match(dockerfile, /\bcalc-engine\.js\b/);
assert.match(server, /\['\/calc-core\.js', \['calc-core\.js', 'text\/javascript/);
for (const name of ['calculateAll', 'calculateInvoiceSummary', 'calculateDeptAllocations', 'codaPageByDept']) {
assert.equal(html.includes(`function ${name}(`), false, `${name} must live only in calc-core.js`);
}
});