|
46 | 46 | */ |
47 | 47 |
|
48 | 48 | import { randomUUID } from 'node:crypto'; |
49 | | -import { mkdtempSync, rmSync } from 'node:fs'; |
| 49 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
50 | 50 | import { tmpdir } from 'node:os'; |
51 | 51 | import { join } from 'node:path'; |
52 | 52 | import { afterEach, describe, expect, it, vi } from 'vitest'; |
@@ -320,3 +320,132 @@ describe('the metadata service\'s copy of a runtime-authored object follows the |
320 | 320 | expect((await copyOf(k, OTHER)).copy).toMatchObject({ enable: { apiEnabled: true } }); |
321 | 321 | }); |
322 | 322 | }); |
| 323 | + |
| 324 | +/** |
| 325 | + * The same copy on a kernel with an `environmentId` that hydrates its own |
| 326 | + * `sys_metadata`: `createStandaloneStack`'s composition (`os dev` as |
| 327 | + * `env_local`, `os serve`, `os start`) and the cloud single-environment |
| 328 | + * runtime. Its `metadata` service is sourced from the artifact and the boot |
| 329 | + * bridge does not run, so before the fix a runtime-authored object had NO copy |
| 330 | + * there, exposed or not, and both MCP exposure gates served one saved with |
| 331 | + * `apiEnabled: false` (measured through the real `os dev`). |
| 332 | + */ |
| 333 | +describe('on a kernel with an environmentId, a runtime-authored object has a copy that follows the registry', () => { |
| 334 | + const ENV = 'env_local'; |
| 335 | + const live: ObjectKernel[] = []; |
| 336 | + const roots: string[] = []; |
| 337 | + afterEach(async () => { |
| 338 | + while (live.length) { |
| 339 | + const kernel = live.pop()!; |
| 340 | + if (kernel.getState() === 'running') await kernel.shutdown(); |
| 341 | + } |
| 342 | + while (roots.length) rmSync(roots.pop()!, { recursive: true, force: true }); |
| 343 | + }); |
| 344 | + |
| 345 | + /** |
| 346 | + * One boot over `stores`, with `createStandaloneStack`'s two options: |
| 347 | + * `MetadataPlugin` reading an (empty) artifact under the environment, and |
| 348 | + * `ObjectQLPlugin` with the `environmentId` and `hydrateMetadataFromDb`. |
| 349 | + */ |
| 350 | + async function boot(stores: Stores, beforeBoot?: (engine: ObjectQL) => void): Promise<Booted> { |
| 351 | + const kernel = new ObjectKernel({ logger: { level: 'silent' }, gracefulShutdown: false }); |
| 352 | + live.push(kernel); |
| 353 | + const root = mkdtempSync(join(tmpdir(), 'os-23689-')); |
| 354 | + roots.push(root); |
| 355 | + const artifact = join(root, 'objectstack.json'); |
| 356 | + writeFileSync(artifact, JSON.stringify({ |
| 357 | + manifest: { id: 'com.qa.empty', name: 'empty', version: '0.0.0', type: 'app' }, |
| 358 | + objects: [], views: [], apps: [], flows: [], requires: [], |
| 359 | + })); |
| 360 | + const engine = new ObjectQL(); |
| 361 | + engine.registerDriver(makeMemoryDriver(stores), true); |
| 362 | + await kernel.use(new MetadataPlugin({ |
| 363 | + watch: false, artifactWatch: false, environmentId: ENV, |
| 364 | + artifactSource: { mode: 'local-file', path: artifact }, rootDir: root, |
| 365 | + })); |
| 366 | + await kernel.use(new ObjectQLPlugin({ ql: engine, environmentId: ENV, hydrateMetadataFromDb: true })); |
| 367 | + beforeBoot?.(engine); |
| 368 | + await kernel.bootstrap(); |
| 369 | + return { kernel, engine, protocol: kernel.getService('protocol'), metadata: kernel.getService('metadata') }; |
| 370 | + } |
| 371 | + |
| 372 | + const save = (k: Booted, name: string, label: string, enable?: Record<string, unknown>) => |
| 373 | + k.protocol.saveMetaItem({ type: 'object', name, item: objectBody(name, label, enable) }); |
| 374 | + |
| 375 | + async function copyOf(k: Booted, name: string) { |
| 376 | + const listed = ((await k.metadata.listObjects()) as Array<{ name?: string }>).find((o) => o?.name === name); |
| 377 | + return { copy: await k.metadata.getObject(name), listed }; |
| 378 | + } |
| 379 | + |
| 380 | + const withShippedPackage = (engine: ObjectQL) => { |
| 381 | + engine.registerApp({ |
| 382 | + id: SHIPPED_PKG, name: SHIPPED_PKG, version: '1.0.0', type: 'app', |
| 383 | + objects: [objectBody(SHIPPED, 'Shipped')], |
| 384 | + }); |
| 385 | + }; |
| 386 | + |
| 387 | + it('created after boot with exposure off: a copy at once, carrying its declaration', async () => { |
| 388 | + const k = await boot(new Map()); |
| 389 | + expect((await save(k, NAME, 'Gadget', { apiEnabled: false })).success).toBe(true); |
| 390 | + |
| 391 | + const { copy, listed } = await copyOf(k, NAME); |
| 392 | + expect(copy).toMatchObject({ label: 'Gadget', enable: { apiEnabled: false }, _provenance: 'org' }); |
| 393 | + expect(listed).toBeDefined(); |
| 394 | + }); |
| 395 | + |
| 396 | + it('saved exposed, then turned off: the copy says so before the save returns', async () => { |
| 397 | + const k = await boot(new Map()); |
| 398 | + expect((await save(k, NAME, 'Gadget', { apiEnabled: true })).success).toBe(true); |
| 399 | + expect((await save(k, NAME, 'Gadget Q', { apiEnabled: false })).success).toBe(true); |
| 400 | + |
| 401 | + expect((await copyOf(k, NAME)).copy) |
| 402 | + .toMatchObject({ label: 'Gadget Q', enable: { apiEnabled: false }, _provenance: 'org' }); |
| 403 | + }); |
| 404 | + |
| 405 | + it('saved with exposure off, then a restart: the boot gives the hydrated object its copy (no mutation fires for it)', async () => { |
| 406 | + const stores: Stores = new Map(); |
| 407 | + const k1 = await boot(stores); |
| 408 | + expect((await save(k1, NAME, 'Gadget', { apiEnabled: false })).success).toBe(true); |
| 409 | + expect((await save(k1, OTHER, 'Other', { apiEnabled: true })).success).toBe(true); |
| 410 | + await k1.kernel.shutdown(); |
| 411 | + |
| 412 | + const k2 = await boot(stores); |
| 413 | + expect(k2.engine.registry.getObject(NAME)).toMatchObject({ enable: { apiEnabled: false } }); |
| 414 | + expect((await copyOf(k2, NAME)).copy) |
| 415 | + .toMatchObject({ label: 'Gadget', enable: { apiEnabled: false }, _provenance: 'org' }); |
| 416 | + expect((await copyOf(k2, OTHER)).copy).toMatchObject({ enable: { apiEnabled: true } }); |
| 417 | + }); |
| 418 | + |
| 419 | + it('deleted: the copy is gone and the listing no longer names it', async () => { |
| 420 | + const stores: Stores = new Map(); |
| 421 | + const k1 = await boot(stores); |
| 422 | + expect((await save(k1, NAME, 'Gadget', { apiEnabled: true })).success).toBe(true); |
| 423 | + await k1.kernel.shutdown(); |
| 424 | + const k = await boot(stores); |
| 425 | + expect((await copyOf(k, NAME)).copy).toBeDefined(); |
| 426 | + |
| 427 | + expect(await k.protocol.deleteMetaItem({ type: 'object', name: NAME })).toMatchObject({ success: true, reset: true }); |
| 428 | + |
| 429 | + await vi.waitFor(async () => { |
| 430 | + const { copy, listed } = await copyOf(k, NAME); |
| 431 | + expect(copy).toBeUndefined(); |
| 432 | + expect(listed).toBeUndefined(); |
| 433 | + }); |
| 434 | + }); |
| 435 | + |
| 436 | + it('a code package\'s object gets no copy from the boot pass or from a runtime edit beside it', async () => { |
| 437 | + const stores: Stores = new Map(); |
| 438 | + const k1 = await boot(stores, withShippedPackage); |
| 439 | + expect((await save(k1, NAME, 'Gadget', { apiEnabled: true })).success).toBe(true); |
| 440 | + await k1.kernel.shutdown(); |
| 441 | + |
| 442 | + const k = await boot(stores, withShippedPackage); |
| 443 | + expect(k.engine.registry.getObject(SHIPPED)).toMatchObject({ label: 'Shipped' }); |
| 444 | + // The code package's objects reach this service from the artifact, never from this pass. |
| 445 | + expect(await k.metadata.getObject(SHIPPED)).toBeUndefined(); |
| 446 | + expect((await save(k, NAME, 'Gadget Q', { apiEnabled: false })).success).toBe(true); |
| 447 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 448 | + expect(await k.metadata.getObject(SHIPPED)).toBeUndefined(); |
| 449 | + expect((await copyOf(k, NAME)).copy).toMatchObject({ enable: { apiEnabled: false } }); |
| 450 | + }); |
| 451 | +}); |
0 commit comments