|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// framework#3793 — the startup banner's `Driver:` row. |
| 4 | +// |
| 5 | +// The banner exists so a developer sees at a glance which database they are |
| 6 | +// talking to. When the driver was wired by an app preset or by |
| 7 | +// `EnvironmentKernelFactory` rather than by serve's own `OS_DATABASE_URL` |
| 8 | +// fallback, that row comes from probing the registered driver — and for a |
| 9 | +// datasource declared with a DSN it read `SqlDriver(pg) → (unknown)`: the |
| 10 | +// right driver, no address, at exactly the moment (an unreachable database) |
| 11 | +// when the address is what you need. |
| 12 | + |
| 13 | +import { describe, it, expect } from 'vitest'; |
| 14 | +import { describeRegisteredDriver } from './serve.js'; |
| 15 | + |
| 16 | +/** A kernel whose `getService` throws for anything not registered, like the real one. */ |
| 17 | +function fakeKernel(services: Record<string, unknown>) { |
| 18 | + return { |
| 19 | + getService(name: string) { |
| 20 | + if (!(name in services)) throw new Error(`Service '${name}' not found`); |
| 21 | + return services[name]; |
| 22 | + }, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +describe('describeRegisteredDriver', () => { |
| 27 | + it('reads a DSN-declared pg datasource — the #3793 (unknown)', () => { |
| 28 | + // Exactly what `defaultDatasourceDriverFactory` hands `new SqlDriver(...)` |
| 29 | + // for a datasource whose config is `{ url }` / `{ connectionString }`. |
| 30 | + const kernel = fakeKernel({ |
| 31 | + 'driver.com.objectstack.driver.sql': { |
| 32 | + config: { |
| 33 | + client: 'pg', |
| 34 | + connection: { connectionString: 'postgres://u:p@127.0.0.1:59437/nope' }, |
| 35 | + pool: { min: 0, max: 5 }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }); |
| 39 | + expect(describeRegisteredDriver(kernel)).toEqual({ |
| 40 | + label: 'SqlDriver(pg)', |
| 41 | + url: 'postgres://127.0.0.1:59437/nope', |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('keeps reading the shapes it already knew', () => { |
| 46 | + expect(describeRegisteredDriver(fakeKernel({ |
| 47 | + 'driver.sql': { config: { client: 'pg', connection: 'postgres://u:p@host:5432/app' } }, |
| 48 | + }))).toEqual({ label: 'SqlDriver(pg)', url: 'postgres://host:5432/app' }); |
| 49 | + |
| 50 | + expect(describeRegisteredDriver(fakeKernel({ |
| 51 | + 'driver.sql': { config: { client: 'better-sqlite3', connection: { filename: './data.db' } } }, |
| 52 | + }))).toEqual({ label: 'SqlDriver(better-sqlite3)', url: './data.db' }); |
| 53 | + |
| 54 | + expect(describeRegisteredDriver(fakeKernel({ |
| 55 | + 'driver.sql': { config: { client: 'pg', connection: { host: 'db.example.com', port: 5432, database: 'app' } } }, |
| 56 | + }))).toEqual({ label: 'SqlDriver(pg)', url: 'db.example.com:5432/app' }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('never puts credentials in the banner', () => { |
| 60 | + for (const connection of [ |
| 61 | + 'postgres://admin:hunter2@db.example.com:5432/app', |
| 62 | + { connectionString: 'postgres://admin:hunter2@db.example.com:5432/app' }, |
| 63 | + { host: 'db.example.com', port: 5432, database: 'app', user: 'admin', password: 'hunter2' }, |
| 64 | + ]) { |
| 65 | + const probe = describeRegisteredDriver(fakeKernel({ |
| 66 | + 'driver.sql': { config: { client: 'pg', connection } }, |
| 67 | + })); |
| 68 | + expect(probe?.url).not.toContain('hunter2'); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + it('reads the top-level url a MongoDBDriver keeps on its config', () => { |
| 73 | + // Every driver has a `config` property (MongoDBDriver's is `{ url, … }`, |
| 74 | + // InMemoryDriver's is `{}`), so the old "has a config → it's a SqlDriver" |
| 75 | + // early return meant *every* mongo boot banner read `(unknown)`. |
| 76 | + const kernel = fakeKernel({ |
| 77 | + 'driver.com.objectstack.driver.mongodb': { |
| 78 | + constructor: { name: 'MongoDBDriver' }, |
| 79 | + name: 'com.objectstack.driver.mongodb', |
| 80 | + config: { url: 'mongodb://admin:hunter2@cluster0.mongodb.net/app', database: 'app' }, |
| 81 | + }, |
| 82 | + }); |
| 83 | + expect(describeRegisteredDriver(kernel)).toEqual({ |
| 84 | + label: 'MongoDBDriver', |
| 85 | + url: 'mongodb://cluster0.mongodb.net/app', |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('redacts the URL a driver exposes on the instance instead', () => { |
| 90 | + const kernel = fakeKernel({ |
| 91 | + 'driver.com.objectstack.driver.turso': { |
| 92 | + constructor: { name: 'TursoDriver' }, |
| 93 | + url: 'libsql://app-org.turso.io?authToken=secret-jwt', |
| 94 | + }, |
| 95 | + }); |
| 96 | + expect(describeRegisteredDriver(kernel)).toEqual({ |
| 97 | + label: 'TursoDriver', |
| 98 | + url: 'libsql://app-org.turso.io', |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('still says (unknown) when the config genuinely carries no address', () => { |
| 103 | + const kernel = fakeKernel({ |
| 104 | + // knex lets the host build a connection per pool checkout; there is |
| 105 | + // nothing to read until it is called. |
| 106 | + 'driver.sql': { config: { client: 'pg', connection: () => ({ host: 'h' }) } }, |
| 107 | + }); |
| 108 | + expect(describeRegisteredDriver(kernel)).toEqual({ label: 'SqlDriver(pg)', url: '(unknown)' }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('labels an in-memory driver "(in-memory)", not "(unknown)"', () => { |
| 112 | + const kernel = fakeKernel({ |
| 113 | + // InMemoryDriver always has a `config` — `{}` when none was passed. |
| 114 | + 'driver.memory': { |
| 115 | + constructor: { name: 'InMemoryDriver' }, |
| 116 | + name: 'com.objectstack.driver.memory', |
| 117 | + config: {}, |
| 118 | + }, |
| 119 | + }); |
| 120 | + expect(describeRegisteredDriver(kernel)).toEqual({ label: 'InMemoryDriver', url: '(in-memory)' }); |
| 121 | + }); |
| 122 | + |
| 123 | + it('does not paste a Client class into the label (SqliteWasmDriver)', () => { |
| 124 | + // SqliteWasmDriver passes knex a dialect *class* as `client`; interpolating |
| 125 | + // it would put the class source in the banner. |
| 126 | + class Client_WasmSqlite {} |
| 127 | + const kernel = fakeKernel({ |
| 128 | + 'driver.sql': { |
| 129 | + constructor: { name: 'SqliteWasmDriver' }, |
| 130 | + config: { client: Client_WasmSqlite, connection: { filename: './app.wasm.db' } }, |
| 131 | + }, |
| 132 | + }); |
| 133 | + expect(describeRegisteredDriver(kernel)).toEqual({ |
| 134 | + label: 'SqliteWasmDriver', |
| 135 | + url: './app.wasm.db', |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('returns null when no known driver is registered', () => { |
| 140 | + expect(describeRegisteredDriver(fakeKernel({}))).toBeNull(); |
| 141 | + }); |
| 142 | +}); |
0 commit comments