Files
leadboard/tests/calc-engine.test.mjs
walkdoggo.yehandClaude Opus 5 0fa7cc733d
CI / build (push) Successful in 32s
CI / build (pull_request) Successful in 32s
測試:核心參數的 CODA 分配規則與來源部門不顯示倍率
Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
2026-11-25 01:34:46 +00:00

97 lines
5.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import test from 'node:test';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { computeModel } from '../calc-engine.js';
const seed = JSON.parse(await readFile(new URL('../seed/demo.json', import.meta.url), 'utf8'));
test('server computes the board model from the shared calculation core', () => {
const model = computeModel(seed);
assert.equal(model.people.length, seed.baseData.length);
assert.ok(model.invoice.finalInvoiceTotal > 0);
assert.ok(Object.keys(model.deptStats).length > 0);
assert.equal(typeof model.allocations.deptData, 'object');
assert.ok(Object.values(model.coda).every(Number.isFinite));
});
test('each computation starts from a clean copy and leaves the input untouched', () => {
const before = JSON.stringify(seed);
const first = computeModel(seed);
const second = computeModel(seed);
assert.equal(JSON.stringify(seed), before);
assert.deepEqual(first, second);
});
test('interns count salary × 1 in the invoice only, never in CODA; 工讀生 count as O', () => {
const globalParams = { ...seed.globalParams, reportYear: 2027 };
const dept = Object.keys(computeModel(seed).coda)[0];
const staff = { name: 'A', title: 'Artist', role: 'A - 藝術家', dept, company: '夢想創造', salary: 50000, h1Start: 1, h1End: 6, h2Start: 7, h2End: 12 };
const intern = { ...staff, name: 'I', title: 'Intern', salary: 30000 };
const without = computeModel({ baseData: [staff], globalParams });
const withIntern = computeModel({ baseData: [staff, intern], globalParams });
assert.equal(withIntern.coda[dept], without.coda[dept]);
const internRow = withIntern.people.find(p => p.name === 'I');
assert.equal(internRow.totalCost, 0);
assert.equal(internRow.invoiceCost, 30000 * 12);
assert.ok(Math.abs(withIntern.invoice.totalPA - without.invoice.totalPA - 30000 * 12) < 1e-6);
const partTime = { ...staff, name: 'W', title: '工讀生', role: 'O - 外包', partTime: true, salary: 0, dayrate: 1600, salaryType: 'daily' };
const withPartTime = computeModel({ baseData: [staff, partTime], globalParams });
assert.equal(withPartTime.coda[dept], without.coda[dept]);
assert.ok(withPartTime.invoice.totalO > 0);
});
test('a group that is both 研發 and CG Lead gets both reductions; groups still add up to the department', async () => {
const synthetic = JSON.parse(await readFile(new URL('../seed/demo.json', import.meta.url), 'utf8'));
const dept = Object.keys(computeModel(synthetic).coda)[0];
const person = (name, group, salary) => ({ name, title: 'Artist', role: 'A - 藝術家', dept, group, company: '夢想創造', salary, h1Start: 1, h1End: 6, h2Start: 7, h2End: 12 });
const globalParams = { ...synthetic.globalParams, rdGroups: [`${dept}|R`], cgLeadGroups: [`${dept}|R`], rdReductionPct: 0.35, cgLeadReductionPct: 0.85 };
const model = computeModel({ baseData: [person('a', 'R', 50000), person('b', 'N', 50000)], globalParams });
const groups = model.codaGroups[dept].groups;
const initial = model.codaGroups[dept].coda / 2;
assert.ok(Math.abs(groups.R.coda - initial * 0.65 * 0.85) < 1e-6);
assert.ok(Math.abs(groups.R.coda + groups.N.coda - model.codaGroups[dept].coda) < 1e-6);
});
// 業務部這類單位:整包 CODA 分給四個部門,自己只算成本,不談指標。
function shareFixture() {
const base = { company: '夢想創造', h1Start: 1, h1End: 6, h2Start: 7, h2End: 12 };
return {
baseData: [
{ ...base, name: 'a1', title: 'Artist', role: 'A - 藝術家', dept: '甲部', salary: 90000 },
{ ...base, name: 'a2', title: 'Artist', role: 'A - 藝術家', dept: '乙部', salary: 30000 },
{ ...base, name: 'b1', title: 'Artist', role: 'A - 藝術家', dept: '業務部', salary: 40000 },
{ ...base, name: 'b2', title: 'Producer', role: 'P - 製作', dept: '業務部', salary: 20000 },
],
globalParams: { ...seed.globalParams, reportYear: 2027, orgStructure: null },
};
}
test('CODA 分配規則可由核心參數設定:來源部門歸零、不顯示倍率,總額不變', () => {
const fixture = shareFixture();
const before = computeModel(fixture);
assert.ok(before.coda['業務部'] > 0);
assert.ok(before.multiplierUnits.some(unit => unit.label === '業務部'));
const rules = [{ company: '夢想創造', from: ['業務部'], to: ['甲部', '乙部'] }];
const after = computeModel({ ...fixture, globalParams: { ...fixture.globalParams, codaShareRules: rules } });
assert.ok(Math.abs(after.coda['業務部']) < 1e-6);
assert.equal(after.multiplierUnits.some(unit => unit.label === '業務部'), false);
assert.ok(after.multiplierUnits.some(unit => unit.label === '甲部'));
const total = model => Object.values(model.coda).reduce((sum, value) => sum + value, 0);
assert.ok(Math.abs(total(before) - total(after)) < 1e-6);
// 依各自 CODA 比例分配:甲部的 CODA 是乙部的三倍,拿到的也是三倍。
const gained = dept => after.coda[dept] - before.coda[dept];
assert.ok(Math.abs(gained('甲部') / gained('乙部') - 3) < 1e-6);
});
test('格式不對的 CODA 分配規則整條跳過,空陣列代表完全不分配', () => {
const fixture = shareFixture();
const broken = [{ from: [], to: ['甲部'] }, { from: ['業務部'], to: [] }, 'nope', null];
const withBroken = computeModel({ ...fixture, globalParams: { ...fixture.globalParams, codaShareRules: broken } });
const withNone = computeModel({ ...fixture, globalParams: { ...fixture.globalParams, codaShareRules: [] } });
assert.deepEqual(withBroken.coda, withNone.coda);
assert.ok(withNone.coda['業務部'] > 0);
assert.ok(withNone.multiplierUnits.some(unit => unit.label === '業務部'));
});