|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #22695 — `POST /api/v1/marketplace/install-local` answers `hotLoaded` from |
| 5 | + * what its hot-register (step 3, `manifest.register`) actually did. |
| 6 | + * |
| 7 | + * ## What was wrong |
| 8 | + * |
| 9 | + * The answer wrote `hotLoaded: true` as a literal. A cloud-fetched manifest |
| 10 | + * whose register throws takes the LENIENT path — the install still persists its |
| 11 | + * ledger entry and answers `200`, and the package loads at the next restart — |
| 12 | + * so that answer told its caller the running kernel held a package it did not. |
| 13 | + * The console read it as "the app should now appear" and refreshed an app list |
| 14 | + * that did not have the app. |
| 15 | + * |
| 16 | + * ## What these cases pin |
| 17 | + * |
| 18 | + * - the lenient path: `200`, `hotLoaded: false`, the register error's message |
| 19 | + * as `hotLoadError`, the ledger entry written (the path stays lenient — ⛔ |
| 20 | + * whether it should is not decided here), and the note no longer claims the |
| 21 | + * app is available; |
| 22 | + * - CONTROL, a register that resolves: `hotLoaded: true` and NO `hotLoadError` |
| 23 | + * key at all — omitted, not nulled, so the success answer is unchanged; |
| 24 | + * - CONTROL, an inline manifest whose register throws: still refused with |
| 25 | + * `422 PLUGIN_REGISTER_FAILED` in the declared envelope, nothing written. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 29 | +import { mkdtempSync, rmSync, readdirSync } from 'node:fs'; |
| 30 | +import { join } from 'node:path'; |
| 31 | +import { tmpdir } from 'node:os'; |
| 32 | +import { BaseResponseSchema, ApiErrorSchema, envelopeViolations } from '@objectstack/spec/api'; |
| 33 | +// An install binds the package's handlers through `@objectstack/runtime` (a |
| 34 | +// lazy `import()` inside the plugin). Its first load is paid here, at module |
| 35 | +// top — never inside a clocked `it` (`scripts/check-test-source-alias.mjs`). |
| 36 | +import '@objectstack/runtime'; |
| 37 | +import { MarketplaceInstallLocalPlugin } from './marketplace-install-local-plugin.js'; |
| 38 | +import { LocalManifestSource } from './local-manifest-source.js'; |
| 39 | +import { installerAuthService, withInstallerGrants } from './install-local-principal.fixtures.js'; |
| 40 | + |
| 41 | +type Handler = (c: any) => Promise<any>; |
| 42 | + |
| 43 | +const ROUTE = '/api/v1/marketplace/install-local'; |
| 44 | +const APP_ID = 'com.example.crm'; |
| 45 | +const REGISTER_FAILURE = 'object crm_account: field owner references unknown object sys_owner'; |
| 46 | + |
| 47 | +function makeRawApp() { |
| 48 | + const routes = new Map<string, Handler>(); |
| 49 | + return { |
| 50 | + routes, |
| 51 | + get: (p: string, h: Handler) => routes.set(`GET ${p}`, h), |
| 52 | + post: (p: string, h: Handler) => routes.set(`POST ${p}`, h), |
| 53 | + delete: (p: string, h: Handler) => routes.set(`DELETE ${p}`, h), |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Mount the plugin through its real lifecycle (`start()` + `kernel:ready`). |
| 59 | + * `failRegisterOf` names the ONE manifest id whose register throws — the |
| 60 | + * plugin's own UI bundle still registers at `kernel:ready`, as on a real boot. |
| 61 | + */ |
| 62 | +async function mount(dir: string, opts: { controlPlaneUrl?: string; failRegisterOf?: string } = {}) { |
| 63 | + const rawApp = makeRawApp(); |
| 64 | + const hooks = new Map<string, any>(); |
| 65 | + const registry: any[] = []; |
| 66 | + const register = vi.fn(async (m: any) => { |
| 67 | + if (opts.failRegisterOf !== undefined && m?.id === opts.failRegisterOf) { |
| 68 | + throw new Error(REGISTER_FAILURE); |
| 69 | + } |
| 70 | + registry.push({ manifest: m }); |
| 71 | + }); |
| 72 | + const syncSchemas = vi.fn(async () => undefined); |
| 73 | + const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() }; |
| 74 | + const services: Record<string, any> = { |
| 75 | + manifest: { register }, |
| 76 | + auth: installerAuthService(), |
| 77 | + objectql: withInstallerGrants({ syncSchemas, registry: { getAllPackages: () => registry } }), |
| 78 | + }; |
| 79 | + const ctx = { |
| 80 | + hook: (e: string, h: any) => hooks.set(e, h), |
| 81 | + getService: (name: string) => { |
| 82 | + if (name === 'http-server') return { getRawApp: () => rawApp }; |
| 83 | + const svc = services[name]; |
| 84 | + if (svc === undefined) throw new Error(`no ${name}`); |
| 85 | + return svc; |
| 86 | + }, |
| 87 | + logger, |
| 88 | + }; |
| 89 | + const plugin = new MarketplaceInstallLocalPlugin({ controlPlaneUrl: opts.controlPlaneUrl ?? 'off', storageDir: dir }); |
| 90 | + await plugin.start(ctx as any); |
| 91 | + await hooks.get('kernel:ready')?.(); |
| 92 | + const registeredAtBoot = register.mock.calls.length; |
| 93 | + return { |
| 94 | + install: (body: unknown) => rawApp.routes.get(`POST ${ROUTE}`)!(makeC(body)), |
| 95 | + logger, |
| 96 | + /** The manifest ids the install ASKED to register, whether or not the register resolved. */ |
| 97 | + installRegisterAttempts: () => register.mock.calls.slice(registeredAtBoot).map((call) => call[0]?.id), |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +function makeC(body: any) { |
| 102 | + const json = vi.fn((payload: any, status?: number) => ({ payload, status: status ?? 200 })); |
| 103 | + return { |
| 104 | + req: { |
| 105 | + url: `http://localhost:3000${ROUTE}`, |
| 106 | + raw: new Request(`http://localhost:3000${ROUTE}`), |
| 107 | + json: async () => body, |
| 108 | + param: () => undefined, |
| 109 | + }, |
| 110 | + json, |
| 111 | + }; |
| 112 | +} |
| 113 | + |
| 114 | +/** Serve one manifest snapshot from the cloud leg; the public fast-path is off. */ |
| 115 | +function serveSnapshot(manifest: unknown) { |
| 116 | + vi.stubEnv('OS_MARKETPLACE_PUBLIC_BASE_URL', 'off'); |
| 117 | + // A non-empty service key, so the credential read never reaches the |
| 118 | + // developer's own on-disk binding. |
| 119 | + vi.stubEnv('OS_CLOUD_API_KEY', 'svc-test-key'); |
| 120 | + const fetchMock = vi.fn(async () => new Response( |
| 121 | + JSON.stringify({ data: { manifest, version: '1.0.0', version_id: 'ver_1' } }), |
| 122 | + { status: 200, headers: { 'Content-Type': 'application/json' } }, |
| 123 | + )); |
| 124 | + vi.stubGlobal('fetch', fetchMock); |
| 125 | + return fetchMock; |
| 126 | +} |
| 127 | + |
| 128 | +const MANIFEST = { id: APP_ID, version: '1.0.0', objects: [] }; |
| 129 | + |
| 130 | +let dir: string; |
| 131 | +beforeEach(() => { dir = mkdtempSync(join(tmpdir(), 'mil-hot-loaded-')); }); |
| 132 | +afterEach(() => { |
| 133 | + rmSync(dir, { recursive: true, force: true }); |
| 134 | + vi.unstubAllGlobals(); |
| 135 | + vi.unstubAllEnvs(); |
| 136 | + vi.restoreAllMocks(); |
| 137 | +}); |
| 138 | + |
| 139 | +describe('install-local answers hotLoaded from what the hot-register did', () => { |
| 140 | + it('a cloud-fetched manifest whose register throws answers 200 with hotLoaded: false and the register error as hotLoadError', async () => { |
| 141 | + const fetchMock = serveSnapshot(MANIFEST); |
| 142 | + const h = await mount(dir, { controlPlaneUrl: 'http://cloud.test', failRegisterOf: APP_ID }); |
| 143 | + |
| 144 | + const res = await h.install({ packageId: 'pkg_crm' }); |
| 145 | + |
| 146 | + // Identity: the manifest came from the cloud leg, and the install |
| 147 | + // really asked the kernel to register it — the throw is step 3's. |
| 148 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 149 | + expect(h.installRegisterAttempts()).toEqual([APP_ID]); |
| 150 | + |
| 151 | + expect(res.status, JSON.stringify(res.payload)).toBe(200); |
| 152 | + expect(res.payload.success).toBe(true); |
| 153 | + expect(res.payload.data.manifestId).toBe(APP_ID); |
| 154 | + expect(res.payload.data.hotLoaded).toBe(false); |
| 155 | + expect(res.payload.data.hotLoadError).toBe(REGISTER_FAILURE); |
| 156 | + // The note makes the same claim `hotLoaded` does, so it no longer says |
| 157 | + // the app is available in a kernel that does not hold it. |
| 158 | + expect(res.payload.data.note).not.toContain('now available'); |
| 159 | + |
| 160 | + // Still lenient: the ledger entry is written, so the next restart |
| 161 | + // loads it. Whether this path should stay lenient is not decided here. |
| 162 | + expect(new LocalManifestSource(dir).read(APP_ID).entry?.manifestId).toBe(APP_ID); |
| 163 | + }); |
| 164 | + |
| 165 | + it('CONTROL — a register that resolves answers hotLoaded: true, with no hotLoadError key at all', async () => { |
| 166 | + serveSnapshot(MANIFEST); |
| 167 | + const h = await mount(dir, { controlPlaneUrl: 'http://cloud.test' }); |
| 168 | + |
| 169 | + const res = await h.install({ packageId: 'pkg_crm' }); |
| 170 | + |
| 171 | + expect(res.status, JSON.stringify(res.payload)).toBe(200); |
| 172 | + expect(res.payload.success).toBe(true); |
| 173 | + expect(h.installRegisterAttempts()).toEqual([APP_ID]); |
| 174 | + expect(res.payload.data.hotLoaded).toBe(true); |
| 175 | + expect(Object.keys(res.payload.data)).not.toContain('hotLoadError'); |
| 176 | + expect(res.payload.data.note).toContain('now available'); |
| 177 | + }); |
| 178 | + |
| 179 | + it('CONTROL — an inline manifest whose register throws is still refused: 422 PLUGIN_REGISTER_FAILED, nothing written', async () => { |
| 180 | + const h = await mount(dir, { failRegisterOf: APP_ID }); |
| 181 | + |
| 182 | + const res = await h.install({ manifest: MANIFEST }); |
| 183 | + |
| 184 | + expect(h.installRegisterAttempts()).toEqual([APP_ID]); |
| 185 | + expect(res.status).toBe(422); |
| 186 | + expect(BaseResponseSchema.safeParse(res.payload).success).toBe(true); |
| 187 | + expect(envelopeViolations(res.payload)).toEqual([]); |
| 188 | + expect(ApiErrorSchema.safeParse(res.payload.error).success).toBe(true); |
| 189 | + expect(res.payload.success).toBe(false); |
| 190 | + expect(res.payload.error.code).toBe('PLUGIN_REGISTER_FAILED'); |
| 191 | + expect(res.payload.error.message).toContain(REGISTER_FAILURE); |
| 192 | + expect(readdirSync(dir)).toEqual([]); |
| 193 | + }); |
| 194 | +}); |
0 commit comments