Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pm-stack/aggregator/src/token-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,16 @@ export function collectFileExportAdapter(opts: {
}
if (
!isNonEmptyString(w.name) ||
!isFiniteNumber(w.usedPct)
!isFiniteNumber(w.usedPct) ||
w.usedPct < 0 ||
w.usedPct > 100
) {
return retainedOutcome(
"unparseable",
previous,
now,
"ok",
`file-export drift: window ${String(w.name)} lacks a name or a finite usedPct`,
`file-export drift: window ${String(w.name)} lacks a name, a finite usedPct, or a usedPct within 0-100`,
);
}
}
Expand Down
107 changes: 107 additions & 0 deletions pm-stack/aggregator/test/token-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,113 @@ describe("file-export adapter (the framework's first adapter)", () => {
}
});

test("an out-of-range window usedPct is unparseable drift, previous retained (#4269)", () => {
// The mapping treats pct < 0 or pct > 100 as malformed and silently
// skips the window; the adapter must fail closed instead of publishing
// the partial (or empty) entry as a fresh ok.
for (const windows of [
[
{ name: "5h", usedPct: -5 },
Comment on lines +249 to +252

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add property coverage for the percentage-validation invariant

The new parser validation is covered only by fixed examples, but the testing doctrine in AGENTS.md explicitly requires property tests for meaningful parser invariants. Add generated percentages and window collections asserting that any out-of-range percentage rejects the entire document and retains the previous entry, while finite percentages within the inclusive bounds preserve the windows. Keep these regression examples as complementary coverage.

AGENTS.md reference: AGENTS.md:L41-L45

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — added property coverage for the percentage-validation invariant (commit pushed to this PR). The repo's TypeScript has no property-testing library (checked pm-stack: no fast-check or equivalent in any package), so the property test is a deterministic seeded sweep, the same generated-inputs/invariant shape as the Rust proptest coverage: a mulberry32 generator with a fixed seed (failures reproduce exactly) draws percentages from the in-range, below-range, above-range, and non-finite pools and builds window collections of 1–4 windows over 200 iterations, asserting over all of them: any window outside the finite inclusive 0–100 bounds ⇒ the whole document is unparseable and the previous entry is retained; all windows in range ⇒ fresh ok with every window preserved (name and pct). The fixed regression examples stay as complementary coverage, as suggested. bun test test/token-adapter.test.ts: 19 pass; tsc --noEmit clean.

{ name: "weekly", usedPct: 10 },
],
[
{ name: "5h", usedPct: 250 },
{ name: "weekly", usedPct: 10 },
],
[{ name: "5h", usedPct: -5 }],
]) {
const out = collectFileExportAdapter({
now: NOW,
previous: PREV_ENTRY,
fileMtimeIso: "2026-10-29T04:59:00Z",
stale: false,
doc: {
service: "meta",
source: "file-export/meta-laptop",
status: "ok",
measuredAt: "2026-10-29T04:59:00Z",
windows,
},
});
expect(out.status).toBe("unparseable");
expect(out.value?.measuredAt).toBe("2026-10-29T04:30:00Z");
}
});

test("window usedPct at the 0 and 100 boundaries is a healthy reading (#4269)", () => {
const out = collectFileExportAdapter({
now: NOW,
previous: null,
fileMtimeIso: "2026-10-29T04:59:00Z",
stale: false,
doc: {
service: "meta",
source: "file-export/meta-laptop",
status: "ok",
measuredAt: "2026-10-29T04:59:00Z",
windows: [
{ name: "5h", usedPct: 0 },
{ name: "weekly", usedPct: 100 },
],
},
});
expect(out.status).toBe("ok");
expect(out.value?.windows).toHaveLength(2);
});

test("property: any out-of-range window rejects the whole document; all-in-range preserves every window (#4269)", () => {
// Deterministic seeded sweep (fixed seed: failures reproduce exactly).
// Generates percentages from the in-range, out-of-range, and non-finite
// pools and window collections of varying length, and asserts the
// adapter invariant over all of them: one bad window => the whole
// document is unparseable and the previous entry is retained; all
// windows finite within the inclusive 0-100 bounds => fresh ok with
// every window preserved.
let state = 0x4269;
const rand = () => {
state |= 0;
state = (state + 0x6d2b79f5) | 0;
let x = Math.imul(state ^ (state >>> 15), 1 | state);
x = (x + Math.imul(x ^ (x >>> 7), 61 | x)) ^ x;
return ((x ^ (x >>> 14)) >>> 0) / 4294967296;
};
const genPct = (): number => {
const pool = rand();
if (pool < 0.45) return rand() * 100; // in range
if (pool < 0.65) return -(rand() * 1000 + 0.001); // below range
if (pool < 0.85) return 100.001 + rand() * 1000; // above range
return [NaN, Infinity, -Infinity][Math.floor(rand() * 3)]; // non-finite
};
const inRange = (pct: number) => Number.isFinite(pct) && pct >= 0 && pct <= 100;
for (let iter = 0; iter < 200; iter++) {
const count = 1 + Math.floor(rand() * 4);
const windows = Array.from({ length: count }, (_, i) => ({ name: `w${i}`, usedPct: genPct() }));
const out = collectFileExportAdapter({
now: NOW,
previous: PREV_ENTRY,
fileMtimeIso: "2026-10-29T04:59:00Z",
stale: false,
doc: {
service: "meta",
source: "file-export/meta-laptop",
status: "ok",
measuredAt: "2026-10-29T04:59:00Z",
windows,
},
});
if (windows.every((w) => inRange(w.usedPct))) {
expect(out.status).toBe("ok");
expect(out.value?.windows).toHaveLength(count);
for (const w of windows) {
expect(out.value?.windows.some((v) => v.name === w.name && v.pctOfLimit === w.usedPct)).toBe(true);
}
} else {
expect(out.status).toBe("unparseable");
expect(out.value?.measuredAt).toBe("2026-10-29T04:30:00Z");
}
}
});

test("a non-string window name is unparseable drift, not a healthy reading", () => {
const out = collectFileExportAdapter({
now: NOW,
Expand Down
Loading