|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `listRemoteTables` REQUEST-shape equivalence across its two live spellings |
| 5 | + * (#7955). |
| 6 | + * |
| 7 | + * `IExternalDatasourceService.listRemoteTables` is reachable through two |
| 8 | + * mounted routes, in two packages: |
| 9 | + * |
| 10 | + * GET /api/v1/datasources/:name/external/tables ← this package, federation |
| 11 | + * GET /api/v1/datasources/:name/remote-tables ← @objectstack/service-datasource, admin |
| 12 | + * |
| 13 | + * They are not near-duplicates that happen to look alike: both resolve the SAME |
| 14 | + * `external-datasource` service slot and call the SAME method with the same |
| 15 | + * datasource name. #4249 already reconciled what happens when that one call |
| 16 | + * THROWS ("One operation, one failure contract now, on both paths", |
| 17 | + * `external-datasource-routes.ts`). What was never compared is the other half — |
| 18 | + * what the two paths do with the REQUEST — and #7955 is the residue: the admin |
| 19 | + * spelling never read `req.query`, so `?schema=public` came back as the |
| 20 | + * UNFILTERED listing there and the filtered one here. Not an error, not the |
| 21 | + * filter: the quietest form of "declared ≠ enforced". |
| 22 | + * |
| 23 | + * ## Why this test is here, and why it drives BOTH routes |
| 24 | + * |
| 25 | + * A test that exercised only the fixed route could not fail if the twins drift |
| 26 | + * apart again — it would pin one path's behaviour and say nothing about the |
| 27 | + * relationship, which IS the invariant. So every case below issues the same |
| 28 | + * query to both spellings and asserts the two answers are equal, against ONE |
| 29 | + * service instance mounted on ONE server: the difference between the readings |
| 30 | + * can then only come from the two handlers. |
| 31 | + * |
| 32 | + * It lives in `packages/rest` because that is the side that can reach both |
| 33 | + * halves without widening anyone's public API: `registerDatasourceAdminRoutes` |
| 34 | + * is exported from `@objectstack/service-datasource`'s index, while |
| 35 | + * `registerExternalDatasourceRoutes` is deliberately internal here (it is |
| 36 | + * composed by `rest-api-plugin.ts`, not published). The dependency is dev-only |
| 37 | + * and points rest → service-datasource, which is not a cycle: that package |
| 38 | + * depends on `core`/`spec`/`types` and never on this one. Same reasoning the |
| 39 | + * client-side ledger guard used when it chose its side (`service-route-ledger- |
| 40 | + * coverage.test.ts`, "a service→client package edge would be backwards"). |
| 41 | + * |
| 42 | + * The service under the routes is the REAL `ExternalDatasourceService` over a |
| 43 | + * fake introspector, not a `vi.fn()` recording its arguments. A mock would only |
| 44 | + * prove the admin handler now passes an options bag; what the card asks is that |
| 45 | + * the two routes return the same SET, which takes the real filter. |
| 46 | + * |
| 47 | + * Driven through the real `HonoHttpServer` — the adapter `os serve` mounts — so |
| 48 | + * `?schema=` is parsed by the code that parses it in production. That matters |
| 49 | + * for the last case: a repeated key reaches a handler as an ARRAY, and only a |
| 50 | + * real adapter produces one. |
| 51 | + */ |
| 52 | + |
| 53 | +import { describe, it, expect } from 'vitest'; |
| 54 | +import { HonoHttpServer } from '@objectstack/plugin-hono-server'; |
| 55 | +import { |
| 56 | + ExternalDatasourceService, |
| 57 | + registerDatasourceAdminRoutes, |
| 58 | +} from '@objectstack/service-datasource'; |
| 59 | +import type { IntrospectedColumn, IntrospectedSchema } from '@objectstack/spec/contracts'; |
| 60 | +import { registerExternalDatasourceRoutes } from './external-datasource-routes.js'; |
| 61 | + |
| 62 | +const DS = 'demo_ext'; |
| 63 | + |
| 64 | +/** One remote column, spelled in full so the fixture needs no cast to be an |
| 65 | + * `IntrospectedSchema` — `primaryKey` and `nullable` are both required. */ |
| 66 | +const col = (name: string, primaryKey = false): IntrospectedColumn => ({ |
| 67 | + name, |
| 68 | + type: name === 'id' ? 'uuid' : 'text', |
| 69 | + nullable: !primaryKey, |
| 70 | + primaryKey, |
| 71 | +}); |
| 72 | + |
| 73 | +/** Two remote schemas, so a `?schema=` filter has something to exclude. */ |
| 74 | +const REMOTE: IntrospectedSchema = { |
| 75 | + dialect: 'postgres', |
| 76 | + introspectedAt: '2026-08-12T00:00:00.000Z', |
| 77 | + tables: { |
| 78 | + 'public.customers': { |
| 79 | + name: 'public.customers', |
| 80 | + columns: [col('id', true), col('email')], |
| 81 | + indexes: [], |
| 82 | + }, |
| 83 | + 'public.orders': { |
| 84 | + name: 'public.orders', |
| 85 | + columns: [col('id', true)], |
| 86 | + indexes: [], |
| 87 | + }, |
| 88 | + 'analytics.events': { |
| 89 | + name: 'analytics.events', |
| 90 | + columns: [col('id', true)], |
| 91 | + indexes: [], |
| 92 | + }, |
| 93 | + }, |
| 94 | +}; |
| 95 | + |
| 96 | +/** |
| 97 | + * One server, one service, both registrars — the point of the fixture. |
| 98 | + * |
| 99 | + * `registerDatasourceAdminRoutes` also mounts the datasource-lifecycle family, |
| 100 | + * whose services are absent here; those routes answer 503 and are simply never |
| 101 | + * driven. The two paths under test both resolve `external-datasource`, which is |
| 102 | + * the one service wired. |
| 103 | + */ |
| 104 | +function mountBoth() { |
| 105 | + const service = new ExternalDatasourceService({ |
| 106 | + introspect: async () => REMOTE, |
| 107 | + getDatasource: async (name: string) => ({ name }), |
| 108 | + getObject: async () => undefined, |
| 109 | + listObjects: async () => [], |
| 110 | + }); |
| 111 | + const server = new HonoHttpServer(0); |
| 112 | + const ctx = { |
| 113 | + getService: (name: string) => { |
| 114 | + if (name === 'external-datasource') return service; |
| 115 | + throw new Error(`no service: ${name}`); |
| 116 | + }, |
| 117 | + } as any; |
| 118 | + registerExternalDatasourceRoutes(server, ctx, '/api/v1'); |
| 119 | + registerDatasourceAdminRoutes(server, ctx, '/api/v1'); |
| 120 | + return server.getRawApp(); |
| 121 | +} |
| 122 | + |
| 123 | +/** The two wire spellings of the one operation, keyed by how they are named. */ |
| 124 | +const SPELLING = { |
| 125 | + federation: (qs: string) => `/api/v1/datasources/${DS}/external/tables${qs}`, |
| 126 | + admin: (qs: string) => `/api/v1/datasources/${DS}/remote-tables${qs}`, |
| 127 | +} as const; |
| 128 | + |
| 129 | +interface Reading { |
| 130 | + status: number; |
| 131 | + tables: Array<{ schema?: string; name: string }>; |
| 132 | +} |
| 133 | + |
| 134 | +/** Drive one spelling and read back the table set it answers with. */ |
| 135 | +async function read(app: any, spelling: keyof typeof SPELLING, qs: string): Promise<Reading> { |
| 136 | + const res = await app.fetch(new Request(`http://local${SPELLING[spelling](qs)}`)); |
| 137 | + const body = (await res.json()) as { success: boolean; data?: { tables?: Reading['tables'] } }; |
| 138 | + return { status: res.status, tables: body.data?.tables ?? [] }; |
| 139 | +} |
| 140 | + |
| 141 | +/** Both spellings, same query — the comparison every case makes. */ |
| 142 | +async function readBoth(qs: string): Promise<{ federation: Reading; admin: Reading }> { |
| 143 | + const app = mountBoth(); |
| 144 | + return { |
| 145 | + federation: await read(app, 'federation', qs), |
| 146 | + admin: await read(app, 'admin', qs), |
| 147 | + }; |
| 148 | +} |
| 149 | + |
| 150 | +const qualified = (r: Reading) => r.tables.map((t) => `${t.schema}.${t.name}`).sort(); |
| 151 | + |
| 152 | +describe('listRemoteTables twins agree on the request shape (#7955)', () => { |
| 153 | + it('?schema= returns the SAME filtered set on both spellings', async () => { |
| 154 | + const { federation, admin } = await readBoth('?schema=public'); |
| 155 | + |
| 156 | + expect(federation.status).toBe(200); |
| 157 | + expect(admin.status).toBe(200); |
| 158 | + // The filter really filtered — otherwise "equal" could mean "both unfiltered", |
| 159 | + // which is precisely the pre-#7955 reading on one of the two paths. |
| 160 | + expect(qualified(federation)).toEqual(['public.customers', 'public.orders']); |
| 161 | + expect(qualified(admin)).toEqual(qualified(federation)); |
| 162 | + }); |
| 163 | + |
| 164 | + it('no ?schema= returns the SAME unfiltered set on both spellings', async () => { |
| 165 | + const { federation, admin } = await readBoth(''); |
| 166 | + |
| 167 | + expect(qualified(federation)).toEqual([ |
| 168 | + 'analytics.events', |
| 169 | + 'public.customers', |
| 170 | + 'public.orders', |
| 171 | + ]); |
| 172 | + // The absent-parameter arm is not a formality: a fix that filtered |
| 173 | + // unconditionally would satisfy the case above and break every existing |
| 174 | + // caller of the admin spelling, which has never passed one. |
| 175 | + expect(qualified(admin)).toEqual(qualified(federation)); |
| 176 | + }); |
| 177 | + |
| 178 | + it('a ?schema= that matches nothing returns the SAME empty set on both spellings', async () => { |
| 179 | + const { federation, admin } = await readBoth('?schema=nonexistent'); |
| 180 | + |
| 181 | + expect(federation.tables).toEqual([]); |
| 182 | + expect(admin.tables).toEqual([]); |
| 183 | + expect(admin.status).toBe(federation.status); |
| 184 | + }); |
| 185 | + |
| 186 | + it('a repeated ?schema= degrades to no filter on both spellings, identically', async () => { |
| 187 | + // The adapter surfaces a repeated key as an array; `typeof … === 'string'` |
| 188 | + // is what both handlers do with it, so both fall back to the unfiltered |
| 189 | + // listing rather than filtering by an arbitrary one of the two. Whether such |
| 190 | + // a request should be REFUSED instead is #7606's global ingress question — |
| 191 | + // this case pins that the twins answer it the SAME way today, whatever that |
| 192 | + // card decides tomorrow. |
| 193 | + const { federation, admin } = await readBoth('?schema=public&schema=analytics'); |
| 194 | + |
| 195 | + expect(qualified(federation)).toEqual([ |
| 196 | + 'analytics.events', |
| 197 | + 'public.customers', |
| 198 | + 'public.orders', |
| 199 | + ]); |
| 200 | + expect(qualified(admin)).toEqual(qualified(federation)); |
| 201 | + expect(admin.status).toBe(federation.status); |
| 202 | + }); |
| 203 | + |
| 204 | + it('an empty ?schema= is no filter on both spellings, identically', async () => { |
| 205 | + // `?schema=` parses to the empty string, which is a string — so the |
| 206 | + // coercion keeps it and the service's own `opts?.schema &&` guard is what |
| 207 | + // treats it as "no filter". Both spellings inherit that from the one |
| 208 | + // service, and this pins that neither route second-guesses it. |
| 209 | + const { federation, admin } = await readBoth('?schema='); |
| 210 | + |
| 211 | + expect(qualified(federation)).toEqual([ |
| 212 | + 'analytics.events', |
| 213 | + 'public.customers', |
| 214 | + 'public.orders', |
| 215 | + ]); |
| 216 | + expect(qualified(admin)).toEqual(qualified(federation)); |
| 217 | + }); |
| 218 | +}); |
0 commit comments