|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A local or replica `TursoDriver` with nothing durable behind its engine is |
| 5 | + * refused at construction. Before this, it silently ran on a private |
| 6 | + * `:memory:` database. |
| 7 | + * |
| 8 | + * # What was measured (the reading this refusal stands on) |
| 9 | + * |
| 10 | + * The local and replica arms run every read and write through the inherited |
| 11 | + * Knex + better-sqlite3 engine, and `TursoDriver.toKnexConfig` could hand that |
| 12 | + * engine only a `file:` path or `:memory:`. Everything else went to its last |
| 13 | + * arm, which was `:memory:`. On `main` @ `2c1011b01b`, `initObjects`, then |
| 14 | + * `create`, then `find`, then a fresh driver on the same config: |
| 15 | + * |
| 16 | + * ``` |
| 17 | + * libsql:// + syncUrl + client stub, sync.onConnect false -> replica, knex :memory:, |
| 18 | + * find 1 row, the client's own database held NO tables, 0 rows after restart |
| 19 | + * libsql:// / https:// / wss:// + syncUrl, real client, onConnect false -> same, 0 after restart |
| 20 | + * libsql:// + syncUrl, real client, default sync -> connect() rejects SYNC_NOT_SUPPORTED |
| 21 | + * libsql:// + mode 'replica' (no syncUrl) -> 1 row, 0 after restart |
| 22 | + * libsql:// + mode 'local' -> 1 row, 0 after restart |
| 23 | + * :memory: + syncUrl, real client, default sync -> connect() rejects URL_INVALID |
| 24 | + * :memory: + syncUrl + client stub, onConnect false -> 1 row, stub holds no tables, 0 after restart |
| 25 | + * file: + syncUrl + client stub (CONTROL) -> knex on the file, 1 row, 1 after restart |
| 26 | + * ``` |
| 27 | + * |
| 28 | + * `@libsql/client@0.17.4` builds no embedded replica for a remote url: |
| 29 | + * `lib-esm/node.js` routes http/https to its HTTP client and ws/wss to its |
| 30 | + * WebSocket client, `syncUrl` is read only in `lib-esm/sqlite3.js` (a `syncUrl` |
| 31 | + * grep over `http.js` / `ws.js` returns zero, while `authToken` returns six in |
| 32 | + * each), and both remote clients' `sync()` throw `SYNC_NOT_SUPPORTED`. The |
| 33 | + * sqlite3 client refuses an in-memory replica itself: "Embedded replica must use |
| 34 | + * file for local db". |
| 35 | + * |
| 36 | + * # What this file pins |
| 37 | + * |
| 38 | + * - The card's own reproduction (remote url, `syncUrl`, a client stub, |
| 39 | + * `sync.onConnect: false`) is refused at construction, as the ADR-0112 |
| 40 | + * envelope (`code` + `status`). The stub is never touched. Each remote scheme |
| 41 | + * the classifier knows is covered. |
| 42 | + * - Each other arm, as measured: a forced `mode: 'replica'` or `mode: 'local'` |
| 43 | + * beside a remote url, and a replica on `:memory:` / `file::memory:` / a |
| 44 | + * url that is not `file:`. |
| 45 | + * - The message names the way out and never echoes the url, which may carry a |
| 46 | + * live `?authToken=`. |
| 47 | + * - PRESERVATION: the `file:` replica still constructs, connects, writes, and |
| 48 | + * keeps its rows across a restart. So do the local, in-memory and remote |
| 49 | + * faces. `detectMode` still classifies the refused pair `replica`: the |
| 50 | + * refusal is in the constructor, not a re-classification. |
| 51 | + * |
| 52 | + * # Reverse verification: direction predicted before it was run |
| 53 | + * |
| 54 | + * With the constructor's `localEngineDefect` call removed, every refusal case |
| 55 | + * goes RED (the constructor returns a driver on `:memory:`, with no envelope to |
| 56 | + * read) and every preservation case stays GREEN. Measured; see the PR. |
| 57 | + */ |
| 58 | + |
| 59 | +import { afterAll, describe, expect, it } from 'vitest'; |
| 60 | +import { createTursoDriver } from './index.js'; |
| 61 | +import { TursoDriver, type TursoDriverConfig } from './turso-driver.js'; |
| 62 | +import { makeLibsqlSqliteStub } from './libsql-sqlite-stub.testkit.js'; |
| 63 | +import { replicaFiles } from './replica-file.testkit.js'; |
| 64 | + |
| 65 | +type Refusal = Error & { code?: string; status?: number }; |
| 66 | + |
| 67 | +/** The error `build` threw, or `null` when it returned. */ |
| 68 | +function refusalOf(build: () => unknown): Refusal | null { |
| 69 | + try { |
| 70 | + build(); |
| 71 | + return null; |
| 72 | + } catch (error) { |
| 73 | + return error as Refusal; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function expectEnvelope(refusal: Refusal | null): asserts refusal is Refusal { |
| 78 | + expect(refusal, 'expected the constructor to refuse, and it returned a driver').not.toBeNull(); |
| 79 | + expect(refusal!.code).toBe('VALIDATION_ERROR'); |
| 80 | + expect(refusal!.status).toBe(400); |
| 81 | +} |
| 82 | + |
| 83 | +const PRIMARY = 'libsql://primary.example.turso.io'; |
| 84 | +const NOTE = { name: 'note', fields: { title: { type: 'string' } } }; |
| 85 | +const files = replicaFiles(); |
| 86 | +afterAll(() => files.removeAll()); |
| 87 | + |
| 88 | +describe('a remote url beside syncUrl: refused, never a replica on :memory:', () => { |
| 89 | + it("the card's reproduction: libsql:// + syncUrl + a client stub + sync.onConnect false", () => { |
| 90 | + const stub = makeLibsqlSqliteStub(); |
| 91 | + const tablesInStub = () => |
| 92 | + stub.raw.prepare(`select count(*) as c from sqlite_master where type='table'`).all()[0].c; |
| 93 | + |
| 94 | + const refusal = refusalOf( |
| 95 | + () => |
| 96 | + new TursoDriver({ |
| 97 | + url: 'libsql://r.turso.io', |
| 98 | + syncUrl: 'libsql://r.turso.io', |
| 99 | + client: stub as never, |
| 100 | + sync: { onConnect: false }, |
| 101 | + }), |
| 102 | + ); |
| 103 | + |
| 104 | + expectEnvelope(refusal); |
| 105 | + // Both ways out, by the subjects they name: the key to drop for a remote |
| 106 | + // database, the url spelling for a replica. |
| 107 | + expect(refusal.message).toContain('`syncUrl`'); |
| 108 | + expect(refusal.message).toContain("url: 'file:"); |
| 109 | + // Refused before any client work: the stub is untouched. |
| 110 | + expect(tablesInStub()).toBe(0); |
| 111 | + stub.close(); |
| 112 | + }); |
| 113 | + |
| 114 | + it.each(['libsql://', 'https://', 'http://', 'wss://', 'ws://'])( |
| 115 | + '%s + syncUrl is refused, naming the scheme it met', |
| 116 | + (scheme) => { |
| 117 | + const refusal = refusalOf(() => new TursoDriver({ url: `${scheme}db.example.turso.io`, syncUrl: PRIMARY })); |
| 118 | + |
| 119 | + expectEnvelope(refusal); |
| 120 | + expect(refusal.message).toContain(`\`${scheme}\``); |
| 121 | + expect(refusal.message).toContain('`syncUrl`'); |
| 122 | + }, |
| 123 | + ); |
| 124 | + |
| 125 | + it('the factory door meets the same refusal: createTursoDriver is not a way around it', () => { |
| 126 | + expectEnvelope(refusalOf(() => createTursoDriver({ url: 'libsql://r.turso.io', syncUrl: PRIMARY }))); |
| 127 | + }); |
| 128 | + |
| 129 | + it('a `timeout` beside the pair does not change which refusal fires: this one owns the pair', () => { |
| 130 | + const refusal = refusalOf(() => new TursoDriver({ url: 'wss://r.turso.io', syncUrl: PRIMARY, timeout: 30_000 })); |
| 131 | + |
| 132 | + expectEnvelope(refusal); |
| 133 | + expect(refusal.message).toContain('`syncUrl`'); |
| 134 | + expect(refusal.message).not.toContain('TursoDriverConfig.timeout'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('never echoes the url: a live `?authToken=` in it stays out of the boot log', () => { |
| 138 | + const refusal = refusalOf( |
| 139 | + () => new TursoDriver({ url: 'libsql://r.turso.io?authToken=SECRET-TOKEN-VALUE', syncUrl: PRIMARY }), |
| 140 | + ); |
| 141 | + |
| 142 | + expectEnvelope(refusal); |
| 143 | + expect(refusal.message).not.toContain('SECRET-TOKEN-VALUE'); |
| 144 | + expect(refusal.message).not.toContain('r.turso.io'); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe('a forced local or replica mode beside a remote url: refused', () => { |
| 149 | + it.each<[string, TursoDriverConfig]>([ |
| 150 | + ["mode 'replica', no syncUrl", { url: 'libsql://r.turso.io', mode: 'replica' }], |
| 151 | + ["mode 'replica' + syncUrl", { url: 'libsql://r.turso.io', syncUrl: PRIMARY, mode: 'replica' }], |
| 152 | + ["mode 'local'", { url: 'libsql://r.turso.io', mode: 'local' }], |
| 153 | + ["mode 'local', https://", { url: 'https://r.turso.io', mode: 'local' }], |
| 154 | + ])('%s', (_label, config) => { |
| 155 | + const refusal = refusalOf(() => new TursoDriver(config)); |
| 156 | + |
| 157 | + expectEnvelope(refusal); |
| 158 | + // The way out for a remote database names the key that forced the mode, |
| 159 | + // and the value that would have been right. |
| 160 | + expect(refusal.message).toContain(`\`mode: '${config.mode}'\``); |
| 161 | + expect(refusal.message).toContain("mode: 'remote'"); |
| 162 | + }); |
| 163 | +}); |
| 164 | + |
| 165 | +describe('a replica that is not a local file: refused', () => { |
| 166 | + it.each<[string, TursoDriverConfig]>([ |
| 167 | + [':memory: + syncUrl', { url: ':memory:', syncUrl: PRIMARY }], |
| 168 | + [':memory: + syncUrl + a supplied client', { url: ':memory:', syncUrl: PRIMARY, client: {} as never, sync: { onConnect: false } }], |
| 169 | + ['file::memory: + syncUrl', { url: 'file::memory:', syncUrl: PRIMARY }], |
| 170 | + ["file::memory:?cache=shared + mode 'replica'", { url: 'file::memory:?cache=shared', mode: 'replica' }], |
| 171 | + [":memory: + mode 'replica'", { url: ':memory:', mode: 'replica' }], |
| 172 | + ["a bare path + mode 'replica'", { url: './data/replica.db', mode: 'replica' }], |
| 173 | + ])('%s', (_label, config) => { |
| 174 | + const refusal = refusalOf(() => new TursoDriver(config)); |
| 175 | + |
| 176 | + expectEnvelope(refusal); |
| 177 | + expect(refusal.message).toContain('embedded replica'); |
| 178 | + expect(refusal.message).toContain("url: 'file:"); |
| 179 | + }); |
| 180 | +}); |
| 181 | + |
| 182 | +describe('PRESERVATION: every configuration with a durable (or declared-ephemeral) engine still constructs', () => { |
| 183 | + it('the file: replica constructs, connects, writes, and keeps its rows across a restart', async () => { |
| 184 | + const url = files.next(); |
| 185 | + const stub = makeLibsqlSqliteStub(); |
| 186 | + const make = () => new TursoDriver({ url, syncUrl: PRIMARY, client: stub as never, sync: { onConnect: false } }); |
| 187 | + |
| 188 | + const first = make(); |
| 189 | + expect(first.transportMode).toBe('replica'); |
| 190 | + await first.connect(); |
| 191 | + await first.initObjects([NOTE as never]); |
| 192 | + await first.create('note', { id: 'n1', title: 'kept' }); |
| 193 | + expect(await first.find('note', {})).toHaveLength(1); |
| 194 | + await first.disconnect(); |
| 195 | + |
| 196 | + const second = make(); |
| 197 | + await second.connect(); |
| 198 | + await second.initObjects([NOTE as never]); |
| 199 | + expect((await second.find('note', {})).map((r: { title?: unknown }) => r.title)).toEqual(['kept']); |
| 200 | + await second.disconnect(); |
| 201 | + stub.close(); |
| 202 | + }); |
| 203 | + |
| 204 | + it.each<[string, TursoDriverConfig, string]>([ |
| 205 | + ['file: local', { url: 'file:./data/app.db' }, 'local'], |
| 206 | + [':memory: local (ephemeral by declaration)', { url: ':memory:' }, 'local'], |
| 207 | + ["file: + mode 'local'", { url: 'file:./data/app.db', mode: 'local' }, 'local'], |
| 208 | + ["file: + mode 'replica' + syncUrl", { url: 'file:./data/replica.db', syncUrl: PRIMARY, mode: 'replica' }, 'replica'], |
| 209 | + ['libsql:// remote', { url: 'libsql://r.turso.io', authToken: 't' }, 'remote'], |
| 210 | + ["libsql:// + mode 'remote'", { url: 'libsql://r.turso.io', mode: 'remote' }, 'remote'], |
| 211 | + ])('%s', (_label, config, mode) => { |
| 212 | + // Knex opens its connection lazily, so constructing never touches the file. |
| 213 | + expect(new TursoDriver(config).transportMode).toBe(mode); |
| 214 | + }); |
| 215 | + |
| 216 | + it('detectMode still classifies the refused pair `replica`: the refusal is in the constructor, not a re-classification', () => { |
| 217 | + expect(TursoDriver.detectMode({ url: 'libsql://r.turso.io', syncUrl: PRIMARY })).toBe('replica'); |
| 218 | + expect(TursoDriver.detectMode({ url: ':memory:', syncUrl: PRIMARY })).toBe('replica'); |
| 219 | + }); |
| 220 | +}); |
0 commit comments