|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// test-budget: 40 — spawns `os serve` (src, via runServe) once per single-cause fixture: four boots, two refused and two that reach the ready banner; budget to be re-measured |
| 3 | + |
| 4 | +/** |
| 5 | + * `os serve` refuses to boot when a `plugins` entry fails to load and it cannot |
| 6 | + * rule out that the entry provides the crypto provider (`crypto-provider`). |
| 7 | + * |
| 8 | + * The crypto provider seals every secret the deployment holds. A boot that |
| 9 | + * goes on without the plugin that provides it chooses another one at |
| 10 | + * `kernel:ready`, so this boot refuses instead, naming the entry. That is the |
| 11 | + * rule the one-shot secret commands keep for the same host config. |
| 12 | + * |
| 13 | + * - an entry that does not materialize (a specifier that does not resolve): |
| 14 | + * nothing says what it provides, so the boot is refused; |
| 15 | + * - an entry that materializes, declares the crypto provider in |
| 16 | + * `providesServices`, and is refused by the kernel: refused; |
| 17 | + * - CONTROL: an entry that materializes, declares nothing of the kind, and is |
| 18 | + * refused by the kernel: the boot goes on, as before, and says so; |
| 19 | + * - CONTROL: a config whose every entry loads boots. |
| 20 | + * |
| 21 | + * A real `os serve` process per case (`runServe`): the refusal is a property of |
| 22 | + * the boot loop inside `run()`, which no in-process call reaches. |
| 23 | + */ |
| 24 | + |
| 25 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 26 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 27 | +import { tmpdir } from 'node:os'; |
| 28 | +import { join } from 'node:path'; |
| 29 | +import { runServe } from './helpers/serve-process.js'; |
| 30 | + |
| 31 | +/** Either terminal outcome: the complete ready banner, or the refusal line. */ |
| 32 | +const SETTLED = /Press Ctrl\+C to stop|Refusing to boot/; |
| 33 | +const REFUSED = /Refusing to boot/; |
| 34 | + |
| 35 | +const OBJECT = `objects: [{ name: 'cpl_probe', label: 'Probe', fields: { title: { type: 'text', label: 'Title' } } }],`; |
| 36 | + |
| 37 | +const CONFIGS = { |
| 38 | + // A plugin instance beside the specifier makes this a host config, whose |
| 39 | + // `plugins` the boot loop mounts (a config of specifiers alone boots the |
| 40 | + // library stack instead). |
| 41 | + unresolvable: `export default { |
| 42 | + plugins: [{ name: 'com.acme.host', version: '1.0.0', init: async () => {} }, '@acme/not-installed-crypto-provider'], |
| 43 | + ${OBJECT} |
| 44 | +}; |
| 45 | +`, |
| 46 | + declaringRefused: `export default { |
| 47 | + plugins: [{ |
| 48 | + name: 'com.acme.kms-crypto-provider', |
| 49 | + version: 'not-a-version', |
| 50 | + providesServices: ['crypto-provider'], |
| 51 | + init: async () => {}, |
| 52 | + }], |
| 53 | + ${OBJECT} |
| 54 | +}; |
| 55 | +`, |
| 56 | + nonDeclaringRefused: `export default { |
| 57 | + plugins: [{ |
| 58 | + name: 'com.acme.unrelated', |
| 59 | + version: 'not-a-version', |
| 60 | + providesServices: ['acme-widgets'], |
| 61 | + init: async () => {}, |
| 62 | + }], |
| 63 | + ${OBJECT} |
| 64 | +}; |
| 65 | +`, |
| 66 | + allLoad: `export default { |
| 67 | + plugins: [{ name: 'com.acme.fine', version: '1.0.0', init: async () => {} }], |
| 68 | + ${OBJECT} |
| 69 | +}; |
| 70 | +`, |
| 71 | +} as const; |
| 72 | + |
| 73 | +const dirs: Partial<Record<keyof typeof CONFIGS, string>> = {}; |
| 74 | + |
| 75 | +beforeAll(() => { |
| 76 | + for (const [key, config] of Object.entries(CONFIGS) as Array<[keyof typeof CONFIGS, string]>) { |
| 77 | + const dir = mkdtempSync(join(tmpdir(), `os-crypto-plugin-load-${key}-`)); |
| 78 | + writeFileSync(join(dir, 'objectstack.config.ts'), config, 'utf8'); |
| 79 | + dirs[key] = dir; |
| 80 | + } |
| 81 | +}); |
| 82 | + |
| 83 | +afterAll(() => { |
| 84 | + for (const dir of Object.values(dirs)) if (dir) rmSync(dir, { recursive: true, force: true }); |
| 85 | +}); |
| 86 | + |
| 87 | +const boot = async (key: keyof typeof CONFIGS) => { |
| 88 | + const run = await runServe(dirs[key]!, [], { waitFor: SETTLED, timeoutMs: 150_000 }); |
| 89 | + return { ...run, output: run.stdout + run.stderr }; |
| 90 | +}; |
| 91 | + |
| 92 | +describe('os serve — a plugins entry that fails to load and may provide the crypto provider', () => { |
| 93 | + it('an entry that does not materialize: the boot is refused, naming the entry', async () => { |
| 94 | + const { output, port } = await boot('unresolvable'); |
| 95 | + expect(output).toContain('Failed to load plugin'); |
| 96 | + expect(output, output).toMatch(REFUSED); |
| 97 | + expect(output).toContain('@acme/not-installed-crypto-provider'); |
| 98 | + expect(output).toContain("'crypto-provider'"); |
| 99 | + expect(port, 'a refused boot binds nothing').toBeUndefined(); |
| 100 | + }, 180_000); |
| 101 | + |
| 102 | + it('an entry that declares the crypto provider and is refused by the kernel: the boot is refused, naming it', async () => { |
| 103 | + const { output, port } = await boot('declaringRefused'); |
| 104 | + expect(output).toContain('Failed to load plugin'); |
| 105 | + expect(output, output).toMatch(REFUSED); |
| 106 | + expect(output).toContain('com.acme.kms-crypto-provider'); |
| 107 | + expect(port).toBeUndefined(); |
| 108 | + }, 180_000); |
| 109 | + |
| 110 | + it('CONTROL: an entry that declares no crypto provider and is refused by the kernel: the boot goes on', async () => { |
| 111 | + const { output, port } = await boot('nonDeclaringRefused'); |
| 112 | + expect(output).toContain('Failed to load plugin'); |
| 113 | + expect(output, output).not.toMatch(REFUSED); |
| 114 | + expect(output).toMatch(/Press Ctrl\+C to stop/); |
| 115 | + expect(port).toBeGreaterThan(0); |
| 116 | + }, 180_000); |
| 117 | + |
| 118 | + it('CONTROL: a config whose every entry loads boots', async () => { |
| 119 | + const { output, port } = await boot('allLoad'); |
| 120 | + expect(output).not.toContain('Failed to load plugin'); |
| 121 | + expect(output, output).not.toMatch(REFUSED); |
| 122 | + expect(port).toBeGreaterThan(0); |
| 123 | + }, 180_000); |
| 124 | +}); |
0 commit comments