import { expect, it } from "vitest"; import { isComparisonOrCountFile, optionalFactDefaultCount, } from "../../scripts/verify/optional-fact-default-guard.mjs"; const expectDefaultCount = (source: string, expected: number): void => { expect(optionalFactDefaultCount(source), source).toBe(expected); }; it("counts property reads defaulted to 0, [] or false in any layout", () => { expectDefaultCount("const n = report.count ?? 0;", 1); expectDefaultCount("side?.omitted_fields ?? 0", 1); expectDefaultCount("const xs = result.items ?? [];", 1); expectDefaultCount("flags.complete ?? false", 1); expectDefaultCount("const n =\n report.count ??\n 0;\n", 1); expectDefaultCount("(left.exported_types?.length ?? 0)", 1); // The last link of a chain is the operand the default replaces. expectDefaultCount("left.count ?? right.count ?? 0", 1); expectDefaultCount( "// optional-fact-default: request default, not an observation\nconst n = left.count ??\n right.count ?? 0;", 0, ); }); it("ignores accumulators, indexed reads and other defaults", () => { expectDefaultCount("counts.get(key) ?? 0", 0); expectDefaultCount("bytes[offset] ?? 0", 0); expectDefaultCount("report.count ?? 0.5", 0); expectDefaultCount("report.count ?? 10", 0); expectDefaultCount("report.count ?? null", 0); expectDefaultCount("report.count ?? fallback.count", 0); }); it("honours an exemption marker that gives a reason", () => { expectDefaultCount( "// optional-fact-default: request default, not an observation\nconst offset = page.offset ?? 0;", 0, ); expectDefaultCount( "const offset = page.offset ?? 0; // optional-fact-default: request default, not observed", 0, ); expectDefaultCount("// e.g. report.count ?? 0 hides unknown", 0); // A leading block comment does not hide live code after it. expectDefaultCount("/* paging note */ const n = report.count ?? 0;", 1); // A default inside a block comment without leading stars is not code. expectDefaultCount( "/*\n report.count ?? 0 would hide unknown\n*/\nconst url = 'https://x';", 0, ); // `//` inside a string is not a comment. expectDefaultCount("const u = 'https://x'; const n = r.count ?? 0;", 1); // A bare marker, or one two lines above, does not exempt the default. expectDefaultCount( "// optional-fact-default:\nconst offset = page.offset ?? 0;", 1, ); // Any non-empty reason exempts, as for the other reason markers. expectDefaultCount( "// optional-fact-default: request option\nconst offset = page.offset ?? 0;", 0, ); expectDefaultCount( "// optional-fact-default: request default, not observed\n\nconst n = report.count ?? 0;", 1, ); }); it("counts executable defaults only, whatever strings and regular expressions hold", () => { // Text in a string is not code. expectDefaultCount('const example = "report.count ?? 0";', 0); expectDefaultCount("const t = `${a} report.count ?? 0`;", 0); // A regular expression holding `/*` or `//` does not hide the code after it. expectDefaultCount("const pattern = /[/*]/; const n = report.count ?? 0;", 1); expectDefaultCount("const p = /a\\/\\//u; const n = report.count ?? 0;", 1); // A marker inside a string does not exempt the default. expectDefaultCount( 'const s = "optional-fact-default: not a real reason"; const n = report.count ?? 0;', 1, ); }); it("tracks template substitutions and division", () => { // A substitution starts an expression, so its `/` opens a regular // expression whose `/*` does not hide the code after the template. expectDefaultCount( "const s = `${/[/*]/.test(name)}`;\nconst n = report.count ?? 0;", 1, ); expectDefaultCount( "const s = `${a} and ${/[/*]/.test(b)}`;\nconst n = report.count ?? 0;", 1, ); // A complete template ends an operand, so `/` after it divides. expectDefaultCount( "const r = `${a}` / 2; const n = report.count ?? 0; // /", 1, ); // A keyword-named property is an operand, so `/` after it divides. for (const name of ["return", "typeof", "in", "yield"]) expectDefaultCount(`const r = row.${name} / (report.count ?? 0);`, 1); expectDefaultCount( "const t = `${{ a: 1 }.a} x ?? 0`; const n = report.count ?? 0;", 1, ); expectDefaultCount( "const r = total / 2; const n = report.count ?? 0; // /", 1, ); // A keyword-named member call's `)` ends an operand, so `/` divides. expectDefaultCount( "const ratio = policy.if(ready) / (report.count ?? 0);", 1, ); expectDefaultCount("const n = (report.count ?? 0) + 1;", 1); expectDefaultCount("const n = report.count ?? 0 + 1;", 0); // A tighter operator makes the right operand a computed value. expectDefaultCount("const m = report.mask ?? 0 | fallbackMask;", 0); expectDefaultCount("f(report.count ?? 0, other.items ?? [])", 2); expectDefaultCount("const n = cond ? report.count ?? 0 : 1;", 1); }); it("counts defaults in template substitutions and after postfix updates", () => { expectDefaultCount("const s = `${report.count ?? 0}`;", 1); expectDefaultCount("const s = `${report.count ?? 0\n}`;", 1); expectDefaultCount("const ratio = i++ / (report.count ?? 0);", 1); expectDefaultCount("const ratio = i-- / (report.count ?? 0);", 1); // A non-null assertion completes its operand; a prefix `!` starts one. expectDefaultCount("const ratio = value! / (report.count ?? 0);", 1); expectDefaultCount("const ratio = f()! / (report.count ?? 0);", 1); expectDefaultCount("const ok = !/(report.count ?? 0)/u.test(s);", 0); // `export default` starts an expression, so `!` and `/` there are prefix // and a regular expression, not an assertion and a division. expectDefaultCount( "export default !/[/*]/.test(s); const n = report.count ?? 0;", 1, ); expectDefaultCount("export default !/(report.count ?? 0)/u.test(s);", 0); expectDefaultCount("export default /[/*]/; const n = report.count ?? 0;", 1); }); it("does not let a nested expression's marker exempt an outer default", () => { expectDefaultCount( "const n = build(\n // optional-fact-default: request default, not an observation\n options.offset ?? 0,\n).count ?? 0;", 1, ); }); it("exempts a default whose operand starts on an earlier line", () => { expectDefaultCount( "// optional-fact-default: request default, not an observation\nconst offset = getOptions(\n request,\n).offset ?? 0;", 0, ); expectDefaultCount( "// optional-fact-default: request default, not an observation\nconst offset = pages[\n index\n]?.options.offset ?? 0;", 0, ); // Without a marker the same defaults count. expectDefaultCount( "const offset = getOptions(\n request,\n).offset ?? 0;", 1, ); }); it("selects comparison, coverage and count files only", () => { expect(isComparisonOrCountFile("src/domain/artifactComparison.ts")).toBe( true, ); expect( isComparisonOrCountFile("src/domain/process/processCaptureCoverage.ts"), ).toBe(true); expect(isComparisonOrCountFile("src/domain/evidence.ts")).toBe(false); });