|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#25744] An object's OWN `actions`, registered by a code package through |
| 5 | + * {@link SchemaRegistry.registerObject}, are stored as their `ActionSchema` |
| 6 | + * parse — the parse a standalone `action` item is stored as since #25649 — so a |
| 7 | + * declared `requiresFeature` / `requiresMembershipReach` is lowered into |
| 8 | + * `visible`, and an action that does not parse is refused (`INVALID_METADATA`, |
| 9 | + * 422) before anything of the object is stored. |
| 10 | + * |
| 11 | + * Before, a raw manifest or a `strict: false` stack registered with |
| 12 | + * `ObjectQL.registerApp` stored its objects' actions as written: served by |
| 13 | + * `GET /api/v1/meta/objects/:name` (`protocol.getMetaItem({ type: 'object' })`) |
| 14 | + * with `requiresFeature` riding along raw and no `visible` — a feature gate no |
| 15 | + * reader enforces. A strict `defineStack`, `os validate` and the metadata save |
| 16 | + * door already parse the same body through `ActionSchema`. |
| 17 | + */ |
| 18 | + |
| 19 | +import { describe, it, expect } from 'vitest'; |
| 20 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 21 | +import { defineStack } from '@objectstack/spec'; |
| 22 | +import { ObjectSchema } from '@objectstack/spec/data'; |
| 23 | +import { SysMetadataAuditObject, SysMetadataCommitObject, SysMetadataHistoryObject } from '@objectstack/metadata-core'; |
| 24 | +import { ObjectQL } from './engine.js'; |
| 25 | + |
| 26 | +/** One gated declaration, authored on an object's own action. */ |
| 27 | +const GATED = { |
| 28 | + name: 'gated', label: 'Gated', type: 'url' as const, target: 'https://example.com/gated', |
| 29 | + locations: ['record_header' as const], visible: "record.name != ''", |
| 30 | + requiresFeature: 'multiOrgEnabled' as const, |
| 31 | +}; |
| 32 | +const LOWERED = { dialect: 'cel', source: "(record.name != '') && features.multiOrgEnabled != false" }; |
| 33 | + |
| 34 | +const objectWith = (actions: unknown[]) => ({ |
| 35 | + name: 'oa_org', label: 'Org', |
| 36 | + fields: { name: { name: 'name', type: 'text', label: 'Name' } }, |
| 37 | + actions, |
| 38 | +}); |
| 39 | +const rawManifest = (actions: unknown[]) => ({ id: 'com.test.raw', name: 'raw', objects: [objectWith(actions)] }); |
| 40 | + |
| 41 | +/** A driver that holds nothing: the reads below never reach a stored row. */ |
| 42 | +const emptyDriver: any = { |
| 43 | + name: 'memory', version: '0.0.0', supports: {}, |
| 44 | + async connect() {}, async disconnect() {}, async checkHealth() { return true; }, |
| 45 | + async execute() { return null; }, |
| 46 | + async find() { return []; }, async findOne() { return null; }, async count() { return 0; }, |
| 47 | +}; |
| 48 | + |
| 49 | +/** The stored-metadata family the by-name read consults for an overlay row (none here). */ |
| 50 | +const sysMetadataObject = { |
| 51 | + name: 'sys_metadata', label: 'System Metadata', |
| 52 | + fields: { |
| 53 | + id: { name: 'id', label: 'ID', type: 'text' as const, primaryKey: true }, |
| 54 | + type: { name: 'type', label: 'Type', type: 'text' as const, required: true }, |
| 55 | + name: { name: 'name', label: 'Name', type: 'text' as const, required: true }, |
| 56 | + organization_id: { name: 'organization_id', label: 'Org', type: 'text' as const }, |
| 57 | + package_id: { name: 'package_id', label: 'Package', type: 'text' as const }, |
| 58 | + metadata: { name: 'metadata', label: 'Body', type: 'textarea' as const }, |
| 59 | + state: { name: 'state', label: 'State', type: 'text' as const }, |
| 60 | + }, |
| 61 | +}; |
| 62 | + |
| 63 | +async function host() { |
| 64 | + const engine = new ObjectQL(); |
| 65 | + engine.registerDriver(emptyDriver, true); |
| 66 | + await engine.init(); |
| 67 | + engine.registry.registerObject(sysMetadataObject); |
| 68 | + for (const o of [SysMetadataHistoryObject, SysMetadataAuditObject, SysMetadataCommitObject]) engine.registry.registerObject(o as any); |
| 69 | + return { engine, protocol: new ObjectStackProtocolImplementation(engine) }; |
| 70 | +} |
| 71 | + |
| 72 | +const servedAction = async (protocol: ObjectStackProtocolImplementation, name: string) => { |
| 73 | + const served: any = await protocol.getMetaItem({ type: 'object', name: 'oa_org' } as any); |
| 74 | + return (served.item.actions as any[]).find((a) => a?.name === name); |
| 75 | +}; |
| 76 | + |
| 77 | +const refusalOf = (fn: () => unknown): any => { |
| 78 | + try { fn(); } catch (e) { return e; } |
| 79 | + return undefined; |
| 80 | +}; |
| 81 | + |
| 82 | +describe("an object's own action is stored as its ActionSchema parse", () => { |
| 83 | + it('a raw manifest: GET /meta/object/<name> serves `requiresFeature` lowered into `visible`', async () => { |
| 84 | + const { engine, protocol } = await host(); |
| 85 | + engine.registerApp(rawManifest([GATED])); |
| 86 | + const served = await servedAction(protocol, 'gated'); |
| 87 | + expect(served.visible).toEqual(LOWERED); |
| 88 | + expect(served).not.toHaveProperty('requiresFeature'); |
| 89 | + // The same lowering `ObjectSchema.create` gives a platform object's action of the same declaration. |
| 90 | + const authored: any = ObjectSchema.create(objectWith([GATED]) as any); |
| 91 | + expect(served.visible).toEqual(authored.actions[0].visible); |
| 92 | + // The registry's own resolved object holds the same parsed body. |
| 93 | + expect(engine.registry.getObject('oa_org')?.actions?.[0]).toEqual(served); |
| 94 | + }); |
| 95 | + |
| 96 | + it('an action with no `visible` of its own is served the gate alone', async () => { |
| 97 | + const { visible: _visible, ...ungated } = GATED; |
| 98 | + const { engine, protocol } = await host(); |
| 99 | + engine.registerApp(rawManifest([ungated])); |
| 100 | + const served = await servedAction(protocol, 'gated'); |
| 101 | + expect(served.visible).toEqual({ dialect: 'cel', source: 'features.multiOrgEnabled != false' }); |
| 102 | + expect(served).not.toHaveProperty('requiresFeature'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('`requiresMembershipReach` is lowered the same way', async () => { |
| 106 | + const { requiresFeature: _feature, ...rest } = GATED; |
| 107 | + const { engine, protocol } = await host(); |
| 108 | + engine.registerApp(rawManifest([{ ...rest, requiresMembershipReach: 'invite_member' }])); |
| 109 | + const served = await servedAction(protocol, 'gated'); |
| 110 | + expect(served).not.toHaveProperty('requiresMembershipReach'); |
| 111 | + expect(served.visible).toEqual(ObjectSchema.create(objectWith([{ ...rest, requiresMembershipReach: 'invite_member' }]) as any).actions![0].visible); |
| 112 | + }); |
| 113 | + |
| 114 | + it('a `strict: false` stack registered with registerApp is parsed at the registry', async () => { |
| 115 | + const stack: any = defineStack({ |
| 116 | + manifest: { id: 'com.test.loose', name: 'loose', version: '1.0.0', type: 'app' }, |
| 117 | + objects: [objectWith([GATED])], |
| 118 | + } as any, { strict: false }); |
| 119 | + // The loose stack hands the body on as written. |
| 120 | + expect(stack.objects[0].actions[0]).toHaveProperty('requiresFeature'); |
| 121 | + const { engine, protocol } = await host(); |
| 122 | + engine.registerApp({ ...stack, ...stack.manifest }); |
| 123 | + const served = await servedAction(protocol, 'gated'); |
| 124 | + expect(served.visible).toEqual(LOWERED); |
| 125 | + expect(served).not.toHaveProperty('requiresFeature'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('a direct registerObject under a package is parsed; the caller\'s body is not mutated', async () => { |
| 129 | + const { engine } = await host(); |
| 130 | + const def = objectWith([structuredClone(GATED)]); |
| 131 | + engine.registry.registerObject(def as any, 'com.test.direct'); |
| 132 | + expect(engine.registry.getObject('oa_org')?.actions?.[0]).toMatchObject({ visible: LOWERED }); |
| 133 | + expect(def.actions[0]).toHaveProperty('requiresFeature', 'multiOrgEnabled'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('control: an object a strict parse already produced is stored deep-equal to what it arrived as', async () => { |
| 137 | + const authored: any = ObjectSchema.create(objectWith([GATED]) as any); |
| 138 | + const { engine, protocol } = await host(); |
| 139 | + engine.registerApp({ id: 'com.test.strict', name: 'strict', objects: [structuredClone(authored)] }); |
| 140 | + expect(await servedAction(protocol, 'gated')).toEqual(authored.actions[0]); |
| 141 | + }); |
| 142 | + |
| 143 | + it('an action that does not parse is refused loudly, naming the action, its object and package — nothing stored', async () => { |
| 144 | + const { engine } = await host(); |
| 145 | + const err = refusalOf(() => engine.registerApp(rawManifest([{ ...GATED, visible: false }]))); |
| 146 | + expect(err).toMatchObject({ code: 'INVALID_METADATA', status: 422 }); |
| 147 | + expect(err.message).toContain("Action 'gated' of object 'oa_org' in package 'com.test.raw' does not satisfy the action contract"); |
| 148 | + expect(err.issues).toEqual(expect.arrayContaining([expect.objectContaining({ path: 'actions.0.requiresFeature' })])); |
| 149 | + expect(engine.registry.getObject('oa_org')).toBeUndefined(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('a refused re-registration leaves the object already registered under that package in place', async () => { |
| 153 | + const { engine } = await host(); |
| 154 | + engine.registerApp(rawManifest([GATED])); |
| 155 | + const err = refusalOf(() => engine.registry.registerObject(objectWith([{ ...GATED, bogus: true }]) as any, 'com.test.raw')); |
| 156 | + expect(err).toMatchObject({ code: 'INVALID_METADATA', status: 422 }); |
| 157 | + expect(err.issues).toEqual(expect.arrayContaining([expect.objectContaining({ code: 'unrecognized_keys' })])); |
| 158 | + expect(engine.registry.getObject('oa_org')?.actions?.[0]).toMatchObject({ name: 'gated', visible: LOWERED }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments