Files
reverse-engineering-anything/tests/evaluation/budgetArithmeticGuard.test.ts
T
morluto 28332d43ca refactor(javascript): charge the worker's retained result bytes through an over-committing Budget (#11399)
* refactor(javascript): charge the worker's retained result bytes through an over-committing Budget

Static checkpoints may retain past the result budget up to the static capacity, and the worker reports that exhaustion itself. Budget.chargeOverCommitting records the excess (overCommitted) and reports no remainder until refunds bring use back under the maximum. The budget-arithmetic baseline is now empty.

* refactor(domain): charge hand-written allowances through Budget and count limit-checked fields (#11405)

* refactor(domain): charge hand-written allowances through Budget and count limit-checked fields

Pyc report and key-walk tallies, reverse-engineering capture admission, Playwright trace fact retention and semantic alias selections now charge a Budget. The budget-arithmetic guard also counts a field outside Budget classes that is adjusted by hand and compared with a limit-named value; the eleven remaining sites are a counted baseline.

* fix(verify): scope limit-checked fields to their class and match limit names in any case
2026-11-16 03:34:31 +08:00

104 lines
3.0 KiB
TypeScript

import { expect, it } from "vitest";
import { budgetArithmeticCount } from "../../scripts/verify/budget-arithmetic-guard.mjs";
it("counts arithmetic on fields named for an allowance", () => {
expect(budgetArithmeticCount("this.#remainingBytes -= size;")).toBe(1);
expect(budgetArithmeticCount("this.retained += size;")).toBe(1);
expect(budgetArithmeticCount("this.#arenaWriteOffset += size;")).toBe(0);
});
it("counts hand-checked fields inside a hand-written Budget class", () => {
const cuda = [
"export class CudaMetadataBudget {",
" #visits = 0;",
" #readBytes = 0;",
" visit(): void {",
" if (++this.#visits > 262144) throw failure();",
" }",
" read(size: number): void {",
" if (size > LIMIT - this.#readBytes) throw failure();",
" this.#readBytes += size;",
" }",
"}",
].join("\n");
expect(budgetArithmeticCount(cuda)).toBe(2);
});
it("ignores unchecked tallies and fields not compared with a limit", () => {
const tallies = [
"class ProtobufBudget {",
" async checkpoint(): Promise<void> {",
" if (++this.#work % 256 === 0) await yieldTurn();",
" this.#transient += bytes;",
" const done = () => this.#transient;",
" }",
"}",
"class Reader {",
" read(size: number): void {",
" this.#observed += size;",
" if (this.#observed > this.#previous) this.#grew = true;",
" }",
"}",
].join("\n");
expect(budgetArithmeticCount(tallies)).toBe(0);
});
it("counts a field outside Budget classes compared with a limit once", () => {
const reader = [
"class Reader {",
" read(size: number): void {",
" if (this.#bytes + size > LIMIT) throw failure();",
" this.#bytes += size;",
" }",
" key(size: number): void {",
" this.#keyBytes += size;",
" if (this.#keyBytes > this.input.max_file_bytes) throw failure();",
" this.#keyBytes += 1;",
" }",
"}",
].join("\n");
expect(budgetArithmeticCount(reader)).toBe(2);
});
it("counts the same field name separately in each class", () => {
const readers = [
"class First {",
" read(size: number): void {",
" if (this.#bytes + size > LIMIT) throw failure();",
" this.#bytes += size;",
" }",
"}",
"class SecondBudget {",
" #bytes = 0;",
" observe(size: number): void {",
" this.#bytes += size;",
" }",
"}",
"class Third {",
" read(size: number): void {",
" this.#bytes += size;",
" if (this.#bytes > LIMIT) throw failure();",
" }",
"}",
].join("\n");
expect(budgetArithmeticCount(readers)).toBe(2);
});
it.each([
"CAPACITY",
"FACT_ALLOWANCE",
"FACT_BUDGET",
"MAX_BYTES",
"byteLimit",
])("counts a field compared with %s", (limit) => {
const reader = [
"class Reader {",
" read(size: number): void {",
" this.#facts += size;",
` if (this.#facts > ${limit}) throw failure();`,
" }",
"}",
].join("\n");
expect(budgetArithmeticCount(reader)).toBe(1);
});