|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// ADR-0062 D1 (#3826): the standalone `default` datasource is a DECLARATION, |
| 4 | +// connected at boot by DefaultDatasourcePlugin through the same |
| 5 | +// DatasourceConnectionService as every declared/runtime datasource — one |
| 6 | +// connect path, one failure verdict, one escape hatch. These boots exercise |
| 7 | +// the real kernel (init-all → start-all) with the real driver factory. |
| 8 | + |
| 9 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 10 | +import { Runtime } from './runtime.js'; |
| 11 | +import { DefaultDatasourcePlugin } from './default-datasource-plugin.js'; |
| 12 | +import { AppPlugin } from './app-plugin.js'; |
| 13 | + |
| 14 | +const BOOT_TIMEOUT = 60_000; |
| 15 | +const ENV = 'OS_ALLOW_DRIVER_CONNECT_FAILURE'; |
| 16 | + |
| 17 | +async function assemble(opts: { |
| 18 | + driver?: string; |
| 19 | + withAdminPlugin?: boolean; |
| 20 | + connectPolicy?: any; |
| 21 | + bundle?: any; |
| 22 | +} = {}) { |
| 23 | + const { ObjectQLPlugin } = await import('@objectstack/objectql'); |
| 24 | + const runtime = new Runtime({ cluster: false }); |
| 25 | + const kernel = runtime.getKernel(); |
| 26 | + // Order matters for START: the default datasource must connect before |
| 27 | + // ObjectQLPlugin.start() runs boot schema-sync. |
| 28 | + await kernel.use(new DefaultDatasourcePlugin({ driver: opts.driver ?? 'memory' })); |
| 29 | + await kernel.use(new ObjectQLPlugin()); |
| 30 | + if (opts.bundle) await kernel.use(new AppPlugin(opts.bundle)); |
| 31 | + if (opts.withAdminPlugin !== false) { |
| 32 | + const { DatasourceAdminServicePlugin, createDefaultDatasourceDriverFactory } = await import( |
| 33 | + '@objectstack/service-datasource' |
| 34 | + ); |
| 35 | + await kernel.use( |
| 36 | + new DatasourceAdminServicePlugin({ |
| 37 | + driverFactory: createDefaultDatasourceDriverFactory(), |
| 38 | + connectPolicy: opts.connectPolicy, |
| 39 | + }), |
| 40 | + ); |
| 41 | + } |
| 42 | + return kernel; |
| 43 | +} |
| 44 | + |
| 45 | +describe('DefaultDatasourcePlugin — the default datasource as a declaration (#3826)', () => { |
| 46 | + let saved: string | undefined; |
| 47 | + beforeEach(() => { saved = process.env[ENV]; delete process.env[ENV]; }); |
| 48 | + afterEach(() => { |
| 49 | + if (saved === undefined) delete process.env[ENV]; |
| 50 | + else process.env[ENV] = saved; |
| 51 | + }); |
| 52 | + |
| 53 | + it('boots, registers the driver as DEFAULT, and serves reads/writes end to end', async () => { |
| 54 | + const kernel = await assemble({ |
| 55 | + bundle: { |
| 56 | + manifest: { id: 'com.test.default-ds', name: 'Default DS', version: '1.0.0' }, |
| 57 | + objects: [{ name: 'note', label: 'Note', fields: { title: { type: 'text' } } }], |
| 58 | + }, |
| 59 | + }); |
| 60 | + try { |
| 61 | + await kernel.bootstrap(); |
| 62 | + const engine = kernel.getService<any>('data'); |
| 63 | + // The driver keeps its NATURAL name (no 'default' stamping) — routing to |
| 64 | + // `default` goes through the engine's default-driver fallback. |
| 65 | + expect(engine.getDriverByName('default')).toBeUndefined(); |
| 66 | + await engine.insert('note', { title: 'through-the-default' }); |
| 67 | + const rows = await engine.find('note'); |
| 68 | + expect(rows.map((r: any) => r.title)).toContain('through-the-default'); |
| 69 | + } finally { |
| 70 | + try { await (kernel as any)?.stop?.(); } catch { /* noop */ } |
| 71 | + } |
| 72 | + }, BOOT_TIMEOUT); |
| 73 | + |
| 74 | + it('shows the primary DB in the datasource-admin list with a REAL status (#3827)', async () => { |
| 75 | + const kernel = await assemble({}); |
| 76 | + try { |
| 77 | + await kernel.bootstrap(); |
| 78 | + const admin = kernel.getService<{ listDatasources(): Promise<any[]> }>('datasource-admin'); |
| 79 | + const def = (await admin.listDatasources()).find((d) => d.name === 'default'); |
| 80 | + expect(def).toBeDefined(); |
| 81 | + expect(def!.status).toBe('ok'); |
| 82 | + } finally { |
| 83 | + try { await (kernel as any)?.stop?.(); } catch { /* noop */ } |
| 84 | + } |
| 85 | + }, BOOT_TIMEOUT); |
| 86 | + |
| 87 | + it('works without the datasource-admin plugin — same class, locally instantiated', async () => { |
| 88 | + const kernel = await assemble({ withAdminPlugin: false }); |
| 89 | + try { |
| 90 | + await kernel.bootstrap(); |
| 91 | + const engine = kernel.getService<any>('data'); |
| 92 | + await engine.insert('sys_metadata', undefined as never).catch(() => { /* shape probe only */ }); |
| 93 | + // The default driver exists and the engine can answer a trivial query path. |
| 94 | + expect(typeof engine.find).toBe('function'); |
| 95 | + } finally { |
| 96 | + try { await (kernel as any)?.stop?.(); } catch { /* noop */ } |
| 97 | + } |
| 98 | + }, BOOT_TIMEOUT); |
| 99 | + |
| 100 | + it('refuses the boot when the default cannot be built/connected (bootCritical ⇒ fail-fast)', async () => { |
| 101 | + const kernel = await assemble({ driver: 'not-a-real-driver' }); |
| 102 | + const err = await kernel.bootstrap().then( |
| 103 | + () => { throw new Error('bootstrap() resolved but should have thrown'); }, |
| 104 | + (e: unknown) => e as Error, |
| 105 | + ); |
| 106 | + expect(err.message).toMatch(/default/); |
| 107 | + expect(err.message).toMatch(/boot-critical/); |
| 108 | + expect(err.message).toContain('OS_ALLOW_DRIVER_CONNECT_FAILURE'); |
| 109 | + try { await (kernel as any)?.stop?.(); } catch { /* noop */ } |
| 110 | + }, BOOT_TIMEOUT); |
| 111 | + |
| 112 | + it('boots degraded under OS_ALLOW_DRIVER_CONNECT_FAILURE — same escape hatch as the engine guard', async () => { |
| 113 | + process.env[ENV] = '1'; |
| 114 | + const kernel = await assemble({ driver: 'not-a-real-driver' }); |
| 115 | + try { |
| 116 | + await expect(kernel.bootstrap()).resolves.not.toThrow(); |
| 117 | + } finally { |
| 118 | + try { await (kernel as any)?.stop?.(); } catch { /* noop */ } |
| 119 | + } |
| 120 | + }, BOOT_TIMEOUT); |
| 121 | + |
| 122 | + it('is NOT gated by the host connect policy — a deny-all policy cannot block the primary DB', async () => { |
| 123 | + // Byte-for-byte with the pre-#3826 boot: the default never consulted a |
| 124 | + // DatasourceConnectPolicy (that gate exists for optional/external |
| 125 | + // datasources). A multi-tenant host's deny-all must not brick every boot. |
| 126 | + const kernel = await assemble({ |
| 127 | + connectPolicy: { canConnect: () => ({ allow: false, reason: 'egress blocked' }) }, |
| 128 | + }); |
| 129 | + try { |
| 130 | + await expect(kernel.bootstrap()).resolves.not.toThrow(); |
| 131 | + const engine = kernel.getService<any>('data'); |
| 132 | + expect(engine.getDefaultDriverName()).toBeDefined(); |
| 133 | + } finally { |
| 134 | + try { await (kernel as any)?.stop?.(); } catch { /* noop */ } |
| 135 | + } |
| 136 | + }, BOOT_TIMEOUT); |
| 137 | + |
| 138 | + it("rejects an app bundle that declares a datasource named 'default' (host-reserved name)", async () => { |
| 139 | + const kernel = await assemble({ |
| 140 | + bundle: { |
| 141 | + manifest: { id: 'com.test.reserved', name: 'Reserved', version: '1.0.0' }, |
| 142 | + objects: [{ name: 'note', label: 'Note', fields: { title: { type: 'text' } } }], |
| 143 | + datasources: [{ name: 'default', driver: 'memory', config: {} }], |
| 144 | + }, |
| 145 | + }); |
| 146 | + const err = await kernel.bootstrap().then( |
| 147 | + () => { throw new Error('bootstrap() resolved but should have thrown'); }, |
| 148 | + (e: unknown) => e as Error, |
| 149 | + ); |
| 150 | + expect(err.message).toMatch(/reserved for the host's primary datasource/); |
| 151 | + try { await (kernel as any)?.stop?.(); } catch { /* noop */ } |
| 152 | + }, BOOT_TIMEOUT); |
| 153 | +}); |
0 commit comments