|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #22729 — `os package install` reads the install-local answer's `hotLoaded` |
| 5 | + * and `hotLoadError`, and says what the runtime actually did. |
| 6 | + * |
| 7 | + * `POST /api/v1/marketplace/install-local` answers `200` on its LENIENT path |
| 8 | + * too: a cloud-fetched manifest whose `manifest.register` throws is written to |
| 9 | + * the runtime's ledger and loads at the runtime's next restart, while the |
| 10 | + * running kernel does not hold it. Since #22695 that answer says so — |
| 11 | + * `hotLoaded: false`, with the register error's message as `hotLoadError` — |
| 12 | + * and before this change the command printed "Package installed into the |
| 13 | + * running kernel" over it, for any `200` alike. |
| 14 | + * |
| 15 | + * Four cases, because the read is additive and each half needs its own pin: |
| 16 | + * - the lenient answer is NOT reported as a hot install, and names the reason; |
| 17 | + * - CONTROL: a hot-loaded answer prints as it always did; |
| 18 | + * - an answer WITHOUT the keys (a host that predates #22695) prints as it |
| 19 | + * always did — absence is not read as `false`; |
| 20 | + * - a `hotLoaded: false` with no usable reason prints no invented one. |
| 21 | + * |
| 22 | + * Catalog mode only: the air-gapped file path sends an INLINE manifest, and |
| 23 | + * install-local refuses an inline manifest whose register throws with |
| 24 | + * `422 PLUGIN_REGISTER_FAILED` before anything is written, so that path never |
| 25 | + * reaches a `hotLoaded: false` answer. The reader is the same code either way. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 29 | +import PackageInstall from '../src/commands/package/install.js'; |
| 30 | + |
| 31 | +/** The register error the runtime passes through, verbatim. */ |
| 32 | +const REGISTER_ERROR = "object 'crm_account' declares field 'owner' with unknown type 'lookupx'"; |
| 33 | + |
| 34 | +/** Stub the runtime's install POST with the given `data` block. */ |
| 35 | +function stubRuntime(data: Record<string, unknown>): void { |
| 36 | + vi.stubGlobal('fetch', vi.fn(async () => ({ |
| 37 | + ok: true, |
| 38 | + status: 200, |
| 39 | + statusText: 'OK', |
| 40 | + headers: { get: () => null }, |
| 41 | + json: async () => ({ success: true, data }), |
| 42 | + }) as any)); |
| 43 | +} |
| 44 | + |
| 45 | +/** Run the command in catalog mode; return everything it printed and how it exited. */ |
| 46 | +async function runInstall(): Promise<{ out: string; exit: number | undefined }> { |
| 47 | + const lines: string[] = []; |
| 48 | + const capture = (...args: unknown[]) => { lines.push(args.map(String).join(' ')); }; |
| 49 | + vi.spyOn(console, 'log').mockImplementation(capture); |
| 50 | + vi.spyOn(console, 'error').mockImplementation(capture); |
| 51 | + let exit: number | undefined; |
| 52 | + try { |
| 53 | + await PackageInstall.run(['com.acme.crm', '--runtime', 'http://runtime.test']); |
| 54 | + } catch (err: any) { |
| 55 | + exit = err?.oclif?.exit ?? err?.code; |
| 56 | + } |
| 57 | + return { out: lines.join('\n'), exit }; |
| 58 | +} |
| 59 | + |
| 60 | +/** The answer's keys every install carries, whatever the hot-register did. */ |
| 61 | +const INSTALLED = { |
| 62 | + manifestId: 'com.acme.crm', |
| 63 | + version: '1.0.0', |
| 64 | + versionId: 'ver_1', |
| 65 | + installedAt: '2026-10-10T00:00:00.000Z', |
| 66 | +}; |
| 67 | + |
| 68 | +const HOT_INSTALL_LINE = 'Package installed into the running kernel'; |
| 69 | + |
| 70 | +describe('os package install — reads hotLoaded / hotLoadError', () => { |
| 71 | + afterEach(() => { |
| 72 | + vi.unstubAllGlobals(); |
| 73 | + vi.restoreAllMocks(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('on hotLoaded: false, says installed, loads at the next restart, and names hotLoadError', async () => { |
| 77 | + stubRuntime({ ...INSTALLED, hotLoaded: false, hotLoadError: REGISTER_ERROR }); |
| 78 | + |
| 79 | + const { out, exit } = await runInstall(); |
| 80 | + |
| 81 | + // The defect: a hot install claimed over a kernel that does not hold it. |
| 82 | + expect(out).not.toContain(HOT_INSTALL_LINE); |
| 83 | + // What happened instead, and when the package does load. |
| 84 | + expect(out).toMatch(/Package installed, but the running kernel could not load it/); |
| 85 | + expect(out).toMatch(/loads at the runtime's next restart/); |
| 86 | + // The reason, as the runtime answered it. |
| 87 | + expect(out).toContain(REGISTER_ERROR); |
| 88 | + // The install itself succeeded (ledger written, `200`): same exit as a hot one. |
| 89 | + expect(exit).toBeUndefined(); |
| 90 | + expect(out).toContain('com.acme.crm'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('CONTROL: on hotLoaded: true, prints as before', async () => { |
| 94 | + stubRuntime({ ...INSTALLED, hotLoaded: true }); |
| 95 | + |
| 96 | + const { out, exit } = await runInstall(); |
| 97 | + |
| 98 | + expect(out).toContain(HOT_INSTALL_LINE); |
| 99 | + expect(out).not.toContain('next restart'); |
| 100 | + expect(out).not.toContain('could not load'); |
| 101 | + expect(exit).toBeUndefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('an answer without hotLoaded (a host that predates the key) prints as before', async () => { |
| 105 | + stubRuntime({ ...INSTALLED }); |
| 106 | + |
| 107 | + const { out, exit } = await runInstall(); |
| 108 | + |
| 109 | + expect(out).toContain(HOT_INSTALL_LINE); |
| 110 | + expect(out).not.toContain('next restart'); |
| 111 | + expect(out).not.toContain('could not load'); |
| 112 | + expect(exit).toBeUndefined(); |
| 113 | + }); |
| 114 | + |
| 115 | + it.each([ |
| 116 | + ['absent', undefined], |
| 117 | + ['an empty string', ''], |
| 118 | + ['whitespace', ' '], |
| 119 | + ['not a string', 42], |
| 120 | + ])('on hotLoaded: false with hotLoadError %s, invents no reason', async (_label, value) => { |
| 121 | + stubRuntime({ ...INSTALLED, hotLoaded: false, ...(value === undefined ? {} : { hotLoadError: value }) }); |
| 122 | + |
| 123 | + const { out } = await runInstall(); |
| 124 | + |
| 125 | + expect(out).not.toContain(HOT_INSTALL_LINE); |
| 126 | + expect(out).toMatch(/loads at the runtime's next restart/); |
| 127 | + expect(out).not.toContain('Hot-load error'); |
| 128 | + expect(out).not.toContain('undefined'); |
| 129 | + expect(out).not.toContain('42'); |
| 130 | + }); |
| 131 | +}); |
0 commit comments