Skip to content

Commit 84a1ca2

Browse files
committed
test(runtime): pin the App unit at the ensure door and the HotCRM-shape rehearsal
Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ihL2dM9PvPPB9BMVdhf2R
1 parent fff54e3 commit 84a1ca2

2 files changed

Lines changed: 193 additions & 0 deletions

File tree

‎packages/runtime/src/domains/meta-object-extension-item.test.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,3 +404,94 @@ describe('[ADR-0146 D10] a module\'s extension that re-declares the base\'s fiel
404404
expect(rig.extenders()).toEqual([MODULE]);
405405
}, 60_000);
406406
});
407+
408+
/**
409+
* [ruling record 6691022506, ADR-0146 D3 as amended] The HotCRM rehearsal, on
410+
* an in-repo fixture of HotCRM's shape (the HotCRM repository is not part of
411+
* this tree): the App `app.objectstack.hotcrm` and three modules that declare
412+
* it, all `namespace: 'crm'`, all held as code. The ensure door is asked for
413+
* each — one `app.objectstack.hotcrm.custom` — and a MODULE's object is then
414+
* extended in that pair and read back on every object read.
415+
*/
416+
describe('[#29594] the App unit: HotCRM and three modules share one pair, and a module object is customized in it', () => {
417+
const APP = 'app.objectstack.hotcrm';
418+
const APP_PAIR = `${APP}.custom`;
419+
const MODULES = ['service', 'revenue', 'marketing'] as const;
420+
const CASE = 'crm_service_case';
421+
const ADMIN_CTX: any = {
422+
request: {},
423+
environmentId: 'hotcrm-app-unit',
424+
executionContext: { userId: 'u_admin', systemPermissions: ['manage_metadata', 'studio.access', 'setup.access'] },
425+
};
426+
427+
async function hotcrm() {
428+
const engine = new ObjectQL();
429+
liveEngines.push(engine);
430+
engine.registerDriver(new SqlDriver({
431+
client: 'better-sqlite3',
432+
connection: { filename: ':memory:' },
433+
useNullAsDefault: true,
434+
}), true);
435+
await engine.init();
436+
engine.registry.registerObject(SysMetadata as any, TEST_PACKAGE_ID);
437+
engine.registry.registerObject(SysMetadataHistoryObject as any, TEST_PACKAGE_ID);
438+
engine.registry.registerObject(SysMetadataAuditObject as any, TEST_PACKAGE_ID);
439+
(engine.registry as unknown as { logLevel: string }).logLevel = 'silent';
440+
engine.registerApp({
441+
id: APP, name: 'hotcrm', version: '6.1.0', type: 'app', namespace: 'crm',
442+
objects: [{ name: 'crm_account', label: 'Account', fields: { name: { name: 'name', label: 'Name', type: 'text' } } }],
443+
});
444+
for (const m of MODULES) {
445+
engine.registerApp({
446+
id: `${APP}.${m}`, name: `hotcrm-${m}`, version: '6.1.0', type: 'module', namespace: 'crm',
447+
dependencies: { [APP]: '^6.1.0' },
448+
objects: [{ name: `crm_${m}_case`, label: `${m} case`, fields: { subject: { name: 'subject', label: 'Subject', type: 'text' } } }],
449+
});
450+
}
451+
await engine.syncSchemas();
452+
const protocol: any = new ObjectStackProtocolImplementation(engine as any, () => new Map());
453+
const named: Record<string, unknown> = { protocol, objectql: engine };
454+
const get = (name: string) => named[name] ?? null;
455+
const kernel: any = { context: { getService: get }, getService: get, getServiceAsync: async (name: string) => get(name) };
456+
const dispatcher = new HttpDispatcher(kernel);
457+
const ensure = (id: string) =>
458+
dispatcher.handlePackages(`/${encodeURIComponent(id)}/ensure-customization`, 'POST', undefined, {}, ADMIN_CTX) as Promise<any>;
459+
return { engine, protocol, ensure };
460+
}
461+
462+
it('ensure on the App and each module answers one app.objectstack.hotcrm.custom; a module object extended there reads back', async () => {
463+
const { engine, protocol, ensure } = await hotcrm();
464+
const answers: Array<[string, number, string]> = [];
465+
for (const id of [APP, ...MODULES.map((m) => `${APP}.${m}`)]) {
466+
const r = await ensure(id);
467+
answers.push([id, r.response.status, r.response.body?.data?.packageId]);
468+
}
469+
expect(answers).toEqual([
470+
[APP, 201, APP_PAIR],
471+
[`${APP}.service`, 200, APP_PAIR],
472+
[`${APP}.revenue`, 200, APP_PAIR],
473+
[`${APP}.marketing`, 200, APP_PAIR],
474+
]);
475+
const pairs = engine.registry.getAllPackages().map((p: any) => p.manifest?.id).filter((id: string) => id.endsWith('.custom'));
476+
expect(pairs).toEqual([APP_PAIR]);
477+
478+
// Customize a MODULE's object in the App's pair: Studio's add-field body.
479+
await protocol.saveMetaItem({
480+
type: TYPE, name: CASE, mode: 'draft', packageId: APP_PAIR,
481+
item: { extend: CASE, fields: { sla_tier__c: { type: 'text', label: 'SLA tier' } } },
482+
});
483+
await protocol.publishMetaItem({ type: TYPE, name: CASE, packageId: APP_PAIR });
484+
485+
const byName = (await protocol.getMetaItem({ type: 'object', name: CASE }))?.item;
486+
const layered = await protocol.getMetaItemLayered({ type: 'object', name: CASE });
487+
expect(Object.keys(byName?.fields ?? {})).toEqual(expect.arrayContaining(['subject', 'sla_tier__c']));
488+
expect(Object.keys(layered?.effective?.fields ?? {})).toContain('sla_tier__c');
489+
expect(Object.keys((engine.registry.getObject(CASE) as any)?.fields ?? {})).toContain('sla_tier__c');
490+
expect(engine.registry.getObjectContributors(CASE).filter((c: any) => c.ownership === 'extend').map((c: any) => c.packageId))
491+
.toEqual([APP_PAIR]);
492+
// A record stores a value in the field.
493+
const created: any = await engine.insert(CASE, { subject: 'Outage', sla_tier__c: 'gold' }, { context: { isSystem: true } });
494+
const back: any = await engine.findOne(CASE, { where: { id: created.id }, context: { isSystem: true } });
495+
expect(back?.sla_tier__c).toBe('gold');
496+
}, 60_000);
497+
});

‎packages/runtime/src/domains/packages-ensure-customization.test.ts‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,105 @@ describe('POST /api/v1/packages/:id/duplicate: a copy into a pair id is refused,
490490
expect(d.published).toHaveLength(writes);
491491
});
492492
});
493+
494+
/**
495+
* [ruling record 6691022506, ADR-0146 D3 as amended] The customization unit is
496+
* the App: the door asked for any package of an App answers the App's one
497+
* pair, `<app id>.custom`, created on the first ask and answered after —
498+
* never `422` for a package of the same App. The App is read from what the
499+
* installed manifests declare (merge question 11); a per-package pair an
500+
* earlier version created stays installed and served (merge question 12).
501+
*/
502+
describe(`${ENSURE_PATH.replace(CRM.id, ':id')}: the App unit — one pair for an App and its modules`, () => {
503+
const APP = { id: 'app.objectstack.hotcrm', name: 'HotCRM', version: '6.1.0', type: 'app', namespace: 'crm' };
504+
const MODULES = ['service', 'revenue', 'marketing'].map((m) => ({
505+
id: `${APP.id}.${m}`, name: `HotCRM ${m}`, version: '6.1.0', type: 'module', namespace: 'crm',
506+
dependencies: { [APP.id]: '^6.1.0' },
507+
}));
508+
const APP_PAIR = `${APP.id}.custom`;
509+
510+
/** HotCRM as `dev:crm` boots it: the App and its modules, held as code. */
511+
function hotcrm(): Door {
512+
const d = door();
513+
for (const manifest of [APP, ...MODULES]) withBase(d, manifest);
514+
return d;
515+
}
516+
const pairsIn = (d: Door) => ids(d).filter((id) => id.endsWith('.custom'));
517+
518+
it('a module first, then the App and the other modules: one app.objectstack.hotcrm.custom, 201 then 200', async () => {
519+
const d = hotcrm();
520+
const asked = [MODULES[0]!.id, APP.id, MODULES[1]!.id, MODULES[2]!.id];
521+
const answers: any[] = [];
522+
for (const id of asked) {
523+
const r = await ensure(d, id);
524+
answers.push({ id, status: r.response.status, data: r.response.body.data });
525+
}
526+
expect(answers.map((a) => a.status), JSON.stringify(answers)).toEqual([201, 200, 200, 200]);
527+
for (const a of answers) {
528+
expect(EnsureCustomizationPackageResponseSchema.parse(a.data)).toEqual({
529+
basePackageId: APP.id,
530+
packageId: APP_PAIR,
531+
namespace: 'crm_custom',
532+
dependencyRange: '^6',
533+
created: a.status === 201,
534+
writable: true,
535+
});
536+
}
537+
expect(pairsIn(d)).toEqual([APP_PAIR]);
538+
expect(d.registry.getPackage(APP_PAIR)?.manifest).toMatchObject({
539+
id: APP_PAIR, name: 'HotCRM (custom)', namespace: 'crm_custom', dependencies: { [APP.id]: '^6' },
540+
});
541+
expect(d.published).toHaveLength(1);
542+
});
543+
544+
it('the namespace held by a package outside the App: 422 NAMESPACE_CONFLICT still, asked from a module', async () => {
545+
const d = hotcrm();
546+
register(d, { id: 'com.other.squatter', name: 'Squatter', version: '1.0.0', type: 'app', namespace: 'crm_custom' }, 'code');
547+
const before = ids(d);
548+
const r = await ensure(d, MODULES[1]!.id);
549+
expectRefusal(r, 422, 'NAMESPACE_CONFLICT', "its namespace 'crm_custom' is already owned by package 'com.other.squatter'");
550+
expect(ids(d)).toEqual(before);
551+
expect(d.published).toHaveLength(0);
552+
});
553+
554+
it('a per-package pair an earlier version created for a module holds the namespace: 409 naming it, never 422; it stays installed (merge question 12)', async () => {
555+
const d = hotcrm();
556+
const legacy = {
557+
id: `${MODULES[0]!.id}.custom`, name: 'Service (custom)', version: '0.1.0', type: 'module', namespace: 'crm_custom',
558+
dependencies: { [MODULES[0]!.id]: '^6' },
559+
};
560+
register(d, legacy);
561+
const before = structuredClone(d.registry.getPackage(legacy.id));
562+
for (const asked of [APP.id, MODULES[0]!.id, MODULES[2]!.id]) {
563+
const r = await ensure(d, asked);
564+
expectRefusal(r, 409, 'RESOURCE_CONFLICT',
565+
`Cannot ensure '${APP_PAIR}' yet`, `held by '${legacy.id}'`, 'stays installed', 'is not created beside it');
566+
}
567+
expect(pairsIn(d)).toEqual([legacy.id]);
568+
expect(d.registry.getPackage(legacy.id)).toEqual(before);
569+
expect(d.published).toHaveLength(0);
570+
});
571+
572+
it('a package declaring two Apps whose namespace picks neither: 409, no single App, nothing created', async () => {
573+
const d = hotcrm();
574+
const other = { id: 'com.example.showcase', name: 'Showcase', version: '1.0.0', type: 'app', namespace: 'showcase' };
575+
withBase(d, other);
576+
const bridge = {
577+
id: 'com.acme.bridge', name: 'Bridge', version: '1.0.0', type: 'plugin', namespace: 'bridge',
578+
dependencies: { [APP.id]: '^6', [other.id]: '^1' },
579+
};
580+
withBase(d, bridge);
581+
const r = await ensure(d, bridge.id);
582+
expectRefusal(r, 409, 'RESOURCE_CONFLICT', `Package '${bridge.id}' declares several Apps`);
583+
expect(pairsIn(d)).toEqual([]);
584+
});
585+
586+
it('a package that declares no App is its own unit: its own pair, as before', async () => {
587+
const d = hotcrm();
588+
const solo = { id: 'com.acme.solo', name: 'Solo', version: '2.0.0', type: 'plugin', namespace: 'solo' };
589+
withBase(d, solo);
590+
const r = await ensure(d, solo.id);
591+
expect(r.response.status, JSON.stringify(r.response.body)).toBe(201);
592+
expect(r.response.body.data).toMatchObject({ basePackageId: solo.id, packageId: 'com.acme.solo.custom', namespace: 'solo_custom', dependencyRange: '^2' });
593+
});
594+
});

0 commit comments

Comments
 (0)