diff --git a/src/application/investigation/EvidenceLedger.residualUnknown.test.ts b/src/application/investigation/EvidenceLedger.residualUnknown.test.ts index fc97b3c7579..15d681f706f 100644 --- a/src/application/investigation/EvidenceLedger.residualUnknown.test.ts +++ b/src/application/investigation/EvidenceLedger.residualUnknown.test.ts @@ -295,6 +295,59 @@ describe("residual unknown dependency histories", () => { }); }); +it("pages combined Unknown filters in ID order and sees revised heads", () => { + const store = ledger(); + const rows = Array.from({ length: 12 }, (_, index) => { + const result = store.recordUnknown( + input(`Question ${String(index)}`, { + domain: index < 8 ? "protocol" : "other", + severity: index % 2 === 0 ? "high" : "low", + }), + mutation(`page-${String(index)}`), + ); + if (!result.ok) throw result.error; + return result.value; + }); + const matches = [rows[0]!, rows[2]!, rows[4]!, rows[6]!].sort((left, right) => + left.unknown_id.localeCompare(right.unknown_id), + ); + const query = { + status: "open", + severity: "high", + domain: "protocol", + } as const; + expect(store.listUnknownsPage({ ...query, offset: 1, limit: 1 })).toEqual({ + items: matches.slice(1, 2), + total: 4, + offset: 1, + next_offset: 2, + }); + expect(store.listUnknownsPage({ ...query, offset: 4, limit: 1 })).toEqual({ + items: [], + total: 4, + offset: 4, + next_offset: null, + }); + expect(store.listUnknownsPage({ ...query, offset: 9, limit: 1 })).toEqual({ + items: [], + total: 4, + offset: 9, + next_offset: null, + }); + expect( + store.updateUnknown(update(matches[0]!), mutation("page-revision")).ok, + ).toBe(true); + expect(store.listUnknownsPage({ ...query, limit: 1 })).toEqual({ + items: matches.slice(1, 2), + total: 3, + offset: 0, + next_offset: 1, + }); + expect( + store.listUnknowns({ ...query, status: "investigating" }), + ).toMatchObject([{ unknown_id: matches[0]!.unknown_id, revision: 2 }]); +}); + describe("residual unknown registry resolution and history", () => { it("rejects inference for observed resolution, then accepts matching live evidence", () => { const store = ledger(); diff --git a/src/application/investigation/EvidenceLedger.ts b/src/application/investigation/EvidenceLedger.ts index ce905e1588b..f88aec1082e 100644 --- a/src/application/investigation/EvidenceLedger.ts +++ b/src/application/investigation/EvidenceLedger.ts @@ -1,3 +1,4 @@ +import { selectViewRows } from "../../domain/analysisView/selectViewRows.js"; import type { EvidenceInspectionRecords } from "../../domain/evidenceRecordSummary.js"; import { InvalidResidualUnknownGraphError } from "../../domain/residualUnknownGraphError.js"; import { z } from "zod"; @@ -552,30 +553,24 @@ export class EvidenceLedger { query.status === undefined && query.severity === undefined && query.domain === undefined; - const matching = unfiltered - ? sorted - : sorted.filter( - (unknown) => + const { rows, total } = selectViewRows( + sorted, + { offset, limit: query.limit ?? sorted.length }, + unfiltered + ? undefined + : (unknown) => (query.status === undefined || unknown.status === query.status) && (query.severity === undefined || unknown.severity === query.severity) && (query.domain === undefined || unknown.domain === query.domain), - ); - const start = Math.min(offset, matching.length); - const end = - query.limit === undefined - ? undefined - : start + Math.min(query.limit, matching.length - start); - const page = matching.slice(start, end); - const nextOffset = - offset <= matching.length && start + page.length < matching.length - ? start + page.length - : null; + (unknown) => structuredClone(unknown), + ); + const end = offset + rows.length; return { - items: page.map((unknown) => structuredClone(unknown)), - total: matching.length, + items: rows, + total, offset, - next_offset: nextOffset, + next_offset: end < total ? end : null, }; }