diff --git a/src/application/desktop/DesktopSqliteState.test.ts b/src/application/desktop/DesktopSqliteState.test.ts index 3a241c19e1..b6a4a0a74a 100644 --- a/src/application/desktop/DesktopSqliteState.test.ts +++ b/src/application/desktop/DesktopSqliteState.test.ts @@ -2,10 +2,7 @@ import { unknownTarget } from "../../domain/targetIdentity.js"; import { describe, expect, it } from "vitest"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; -import { - AnalysisCancelledError, - AnalysisOutputError, -} from "../../domain/analysisErrorCore.js"; +import { AnalysisCancelledError } from "../../domain/analysisErrorCore.js"; import { createEvidence } from "../../domain/evidence.js"; import { err, ok } from "../../domain/result.js"; import type { SqliteDatabase } from "../../domain/sqlite/sqliteDatabase.js"; @@ -36,8 +33,13 @@ const table = (name: string) => ({ ], }); -/** A SQLite report with two tables, and one row of the selected table. */ -const report = (selected: string | undefined): SqliteDatabase => ({ +type Rows = NonNullable["values"]; + +const report = ( + selected: string | undefined, + values: Rows, + tables: string[], +): SqliteDatabase => ({ artifact: { path: "/staged/state.db", sha256: "a".repeat(64), bytes: 4096 }, wal: null, engine: { name: "SQLite", version: "3.50.2" }, @@ -46,7 +48,7 @@ const report = (selected: string | undefined): SqliteDatabase => ({ schema: { scope: "database", completeness: "complete", - tables: [table("notes"), table("broken")], + tables: tables.map(table), indexes: [], views: [], triggers: [], @@ -57,9 +59,9 @@ const report = (selected: string | undefined): SqliteDatabase => ({ : { table: selected, columns: ["id"], - values: [[{ type: "integer", value: "1" }]], + values, row_limit: 500, - returned_rows: 1, + returned_rows: values.length, truncated: false, order: "unspecified", row_offset: 0, @@ -68,12 +70,10 @@ const report = (selected: string | undefined): SqliteDatabase => ({ limitations: [], }); -/** A SQLite service whose table reads fail where `failure` says so. */ -const service = (failure: (table: string) => AnalysisError | undefined) => ({ +const service = (values: Rows, failure?: AnalysisError) => ({ inspect: async (input: unknown) => { const selected = (input as { readonly table?: string }).table; - const refused = selected === undefined ? undefined : failure(selected); - if (refused !== undefined) return err(refused); + if (selected === "broken" && failure !== undefined) return err(failure); return ok( createEvidence( unknownTarget("not-observed"), @@ -81,7 +81,11 @@ const service = (failure: (table: string) => AnalysisError | undefined) => ({ { operation: OPERATION, parameters: {}, - result: report(selected), + result: report( + selected, + values, + failure === undefined ? ["notes"] : ["notes", "broken"], + ), confidence: "observed", }, ), @@ -91,46 +95,24 @@ const service = (failure: (table: string) => AnalysisError | undefined) => ({ describe("readSqliteStateView", () => { it("distinguishes special REAL values from text when comparing captured rows", async () => { - const read = async (type: "real" | "text") => - readSqliteStateView( - { - inspect: async (input: unknown) => { - const selected = (input as { table?: string }).table; - const result: SqliteDatabase = report(selected); - result.schema.tables = [table("notes")]; - if (result.rows !== null) { - result.rows.values = ( - ["Infinity", "-Infinity", "-0"] as const - ).map((value) => [ - type === "real" - ? { type: "real", value } - : { - type: "text", - value, - bytes_hex: Buffer.from(value).toString("hex"), - encoding: "UTF-8", - }, - ]); - result.rows.returned_rows = 3; - } - return ok( - createEvidence( - unknownTarget("not-observed"), - { id: "sqlite", name: "SQLite", version: "1" }, - { - operation: OPERATION, - parameters: {}, - result, - confidence: "observed", - }, - ), - ); + const values = ["Infinity", "-Infinity", "-0"] as const; + const real = await readSqliteStateView( + service(values.map((value) => [{ type: "real", value }])), + "/staged/state.db", + ); + const text = await readSqliteStateView( + service( + values.map((value) => [ + { + type: "text", + value, + bytes_hex: Buffer.from(value).toString("hex"), + encoding: "UTF-8", }, - }, - "/staged/state.db", - ); - const real = await read("real"); - const text = await read("text"); + ]), + ), + "/staged/state.db", + ); expect(real).toMatchObject({ ok: true, view: { @@ -163,28 +145,6 @@ describe("readSqliteStateView", () => { }); }); - it("keeps the tables that read and records the error of one that did not", async () => { - const read = await readSqliteStateView( - service((name) => - name === "broken" - ? new AnalysisOutputError(OPERATION, "table unreadable") - : undefined, - ), - "/staged/state.db", - ); - - expect(read).toMatchObject({ - ok: true, - view: { - tables: { - notes: { primary_key: ["id"], rows: [[1]], truncated: false }, - broken: { error: expect.stringContaining("table unreadable") }, - }, - tables_truncated: false, - }, - }); - }); - it("propagates cancellation from a table read", async () => { const cancelled = new AnalysisCancelledError(OPERATION, { cause: "stopped", @@ -192,7 +152,7 @@ describe("readSqliteStateView", () => { await expect( readSqliteStateView( - service((name) => (name === "broken" ? cancelled : undefined)), + service([[{ type: "integer", value: "1" }]], cancelled), "/staged/state.db", ), ).rejects.toBe(cancelled); diff --git a/tests/acceptance/sqliteDatabaseCli.test.ts b/tests/acceptance/sqliteDatabaseCli.test.ts index a70f8be45a..aebc91cb59 100644 --- a/tests/acceptance/sqliteDatabaseCli.test.ts +++ b/tests/acceptance/sqliteDatabaseCli.test.ts @@ -716,11 +716,11 @@ const sourceState = async (path: string) => { }; }; -const createLosslessSnapshot = (path: string, journalMode: string): void => { +const createLosslessSnapshot = (path: string): void => { const database = new DatabaseSync(path); try { - database.exec(`PRAGMA journal_mode=${journalMode}`); database.exec(` + PRAGMA journal_mode=PERSIST; CREATE TABLE "odd "" table" ( id INTEGER PRIMARY KEY, payload BLOB, @@ -753,15 +753,17 @@ const createLosslessSnapshot = (path: string, journalMode: string): void => { } }; -cliTest - .runIf(SQLITE_NATIVE_LIMITS_AVAILABLE) - .for(["DELETE", "PERSIST", "TRUNCATE"])( - "inspects a real %s SQLite schema and lossless selected records identically through CLI and MCP", - async (journalMode, { cli }) => { +cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)( + "inspects a lossless SQLite snapshot with a retained rollback journal through CLI and MCP", + async ({ cli }) => { const root = await createTestTempDirectory("rea-sqlite-public-"); const path = join(root, "selected.db"); - createLosslessSnapshot(path, journalMode); + createLosslessSnapshot(path); const original = await readFile(path); + const journalPath = `${path}-journal`; + const { bytes: journalBytes, ...journalIdentity } = + await sourceState(journalPath); + expect(journalBytes.length).toBeGreaterThanOrEqual(28); const result = await cli.run({ arguments: [ "inspect-sqlite-database", @@ -875,6 +877,10 @@ cliTest "selected.db", ); expect(Buffer.compare(await readFile(path), original)).toBe(0); + const { bytes: afterBytes, ...afterIdentity } = + await sourceState(journalPath); + expect(afterBytes.equals(journalBytes)).toBe(true); + expect(afterIdentity).toEqual(journalIdentity); }, ); diff --git a/tests/boundary/providers/desktopExportSqliteMetadata.test.ts b/tests/boundary/providers/desktopExportSqliteMetadata.test.ts index 191c63ac94..5b821e1253 100644 --- a/tests/boundary/providers/desktopExportSqliteMetadata.test.ts +++ b/tests/boundary/providers/desktopExportSqliteMetadata.test.ts @@ -13,18 +13,14 @@ import { PYTHON_TASK_EVALUATOR } from "../../../src/domain/desktop/appTaskPython import { inspectSqliteDatabaseSnapshot } from "../../../src/sqlite/SqliteDatabaseInspection.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; -const collisions = [ - ["table", "pragma_table_list"], - ["table", "pragma_table_info"], - ["view", "pragma_table_list"], - ["view", "pragma_table_info"], -] as const; - -it.each( - (["UTF-8", "UTF-16le", "UTF-16be"] as const).flatMap((encoding) => - collisions.map(([kind, name]) => [encoding, kind, name] as const), - ), -)( +// Each PRAGMA must resolve past both kinds of application object. The other +// SQLite encoding boundaries exercise all three encodings independently. +it.each([ + ["UTF-8", "table", "pragma_table_list"], + ["UTF-16le", "table", "pragma_table_info"], + ["UTF-16be", "view", "pragma_table_list"], + ["UTF-8", "view", "pragma_table_info"], +] as const)( "reads exported %s SQLite state when a %s shadows %s", async (encoding, kind, name) => { const root = await createTestTempDirectory("desktop-sqlite-metadata-"); diff --git a/tests/boundary/providers/desktopSqliteViewBudget.test.ts b/tests/boundary/providers/desktopSqliteViewBudget.test.ts index 5ae57554b5..7e3d43e48d 100644 --- a/tests/boundary/providers/desktopSqliteViewBudget.test.ts +++ b/tests/boundary/providers/desktopSqliteViewBudget.test.ts @@ -42,7 +42,9 @@ const sqliteReaders = async (populate: (db: DatabaseSync) => void) => { const path = join(scratch, "state.db"); const db = new DatabaseSync(path); try { - db.exec("CREATE TABLE healthy(x); INSERT INTO healthy VALUES(1)"); + db.exec( + "CREATE TABLE healthy(x INTEGER PRIMARY KEY); INSERT INTO healthy VALUES(1)", + ); populate(db); } finally { db.close(); @@ -109,7 +111,7 @@ it("bounds each projected SQLite table and recovers the same complete facts in b expect(emitted.view).toEqual(typescript); expect(emitted.view.tables.healthy).toEqual({ columns: ["x"], - primary_key: [], + primary_key: ["x"], rows: [[1]], truncated: false, }); @@ -171,7 +173,7 @@ print(len(json.dumps(expected, ensure_ascii=False, separators=(",", ":")).encode } }); -it("keeps the native SQLite record limit independent of a larger view allowance", async () => { +it("keeps SQLite record and worker limits independent of a larger view allowance", async () => { // A tiny stored declaration expands beyond the native hex/record allowance. const fixture = await sqliteReaders((db) => db.exec( @@ -180,15 +182,25 @@ it("keeps the native SQLite record limit independent of a larger view allowance" ); const bytes = 32 * 1024 * 1024; const emitted = await fixture.emitted(bytes); - for (const view of [await fixture.typescript(bytes), emitted.view]) { - expect(view).toMatchObject({ - tables: { - healthy: { rows: [[1]] }, - native_limit: { error: expect.any(String) }, + const typescript = await fixture.typescript(bytes); + const healthy = { + columns: ["x"], + primary_key: ["x"], + rows: [[1]], + truncated: false, + }; + expect(typescript).toEqual({ + tables: { + healthy, + native_limit: { + error: expect.stringContaining( + "16 MiB reply budget before hex projection", + ), }, - }); - expect(JSON.stringify(view)).not.toContain("exceeds sqlite_view_bytes"); - } + }, + tables_truncated: false, + }); + expect(emitted.view).toMatchObject({ tables: { healthy } }); expect(emitted.view.tables.native_limit).toEqual({ error: expect.stringContaining("string or blob too big"), }); diff --git a/tests/boundary/sqlite/sqliteDatabaseSnapshot.test.ts b/tests/boundary/sqlite/sqliteDatabaseSnapshot.test.ts index e9eded8173..f920eefa47 100644 --- a/tests/boundary/sqlite/sqliteDatabaseSnapshot.test.ts +++ b/tests/boundary/sqlite/sqliteDatabaseSnapshot.test.ts @@ -62,6 +62,8 @@ it.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE).each(["PERSIST", "TRUNCATE"])( } const journalPath = `${database.path}-journal`; const before = await readFile(journalPath); + if (mode === "TRUNCATE") expect(before).toHaveLength(0); + else expect(before.subarray(0, 28).equals(Buffer.alloc(28))).toBe(true); const beforeStat = await lstat(journalPath, { bigint: true }); const snapshot = await capture(); const result = inspectSqliteDatabaseSnapshot(snapshot.snapshotPath, {