|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// ADR-0062 Phase 1 acceptance (D1/D2/D5): a stack that only *declares* an |
| 4 | +// external datasource — with NO `onEnable` driver wiring — auto-connects it to |
| 5 | +// a live ObjectQL driver and its federated objects become queryable, while a |
| 6 | +// managed + unrouted datasource stays metadata-only (existing apps unchanged). |
| 7 | +// |
| 8 | +// This boots the host-config shape (instantiated plugins, no MetadataPlugin — |
| 9 | +// the same shape `examples/app-showcase` runs under `os dev`) with the REAL |
| 10 | +// driver factory (`createDefaultDatasourceDriverFactory`) building an in-memory |
| 11 | +// driver, so the full AppPlugin → `datasource-connection` → engine path runs |
| 12 | +// without any native driver dependency. |
| 13 | + |
| 14 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 15 | +import { Runtime } from './runtime.js'; |
| 16 | +import { DriverPlugin } from './driver-plugin.js'; |
| 17 | +import { AppPlugin } from './app-plugin.js'; |
| 18 | +import type { DatasourceConnectPolicy } from '@objectstack/service-datasource'; |
| 19 | + |
| 20 | +const BOOT_TIMEOUT = 60_000; |
| 21 | + |
| 22 | +// One external datasource (auto-connect target) + one managed, unrouted |
| 23 | +// datasource (must stay metadata-only). NO `onEnable` anywhere. |
| 24 | +function artifact() { |
| 25 | + return { |
| 26 | + manifest: { id: 'com.test.ds-autoconnect', name: 'DS AutoConnect', version: '1.0.0' }, |
| 27 | + objects: [ |
| 28 | + // Federated object bound to the external datasource (ADR-0015). |
| 29 | + { |
| 30 | + name: 'ext_note', |
| 31 | + label: 'External Note', |
| 32 | + datasource: 'autoconn_ext', |
| 33 | + external: {}, |
| 34 | + fields: { id: { type: 'text' }, title: { type: 'text' } }, |
| 35 | + }, |
| 36 | + // A normal object on the default datasource. |
| 37 | + { name: 'note', label: 'Note', fields: { title: { type: 'text' } } }, |
| 38 | + ], |
| 39 | + datasources: [ |
| 40 | + { |
| 41 | + name: 'autoconn_ext', |
| 42 | + label: 'External (in-memory)', |
| 43 | + driver: 'memory', |
| 44 | + schemaMode: 'external', |
| 45 | + origin: 'code', |
| 46 | + config: {}, |
| 47 | + external: { allowWrites: false, validation: { onMismatch: 'warn', checkOnBoot: false } }, |
| 48 | + active: true, |
| 49 | + }, |
| 50 | + // Managed + unrouted: nothing binds to it, not external, no autoConnect. |
| 51 | + // Mirrors app-crm's decorative `:memory:` datasources — must NOT connect. |
| 52 | + { |
| 53 | + name: 'decorative', |
| 54 | + label: 'Decorative (unrouted)', |
| 55 | + driver: 'memory', |
| 56 | + schemaMode: 'managed', |
| 57 | + origin: 'code', |
| 58 | + config: {}, |
| 59 | + active: true, |
| 60 | + }, |
| 61 | + ], |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +async function boot(opts: { connectPolicy?: DatasourceConnectPolicy } = {}) { |
| 66 | + const { ObjectQLPlugin } = await import('@objectstack/objectql'); |
| 67 | + const { InMemoryDriver } = await import('@objectstack/driver-memory'); |
| 68 | + const { DatasourceAdminServicePlugin, createDefaultDatasourceDriverFactory } = await import( |
| 69 | + '@objectstack/service-datasource' |
| 70 | + ); |
| 71 | + |
| 72 | + const runtime = new Runtime({ cluster: false }); |
| 73 | + const kernel = runtime.getKernel(); |
| 74 | + await kernel.use(new DriverPlugin(new InMemoryDriver())); // default driver |
| 75 | + await kernel.use(new ObjectQLPlugin()); |
| 76 | + await kernel.use(new AppPlugin(artifact())); |
| 77 | + await kernel.use( |
| 78 | + new DatasourceAdminServicePlugin({ |
| 79 | + driverFactory: createDefaultDatasourceDriverFactory(), |
| 80 | + connectPolicy: opts.connectPolicy, |
| 81 | + }), |
| 82 | + ); |
| 83 | + await kernel.bootstrap(); |
| 84 | + return kernel; |
| 85 | +} |
| 86 | + |
| 87 | +describe('ADR-0062 declared-datasource auto-connect', () => { |
| 88 | + let kernel: Awaited<ReturnType<typeof boot>>; |
| 89 | + |
| 90 | + beforeAll(async () => { |
| 91 | + kernel = await boot(); |
| 92 | + }, BOOT_TIMEOUT); |
| 93 | + |
| 94 | + afterAll(async () => { |
| 95 | + try { await (kernel as any)?.stop?.(); } catch { /* noop */ } |
| 96 | + }); |
| 97 | + |
| 98 | + it('auto-connects the declared EXTERNAL datasource as a live driver (no onEnable)', () => { |
| 99 | + const engine = kernel.getService<{ getDriverByName(n: string): unknown }>('data'); |
| 100 | + expect(engine.getDriverByName('autoconn_ext')).toBeDefined(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('leaves a managed + unrouted datasource metadata-only (app-crm byte-for-byte unchanged)', () => { |
| 104 | + const engine = kernel.getService<{ getDriverByName(n: string): unknown }>('data'); |
| 105 | + expect(engine.getDriverByName('decorative')).toBeUndefined(); |
| 106 | + // …but it is still VISIBLE in the metadata registry. |
| 107 | + // (visibility is asserted via the admin service below) |
| 108 | + }); |
| 109 | + |
| 110 | + it('still surfaces BOTH datasources in the metadata registry (visibility unchanged)', async () => { |
| 111 | + const metadata = kernel.getService<{ list(t: string): Promise<any[]> }>('metadata'); |
| 112 | + const names = (await metadata.list('datasource')).map((d) => d?.name); |
| 113 | + expect(names).toContain('autoconn_ext'); |
| 114 | + expect(names).toContain('decorative'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('makes the federated object queryable through the engine with zero app code', async () => { |
| 118 | + const engine = kernel.getService<{ |
| 119 | + getDriverByName(n: string): any; |
| 120 | + find(object: string, query?: any): Promise<any[]>; |
| 121 | + }>('data'); |
| 122 | + // Seed the live external driver directly (bypassing the read-only write gate, |
| 123 | + // exactly as a real remote DB would already hold the rows). |
| 124 | + const driver = engine.getDriverByName('autoconn_ext'); |
| 125 | + await driver.bulkCreate('ext_note', [ |
| 126 | + { id: 'n1', title: 'first' }, |
| 127 | + { id: 'n2', title: 'second' }, |
| 128 | + ]); |
| 129 | + const rows = await engine.find('ext_note'); |
| 130 | + expect(rows.map((r) => r.title).sort()).toEqual(['first', 'second']); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +describe('ADR-0062 connect policy seam', () => { |
| 135 | + it('a deny policy leaves the external datasource unconnected (cloud egress isolation)', async () => { |
| 136 | + const denyExternal: DatasourceConnectPolicy = { |
| 137 | + canConnect: (ds) => (ds.schemaMode === 'external' ? { allow: false, reason: 'egress blocked' } : { allow: true }), |
| 138 | + }; |
| 139 | + const kernel = await boot({ connectPolicy: denyExternal }); |
| 140 | + try { |
| 141 | + const engine = kernel.getService<{ getDriverByName(n: string): unknown }>('data'); |
| 142 | + expect(engine.getDriverByName('autoconn_ext')).toBeUndefined(); |
| 143 | + // Still visible — denied means metadata-only, not invisible. |
| 144 | + const metadata = kernel.getService<{ list(t: string): Promise<any[]> }>('metadata'); |
| 145 | + expect((await metadata.list('datasource')).map((d) => d?.name)).toContain('autoconn_ext'); |
| 146 | + } finally { |
| 147 | + try { await (kernel as any)?.stop?.(); } catch { /* noop */ } |
| 148 | + } |
| 149 | + }, BOOT_TIMEOUT); |
| 150 | +}); |
0 commit comments