|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { assert, it } from "@effect/vitest"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as FileSystem from "effect/FileSystem"; |
| 5 | +import * as Path from "effect/Path"; |
| 6 | +import * as SqlClient from "effect/unstable/sql/SqlClient"; |
| 7 | + |
| 8 | +import * as NodeSqliteClient from "../src/persistence/NodeSqliteClient.ts"; |
| 9 | +import { runSqliteState } from "./t3-sqlite-state.ts"; |
| 10 | + |
| 11 | +const createFixtureDatabase = Effect.fn("createSqliteStateFixtureDatabase")(function* ( |
| 12 | + baseDir: string, |
| 13 | +) { |
| 14 | + const fs = yield* FileSystem.FileSystem; |
| 15 | + const path = yield* Path.Path; |
| 16 | + const stateDir = path.join(baseDir, "userdata"); |
| 17 | + const databasePath = path.join(stateDir, "state.sqlite"); |
| 18 | + yield* fs.makeDirectory(stateDir, { recursive: true }); |
| 19 | + yield* Effect.gen(function* () { |
| 20 | + const sql = yield* SqlClient.SqlClient; |
| 21 | + yield* sql`CREATE TABLE fixtures (id INTEGER PRIMARY KEY, label TEXT NOT NULL)`; |
| 22 | + yield* sql`INSERT INTO fixtures (id, label) VALUES (1, 'existing')`; |
| 23 | + }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: databasePath }))); |
| 24 | +}); |
| 25 | + |
| 26 | +it.layer(NodeServices.layer)("t3-sqlite-state", (it) => { |
| 27 | + it.effect("reports each invalid SQL source with a specific error", () => |
| 28 | + Effect.gen(function* () { |
| 29 | + const fs = yield* FileSystem.FileSystem; |
| 30 | + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-state-input-" }); |
| 31 | + |
| 32 | + const multipleSources = yield* runSqliteState({ |
| 33 | + operation: "query", |
| 34 | + baseDir, |
| 35 | + sql: "SELECT 1", |
| 36 | + file: "fixture.sql", |
| 37 | + }).pipe(Effect.flip); |
| 38 | + assert.equal(multipleSources._tag, "SqliteStateMultipleSqlSourcesError"); |
| 39 | + assert.equal(multipleSources.message, "Provide only one of --sql or --file."); |
| 40 | + |
| 41 | + const missingSource = yield* runSqliteState({ operation: "query", baseDir }).pipe( |
| 42 | + Effect.flip, |
| 43 | + ); |
| 44 | + assert.equal(missingSource._tag, "SqliteStateMissingSqlSourceError"); |
| 45 | + assert.equal(missingSource.message, "Provide one of --sql or --file."); |
| 46 | + |
| 47 | + const emptySql = yield* runSqliteState({ |
| 48 | + operation: "query", |
| 49 | + baseDir, |
| 50 | + sql: " ", |
| 51 | + }).pipe(Effect.flip); |
| 52 | + assert.equal(emptySql._tag, "SqliteStateEmptySqlError"); |
| 53 | + assert.equal(emptySql.message, "SQL input is empty."); |
| 54 | + }), |
| 55 | + ); |
| 56 | + |
| 57 | + it.effect("queries an isolated database through Effect SQL", () => |
| 58 | + Effect.gen(function* () { |
| 59 | + const fs = yield* FileSystem.FileSystem; |
| 60 | + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-state-query-" }); |
| 61 | + yield* createFixtureDatabase(baseDir); |
| 62 | + |
| 63 | + const result = yield* runSqliteState({ |
| 64 | + operation: "query", |
| 65 | + baseDir, |
| 66 | + sql: "SELECT id, label FROM fixtures", |
| 67 | + }); |
| 68 | + |
| 69 | + assert.equal(result.operation, "query"); |
| 70 | + if (result.operation === "query") { |
| 71 | + assert.deepStrictEqual(result.rows, [{ id: 1, label: "existing" }]); |
| 72 | + } |
| 73 | + }), |
| 74 | + ); |
| 75 | + |
| 76 | + it.effect("backs up isolated state before writes and refuses the shared home", () => |
| 77 | + Effect.gen(function* () { |
| 78 | + const fs = yield* FileSystem.FileSystem; |
| 79 | + const path = yield* Path.Path; |
| 80 | + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-sqlite-state-exec-" }); |
| 81 | + yield* createFixtureDatabase(baseDir); |
| 82 | + |
| 83 | + const mutation = yield* runSqliteState({ |
| 84 | + operation: "exec", |
| 85 | + baseDir, |
| 86 | + sql: "INSERT INTO fixtures (id, label) VALUES (2, 'seeded')", |
| 87 | + }); |
| 88 | + assert.equal(mutation.operation, "exec"); |
| 89 | + if (mutation.operation === "exec") { |
| 90 | + assert.equal((yield* fs.stat(mutation.backup)).mode & 0o777, 0o600); |
| 91 | + } |
| 92 | + |
| 93 | + const error = yield* runSqliteState( |
| 94 | + { |
| 95 | + operation: "exec", |
| 96 | + baseDir, |
| 97 | + sql: "DELETE FROM fixtures", |
| 98 | + }, |
| 99 | + { sharedHome: baseDir }, |
| 100 | + ).pipe(Effect.flip); |
| 101 | + assert.equal(error._tag, "SqliteStateSharedHomeMutationError"); |
| 102 | + |
| 103 | + const aliasParent = yield* fs.makeTempDirectoryScoped({ |
| 104 | + prefix: "t3-sqlite-state-alias-", |
| 105 | + }); |
| 106 | + const aliasBaseDir = path.join(aliasParent, "shared-home-alias"); |
| 107 | + yield* fs.symlink(baseDir, aliasBaseDir); |
| 108 | + const aliasError = yield* runSqliteState( |
| 109 | + { |
| 110 | + operation: "exec", |
| 111 | + baseDir: aliasBaseDir, |
| 112 | + sql: "DELETE FROM fixtures", |
| 113 | + }, |
| 114 | + { sharedHome: baseDir }, |
| 115 | + ).pipe(Effect.flip); |
| 116 | + assert.equal(aliasError._tag, "SqliteStateSharedHomeMutationError"); |
| 117 | + }), |
| 118 | + ); |
| 119 | +}); |
0 commit comments