|
| 1 | +import { expect, test } from "bun:test"; |
| 2 | +import { mkdtemp } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { createCompactionArchive, hashAuthorizedBytes } from "../session/compaction-archive.js"; |
| 6 | +import { evidenceArchiveSearchPlugin } from "./evidence-archive-search-plugin.js"; |
| 7 | + |
| 8 | +test.each(["image/png", "application/pdf", "legacy-image"])( |
| 9 | + "archive %s pages recover every byte after reopening, including beyond the scan ceiling", |
| 10 | + async (format) => { |
| 11 | + const blobs = new Map<string, Uint8Array>(); |
| 12 | + const opts = { |
| 13 | + sessionId: "paging", |
| 14 | + contextDir: await mkdtemp(join(tmpdir(), "archive-paging-")), |
| 15 | + writeBlob: async (key: string, bytes: Uint8Array) => { |
| 16 | + blobs.set(key, bytes); |
| 17 | + }, |
| 18 | + readBlob: async (key: string) => { |
| 19 | + const bytes = blobs.get(key); |
| 20 | + if (bytes === undefined) throw new Error("missing"); |
| 21 | + return bytes; |
| 22 | + }, |
| 23 | + }; |
| 24 | + const archive = createCompactionArchive(opts); |
| 25 | + const bytes = Uint8Array.from( |
| 26 | + { length: format === "legacy-image" ? 6000 : 7 * 1024 * 1024 }, |
| 27 | + (_, index) => index % 251, |
| 28 | + ); |
| 29 | + const legacy = new TextEncoder().encode(Buffer.from(bytes).toString("base64")); |
| 30 | + blobs.set("img-legacy", legacy); |
| 31 | + const occurrence = |
| 32 | + format === "legacy-image" |
| 33 | + ? await archive.recordExistingBlobReference({ |
| 34 | + kind: "attachment", |
| 35 | + blobKey: "img-legacy", |
| 36 | + contentHash: hashAuthorizedBytes(legacy), |
| 37 | + provenance: "persistBlobs:aged-image", |
| 38 | + }) |
| 39 | + : await archive.recordAuthorizedPayload({ |
| 40 | + kind: "attachment", |
| 41 | + payload: bytes, |
| 42 | + binary: { |
| 43 | + encoding: "raw", |
| 44 | + name: "evidence", |
| 45 | + contentType: format, |
| 46 | + byteLength: bytes.length, |
| 47 | + }, |
| 48 | + }); |
| 49 | + const reopened = createCompactionArchive(opts); |
| 50 | + const plugin = evidenceArchiveSearchPlugin(() => reopened); |
| 51 | + const handler = plugin.middleware!(async () => { |
| 52 | + throw new Error("unexpected passthrough"); |
| 53 | + }); |
| 54 | + const read = async (offset: number) => |
| 55 | + String( |
| 56 | + ( |
| 57 | + await handler( |
| 58 | + { |
| 59 | + id: "read", |
| 60 | + name: "read_file", |
| 61 | + arguments: { path: `archive:///${occurrence.occurrenceId}`, offset, limit: 64 }, |
| 62 | + }, |
| 63 | + new AbortController().signal, |
| 64 | + ) |
| 65 | + ).content, |
| 66 | + ); |
| 67 | + if (format !== "legacy-image") { |
| 68 | + const deepPage = await read(110000); |
| 69 | + expect(deepPage).not.toContain("scan limit"); |
| 70 | + expect(deepPage).toMatch(/110001\t/); |
| 71 | + } |
| 72 | + const lines: string[] = []; |
| 73 | + let offset = 0; |
| 74 | + for (let page = 0; page < 3000; page += 1) { |
| 75 | + const text = await read(offset); |
| 76 | + expect(text.length).toBeLessThan(10_000); |
| 77 | + for (const match of text.matchAll(/^\s*(\d+)\t(.*)$/gm)) { |
| 78 | + expect(Number(match[1])).toBe(lines.length + 1); |
| 79 | + lines.push(match[2]!); |
| 80 | + } |
| 81 | + const next = /Use offset=(\d+) to continue/.exec(text); |
| 82 | + if (next === null) break; |
| 83 | + expect(Number(next[1])).toBeGreaterThan(offset); |
| 84 | + offset = Number(next[1]); |
| 85 | + } |
| 86 | + expect(lines[0]).toContain('"encoding":"base64"'); |
| 87 | + expect(Buffer.from(lines.slice(1).join(""), "base64").equals(Buffer.from(bytes))).toBe(true); |
| 88 | + }, |
| 89 | + 120_000, |
| 90 | +); |
0 commit comments