|
| 1 | +/** |
| 2 | + * Unit tests for the file-backed store guards (src/file-store.ts). |
| 3 | + * |
| 4 | + * Both DuckDB and SQLite create an empty database when opened on a path that |
| 5 | + * does not exist. For a warehouse connection that turns a wrong path into a |
| 6 | + * silent empty result set rather than an error, so opening is read-or-fail |
| 7 | + * unless the caller passes `create: true`. |
| 8 | + * |
| 9 | + * The end-to-end proof — a populated store reached through `--dir` from an |
| 10 | + * unrelated cwd, run against a compiled binary — lives in |
| 11 | + * packages/opencode/test/altimate/store-path-resolution.test.ts. |
| 12 | + */ |
| 13 | +import { describe, test, expect, mock, afterEach } from "bun:test" |
| 14 | +import { Database } from "bun:sqlite" |
| 15 | +import * as fs from "fs" |
| 16 | +import * as os from "os" |
| 17 | +import * as path from "path" |
| 18 | +import { allowsCreate, assertStoreExists, isLocalFilePath } from "../src/file-store" |
| 19 | + |
| 20 | +const CANARY_TABLE = "zorbulax_ledger" |
| 21 | + |
| 22 | +const tmpDirs: string[] = [] |
| 23 | +function tmp(): string { |
| 24 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "file-store-guard-")) |
| 25 | + tmpDirs.push(dir) |
| 26 | + return dir |
| 27 | +} |
| 28 | +afterEach(() => { |
| 29 | + while (tmpDirs.length) fs.rmSync(tmpDirs.pop()!, { recursive: true, force: true }) |
| 30 | +}) |
| 31 | + |
| 32 | +describe("isLocalFilePath", () => { |
| 33 | + test("treats in-memory and scheme-qualified targets as non-files", () => { |
| 34 | + expect(isLocalFilePath(":memory:")).toBe(false) |
| 35 | + expect(isLocalFilePath(":memory:named")).toBe(false) |
| 36 | + expect(isLocalFilePath("")).toBe(false) |
| 37 | + expect(isLocalFilePath("md:my_database")).toBe(false) |
| 38 | + expect(isLocalFilePath("motherduck:my_database")).toBe(false) |
| 39 | + expect(isLocalFilePath("s3://bucket/warehouse.duckdb")).toBe(false) |
| 40 | + }) |
| 41 | + |
| 42 | + test("treats real paths — including Windows drive letters — as files", () => { |
| 43 | + expect(isLocalFilePath("warehouse.duckdb")).toBe(true) |
| 44 | + expect(isLocalFilePath("./data/warehouse.duckdb")).toBe(true) |
| 45 | + expect(isLocalFilePath("/var/data/warehouse.duckdb")).toBe(true) |
| 46 | + expect(isLocalFilePath("C:\\data\\warehouse.duckdb")).toBe(true) |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe("assertStoreExists", () => { |
| 51 | + test("throws for a missing local file, naming the path it looked for", () => { |
| 52 | + const missing = path.join(tmp(), "absent.duckdb") |
| 53 | + expect(() => assertStoreExists({ type: "duckdb" }, missing, "DuckDB")).toThrow(missing) |
| 54 | + expect(() => assertStoreExists({ type: "duckdb" }, missing, "DuckDB")).toThrow("not found") |
| 55 | + expect(fs.existsSync(missing)).toBe(false) |
| 56 | + }) |
| 57 | + |
| 58 | + test("passes for an existing file, an in-memory target, or an explicit create", () => { |
| 59 | + const dir = tmp() |
| 60 | + const present = path.join(dir, "present.duckdb") |
| 61 | + fs.writeFileSync(present, "") |
| 62 | + expect(() => assertStoreExists({ type: "duckdb" }, present, "DuckDB")).not.toThrow() |
| 63 | + expect(() => assertStoreExists({ type: "duckdb" }, ":memory:", "DuckDB")).not.toThrow() |
| 64 | + expect(() => |
| 65 | + assertStoreExists({ type: "duckdb", create: true }, path.join(dir, "new.duckdb"), "DuckDB"), |
| 66 | + ).not.toThrow() |
| 67 | + }) |
| 68 | + |
| 69 | + test("allowsCreate only accepts a literal true", () => { |
| 70 | + expect(allowsCreate({ type: "duckdb", create: true })).toBe(true) |
| 71 | + expect(allowsCreate({ type: "duckdb", create: "true" })).toBe(false) |
| 72 | + expect(allowsCreate({ type: "duckdb" })).toBe(false) |
| 73 | + }) |
| 74 | +}) |
| 75 | + |
| 76 | +describe("DuckDB driver create-on-open", () => { |
| 77 | + test("refuses to open a missing store and creates nothing", async () => { |
| 78 | + // The real duckdb addon is an optional dependency; the guard runs before |
| 79 | + // any Database is constructed, so a stub is enough to prove it fires first. |
| 80 | + let constructed = false |
| 81 | + mock.module("duckdb", () => ({ |
| 82 | + default: { |
| 83 | + Database: class { |
| 84 | + constructor(_p: string, optsOrCb: any, cb?: (err: Error | null) => void) { |
| 85 | + constructed = true |
| 86 | + const done = typeof optsOrCb === "function" ? optsOrCb : cb! |
| 87 | + setTimeout(() => done(null), 0) |
| 88 | + } |
| 89 | + connect() { |
| 90 | + return {} |
| 91 | + } |
| 92 | + close(cb: any) { |
| 93 | + if (cb) cb(null) |
| 94 | + } |
| 95 | + }, |
| 96 | + }, |
| 97 | + })) |
| 98 | + |
| 99 | + const missing = path.join(tmp(), "absent.duckdb") |
| 100 | + const { connect } = await import("../src/duckdb") |
| 101 | + const connector = await connect({ type: "duckdb", path: missing }) |
| 102 | + |
| 103 | + await expect(connector.connect()).rejects.toThrow("not found") |
| 104 | + expect(constructed).toBe(false) |
| 105 | + expect(fs.existsSync(missing)).toBe(false) |
| 106 | + }) |
| 107 | +}) |
| 108 | + |
| 109 | +describe("SQLite driver create-on-open", () => { |
| 110 | + test("reads an existing store", async () => { |
| 111 | + const store = path.join(tmp(), "warehouse.db") |
| 112 | + const seed = new Database(store, { create: true }) |
| 113 | + seed.exec(`CREATE TABLE ${CANARY_TABLE}(id INTEGER)`) |
| 114 | + seed.close() |
| 115 | + |
| 116 | + const { connect } = await import("../src/sqlite") |
| 117 | + const connector = await connect({ type: "sqlite", path: store }) |
| 118 | + await connector.connect() |
| 119 | + const tables = await connector.listTables("main") |
| 120 | + await connector.close() |
| 121 | + |
| 122 | + expect(tables.map((t) => t.name)).toContain(CANARY_TABLE) |
| 123 | + }) |
| 124 | + |
| 125 | + test("refuses to open a missing store and creates nothing", async () => { |
| 126 | + const missing = path.join(tmp(), "absent.db") |
| 127 | + const { connect } = await import("../src/sqlite") |
| 128 | + const connector = await connect({ type: "sqlite", path: missing }) |
| 129 | + |
| 130 | + await expect(connector.connect()).rejects.toThrow("not found") |
| 131 | + expect(fs.existsSync(missing)).toBe(false) |
| 132 | + }) |
| 133 | + |
| 134 | + test("refuses a read-only connection to a missing store even with create: true", async () => { |
| 135 | + // A read-only open never creates, so `create` must not excuse the miss. |
| 136 | + const missing = path.join(tmp(), "absent-readonly.db") |
| 137 | + const { connect } = await import("../src/sqlite") |
| 138 | + const connector = await connect({ type: "sqlite", path: missing, readonly: true, create: true }) |
| 139 | + |
| 140 | + await expect(connector.connect()).rejects.toThrow("not found") |
| 141 | + expect(fs.existsSync(missing)).toBe(false) |
| 142 | + }) |
| 143 | + |
| 144 | + test("creates only when the caller opts in", async () => { |
| 145 | + const target = path.join(tmp(), "scratch.db") |
| 146 | + const { connect } = await import("../src/sqlite") |
| 147 | + const connector = await connect({ type: "sqlite", path: target, create: true }) |
| 148 | + await connector.connect() |
| 149 | + await connector.close() |
| 150 | + |
| 151 | + expect(fs.existsSync(target)).toBe(true) |
| 152 | + }) |
| 153 | +}) |
0 commit comments