|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#28143] Step 2a of `POST /api/v1/marketplace/install-local` — a package |
| 5 | + * declaring a datasource name another holder owns is refused before any write |
| 6 | + * — judges with the runtime's ONE judge (`datasourceNameHolder`), and so: |
| 7 | + * |
| 8 | + * - a holder the metadata registry cannot be read for is `503 |
| 9 | + * SERVICE_UNAVAILABLE`, never taken for "no holder": the install does not |
| 10 | + * proceed past an outage; |
| 11 | + * - a datasource registered on the data engine only (the legacy `onEnable` |
| 12 | + * bridge, with no metadata item) holds its name: `409 RESOURCE_CONFLICT`, |
| 13 | + * where the door used to install and answer that pool `already-registered`. |
| 14 | + * |
| 15 | + * Each refusal leaves the runtime as it found it: no ledger entry, nothing |
| 16 | + * registered. The end-to-end refusals (the boot's datasource, the host's |
| 17 | + * `default`) are pinned by `install-local-declared-datasources.dogfood.test.ts`. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 21 | +import { mkdtempSync, rmSync, readdirSync } from 'node:fs'; |
| 22 | +import { join } from 'node:path'; |
| 23 | +import { tmpdir } from 'node:os'; |
| 24 | +// The install and its 2a judge reach `@objectstack/runtime` through a lazy |
| 25 | +// `import()`; its first load is paid here, at module top — never inside a |
| 26 | +// clocked `it` (`scripts/check-test-source-alias.mjs`). |
| 27 | +import '@objectstack/runtime'; |
| 28 | +import { MarketplaceInstallLocalPlugin } from './marketplace-install-local-plugin.js'; |
| 29 | +import { installerAuthService, withInstallerGrants } from './install-local-principal.fixtures.js'; |
| 30 | + |
| 31 | +type Handler = (c: any) => Promise<any>; |
| 32 | + |
| 33 | +const ROUTE = 'POST /api/v1/marketplace/install-local'; |
| 34 | +const DS = 'qah_store'; |
| 35 | + |
| 36 | +function makeRawApp() { |
| 37 | + const routes = new Map<string, Handler>(); |
| 38 | + return { |
| 39 | + routes, |
| 40 | + get: (p: string, h: Handler) => routes.set(`GET ${p}`, h), |
| 41 | + post: (p: string, h: Handler) => routes.set(`POST ${p}`, h), |
| 42 | + delete: (p: string, h: Handler) => routes.set(`DELETE ${p}`, h), |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +/** A kernel whose metadata registry and data engine the case shapes. */ |
| 47 | +function makeCtx(rawApp: any, opts: { metadataFault?: boolean; engineDrivers?: string[] }) { |
| 48 | + const hooks = new Map<string, any>(); |
| 49 | + const registered: any[] = []; |
| 50 | + const drivers = new Set(opts.engineDrivers ?? []); |
| 51 | + const services: Record<string, any> = { |
| 52 | + manifest: { register: (m: any) => registered.push({ manifest: m }) }, |
| 53 | + auth: installerAuthService(), |
| 54 | + objectql: withInstallerGrants({ |
| 55 | + syncSchemas: async () => undefined, |
| 56 | + registry: { getAllPackages: () => registered }, |
| 57 | + getDriverByName: (name: string) => (drivers.has(name) ? { name } : undefined), |
| 58 | + }), |
| 59 | + metadata: { |
| 60 | + get: async () => { |
| 61 | + if (opts.metadataFault) throw new Error('metadata loader unavailable'); |
| 62 | + return undefined; |
| 63 | + }, |
| 64 | + }, |
| 65 | + }; |
| 66 | + return { |
| 67 | + registered, |
| 68 | + ctx: { |
| 69 | + hook: (e: string, h: any) => hooks.set(e, h), |
| 70 | + getService: (name: string) => { |
| 71 | + if (name === 'http-server') return { getRawApp: () => rawApp }; |
| 72 | + const svc = services[name]; |
| 73 | + if (svc === undefined) throw new Error(`no ${name}`); |
| 74 | + return svc; |
| 75 | + }, |
| 76 | + registerService: (name: string, svc: unknown) => { services[name] = svc; }, |
| 77 | + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 78 | + }, |
| 79 | + fire: async () => { await hooks.get('kernel:ready')?.(); }, |
| 80 | + }; |
| 81 | +} |
| 82 | + |
| 83 | +function makeC(body: any) { |
| 84 | + const json = vi.fn((payload: any, status?: number) => ({ payload, status: status ?? 200 })); |
| 85 | + return { req: { url: 'http://localhost:3000/x', raw: new Request('http://localhost:3000/x'), json: async () => body, param: () => undefined }, json }; |
| 86 | +} |
| 87 | + |
| 88 | +const BODY = { |
| 89 | + manifest: { |
| 90 | + id: 'com.example.qah', |
| 91 | + version: '1.0.0', |
| 92 | + datasources: [{ name: DS, label: 'QA store', driver: 'sqlite', config: { filename: ':memory:' } }], |
| 93 | + objects: [], |
| 94 | + }, |
| 95 | +}; |
| 96 | + |
| 97 | +let dir: string; |
| 98 | +beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'mil-ds-holder-')); }); |
| 99 | +afterEach(() => { rmSync(dir, { recursive: true, force: true }); vi.restoreAllMocks(); }); |
| 100 | + |
| 101 | +async function install(opts: { metadataFault?: boolean; engineDrivers?: string[] }) { |
| 102 | + const rawApp = makeRawApp(); |
| 103 | + const k = makeCtx(rawApp, opts); |
| 104 | + const plugin = new MarketplaceInstallLocalPlugin({ controlPlaneUrl: 'off', storageDir: dir }); |
| 105 | + await plugin.start(k.ctx as any); |
| 106 | + await k.fire(); |
| 107 | + const res = await rawApp.routes.get(ROUTE)!(makeC(BODY)); |
| 108 | + // The plugin registers its own Installed Apps UI manifest at start; only |
| 109 | + // the package's registration is this case's. |
| 110 | + const registered = k.registered.map((r) => r.manifest.id).filter((id) => id === 'com.example.qah'); |
| 111 | + return { res, registered, ledger: readdirSync(dir) }; |
| 112 | +} |
| 113 | + |
| 114 | +describe('install-local 2a — who holds a declared datasource name', () => { |
| 115 | + it('a holder that cannot be read refuses 503, and nothing is written', async () => { |
| 116 | + const { res, registered, ledger } = await install({ metadataFault: true }); |
| 117 | + expect(res.status).toBe(503); |
| 118 | + expect(res.payload?.error?.code).toBe('SERVICE_UNAVAILABLE'); |
| 119 | + expect(res.payload?.error?.details).toEqual({ datasource: DS, reading: 'the metadata registry' }); |
| 120 | + expect(registered).toEqual([]); |
| 121 | + expect(ledger).toEqual([]); |
| 122 | + }); |
| 123 | + |
| 124 | + it('a datasource registered on the engine only holds its name: 409, and nothing is written', async () => { |
| 125 | + const { res, registered, ledger } = await install({ engineDrivers: [DS] }); |
| 126 | + expect(res.status).toBe(409); |
| 127 | + expect(res.payload?.error?.code).toBe('RESOURCE_CONFLICT'); |
| 128 | + expect(res.payload?.error?.details).toEqual({ datasource: DS }); |
| 129 | + expect(registered).toEqual([]); |
| 130 | + expect(ledger).toEqual([]); |
| 131 | + }); |
| 132 | + |
| 133 | + it('CONTROL: a name nobody holds installs', async () => { |
| 134 | + const { res, registered } = await install({ engineDrivers: ['someone_else'] }); |
| 135 | + expect(res.status, JSON.stringify(res.payload)).toBe(200); |
| 136 | + expect(res.payload?.success).toBe(true); |
| 137 | + expect(registered).toEqual(['com.example.qah']); |
| 138 | + }); |
| 139 | +}); |
0 commit comments