|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#30208 row 5, ruling A] The key columns of the three lifecycle `map` |
| 5 | + * settings are served from the running system, never listed in the manifest: |
| 6 | + * |
| 7 | + * - `retention_overrides` and `quotas` admit the objects the sweep walks |
| 8 | + * (a lifecycle class other than `record`), read at the moment of asking; |
| 9 | + * - `quota_defaults` admits the lifecycle classes the sweep reads a default |
| 10 | + * for (`LifecycleClassSchema` without `record`); |
| 11 | + * - `ObjectQLPlugin` registers the three sources with the settings service |
| 12 | + * BEFORE the manifest, booted through a real `ObjectKernel`. |
| 13 | + */ |
| 14 | + |
| 15 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 16 | +import { ObjectKernel, type PluginContext } from '@objectstack/core'; |
| 17 | +import { ObjectQLPlugin } from '../plugin.js'; |
| 18 | +import type { ObjectQL } from '../engine.js'; |
| 19 | +import { |
| 20 | + lifecycleClassKeySource, |
| 21 | + lifecycleObjectKeySource, |
| 22 | + type LifecycleKeyRegistryLike, |
| 23 | + type LifecycleKeySource, |
| 24 | +} from './lifecycle-key-options.js'; |
| 25 | + |
| 26 | +describe('lifecycleObjectKeySource', () => { |
| 27 | + const objects: ReturnType<LifecycleKeyRegistryLike['getAllObjects']> = [ |
| 28 | + { name: 'sys_job_run', label: 'Job run', lifecycle: { class: 'telemetry' } }, |
| 29 | + { name: 'sys_audit_log', lifecycle: { class: 'audit' } }, |
| 30 | + { name: 'account', label: 'Account', lifecycle: { class: 'record' } }, |
| 31 | + { name: 'contact', label: 'Contact' }, |
| 32 | + ]; |
| 33 | + |
| 34 | + it('admits exactly the objects the sweep walks, sorted, named by their label', () => { |
| 35 | + const answer = lifecycleObjectKeySource(() => ({ getAllObjects: () => objects }))()!; |
| 36 | + expect(answer.options).toEqual([ |
| 37 | + { value: 'sys_audit_log', label: 'sys_audit_log' }, |
| 38 | + { value: 'sys_job_run', label: 'Job run' }, |
| 39 | + ]); |
| 40 | + expect(answer.admits('sys_job_run')).toBe(true); |
| 41 | + expect(answer.admits('account')).toBe(false); |
| 42 | + expect(answer.admits('contact')).toBe(false); |
| 43 | + expect(answer.admits('sys_job_runs')).toBe(false); |
| 44 | + }); |
| 45 | + |
| 46 | + it('reads the storage objects when the registry has them, as the sweep does', () => { |
| 47 | + const answer = lifecycleObjectKeySource(() => ({ |
| 48 | + getAllObjects: () => [], |
| 49 | + getAllStorageObjects: () => [{ name: 'sys_session', lifecycle: { class: 'transient' } }], |
| 50 | + }))()!; |
| 51 | + expect(answer.options.map((o) => o.value)).toEqual(['sys_session']); |
| 52 | + }); |
| 53 | + |
| 54 | + it('is asked at the moment of use: an object registered later is admitted from then on', () => { |
| 55 | + const live = [...objects]; |
| 56 | + const source = lifecycleObjectKeySource(() => ({ getAllObjects: () => live })); |
| 57 | + expect(source()!.admits('sys_webhook_delivery')).toBe(false); |
| 58 | + live.push({ name: 'sys_webhook_delivery', lifecycle: { class: 'event' } }); |
| 59 | + expect(source()!.admits('sys_webhook_delivery')).toBe(true); |
| 60 | + }); |
| 61 | + |
| 62 | + it('answers nothing while no registry is reachable (the declared table, none, then stands)', () => { |
| 63 | + expect(lifecycleObjectKeySource(() => undefined)()).toBeUndefined(); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('lifecycleClassKeySource', () => { |
| 68 | + it('admits the lifecycle classes a quota default is read for — every class but `record`', () => { |
| 69 | + const answer = lifecycleClassKeySource()()!; |
| 70 | + expect(answer.options.map((o) => o.value)).toEqual(['audit', 'telemetry', 'transient', 'event']); |
| 71 | + expect(answer.admits('record')).toBe(false); |
| 72 | + expect(answer.admits('telemtry')).toBe(false); |
| 73 | + }); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('ObjectQLPlugin registers the three key sources before the lifecycle manifest', () => { |
| 77 | + const kernels: ObjectKernel[] = []; |
| 78 | + afterEach(async () => { |
| 79 | + while (kernels.length) { |
| 80 | + const kernel = kernels.pop()!; |
| 81 | + try { if (kernel.getState() === 'running') await kernel.shutdown(); } catch { /* noop */ } |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + it('at kernel:ready, against the engine registry', async () => { |
| 86 | + const calls: string[] = []; |
| 87 | + const sources = new Map<string, LifecycleKeySource>(); |
| 88 | + const settings = { |
| 89 | + registerOptionSource(namespace: string, key: string, source: LifecycleKeySource) { |
| 90 | + calls.push(`source ${namespace}.${key}`); |
| 91 | + sources.set(key, source); |
| 92 | + }, |
| 93 | + registerManifest(manifest: { namespace: string }) { calls.push(`manifest ${manifest.namespace}`); }, |
| 94 | + }; |
| 95 | + const kernel = new ObjectKernel({ logger: { level: 'silent' }, gracefulShutdown: false }); |
| 96 | + kernels.push(kernel); |
| 97 | + await kernel.use(new ObjectQLPlugin()); |
| 98 | + await kernel.use({ |
| 99 | + name: 'test.settings-and-objects', |
| 100 | + dependencies: ['com.objectstack.engine.objectql'], |
| 101 | + init: async (ctx: PluginContext) => { |
| 102 | + ctx.registerService('settings', settings); |
| 103 | + const ql = ctx.getService<ObjectQL>('objectql'); |
| 104 | + ql.registerDriver({ |
| 105 | + name: 'primary', version: '0.0.0', supports: {}, |
| 106 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, |
| 107 | + async find() { return []; }, async findOne() { return null; }, async count() { return 0; }, |
| 108 | + } as any, true); |
| 109 | + await ctx.getService<{ register(a: unknown): Promise<void> | void }>('manifest').register({ |
| 110 | + id: 'com.test.lifecycle-keys', |
| 111 | + name: 'com.test.lifecycle-keys', |
| 112 | + version: '1.0.0', |
| 113 | + objects: [{ |
| 114 | + name: 'test_ping', |
| 115 | + fields: { note: { name: 'note', type: 'text' } }, |
| 116 | + lifecycle: { class: 'telemetry', retention: { maxAge: '30d' } }, |
| 117 | + }], |
| 118 | + }); |
| 119 | + }, |
| 120 | + } as any); |
| 121 | + await kernel.bootstrap(); |
| 122 | + |
| 123 | + expect(calls).toEqual([ |
| 124 | + 'source lifecycle.retention_overrides', |
| 125 | + 'source lifecycle.quotas', |
| 126 | + 'source lifecycle.quota_defaults', |
| 127 | + 'manifest lifecycle', |
| 128 | + ]); |
| 129 | + expect(sources.get('quotas')!()!.admits('test_ping')).toBe(true); |
| 130 | + expect(sources.get('retention_overrides')!()!.admits('test_ping')).toBe(true); |
| 131 | + expect(sources.get('quota_defaults')!()!.admits('telemetry')).toBe(true); |
| 132 | + }); |
| 133 | +}); |
0 commit comments