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
35 changes: 31 additions & 4 deletions pm-stack/aggregator/scripts/produce-charge-candidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ async function stagedText(name: string): Promise<string | undefined> {
return readFile(join(INPUTS_DIR, name), "utf8");
}

/** #4663: the staged manifest period, validated. The manifest period
* is a date input like every other in this file, so it is held to the
* same isRealCalendarDate standard as the Plaid and Gmail dates, plus
* end >= start ordering. Staging that does not describe a real period
* is absent staging: throw, so the run fails visibly instead of
* writing a period-less candidates file under a success exit. */
export function stagedManifestPeriod(manifest: { start?: unknown; end?: unknown }): {
start: string;
end: string;
} {
const { start, end } = manifest;
if (
typeof start !== "string" ||
typeof end !== "string" ||
!isRealCalendarDate(start) ||
!isRealCalendarDate(end) ||
end < start
) {
throw new Error(
`staged manifest period is not a real calendar window: start=${JSON.stringify(start)} end=${JSON.stringify(end)}`,
);
}
return { start, end };
}

/** Plaid categories that are money movement, not merchant charges. */
const EXCLUDED_CATEGORIES = new Set(["Transfer", "Payment", "Interest", "Bank Fees"]);

Expand Down Expand Up @@ -378,11 +403,13 @@ async function main(): Promise<void> {
// Staged mode: the fetcher's manifest period governs — the staged
// payloads were fetched for exactly that window.
const manifest = JSON.parse(await readFile(join(INPUTS_DIR, "charge-inputs-manifest.json"), "utf8")) as {
start: string;
end: string;
start?: unknown;
end?: unknown;
};
start = manifest.start;
end = manifest.end;
// #4663: validate before any candidate work — a field-less or
// otherwise unreal manifest period must fail the run here, not
// surface as a written candidates file with period {}.
({ start, end } = stagedManifestPeriod(manifest));
}
const outPath =
argValue("--out") ??
Expand Down
46 changes: 45 additions & 1 deletion pm-stack/aggregator/test/produce-charge-candidates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { spawnSync } from "node:child_process";
import { existsSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { amountFromBody, gmailMessageDay, isNotChargeSubject } from "../scripts/produce-charge-candidates.ts";
import { amountFromBody, gmailMessageDay, isNotChargeSubject, stagedManifestPeriod } from "../scripts/produce-charge-candidates.ts";

describe("amountFromBody", () => {
test("records the paid total, not the subtotal", () => {
Expand Down Expand Up @@ -133,6 +133,50 @@ describe("gmailMessageDay (#2448)", () => {
});
});

// #4663: the staged manifest period is validated to the file's
// isRealCalendarDate standard before any candidate work; a manifest
// that does not describe a real period fails the run instead of
// writing a candidates file with period {} under a success exit.
describe("stagedManifestPeriod (#4663)", () => {
test("a field-less manifest throws", () => {
expect(() => stagedManifestPeriod({})).toThrow("not a real calendar window");
});

test("a manifest missing one field throws", () => {
expect(() => stagedManifestPeriod({ start: "2026-11-01" })).toThrow("not a real calendar window");
expect(() => stagedManifestPeriod({ end: "2026-11-02" })).toThrow("not a real calendar window");
});

test("malformed and non-calendar dates throw", () => {
expect(() => stagedManifestPeriod({ start: "2026-13-40", end: "2026-11-02" })).toThrow(
"not a real calendar window",
);
expect(() => stagedManifestPeriod({ start: "2026-11-01", end: "2026-02-30" })).toThrow(
"not a real calendar window",
);
expect(() => stagedManifestPeriod({ start: "11/01/2026", end: "2026-11-02" })).toThrow(
"not a real calendar window",
);
});

test("an inverted window throws", () => {
expect(() => stagedManifestPeriod({ start: "2026-11-02", end: "2026-11-01" })).toThrow(
"not a real calendar window",
);
});

test("a valid window passes through, including a single day", () => {
expect(stagedManifestPeriod({ start: "2026-11-01", end: "2026-11-02" })).toEqual({
start: "2026-11-01",
end: "2026-11-02",
});
expect(stagedManifestPeriod({ start: "2026-11-01", end: "2026-11-01" })).toEqual({
start: "2026-11-01",
end: "2026-11-01",
});
});
});

// #4660: staged mode must never fall back to the live Gmail CLI. A
// gmail-triage.json with fewer responses than the producer's queries is
// a broken staging: the run must fail visibly (non-zero exit, no
Expand Down
Loading