|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * A declared SQLite datasource whose file is not a SQLite database is refused |
| 5 | + * at connect, naming the file, in development as in production. On a |
| 6 | + * development boot the operator's file is neither renamed nor replaced, and |
| 7 | + * nothing steps down to wasm SQLite or the in-memory driver (#24960). |
| 8 | + * |
| 9 | + * Before: `SqlDriver.connect()` swallowed the file's `SQLITE_NOTADB` and |
| 10 | + * resolved, the probe's `SELECT 1` then failed as a `DATABASE_ERROR`, and in |
| 11 | + * development `resolveSqliteDriver` stepped down to wasm SQLite. wasm's open |
| 12 | + * moved the file aside to `<file>.corrupt-<timestamp>` and wrote an empty |
| 13 | + * database in its place, under the warning "native better-sqlite3 unavailable |
| 14 | + * (ABI mismatch or not built)", which named the wrong cause; the datasource |
| 15 | + * read `connected`. Measured through `os migrate apply --yes` on the card. |
| 16 | + * |
| 17 | + * Real `better-sqlite3`, real wasm SQLite and real files in a scratch |
| 18 | + * directory, as in `./sqlite-driver-fallback-file-open-failure.test.ts`. |
| 19 | + */ |
| 20 | + |
| 21 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 22 | +import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 23 | +import { tmpdir } from 'node:os'; |
| 24 | +import { join } from 'node:path'; |
| 25 | +import { resolveSqliteDriver, type ResolvedSqliteDriver } from '../sqlite-driver-fallback.js'; |
| 26 | +import { DatasourceConnectionService } from '../datasource-connection-service.js'; |
| 27 | +import { createDefaultDatasourceDriverFactory } from '../default-datasource-driver-factory.js'; |
| 28 | + |
| 29 | +const PLAIN_TEXT = 'archive notes — this file holds plain text, not a SQLite database\n'; |
| 30 | + |
| 31 | +let dir: string; |
| 32 | +let filename: string; |
| 33 | +let bytes: Buffer; |
| 34 | +const opened: ResolvedSqliteDriver[] = []; |
| 35 | + |
| 36 | +beforeEach(() => { |
| 37 | + dir = mkdtempSync(join(tmpdir(), 'os-24960-')); |
| 38 | + filename = join(dir, 'archive.db'); |
| 39 | + writeFileSync(filename, PLAIN_TEXT); |
| 40 | + bytes = readFileSync(filename); |
| 41 | +}); |
| 42 | + |
| 43 | +afterEach(async () => { |
| 44 | + vi.restoreAllMocks(); |
| 45 | + vi.unstubAllEnvs(); |
| 46 | + await Promise.all(opened.splice(0).map((r) => r.driver?.disconnect?.().catch(() => {}))); |
| 47 | + rmSync(dir, { recursive: true, force: true }); |
| 48 | +}); |
| 49 | + |
| 50 | +/** `promise`'s rejection; a failure naming the engine when it resolves instead. */ |
| 51 | +async function rejectionOf(promise: Promise<ResolvedSqliteDriver>): Promise<any> { |
| 52 | + let resolved: ResolvedSqliteDriver; |
| 53 | + try { |
| 54 | + resolved = await promise; |
| 55 | + } catch (error) { |
| 56 | + return error; |
| 57 | + } |
| 58 | + opened.push(resolved); |
| 59 | + throw new Error(`resolved to engine '${resolved.engine}' for a file that is not a SQLite database`); |
| 60 | +} |
| 61 | + |
| 62 | +/** The driver's refusal of the file: its own error name, SQLite's code, and the file. */ |
| 63 | +function expectNotADatabaseRefusal(error: any): void { |
| 64 | + expect(error?.name).toBe('SqliteDatabaseFileError'); |
| 65 | + expect(error?.code).toBe('SQLITE_NOTADB'); |
| 66 | + expect(error?.filename).toBe(filename); |
| 67 | + expect(String(error?.message)).toContain(filename); |
| 68 | +} |
| 69 | + |
| 70 | +/** The operator's file is exactly as it was, and nothing was written beside it. */ |
| 71 | +function expectFileUntouched(): void { |
| 72 | + expect(readFileSync(filename).equals(bytes)).toBe(true); |
| 73 | + expect(readdirSync(dir)).toEqual(['archive.db']); |
| 74 | +} |
| 75 | + |
| 76 | +describe('resolveSqliteDriver in development: a file that is not a SQLite database (#24960)', () => { |
| 77 | + it('rejects with the driver\'s refusal, never wasm or memory, and leaves the file as it was', async () => { |
| 78 | + const warn = vi.fn(); |
| 79 | + expectNotADatabaseRefusal(await rejectionOf(resolveSqliteDriver({ filename, dev: true, warn }))); |
| 80 | + // No step-down notice: the "native better-sqlite3 unavailable" warning |
| 81 | + // named the wrong cause, and no rung was tried. |
| 82 | + expect(warn).not.toHaveBeenCalled(); |
| 83 | + expectFileUntouched(); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe('resolveSqliteDriver in production: a file that is not a SQLite database (#24960)', () => { |
| 88 | + it('returns native unprobed, and its connect() refuses the file', async () => { |
| 89 | + const warn = vi.fn(); |
| 90 | + const resolved = await resolveSqliteDriver({ filename, dev: false, warn }); |
| 91 | + opened.push(resolved); |
| 92 | + expect(resolved.engine).toBe('better-sqlite3'); |
| 93 | + let refusal: unknown; |
| 94 | + await resolved.driver.connect().catch((e: unknown) => { refusal = e; }); |
| 95 | + expectNotADatabaseRefusal(refusal); |
| 96 | + expect(warn).not.toHaveBeenCalled(); |
| 97 | + expectFileUntouched(); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('DatasourceConnectionService: a declared SQLite datasource on a file that is not a database (#24960)', () => { |
| 102 | + for (const mode of ['development', 'production'] as const) { |
| 103 | + it(`${mode}: reports a failed connection state naming the file, registers no driver, and leaves the file as it was`, async () => { |
| 104 | + vi.stubEnv('NODE_ENV', mode); |
| 105 | + const registered: string[] = []; |
| 106 | + const engine = { |
| 107 | + registerDriver: (driver: { name?: string }) => { registered.push(String(driver?.name)); }, |
| 108 | + getDriverByName: (name: string) => (registered.includes(name) ? {} : undefined), |
| 109 | + }; |
| 110 | + const service = new DatasourceConnectionService({ |
| 111 | + factory: () => createDefaultDatasourceDriverFactory({ dev: mode === 'development' }), |
| 112 | + engine: () => engine as any, |
| 113 | + }); |
| 114 | + |
| 115 | + await service.connectDeclared({ |
| 116 | + datasources: [{ name: 'archive', driver: 'sqlite', config: { filename }, autoConnect: true }], |
| 117 | + }); |
| 118 | + |
| 119 | + const state = service.getConnectionState('archive'); |
| 120 | + expect(state?.availability).toBe('failed'); |
| 121 | + expect(state?.reason).toContain(filename); |
| 122 | + expect(registered).toEqual([]); |
| 123 | + expectFileUntouched(); |
| 124 | + }); |
| 125 | + } |
| 126 | +}); |
0 commit comments