|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #36647, end to end: a real `os build` of a stack that lists a code plugin, |
| 5 | + * then a real boot of the artifact it wrote. The boot runs none of the app's |
| 6 | + * code plugins, and says so, naming each one, once. |
| 7 | + * |
| 8 | + * The build half is the measured premise. `os build` writes the stack's |
| 9 | + * `plugins` into `objectstack.json` as plain JSON (the instance's own data, no |
| 10 | + * `init`), and nothing on the artifact path turns that back into a plugin: |
| 11 | + * `plugins` are artifact ENVELOPE keys, runtime assembly instructions a host |
| 12 | + * hands to `kernel.use()`, never metadata the artifact carries. The boot half |
| 13 | + * is the fix: the artifact door's `AppPlugin` names every listed plugin the |
| 14 | + * kernel's own table does not hold (`@objectstack/runtime`, |
| 15 | + * `artifact-declared-plugins.ts`), so the drop is no longer silent. |
| 16 | + * |
| 17 | + * The boot reaches `@objectstack/runtime` through its `exports` map, so it |
| 18 | + * measures `dist/`: rebuild the runtime before reading a result here. |
| 19 | + */ |
| 20 | + |
| 21 | +import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'; |
| 22 | +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 23 | +import { tmpdir } from 'node:os'; |
| 24 | +import { join, resolve } from 'node:path'; |
| 25 | +import { fileURLToPath } from 'node:url'; |
| 26 | +import { createStandaloneStack, Runtime } from '@objectstack/runtime'; |
| 27 | +import Compile from '../src/commands/compile'; |
| 28 | +import { defineStackSourceFromLiteral, linkSpec } from './helpers/define-stack-fixture.js'; |
| 29 | + |
| 30 | +// Pay the first transform of these dist-resolved workspace deps at MODULE LOAD: |
| 31 | +// the build and the boot below import them lazily, inside a clocked hook |
| 32 | +// (`scripts/check-test-source-alias.mjs`, the clocked-window rule). |
| 33 | +import '@objectstack/objectql/core'; |
| 34 | +import '@objectstack/plugin-security'; |
| 35 | + |
| 36 | +const CLI_ROOT = resolve(fileURLToPath(import.meta.url), '..', '..'); |
| 37 | +const APP_ID = 'com.test.issue-36647-build'; |
| 38 | +const VECTOR = 'com.test.issue-36647-build.vector'; |
| 39 | +const INDEXER = 'com.test.issue-36647-build.indexer'; |
| 40 | +const BOOT_TIMEOUT = 120_000; |
| 41 | + |
| 42 | +/** |
| 43 | + * The stack as an author writes it: two code plugins, live instances with an |
| 44 | + * `init`, the shape `plugins: [new XPlugin()]` evaluates to. |
| 45 | + */ |
| 46 | +const CONFIG_LITERAL = `{ |
| 47 | + manifest: { id: '${APP_ID}', name: 'Plugin Build Probe', version: '1.0.0', type: 'app', namespace: 'probeb' }, |
| 48 | + objects: [{ name: 'probeb_clause', label: 'Clause', sharingModel: 'private', fields: { title: { type: 'text', label: 'Title' } } }], |
| 49 | + plugins: [ |
| 50 | + { name: '${VECTOR}', version: '1.0.0', dependencies: [], options: { dimensions: 1536 }, init: async () => {} }, |
| 51 | + { name: '${INDEXER}', version: '1.0.0', dependencies: ['${VECTOR}'], init: async () => {} }, |
| 52 | + ], |
| 53 | +}`; |
| 54 | + |
| 55 | +let root = ''; |
| 56 | +let artifactPath = ''; |
| 57 | + |
| 58 | +/** Runs `os build` in-process; answers the exit code (0 when the command returned) and what it printed. */ |
| 59 | +async function build(configPath: string, output: string): Promise<{ code: number; stdout: string }> { |
| 60 | + const chunks: string[] = []; |
| 61 | + const spy = vi.spyOn(process.stdout, 'write').mockImplementation(((chunk: unknown, ...rest: unknown[]) => { |
| 62 | + chunks.push(String(chunk)); |
| 63 | + const done = rest.find((a) => typeof a === 'function') as ((e?: Error | null) => void) | undefined; |
| 64 | + done?.(null); |
| 65 | + return true; |
| 66 | + }) as never); |
| 67 | + const exitCodeBefore = process.exitCode; |
| 68 | + try { |
| 69 | + await Compile.run([configPath, '--output', output], { root: CLI_ROOT }); |
| 70 | + return { code: 0, stdout: chunks.join('') }; |
| 71 | + } catch (error) { |
| 72 | + const exit = (error as { oclif?: { exit?: unknown } }).oclif?.exit; |
| 73 | + if (typeof exit !== 'number') throw error; |
| 74 | + return { code: exit, stdout: chunks.join('') }; |
| 75 | + } finally { |
| 76 | + spy.mockRestore(); |
| 77 | + process.exitCode = exitCodeBefore; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +beforeAll(async () => { |
| 82 | + root = mkdtempSync(join(tmpdir(), 'os-36647-build-')); |
| 83 | + const dir = join(root, 'app'); |
| 84 | + mkdirSync(dir, { recursive: true }); |
| 85 | + const configPath = join(dir, 'objectstack.config.ts'); |
| 86 | + writeFileSync(configPath, defineStackSourceFromLiteral(CONFIG_LITERAL)); |
| 87 | + linkSpec(dir); |
| 88 | + mkdirSync(join(root, 'host'), { recursive: true }); |
| 89 | + artifactPath = join(dir, 'dist', 'objectstack.json'); |
| 90 | + const built = await build(configPath, artifactPath); |
| 91 | + expect(built.code, built.stdout).toBe(0); |
| 92 | +}, 60_000); |
| 93 | + |
| 94 | +afterAll(() => { |
| 95 | + if (root) rmSync(root, { recursive: true, force: true }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('#36647 — os build, then an artifact boot of what it wrote', () => { |
| 99 | + it('PREMISE: the artifact holds the plugins as inert descriptors, nothing that can start', () => { |
| 100 | + const artifact = JSON.parse(readFileSync(artifactPath, 'utf8')) as { plugins?: Array<Record<string, unknown>> }; |
| 101 | + expect(artifact.plugins?.map((p) => p.name)).toEqual([VECTOR, INDEXER]); |
| 102 | + for (const p of artifact.plugins ?? []) expect(p).not.toHaveProperty('init'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('the boot runs neither plugin, and warns once naming both', async () => { |
| 106 | + let kernel: any; |
| 107 | + try { |
| 108 | + const standalone = await createStandaloneStack({ |
| 109 | + artifactPath, |
| 110 | + // A directory of its own: the boot's root, apart from the source tree the build read. |
| 111 | + projectRoot: join(root, 'host'), |
| 112 | + databaseUrl: ':memory:', |
| 113 | + skipSeedData: true, |
| 114 | + runPlatformMigrations: false, |
| 115 | + }); |
| 116 | + kernel = new Runtime({ cluster: false }).getKernel(); |
| 117 | + const warn = vi.spyOn(kernel.logger, 'warn'); |
| 118 | + for (const p of standalone.plugins) await kernel.use(p as never); |
| 119 | + await kernel.bootstrap(); |
| 120 | + // CONTROL: the drop is real. |
| 121 | + expect(kernel.hasPlugin(VECTOR)).toBe(false); |
| 122 | + expect(kernel.hasPlugin(INDEXER)).toBe(false); |
| 123 | + const named = warn.mock.calls |
| 124 | + .map(([message, meta]) => ({ message: String(message), meta: meta as any })) |
| 125 | + .filter(({ meta }) => meta?.appId === APP_ID && Array.isArray(meta?.plugins)); |
| 126 | + expect(named).toHaveLength(1); |
| 127 | + expect(named[0]!.meta).toEqual({ appId: APP_ID, plugins: [VECTOR, INDEXER] }); |
| 128 | + expect(named[0]!.message).toContain(VECTOR); |
| 129 | + expect(named[0]!.message).toContain(INDEXER); |
| 130 | + } finally { |
| 131 | + try { await kernel?.shutdown(); } catch { /* noop */ } |
| 132 | + } |
| 133 | + }, BOOT_TIMEOUT); |
| 134 | +}); |
0 commit comments