|
| 1 | +import path from "node:path" |
| 2 | +import { Database } from "bun:sqlite" |
| 3 | +import { expect } from "bun:test" |
| 4 | +import { sql } from "drizzle-orm" |
| 5 | +import { Cause, Effect } from "effect" |
| 6 | +import { SqlClient } from "effect/sql" |
| 7 | +import { EffectDrizzleSqlite } from "@opencode/core/database/drizzle" |
| 8 | +import { sqliteLayer } from "@opencode/core/database/sqlite.bun" |
| 9 | +import { StorageFailure } from "@opencode/core/database/storage-failure" |
| 10 | +import { tmpdirScoped } from "./fixture/tmpdir" |
| 11 | +import { it } from "./lib/effect" |
| 12 | + |
| 13 | +// The node:sqlite driver is covered by sqlite-node.test.ts, which runs under Node. |
| 14 | +const large = "x".repeat(60_000) |
| 15 | + |
| 16 | +it.live("a transaction SQLite rolled back itself fails with the storage failure, not the rollback", () => |
| 17 | + Effect.gen(function* () { |
| 18 | + const client = yield* SqlClient.SqlClient |
| 19 | + const db = yield* EffectDrizzleSqlite.makeWithDefaults() |
| 20 | + yield* client`CREATE TABLE probe (value TEXT)` |
| 21 | + yield* client`PRAGMA max_page_count = 1` |
| 22 | + |
| 23 | + const cause = yield* db |
| 24 | + .transaction(() => db.run(sql`INSERT INTO probe VALUES (${large})`), { behavior: "immediate" }) |
| 25 | + .pipe(Effect.sandbox, Effect.flip) |
| 26 | + expect(StorageFailure.message(cause)).toBe("Storage is full") |
| 27 | + // Nothing was left open: the connection takes the next transaction. |
| 28 | + yield* client`PRAGMA max_page_count = 1073741823` |
| 29 | + yield* db.transaction(() => db.run(sql`INSERT INTO probe VALUES ('ok')`)) |
| 30 | + }).pipe(Effect.provide(sqliteLayer({ filename: ":memory:" }))), |
| 31 | +) |
| 32 | + |
| 33 | +it.live("a full database is a storage failure for a statement and for a query", () => |
| 34 | + Effect.gen(function* () { |
| 35 | + const client = yield* SqlClient.SqlClient |
| 36 | + const db = yield* EffectDrizzleSqlite.makeWithDefaults() |
| 37 | + yield* client`CREATE TABLE probe (value TEXT)` |
| 38 | + // SQLite never lowers the limit below the current size, so 1 means the database may not grow. |
| 39 | + yield* client`PRAGMA max_page_count = 1` |
| 40 | + |
| 41 | + const statement = yield* client`INSERT INTO probe VALUES (${large})`.pipe(Effect.flip) |
| 42 | + expect(StorageFailure.fromSqlError(statement)).toBe("Storage is full") |
| 43 | + expect(StorageFailure.message(Cause.fail(statement))).toBe("Storage is full") |
| 44 | + |
| 45 | + const query = yield* db.run(sql`INSERT INTO probe VALUES (${large})`).pipe(Effect.flip) |
| 46 | + expect(StorageFailure.message(Cause.die(query))).toBe("Storage is full") |
| 47 | + }).pipe(Effect.provide(sqliteLayer({ filename: ":memory:" }))), |
| 48 | +) |
| 49 | + |
| 50 | +it.live("an extended result code is a storage failure by its primary code", () => |
| 51 | + Effect.gen(function* () { |
| 52 | + const dir = yield* tmpdirScoped() |
| 53 | + const filename = path.join(dir.path, "probe.db") |
| 54 | + yield* Effect.gen(function* () { |
| 55 | + const client = yield* SqlClient.SqlClient |
| 56 | + yield* client`CREATE TABLE probe (value TEXT)` |
| 57 | + // SQLITE_BUSY_SNAPSHOT (517): a WAL reader can't become the writer once another connection has committed. |
| 58 | + yield* client`BEGIN` |
| 59 | + yield* client`SELECT * FROM probe` |
| 60 | + yield* Effect.sync(() => { |
| 61 | + const other = new Database(filename) |
| 62 | + other.run("INSERT INTO probe VALUES ('other')") |
| 63 | + other.close() |
| 64 | + }) |
| 65 | + const error = yield* client`INSERT INTO probe VALUES ('mine')`.pipe(Effect.flip) |
| 66 | + yield* client`ROLLBACK` |
| 67 | + expect(error.reason.cause).toMatchObject({ errno: 517 }) |
| 68 | + expect(StorageFailure.fromSqlError(error)).toBe("Storage is in use by another process") |
| 69 | + }).pipe(Effect.provide(sqliteLayer({ filename }))) |
| 70 | + }), |
| 71 | +) |
| 72 | + |
| 73 | +it.live("a constraint, a bad statement and other defects are not storage failures", () => |
| 74 | + Effect.gen(function* () { |
| 75 | + const client = yield* SqlClient.SqlClient |
| 76 | + const db = yield* EffectDrizzleSqlite.makeWithDefaults() |
| 77 | + yield* client`CREATE TABLE probe (id TEXT PRIMARY KEY)` |
| 78 | + yield* client`INSERT INTO probe VALUES ('a')` |
| 79 | + |
| 80 | + const constraint = yield* client`INSERT INTO probe VALUES ('a')`.pipe(Effect.flip) |
| 81 | + expect(StorageFailure.fromSqlError(constraint)).toBeUndefined() |
| 82 | + const missing = yield* db.run(sql`SELECT * FROM missing`).pipe(Effect.flip) |
| 83 | + expect(StorageFailure.message(Cause.die(missing))).toBeUndefined() |
| 84 | + |
| 85 | + expect(StorageFailure.message(Cause.die(new Error("database or disk is full")))).toBeUndefined() |
| 86 | + expect(StorageFailure.message(Cause.interrupt())).toBeUndefined() |
| 87 | + }).pipe(Effect.provide(sqliteLayer({ filename: ":memory:" }))), |
| 88 | +) |
0 commit comments