mirror of
https://github.com/morluto/rea.git
synced 2026-11-18 20:53:00 +00:00
fix(sqlite): refuse one-byte files instead of reporting an empty schema (#8727)
* fix(sqlite): refuse one-byte files instead of reporting an empty schema * test(sqlite): assert refusal facts instead of the full error sentence The one-byte and corrupt-input acceptance tests froze the sentence that SqliteDatabaseWorkerExecution builds from fixed wording, SQLite's diagnostic and the selected path. Share one helper that keeps the code, issue path, reason and single-issue shape exact, and checks the diagnostic and selected path separately, so rewording the sentence no longer breaks either regression. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5fd6c0db71
commit
f64433c93e
2 files changed
+44
-14
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import { statSync } from "node:fs";
|
||||
import { DatabaseSync } from "node:sqlite";
|
||||
import { z } from "zod";
|
||||
import { Budget } from "../domain/budget.js";
|
||||
@@ -1068,6 +1069,10 @@ export const inspectSqliteDatabaseSnapshot = (
|
||||
snapshotPath: string,
|
||||
input: InspectSqliteDatabaseInput,
|
||||
): Inspection => {
|
||||
// SQLite's Unix VFS reports a one-byte file as size zero, so the engine would
|
||||
// open it as an empty database and never reach its own header check.
|
||||
if (statSync(snapshotPath).size === 1)
|
||||
throw new SqliteInspectionFailure("format", "file is not a database");
|
||||
const db = new DatabaseSync(snapshotPath, {
|
||||
readOnly: true,
|
||||
allowExtension: false,
|
||||
|
||||
@@ -939,6 +939,43 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)(
|
||||
},
|
||||
);
|
||||
|
||||
/** Assert the refusal's facts, not the sentence that joins them. */
|
||||
const expectNotADatabaseRefusal = (error: unknown, selected: string): void => {
|
||||
expect(error).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [
|
||||
{
|
||||
path: ["path"],
|
||||
reason: "invalid_format",
|
||||
message: expect.stringContaining("file is not a database"),
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(error).toMatchObject({
|
||||
details: { issues: [{ message: expect.stringContaining(selected) }] },
|
||||
});
|
||||
};
|
||||
|
||||
cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)(
|
||||
"refuses a one-byte file instead of reporting a complete empty schema",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-sqlite-one-byte-");
|
||||
const selected = join(root, "truncated.db");
|
||||
await writeFile(selected, "S");
|
||||
const refused = await cli.run({
|
||||
arguments: ["inspect-sqlite-database", selected, "--json"],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
});
|
||||
expect(refused.exitCode).toBe(1);
|
||||
expectNotADatabaseRefusal(refused.json, selected);
|
||||
const call = await parityMcp.session.call();
|
||||
const response = await call("inspect_sqlite_database", { path: selected });
|
||||
expectNotADatabaseRefusal(parseMcpToolError(response).error, selected);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)(
|
||||
"returns actionable public errors for corrupt input, unknown or ambiguous tables and invalid record choices",
|
||||
async ({ cli }) => {
|
||||
@@ -948,28 +985,16 @@ cliTest.runIf(SQLITE_NATIVE_LIMITS_AVAILABLE)(
|
||||
// Distinct malformed selections each name their own selected path.
|
||||
for (const selected of [path, join(root, "other-invalid.db")]) {
|
||||
await writeFile(selected, "not a SQLite database");
|
||||
const expectedError = {
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [
|
||||
{
|
||||
path: ["path"],
|
||||
reason: "invalid_format",
|
||||
message: `Selected SQLite database is invalid (file is not a database): ${selected}`,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
const corrupt = await cli.run({
|
||||
arguments: ["inspect-sqlite-database", selected, "--json"],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
});
|
||||
expect(corrupt.exitCode).toBe(1);
|
||||
expect(corrupt.json).toMatchObject(expectedError);
|
||||
expectNotADatabaseRefusal(corrupt.json, selected);
|
||||
const response = await call("inspect_sqlite_database", {
|
||||
path: selected,
|
||||
});
|
||||
expect(parseMcpToolError(response).error).toMatchObject(expectedError);
|
||||
expectNotADatabaseRefusal(parseMcpToolError(response).error, selected);
|
||||
}
|
||||
const valid = join(root, "valid.db");
|
||||
const database = new DatabaseSync(valid);
|
||||
|
||||
Reference in new issue
Block a user