|
| 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 | + * A stack whose `plugins` lists no plugin instance (no entry with `init`) is |
| 6 | + * not a host config: `os serve` boots the standalone stack and does not load |
| 7 | + * the authored `plugins` entries. It refuses that boot when one of those |
| 8 | + * entries may provide the crypto provider (`crypto-provider`), naming it: |
| 9 | + * |
| 10 | + * - a package specifier that does not import: nothing says what it provides; |
| 11 | + * - an entry that declares the crypto provider in `providesServices`; |
| 12 | + * - CONTROL: an entry that declares another service: the boot goes on; |
| 13 | + * - CONTROL: a stack with no `plugins` at all boots. |
| 14 | + * |
| 15 | + * A real `os serve` process per case (`runServe`): the check sits in `run()`, |
| 16 | + * before the standalone stack replaces the authored list. |
| 17 | + */ |
| 18 | + |
| 19 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 20 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 21 | +import { tmpdir } from 'node:os'; |
| 22 | +import { join } from 'node:path'; |
| 23 | +import { runServe } from './helpers/serve-process.js'; |
| 24 | + |
| 25 | +/** Either terminal outcome: the complete ready banner, or the refusal line. */ |
| 26 | +const SETTLED = /Press Ctrl\+C to stop|Refusing to boot/; |
| 27 | +const REFUSED = /Refusing to boot/; |
| 28 | + |
| 29 | +const OBJECT = `objects: [{ name: 'csa_probe', label: 'Probe', fields: { title: { type: 'text', label: 'Title' } } }],`; |
| 30 | + |
| 31 | +const CONFIGS = { |
| 32 | + unresolvable: `export default { |
| 33 | + plugins: ['@acme/not-installed-crypto-provider'], |
| 34 | + ${OBJECT} |
| 35 | +}; |
| 36 | +`, |
| 37 | + declaring: `export default { |
| 38 | + plugins: [{ name: 'com.acme.kms-crypto-provider', version: '1.0.0', providesServices: ['crypto-provider'] }], |
| 39 | + ${OBJECT} |
| 40 | +}; |
| 41 | +`, |
| 42 | + otherService: `export default { |
| 43 | + plugins: [{ name: 'com.acme.widgets', version: '1.0.0', providesServices: ['acme-widgets'] }], |
| 44 | + ${OBJECT} |
| 45 | +}; |
| 46 | +`, |
| 47 | + noPlugins: `export default { |
| 48 | + ${OBJECT} |
| 49 | +}; |
| 50 | +`, |
| 51 | +} as const; |
| 52 | + |
| 53 | +const dirs: Partial<Record<keyof typeof CONFIGS, string>> = {}; |
| 54 | + |
| 55 | +beforeAll(() => { |
| 56 | + for (const [key, config] of Object.entries(CONFIGS) as Array<[keyof typeof CONFIGS, string]>) { |
| 57 | + const dir = mkdtempSync(join(tmpdir(), `os-crypto-plugin-set-aside-${key}-`)); |
| 58 | + writeFileSync(join(dir, 'objectstack.config.ts'), config, 'utf8'); |
| 59 | + dirs[key] = dir; |
| 60 | + } |
| 61 | +}); |
| 62 | + |
| 63 | +afterAll(() => { |
| 64 | + for (const dir of Object.values(dirs)) if (dir) rmSync(dir, { recursive: true, force: true }); |
| 65 | +}); |
| 66 | + |
| 67 | +const boot = async (key: keyof typeof CONFIGS) => { |
| 68 | + const run = await runServe(dirs[key]!, [], { waitFor: SETTLED, timeoutMs: 150_000 }); |
| 69 | + return { ...run, output: run.stdout + run.stderr }; |
| 70 | +}; |
| 71 | + |
| 72 | +describe('os serve — a plugins entry the standalone boot sets aside and that may provide the crypto provider', () => { |
| 73 | + it('a specifier that does not import: the boot is refused, naming it', async () => { |
| 74 | + const { output, port } = await boot('unresolvable'); |
| 75 | + expect(output, output).toMatch(REFUSED); |
| 76 | + expect(output).toContain('@acme/not-installed-crypto-provider'); |
| 77 | + expect(output).toContain("'crypto-provider'"); |
| 78 | + expect(port, 'a refused boot binds nothing').toBeUndefined(); |
| 79 | + }, 180_000); |
| 80 | + |
| 81 | + it('an entry that declares the crypto provider: the boot is refused, naming it', async () => { |
| 82 | + const { output, port } = await boot('declaring'); |
| 83 | + expect(output, output).toMatch(REFUSED); |
| 84 | + expect(output).toContain('com.acme.kms-crypto-provider'); |
| 85 | + expect(output).toContain("'crypto-provider'"); |
| 86 | + expect(port).toBeUndefined(); |
| 87 | + }, 180_000); |
| 88 | + |
| 89 | + it('CONTROL: an entry that declares another service: the boot goes on', async () => { |
| 90 | + const { output, port } = await boot('otherService'); |
| 91 | + expect(output, output).not.toMatch(REFUSED); |
| 92 | + expect(port).toBeGreaterThan(0); |
| 93 | + }, 180_000); |
| 94 | + |
| 95 | + it('CONTROL: a stack with no plugins boots', async () => { |
| 96 | + const { output, port } = await boot('noPlugins'); |
| 97 | + expect(output, output).not.toMatch(REFUSED); |
| 98 | + expect(port).toBeGreaterThan(0); |
| 99 | + }, 180_000); |
| 100 | +}); |
0 commit comments