1818 * The contract has one implementation face per command. A file that pinned
1919 * `migrate recorded-by` alone would go green while the others stayed broken,
2020 * and would say nothing at all about a new one. So the family is DISCOVERED
21- * from the source tree — every command that calls `bootSchemaStack` and
22- * declares a `--json` flag — and the discovered set is reconciled against
21+ * from the source tree — every command that calls `bootSchemaStack`, itself or
22+ * through a `src/utils` module that does, and declares a `--json` flag — and
23+ * the discovered set is reconciled against
2324 * {@link FAMILY} below. Add a member and this file goes red until it is listed
2425 * here and passes; drop one and it goes red until it is removed. The seam
2526 * itself (`bootSchemaStack`'s required `jsonOutput` option) makes the mistake a
@@ -60,25 +61,28 @@ import { describe, it, expect, beforeAll, afterAll } from 'vitest';
6061import { execFile } from 'node:child_process' ;
6162import { mkdtempSync , rmSync , writeFileSync , readFileSync , readdirSync , statSync } from 'node:fs' ;
6263import { tmpdir } from 'node:os' ;
63- import { join , resolve , relative , sep } from 'node:path' ;
64+ import { basename , join , resolve , relative , sep } from 'node:path' ;
6465import { fileURLToPath } from 'node:url' ;
66+ import { generateEd25519KeyPair , signPayload } from '@objectstack/core' ;
6567import { maskComments } from '../../../scripts/js-comment-mask.mjs' ;
68+ import { MANIFEST_FILENAME , computeIntegrity , createTarGz , type ArchiveFile } from '../src/utils/osplugin.js' ;
6669import { childEnv } from './helpers/serve-process.js' ;
6770import { CONFIG_MISS_FAMILY , discoverConfigMissFamily } from './helpers/config-miss-family.js' ;
6871
6972const HERE = resolve ( fileURLToPath ( import . meta. url ) , '..' ) ;
7073const CLI = resolve ( HERE , '../bin/run-dev.js' ) ;
7174const TSX = resolve ( HERE , '../../../node_modules/.bin/tsx' ) ;
7275const COMMANDS_DIR = resolve ( HERE , '../src/commands' ) ;
76+ const UTILS_DIR = resolve ( HERE , '../src/utils' ) ;
7377
7478/**
7579 * The family, and how to drive each member into a boot.
7680 *
7781 * The value is the EXTRA argv a member needs beyond `--json`; `[]` means the
78- * bare form already boots. Only one member needs anything: `os migrate meta`
79- * migrates an authored config in memory and boots a kernel solely under
80- * `--stored`, which is why the issue's own list names it as `migrate meta
81- * --stored` rather than `migrate meta` .
82+ * bare form already boots. `os migrate meta` migrates an authored config in
83+ * memory and boots a kernel solely under `--stored`, which is why the issue's
84+ * own list names it as `migrate meta --stored` rather than `migrate meta`. A
85+ * member that needs a fixture as well as argv is prepared by { @link PREPARE} .
8286 */
8387const FAMILY : Record < string , string [ ] > = {
8488 // Report-only is its DEFAULT and the only form driven here: without
@@ -146,6 +150,16 @@ const FAMILY: Record<string, string[]> = {
146150 // face driven here is its empty-work answer for a table not there yet.
147151 'migrate unmapped-columns' : [ '--object' , 'jp_ticket' ] ,
148152 'migrate value-shapes' : [ ] ,
153+ // It boots only to read the trusted key a SIGNED artifact names (through
154+ // `utils/plugin-trust-read.ts`, which is why the discovery follows utils),
155+ // so it is driven with one: `PREPARE` builds and signs the artifact and
156+ // trusts its key in this member's database, and the face driven here is the
157+ // full install, its `installed` document emitted after the read-only boot
158+ // has shut down. An unsigned artifact would refuse without booting at all.
159+ 'plugin install' : [ 'purity.osplugin' ] ,
160+ // Its bare form is read-only (`deferSchemaDdl` + `readOnlyProbe`). No
161+ // database file exists, so the face driven here is its empty listing.
162+ 'plugin trust list' : [ ] ,
149163 // Report-only is its DEFAULT and the only form driven here: without
150164 // `--delete` it boots, reports and writes nothing, so the family gains a
151165 // member without this fixture gaining a destructive run.
@@ -165,6 +179,41 @@ const BOOT_DIAGNOSTICS = [
165179 'Graceful shutdown complete' ,
166180] ;
167181
182+ /**
183+ * Members that need a fixture beyond argv to reach the face driven here. Each
184+ * gets the project directory and its own environment, prepares what it needs,
185+ * and returns the extra environment its run takes. A preparation that fails
186+ * throws: the member would otherwise reach some other face and still pass.
187+ */
188+ const PREPARE : Record < string , ( dir : string , env : Record < string , string > ) => Promise < Record < string , string > > > = {
189+ // An artifact built and signed the way `os plugin build` / `os plugin sign`
190+ // make one, its key trusted by `os plugin trust add` in this member's
191+ // database. The install materializes under OS_HOME, so it gets one of its
192+ // own inside the fixture rather than the runner's.
193+ 'plugin install' : async ( dir , env ) => {
194+ const keyId = 'purity-2026' ;
195+ const publisher = generateEd25519KeyPair ( ) ;
196+ const files : ArchiveFile [ ] = [ { path : 'dist/index.mjs' , data : new TextEncoder ( ) . encode ( 'export const id = "com.purity.e2e";\n' ) } ] ;
197+ const manifest = {
198+ id : 'com.purity.e2e' ,
199+ version : '1.0.0' ,
200+ type : 'plugin' ,
201+ runtime : 'node' ,
202+ packaging : 'bundled' ,
203+ integrity : computeIntegrity ( files ) ,
204+ } ;
205+ const artifact = join ( dir , 'purity.osplugin' ) ;
206+ writeFileSync ( artifact , createTarGz ( [ ...files , { path : MANIFEST_FILENAME , data : new TextEncoder ( ) . encode ( JSON . stringify ( manifest ) ) } ] ) ) ;
207+ writeFileSync ( `${ artifact } .sig` , signPayload ( new Uint8Array ( readFileSync ( artifact ) ) , publisher . privateKeyPem , keyId ) ) ;
208+ writeFileSync ( join ( dir , 'purity.pub.pem' ) , publisher . publicKeyPem ) ;
209+ const trusted = await runCli ( [ 'plugin' , 'trust' , 'add' , 'purity.pub.pem' , '--key-id' , keyId ] , dir , env ) ;
210+ if ( trusted . code !== 0 ) {
211+ throw new Error ( `os plugin trust add exited ${ trusted . code } :\n${ trusted . stdout } \n${ trusted . stderr } ` ) ;
212+ }
213+ return { OS_HOME : join ( dir , 'os-home-plugin-install' ) } ;
214+ } ,
215+ } ;
216+
168217/** Every `.ts` under `src/commands`, excluding tests. */
169218function commandFiles ( dir : string ) : string [ ] {
170219 const out : string [ ] = [ ] ;
@@ -180,13 +229,43 @@ function commandFiles(dir: string): string[] {
180229 return out ;
181230}
182231
232+ /**
233+ * The `src/utils` modules that boot the shared stack on a command's behalf:
234+ * those that call `bootSchemaStack(` (its own definition does not count), and
235+ * those that import one that does, to a fixed point. `os plugin install` boots
236+ * only through `plugin-trust-read.ts`, so a probe of the command's own text
237+ * would drop it from the family while it still boots.
238+ */
239+ function bootingUtils ( ) : Set < string > {
240+ const sources = new Map < string , string > ( ) ;
241+ for ( const abs of commandFiles ( UTILS_DIR ) ) {
242+ sources . set ( basename ( abs , '.ts' ) , maskComments ( readFileSync ( abs , 'utf-8' ) ) ) ;
243+ }
244+ const booting = new Set < string > ( ) ;
245+ for ( const [ name , src ] of sources ) {
246+ if ( src . replace ( / f u n c t i o n b o o t S c h e m a S t a c k \( / g, '' ) . includes ( 'bootSchemaStack(' ) ) booting . add ( name ) ;
247+ }
248+ for ( let grew = true ; grew ; ) {
249+ grew = false ;
250+ for ( const [ name , src ] of sources ) {
251+ if ( booting . has ( name ) ) continue ;
252+ if ( [ ...booting ] . some ( ( b ) => src . includes ( `from './${ b } .js'` ) ) ) {
253+ booting . add ( name ) ;
254+ grew = true ;
255+ }
256+ }
257+ }
258+ return booting ;
259+ }
260+
183261/**
184262 * The family, read off the source rather than remembered: a command belongs iff
185263 * it boots the shared stack AND offers a machine-readable mode. Both halves are
186264 * needed — `os serve` boots a kernel with no `--json`, and `os lint --json`
187265 * emits JSON without booting one.
188266 */
189267function discoverFamily ( ) : string [ ] {
268+ const utils = [ ...bootingUtils ( ) ] ;
190269 const ids : string [ ] = [ ] ;
191270 for ( const abs of commandFiles ( COMMANDS_DIR ) ) {
192271 // Masked: both halves are text probes, and both are satisfiable by prose. A
@@ -195,7 +274,9 @@ function discoverFamily(): string[] {
195274 // diagnostics it never writes — a red in a tier no pull request collects,
196275 // wearing this file's title rather than the docblock's (#18520).
197276 const src = maskComments ( readFileSync ( abs , 'utf-8' ) ) ;
198- if ( ! src . includes ( 'bootSchemaStack(' ) ) continue ;
277+ const boots = src . includes ( 'bootSchemaStack(' )
278+ || utils . some ( ( u ) => new RegExp ( `from '(?:\\.\\./)+utils/${ u } \\.js'` ) . test ( src ) ) ;
279+ if ( ! boots ) continue ;
199280 if ( ! / \b j s o n : \s * F l a g s \. b o o l e a n \( / . test ( src ) ) continue ;
200281 const rel = relative ( COMMANDS_DIR , abs ) . replace ( / \. t s $ / , '' ) ;
201282 const parts = rel . split ( sep ) . filter ( ( p ) => p !== 'index' ) ;
@@ -256,10 +337,12 @@ beforeAll(async () => {
256337 runs = [ ] ;
257338 for ( const [ id , extra ] of Object . entries ( FAMILY ) ) {
258339 const slug = id . replace ( / [ ^ a - z 0 - 9 ] + / gi, '-' ) ;
340+ const env = { OS_DATABASE_URL : `file:${ join ( dir , `${ slug } .db` ) } ` } ;
341+ const prepared = PREPARE [ id ] ? await PREPARE [ id ] ( dir , env ) : { } ;
259342 const { code, stdout, stderr } = await runCli (
260343 [ ...id . split ( ' ' ) , '--json' , ...extra ] ,
261344 dir ,
262- { OS_DATABASE_URL : `file: ${ join ( dir , ` ${ slug } .db` ) } ` } ,
345+ { ... env , ... prepared } ,
263346 ) ;
264347 runs . push ( { id, code, stdout, stderr } ) ;
265348 }
0 commit comments