|
| 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 remote targets as non-files", () => { |
| 34 | + expect(isLocalFilePath(":memory:")).toBe(false) |
| 35 | + expect(isLocalFilePath("")).toBe(false) |
| 36 | + expect(isLocalFilePath("md:my_database")).toBe(false) |
| 37 | + expect(isLocalFilePath("motherduck:my_database")).toBe(false) |
| 38 | + expect(isLocalFilePath("s3://bucket/warehouse.duckdb")).toBe(false) |
| 39 | + expect(isLocalFilePath("https://example.com/warehouse.duckdb")).toBe(false) |
| 40 | + // A scheme from a DuckDB extension we have never heard of is still not a |
| 41 | + // local file; the driver reports an unknown scheme rather than creating one. |
| 42 | + expect(isLocalFilePath("ducklake:my_catalog")).toBe(false) |
| 43 | + }) |
| 44 | + |
| 45 | + test("treats real paths — including Windows drive letters — as files", () => { |
| 46 | + expect(isLocalFilePath("warehouse.duckdb")).toBe(true) |
| 47 | + expect(isLocalFilePath("./data/warehouse.duckdb")).toBe(true) |
| 48 | + expect(isLocalFilePath("/var/data/warehouse.duckdb")).toBe(true) |
| 49 | + expect(isLocalFilePath("C:\\data\\warehouse.duckdb")).toBe(true) |
| 50 | + }) |
| 51 | + |
| 52 | + test("only the exact `:memory:` is in-memory — `:memory:name` is a real file", () => { |
| 53 | + // DuckDB writes a file literally named ":memory:named" for this path, and |
| 54 | + // a colon-prefixed name is an ordinary file to both engines. Classifying |
| 55 | + // them as non-files would let the guard be bypassed by a typo. |
| 56 | + expect(isLocalFilePath(":memory:named")).toBe(true) |
| 57 | + expect(isLocalFilePath(":memory")).toBe(true) |
| 58 | + expect(isLocalFilePath(":foo")).toBe(true) |
| 59 | + }) |
| 60 | +}) |
| 61 | + |
| 62 | +describe("assertStoreExists", () => { |
| 63 | + test("throws for a missing local file, naming the path it looked for", () => { |
| 64 | + const missing = path.join(tmp(), "absent.duckdb") |
| 65 | + expect(() => assertStoreExists({ type: "duckdb" }, missing, "DuckDB")).toThrow(missing) |
| 66 | + expect(() => assertStoreExists({ type: "duckdb" }, missing, "DuckDB")).toThrow("not found") |
| 67 | + expect(fs.existsSync(missing)).toBe(false) |
| 68 | + }) |
| 69 | + |
| 70 | + test("throws for a missing `:memory:`-lookalike rather than letting it be created", () => { |
| 71 | + const dir = tmp() |
| 72 | + const lookalike = path.join(dir, ":memory:named") |
| 73 | + expect(() => assertStoreExists({ type: "duckdb" }, lookalike, "DuckDB")).toThrow("not found") |
| 74 | + expect(fs.existsSync(lookalike)).toBe(false) |
| 75 | + }) |
| 76 | + |
| 77 | + test("passes for an existing file, an in-memory target, or an explicit create", () => { |
| 78 | + const dir = tmp() |
| 79 | + const present = path.join(dir, "present.duckdb") |
| 80 | + fs.writeFileSync(present, "") |
| 81 | + expect(() => assertStoreExists({ type: "duckdb" }, present, "DuckDB")).not.toThrow() |
| 82 | + expect(() => assertStoreExists({ type: "duckdb" }, ":memory:", "DuckDB")).not.toThrow() |
| 83 | + expect(() => |
| 84 | + assertStoreExists({ type: "duckdb", create: true }, path.join(dir, "new.duckdb"), "DuckDB"), |
| 85 | + ).not.toThrow() |
| 86 | + }) |
| 87 | + |
| 88 | + test("allowsCreate only accepts a literal true", () => { |
| 89 | + expect(allowsCreate({ type: "duckdb", create: true })).toBe(true) |
| 90 | + expect(allowsCreate({ type: "duckdb", create: "true" })).toBe(false) |
| 91 | + expect(allowsCreate({ type: "duckdb" })).toBe(false) |
| 92 | + }) |
| 93 | +}) |
| 94 | + |
| 95 | +describe("DuckDB driver create-on-open", () => { |
| 96 | + test("refuses to open a missing store and creates nothing", async () => { |
| 97 | + // The real duckdb addon is an optional dependency; the guard runs before |
| 98 | + // any Database is constructed, so a stub is enough to prove it fires first. |
| 99 | + let constructed = false |
| 100 | + mock.module("duckdb", () => ({ |
| 101 | + default: { |
| 102 | + Database: class { |
| 103 | + constructor(_p: string, optsOrCb: any, cb?: (err: Error | null) => void) { |
| 104 | + constructed = true |
| 105 | + const done = typeof optsOrCb === "function" ? optsOrCb : cb! |
| 106 | + setTimeout(() => done(null), 0) |
| 107 | + } |
| 108 | + connect() { |
| 109 | + return {} |
| 110 | + } |
| 111 | + close(cb: any) { |
| 112 | + if (cb) cb(null) |
| 113 | + } |
| 114 | + }, |
| 115 | + }, |
| 116 | + })) |
| 117 | + |
| 118 | + const missing = path.join(tmp(), "absent.duckdb") |
| 119 | + const { connect } = await import("../src/duckdb") |
| 120 | + const connector = await connect({ type: "duckdb", path: missing }) |
| 121 | + |
| 122 | + await expect(connector.connect()).rejects.toThrow("not found") |
| 123 | + expect(constructed).toBe(false) |
| 124 | + expect(fs.existsSync(missing)).toBe(false) |
| 125 | + }) |
| 126 | +}) |
| 127 | + |
| 128 | +describe("SQLite driver create-on-open", () => { |
| 129 | + test("reads an existing store", async () => { |
| 130 | + const store = path.join(tmp(), "warehouse.db") |
| 131 | + const seed = new Database(store, { create: true }) |
| 132 | + seed.exec(`CREATE TABLE ${CANARY_TABLE}(id INTEGER)`) |
| 133 | + seed.close() |
| 134 | + |
| 135 | + const { connect } = await import("../src/sqlite") |
| 136 | + const connector = await connect({ type: "sqlite", path: store }) |
| 137 | + await connector.connect() |
| 138 | + const tables = await connector.listTables("main") |
| 139 | + await connector.close() |
| 140 | + |
| 141 | + expect(tables.map((t) => t.name)).toContain(CANARY_TABLE) |
| 142 | + }) |
| 143 | + |
| 144 | + test("refuses to open a missing store and creates nothing", async () => { |
| 145 | + const missing = path.join(tmp(), "absent.db") |
| 146 | + const { connect } = await import("../src/sqlite") |
| 147 | + const connector = await connect({ type: "sqlite", path: missing }) |
| 148 | + |
| 149 | + await expect(connector.connect()).rejects.toThrow("not found") |
| 150 | + expect(fs.existsSync(missing)).toBe(false) |
| 151 | + }) |
| 152 | + |
| 153 | + test("refuses a read-only connection to a missing store even with create: true", async () => { |
| 154 | + // A read-only open never creates, so `create` must not excuse the miss. |
| 155 | + const missing = path.join(tmp(), "absent-readonly.db") |
| 156 | + const { connect } = await import("../src/sqlite") |
| 157 | + const connector = await connect({ type: "sqlite", path: missing, readonly: true, create: true }) |
| 158 | + |
| 159 | + await expect(connector.connect()).rejects.toThrow("not found") |
| 160 | + expect(fs.existsSync(missing)).toBe(false) |
| 161 | + }) |
| 162 | + |
| 163 | + test("creates only when the caller opts in", async () => { |
| 164 | + const target = path.join(tmp(), "scratch.db") |
| 165 | + const { connect } = await import("../src/sqlite") |
| 166 | + const connector = await connect({ type: "sqlite", path: target, create: true }) |
| 167 | + await connector.connect() |
| 168 | + await connector.close() |
| 169 | + |
| 170 | + expect(fs.existsSync(target)).toBe(true) |
| 171 | + }) |
| 172 | +}) |
0 commit comments