|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * [#28834, ADR-0146 D10] An `object_extension` item's refusals fit the item |
| 5 | + * door #28574 opened. |
| 6 | + * |
| 7 | + * The business case: an administrator adds "Region" to the vendor's Account |
| 8 | + * through Setup, which saves an `object_extension` item into the base's |
| 9 | + * `.custom` pair. |
| 10 | + * |
| 11 | + * 1. Carrying `overrides`, the save is refused (`declared_type_not_overridable`) |
| 12 | + * with a sentence that fits an extension: it takes no override and has no |
| 13 | + * new name, so the remedy is to edit the extension itself. An `object` |
| 14 | + * carrying `overrides` (`declared_object`) is told both doors an |
| 15 | + * extension is written through: the item door and the manifest entry. |
| 16 | + * Every other type's sentence is unchanged (the permission control). |
| 17 | + * 2. A short `extend` that `getObject` folds to a legacy `<ns>__<name>` key |
| 18 | + * is refused: the registry files an extension under the name it is |
| 19 | + * written with, so it would open a second, phantom object. The key the |
| 20 | + * object is registered under is still admitted (the control). |
| 21 | + * |
| 22 | + * ## The rig |
| 23 | + * |
| 24 | + * The rig of `meta-object-extension-item.test.ts`: better-sqlite3 `:memory:` |
| 25 | + * behind the real `ObjectQL` engine and a real |
| 26 | + * `ObjectStackProtocolImplementation` over its `SchemaRegistry`. The base is a |
| 27 | + * code package registered through `registerApp`; the pair is installed |
| 28 | + * through the protocol's package door. Every refusal asserts its envelope, its |
| 29 | + * sentence and that no row was left. |
| 30 | + */ |
| 31 | + |
| 32 | +import { describe, it, expect, beforeAll, afterAll, afterEach } from 'vitest'; |
| 33 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 34 | +import { tmpdir } from 'node:os'; |
| 35 | +import { join } from 'node:path'; |
| 36 | +import { ObjectQL } from '@objectstack/objectql'; |
| 37 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 38 | +import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol'; |
| 39 | +import { SysMetadata, SysMetadataHistoryObject, SysMetadataAuditObject } from '@objectstack/platform-objects/metadata'; |
| 40 | + |
| 41 | +let home: string; |
| 42 | +let priorHome: string | undefined; |
| 43 | +beforeAll(() => { |
| 44 | + priorHome = process.env.OS_HOME; |
| 45 | + home = mkdtempSync(join(tmpdir(), 'os-object-extension-door-fit-')); |
| 46 | + process.env.OS_HOME = home; |
| 47 | +}); |
| 48 | +afterAll(() => { |
| 49 | + if (priorHome === undefined) delete process.env.OS_HOME; |
| 50 | + else process.env.OS_HOME = priorHome; |
| 51 | + rmSync(home, { recursive: true, force: true }); |
| 52 | +}); |
| 53 | + |
| 54 | +const TEST_PACKAGE_ID = 'objectstack-test'; |
| 55 | +const BASE = 'com.acme.crm'; |
| 56 | +const PAIR = 'com.acme.crm.custom'; |
| 57 | +const TARGET = 'crm_account'; |
| 58 | +/** An object the base ships under a legacy `<ns>__<name>` key: `getObject('case')` folds to it. */ |
| 59 | +const LEGACY_KEY = 'crm__case'; |
| 60 | +const LEGACY_SHORT = 'case'; |
| 61 | +const DECLARATION = { package: BASE, basedOn: '3.4.1' }; |
| 62 | + |
| 63 | +const baseManifest = () => ({ |
| 64 | + id: BASE, |
| 65 | + name: 'crm', |
| 66 | + version: '3.4.1', |
| 67 | + type: 'app', |
| 68 | + namespace: 'crm', |
| 69 | + objects: [ |
| 70 | + { name: TARGET, label: 'Account', fields: { name: { name: 'name', label: 'Name', type: 'text' } } }, |
| 71 | + { name: LEGACY_KEY, label: 'Case', fields: { subject: { name: 'subject', label: 'Subject', type: 'text' } } }, |
| 72 | + ], |
| 73 | +}); |
| 74 | + |
| 75 | +const region = (extend: string, extra: Record<string, unknown> = {}) => ({ |
| 76 | + extend, |
| 77 | + fields: { region__c: { type: 'text', label: 'Region' } }, |
| 78 | + ...extra, |
| 79 | +}); |
| 80 | + |
| 81 | +const liveEngines: ObjectQL[] = []; |
| 82 | +afterEach(async () => { |
| 83 | + while (liveEngines.length) { |
| 84 | + try { await liveEngines.pop()?.destroy(); } catch { /* noop */ } |
| 85 | + } |
| 86 | +}); |
| 87 | + |
| 88 | +async function boot() { |
| 89 | + const engine = new ObjectQL(); |
| 90 | + liveEngines.push(engine); |
| 91 | + engine.registerDriver(new SqlDriver({ |
| 92 | + client: 'better-sqlite3', |
| 93 | + connection: { filename: ':memory:' }, |
| 94 | + useNullAsDefault: true, |
| 95 | + }), true); |
| 96 | + await engine.init(); |
| 97 | + engine.registry.registerObject(SysMetadata as any, TEST_PACKAGE_ID); |
| 98 | + engine.registry.registerObject(SysMetadataHistoryObject as any, TEST_PACKAGE_ID); |
| 99 | + engine.registry.registerObject(SysMetadataAuditObject as any, TEST_PACKAGE_ID); |
| 100 | + (engine.registry as unknown as { logLevel: string }).logLevel = 'silent'; |
| 101 | + engine.registerApp(baseManifest()); |
| 102 | + await engine.syncSchemas(); |
| 103 | + |
| 104 | + const protocol = new ObjectStackProtocolImplementation(engine as any, () => new Map()); |
| 105 | + const p = protocol as any; |
| 106 | + await p.installPackage({ |
| 107 | + manifest: { id: PAIR, name: 'crm-custom', version: '0.1.0', type: 'app', namespace: 'crm_custom', dependencies: { [BASE]: '^3' } }, |
| 108 | + }); |
| 109 | + |
| 110 | + /** Every stored row, read back from the store. */ |
| 111 | + const rows = async () => ((await engine.find('sys_metadata', { where: {}, context: { isSystem: true } })) ?? []) |
| 112 | + .filter((r: any) => r.package_id === PAIR) |
| 113 | + .map((r: any) => ({ type: r.type, name: r.name, state: r.state })); |
| 114 | + const thrown = (run: Promise<unknown>) => run.then(() => undefined, (e: any) => e); |
| 115 | + return { engine, p, rows, thrown }; |
| 116 | +} |
| 117 | + |
| 118 | +const envelope = (e: any) => (e ? { code: e.code, status: e.status } : null); |
| 119 | +const REFUSED = { code: 'NOT_OVERRIDABLE', status: 403 }; |
| 120 | + |
| 121 | +describe('an object_extension item\'s override refusals fit the item door (#28834)', () => { |
| 122 | + it('`overrides` on an object_extension is refused with the edit-the-extension sentence', async () => { |
| 123 | + const { p, rows, thrown } = await boot(); |
| 124 | + const e = await thrown(p.saveMetaItem({ |
| 125 | + type: 'object_extension', name: TARGET, packageId: PAIR, item: region(TARGET, { overrides: DECLARATION }), |
| 126 | + })); |
| 127 | + expect(envelope(e)).toEqual(REFUSED); |
| 128 | + expect(e.message).toBe( |
| 129 | + `'object_extension/${TARGET}' carries \`overrides\`, but an object extension takes no override (ADR-0146 D10): ` |
| 130 | + + 'it adds to the object it extends and is stored under that object\'s name. ' |
| 131 | + + `Remove \`overrides\` and edit the object_extension '${TARGET}' itself.`, |
| 132 | + ); |
| 133 | + expect(e.message).not.toMatch(/new name|new similar/); |
| 134 | + expect(await rows()).toEqual([]); |
| 135 | + }); |
| 136 | + |
| 137 | + it('`overrides` on an object is refused naming the item door beside the manifest entry', async () => { |
| 138 | + const { p, rows, thrown } = await boot(); |
| 139 | + const e = await thrown(p.saveMetaItem({ |
| 140 | + type: 'object', name: TARGET, packageId: PAIR, |
| 141 | + item: { name: TARGET, label: 'Account', fields: {}, overrides: DECLARATION }, |
| 142 | + })); |
| 143 | + expect(envelope(e)).toEqual(REFUSED); |
| 144 | + expect(e.message).toBe( |
| 145 | + `'object/${TARGET}' carries \`overrides\`, but an object is extended, never replaced (ADR-0146 D10). ` |
| 146 | + + 'Remove `overrides` and add the change as an object extension in the base\'s paired .custom package: ' |
| 147 | + + 'an `object_extension` item saved through `/api/v1/meta/object_extension`, or an `objectExtensions` entry of its manifest.', |
| 148 | + ); |
| 149 | + expect(await rows()).toEqual([]); |
| 150 | + }); |
| 151 | + |
| 152 | + it('control: another excluded type keeps its D6 sentence byte for byte', async () => { |
| 153 | + const { p, rows, thrown } = await boot(); |
| 154 | + const e = await thrown(p.saveMetaItem({ |
| 155 | + type: 'permission', name: 'crm_perm', packageId: PAIR, |
| 156 | + item: { name: 'crm_perm', label: 'CRM', overrides: DECLARATION }, |
| 157 | + })); |
| 158 | + expect(envelope(e)).toEqual(REFUSED); |
| 159 | + expect(e.message).toBe( |
| 160 | + '\'permission/crm_perm\' carries `overrides`, but a permission takes no override (ADR-0146 D6). ' |
| 161 | + + 'Remove `overrides` and create your own permission under a new name ("new similar").', |
| 162 | + ); |
| 163 | + expect(await rows()).toEqual([]); |
| 164 | + }); |
| 165 | +}); |
| 166 | + |
| 167 | +describe('an object_extension item names its target by the key it is registered under (#28834)', () => { |
| 168 | + it('a short name getObject folds to a legacy key is refused, naming both spellings, and opens no phantom object', async () => { |
| 169 | + const { engine, p, rows, thrown } = await boot(); |
| 170 | + // The fold this refuses: the short name reaches the legacy entry. |
| 171 | + expect((engine.registry.getObject(LEGACY_SHORT) as any)?.name).toBe(LEGACY_KEY); |
| 172 | + |
| 173 | + for (const mode of ['draft', undefined] as const) { |
| 174 | + const e = await thrown(p.saveMetaItem({ |
| 175 | + type: 'object_extension', name: LEGACY_SHORT, packageId: PAIR, item: region(LEGACY_SHORT), |
| 176 | + ...(mode ? { mode } : {}), |
| 177 | + })); |
| 178 | + expect(envelope(e)).toEqual({ code: 'VALIDATION_ERROR', status: 400 }); |
| 179 | + expect(e.message).toBe( |
| 180 | + `Object extension '${LEGACY_SHORT}' extends '${LEGACY_SHORT}', but that object is registered as '${LEGACY_KEY}': ` |
| 181 | + + `an extension is filed under the name it is written with, so '${LEGACY_SHORT}' would open a second object ` |
| 182 | + + `beside '${LEGACY_KEY}'. Save it as '${LEGACY_KEY}', with \`extend\` set to '${LEGACY_KEY}'.`, |
| 183 | + ); |
| 184 | + expect(e.issues?.map((i: any) => i.path)).toEqual(['extend']); |
| 185 | + } |
| 186 | + expect(await rows()).toEqual([]); |
| 187 | + expect(engine.registry.getObjectContributors(LEGACY_SHORT)).toEqual([]); |
| 188 | + expect(engine.registry.getObjectContributors(LEGACY_KEY).map((c: any) => c.packageId)).toEqual([BASE]); |
| 189 | + }); |
| 190 | + |
| 191 | + it('control: the key the object is registered under is admitted and extends that object', async () => { |
| 192 | + const { engine, p, rows } = await boot(); |
| 193 | + await p.saveMetaItem({ type: 'object_extension', name: LEGACY_KEY, packageId: PAIR, item: region(LEGACY_KEY), mode: 'draft' }); |
| 194 | + await p.publishMetaItem({ type: 'object_extension', name: LEGACY_KEY, packageId: PAIR }); |
| 195 | + expect(await rows()).toEqual([{ type: 'object_extension', name: LEGACY_KEY, state: 'active' }]); |
| 196 | + expect(engine.registry.getObjectContributors(LEGACY_KEY).map((c: any) => [c.packageId, c.ownership])) |
| 197 | + .toEqual([[BASE, 'own'], [PAIR, 'extend']]); |
| 198 | + expect(Object.keys((engine.registry.getObject(LEGACY_KEY) as any)?.fields ?? {})).toEqual(expect.arrayContaining(['region__c', 'subject'])); |
| 199 | + expect(engine.registry.getObjectContributors(LEGACY_SHORT)).toEqual([]); |
| 200 | + }); |
| 201 | +}); |
0 commit comments