speed-round3-patch.test.mjs 2.2 KB

123456789101112131415161718192021222324252627282930
  1. import { test } from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { localCandidates, MOBILE_HELPERS, MONTHLY_HELPERS, OPERATIONS_HELPERS, patchFunctions } from '../speed-round3-patch.mjs';
  4. test('only allowlisted function bodies change; handlers, constants and unselected helpers stay exact', () => {
  5. const before = 'const tenant="co";\nfunction target(){return 1;}\nfunction retained(){return "same";}\nhandler=retained;';
  6. const candidate = 'const tenant="wrong";function target(){return 2;} function added(){return target();} function retained(){throw Error("wrong");} handler=target;';
  7. const result = patchFunctions(before, candidate, ['target', 'added']);
  8. assert.match(result.code, /const tenant="co"/); assert.match(result.code, /function retained\(\)\{return "same";\}/); assert.match(result.code, /handler=retained/);
  9. assert.deepEqual(result.audit.functions.map(row => [row.name, row.action]), [['target', 'replace'], ['added', 'add']]);
  10. assert.equal(patchFunctions(result.code, candidate, ['target', 'added']).code, result.code);
  11. });
  12. test('missing or duplicate declarations fail closed instead of guessing a release target', () => {
  13. assert.throws(() => patchFunctions('', '', ['missing']), /Expected unique/);
  14. assert.throws(() => patchFunctions('function x(){} function x(){}', 'function x(){}', ['x']), /Expected unique/);
  15. assert.throws(() => patchFunctions('function x(){}', 'function x(){} function x(){}', ['x']), /Expected unique/);
  16. assert.throws(() => patchFunctions('', 'function x(){}', ['x', 'x']), /Duplicate helper/);
  17. });
  18. test('every final local mobile/monthly/operations helper can be selected and calculator exposes monthly date semantics', () => {
  19. const candidates = localCandidates();
  20. for (const [scope, names] of [['mobile', [...MOBILE_HELPERS, ...MONTHLY_HELPERS]], ['operations', OPERATIONS_HELPERS]]) {
  21. const built = patchFunctions('function handler(){return "unchanged entrypoint";}', candidates[scope], names);
  22. assert.equal(built.audit.functions.length, names.length);
  23. assert.ok(built.audit.functions.every(row => row.action === 'add'));
  24. assert.match(built.code, /function handler\(\)\{return "unchanged entrypoint";\}/);
  25. }
  26. assert.match(candidates.mobile, /businessDate:payrollBusinessDate/);
  27. });