|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `POST /datasources/:name/test` on a name that resolves no saved datasource. |
| 5 | + * |
| 6 | + * Every other by-name door answers an unknown name `404 RESOURCE_NOT_FOUND` |
| 7 | + * (`datasource-unknown-name.test.ts` holds the four `datasource-admin` ones). |
| 8 | + * Test answered it `200 { ok: false }` with the engine's "has no registered |
| 9 | + * driver to introspect" sentence, which is the answer it gives a saved |
| 10 | + * datasource that is down. An admin tool or script testing a datasource by |
| 11 | + * name could not tell "this datasource is down" from "this datasource does not |
| 12 | + * exist". |
| 13 | + * |
| 14 | + * The triage ruling these pins hold: resolve the saved name with the lookup |
| 15 | + * the sibling routes use and answer `RESOURCE_NOT_FOUND`; keep `{ ok: false }` |
| 16 | + * for a datasource that exists and fails its probe. Two controls ride along: |
| 17 | + * a saved datasource that is up still answers `ok: true`, and the platform's |
| 18 | + * primary datasource, which may have no stored record, is still tested through |
| 19 | + * its live driver. |
| 20 | + * |
| 21 | + * The composition is the real one, end to end: a real `ObjectQL` engine, the |
| 22 | + * real connection service and driver factory, the real admin service, the real |
| 23 | + * `ExternalDatasourceServicePlugin` wiring, and the real admin routes over a |
| 24 | + * Hono server. Both services read the same stored records, as the two plugins |
| 25 | + * do (`metadata.get('datasource', name)`). |
| 26 | + */ |
| 27 | + |
| 28 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 29 | +import { ObjectQL } from '@objectstack/objectql'; |
| 30 | +import { HonoHttpServer } from '@objectstack/plugin-hono-server'; |
| 31 | +import { DatasourceConnectionService, DEFAULT_DATASOURCE_NAME } from '../datasource-connection-service.js'; |
| 32 | +import { createDefaultDatasourceDriverFactory } from '../default-datasource-driver-factory.js'; |
| 33 | +import { ExternalDatasourceServicePlugin } from '../plugin.js'; |
| 34 | +import { ExternalDatasourceService } from '../external-datasource-service.js'; |
| 35 | +import { DatasourceAdminService, type StoredDatasource } from '../datasource-admin-service.js'; |
| 36 | +import { registerDatasourceAdminRoutes } from '../admin-routes.js'; |
| 37 | +import { ENTITLED_CREDENTIAL, createSessionAuthService, createGrantsEngine } from './entitled-caller.fixture.js'; |
| 38 | + |
| 39 | +const teardowns: Array<() => Promise<void>> = []; |
| 40 | +afterEach(async () => { |
| 41 | + for (const t of teardowns.splice(0)) await t(); |
| 42 | +}); |
| 43 | + |
| 44 | +/** |
| 45 | + * The saved datasources. `warehouse` is connected; `offline` is saved but has |
| 46 | + * no live driver (nothing connected it), so its probe fails. The primary |
| 47 | + * datasource has NO record here: it is connected `asDefault`, as |
| 48 | + * `DefaultDatasourcePlugin.init()` does, and nothing stores it. |
| 49 | + */ |
| 50 | +const RECORDS: StoredDatasource[] = [ |
| 51 | + { name: 'warehouse', driver: 'memory', origin: 'runtime', config: {} }, |
| 52 | + { name: 'offline', driver: 'memory', origin: 'runtime', config: {} }, |
| 53 | +]; |
| 54 | + |
| 55 | +async function boot() { |
| 56 | + const ql = new ObjectQL(); |
| 57 | + const connection = new DatasourceConnectionService({ |
| 58 | + factory: () => createDefaultDatasourceDriverFactory({}), |
| 59 | + engine: () => ql as never, |
| 60 | + }); |
| 61 | + teardowns.push(() => connection.disconnectAll()); |
| 62 | + |
| 63 | + await connection.connect( |
| 64 | + { name: DEFAULT_DATASOURCE_NAME, driver: 'memory', config: {}, origin: 'code', bootCritical: true }, |
| 65 | + { asDefault: true, context: { origin: 'code', trigger: 'declared-auto' } }, |
| 66 | + ); |
| 67 | + await connection.connect(RECORDS[0], { context: { origin: 'runtime', trigger: 'runtime-admin' } }); |
| 68 | + |
| 69 | + const records = new Map(RECORDS.map((r) => [r.name, r])); |
| 70 | + const admin = new DatasourceAdminService({ |
| 71 | + listDatasourceRecords: async () => [...records.values()], |
| 72 | + getDatasourceRecord: async (n: string) => records.get(n), |
| 73 | + connectionStates: () => connection.listConnectionStates(), |
| 74 | + } as never); |
| 75 | + |
| 76 | + const services = new Map<string, unknown>([ |
| 77 | + ['data', ql], |
| 78 | + ['metadata', { get: async (type: string, n: string) => (type === 'datasource' ? records.get(n) : undefined) }], |
| 79 | + ['datasource-connection', connection], |
| 80 | + ]); |
| 81 | + await new ExternalDatasourceServicePlugin().init({ |
| 82 | + getService: (n: string) => { |
| 83 | + if (!services.has(n)) throw new Error(`no service: ${n}`); |
| 84 | + return services.get(n); |
| 85 | + }, |
| 86 | + registerService: (n: string, s: unknown) => void services.set(n, s), |
| 87 | + } as never); |
| 88 | + const external = services.get('external-datasource') as ExternalDatasourceService; |
| 89 | + |
| 90 | + const server = new HonoHttpServer(0); |
| 91 | + const authService = createSessionAuthService(); |
| 92 | + const grantsEngine = createGrantsEngine(); |
| 93 | + registerDatasourceAdminRoutes( |
| 94 | + server, |
| 95 | + { |
| 96 | + getService: (n: string) => { |
| 97 | + if (n === 'auth') return authService; |
| 98 | + if (n === 'objectql' || n === 'data') return grantsEngine; |
| 99 | + if (n === 'external-datasource') return external; |
| 100 | + if (n === 'datasource-admin') return admin; |
| 101 | + throw new Error(`no service: ${n}`); |
| 102 | + }, |
| 103 | + } as never, |
| 104 | + '/api/v1', |
| 105 | + ); |
| 106 | + const app = server.getRawApp(); |
| 107 | + const call = async (method: 'GET' | 'POST', path: string) => { |
| 108 | + const res = await app.fetch( |
| 109 | + new Request(`http://local/api/v1/datasources${path}`, { |
| 110 | + method, |
| 111 | + headers: { authorization: ENTITLED_CREDENTIAL, 'content-type': 'application/json' }, |
| 112 | + ...(method === 'POST' ? { body: '{}' } : {}), |
| 113 | + }), |
| 114 | + ); |
| 115 | + return { status: res.status, body: (await res.json()) as any }; |
| 116 | + }; |
| 117 | + |
| 118 | + return { call, records }; |
| 119 | +} |
| 120 | + |
| 121 | +describe('POST /datasources/:name/test on a name that resolves no saved datasource', () => { |
| 122 | + it('answers 404 RESOURCE_NOT_FOUND naming the datasource, not 200 { ok: false }', async () => { |
| 123 | + const { call } = await boot(); |
| 124 | + |
| 125 | + const test = await call('POST', '/no_such_ds/test'); |
| 126 | + |
| 127 | + expect({ status: test.status, success: test.body.success, code: test.body.error?.code }).toEqual({ |
| 128 | + status: 404, |
| 129 | + success: false, |
| 130 | + code: 'RESOURCE_NOT_FOUND', |
| 131 | + }); |
| 132 | + expect(test.body.error.message).toContain('no_such_ds'); |
| 133 | + expect(test.body).not.toHaveProperty('data'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('gives the answer GET /datasources/:name gives the same name: status, code and message', async () => { |
| 137 | + const { call } = await boot(); |
| 138 | + |
| 139 | + const read = await call('GET', '/no_such_ds'); |
| 140 | + const test = await call('POST', '/no_such_ds/test'); |
| 141 | + |
| 142 | + expect(read.status).toBe(404); |
| 143 | + expect({ status: test.status, code: test.body.error?.code, message: test.body.error?.message }).toEqual({ |
| 144 | + status: read.status, |
| 145 | + code: read.body.error.code, |
| 146 | + message: read.body.error.message, |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
| 150 | + |
| 151 | +describe('POST /datasources/:name/test on a datasource that exists', () => { |
| 152 | + it('a saved datasource that fails its probe still answers 200 { ok: false, error }', async () => { |
| 153 | + const { call } = await boot(); |
| 154 | + |
| 155 | + const test = await call('POST', '/offline/test'); |
| 156 | + |
| 157 | + expect(test.status).toBe(200); |
| 158 | + expect(test.body.data.ok).toBe(false); |
| 159 | + expect(typeof test.body.data.error).toBe('string'); |
| 160 | + expect(test.body.data.error).toContain('offline'); |
| 161 | + }); |
| 162 | + |
| 163 | + it('a saved datasource that is up still answers 200 { ok: true }', async () => { |
| 164 | + const { call } = await boot(); |
| 165 | + |
| 166 | + const test = await call('POST', '/warehouse/test'); |
| 167 | + |
| 168 | + expect(test.status).toBe(200); |
| 169 | + expect(test.body.data).toMatchObject({ ok: true }); |
| 170 | + }); |
| 171 | + |
| 172 | + it('the primary datasource with no stored record is still tested through its live driver', async () => { |
| 173 | + const { call, records } = await boot(); |
| 174 | + expect(records.has(DEFAULT_DATASOURCE_NAME)).toBe(false); |
| 175 | + |
| 176 | + const test = await call('POST', `/${DEFAULT_DATASOURCE_NAME}/test`); |
| 177 | + |
| 178 | + expect(test.status).toBe(200); |
| 179 | + expect(test.body.data).toMatchObject({ ok: true }); |
| 180 | + expect(typeof test.body.data.latencyMs).toBe('number'); |
| 181 | + }); |
| 182 | +}); |
0 commit comments