fix(backup): check the weekly copy's preview against the schema section by section

Taking the weekly preview from the writer dropped the schema check the
old scanner ran at the end, so a copy that violates the schema was
described as restorable. The collector now checks each section in its
JSON form as the writer shows it, an array section and a bulk table one
element at a time, plus the schema's one whole-document rule (a
canonical copy names every measurement by id). A copy that fails gets
no stored preview, and the preview route reads the copy itself, as it
did before. Nothing is kept beyond one element.

Measured on the preview memory test under --max-old-space-size=524:
held after the producer 3 MB with and without the check, raw peak
heapUsed 293 MB with it (335 MB without). The test now loads the
backup schema before its baseline: a module loaded once per process,
not memory that grows with the record.
This commit is contained in:
Marc-André Bombeck committed 2026-10-04 22:17:27 +02:00
1 parent 48ed149ef0
commit 4769502a4b
5 files changed
+133 -14

No files matched your search

@@ -101,8 +101,8 @@ describe("createBackupPreviewCollector", () => {
const expectedKeys = new BackupKeyIdCollector();
expectedKeys.visit(FILE);
it("says what a full read of the copy says", () => {
const collector = createBackupPreviewCollector();
it("says what a full read of the copy says", async () => {
const collector = await createBackupPreviewCollector();
observeAsTheWriterDoes(FILE, collector.observe);
const result = collector.finish(countsOf(FILE));
expect(result).not.toBeNull();
@@ -112,8 +112,8 @@ describe("createBackupPreviewCollector", () => {
expect(result!.keys.keyIds()).toEqual(["v3"]);
});
it("answers null for a copy that does not name its version, owner and date", () => {
const collector = createBackupPreviewCollector();
it("answers null for a copy that does not name its version, owner and date", async () => {
const collector = await createBackupPreviewCollector();
const { userId, ...rest } = FILE;
void userId;
observeAsTheWriterDoes(rest, collector.observe);
@@ -121,6 +121,64 @@ describe("createBackupPreviewCollector", () => {
});
});
describe("createBackupPreviewCollector against the schema", () => {
// A copy the schema refuses must not be described as restorable: a null
// preview sends the preview route to read the copy, which says why not.
async function previewOf(file: Record<string, unknown>) {
const collector = await createBackupPreviewCollector();
observeAsTheWriterDoes(file, collector.observe);
return collector.finish(countsOf(FILE));
}
it("answers null when a bulk row violates the schema", async () => {
const measurements = FILE.measurements.map((m, i) =>
i === 1 ? { ...m, measuredAt: "not a date" } : m,
);
expect(await previewOf({ ...FILE, measurements })).toBeNull();
});
it("answers null when a whole section violates the schema", async () => {
const labResults = [{ ...FILE.labResults[0], takenAt: 42 }];
expect(await previewOf({ ...FILE, labResults })).toBeNull();
});
it("answers null when the header does not parse", async () => {
expect(
await previewOf({ ...FILE, exportedAt: "yesterday-ish" }),
).toBeNull();
});
it("answers null for a canonical copy with a measurement without an id", async () => {
const measurements = FILE.measurements.map((m, i) =>
i === 0 ? { ...m, id: undefined } : m,
);
expect(await previewOf({ ...FILE, measurements })).toBeNull();
});
it("agrees with the whole-document parse on every case above", () => {
// The oracle: each refused file is refused by the full schema too.
const bad = [
{
...FILE,
measurements: FILE.measurements.map((m, i) =>
i === 1 ? { ...m, measuredAt: "not a date" } : m,
),
},
{ ...FILE, labResults: [{ ...FILE.labResults[0], takenAt: 42 }] },
{ ...FILE, exportedAt: "yesterday-ish" },
{
...FILE,
measurements: FILE.measurements.map((m, i) =>
i === 0 ? { ...m, id: undefined } : m,
),
},
];
for (const file of bad) {
expect(backupPayloadSchema.safeParse(file).success).toBe(false);
}
});
});
describe("storedPreviewFor", () => {
const chunked = { data: null, chunkCount: 4, chunkStreamId: "s1" };
const preview = () =>