Skip to content

Commit f1dfda0

Browse files
committed
feat(metadata-core): appOf resolves a package to the App that carries it
A member package's pair is now its App's pair, read from the installed manifests (ADR-0146 D3, merge question 11). pairsOf/isPairOf keep a per-package pair an earlier version created served while it is installed (merge question 12). Kernel packages keep their own answer. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017ihL2dM9PvPPB9BMVdhf2R
1 parent 5f00374 commit f1dfda0

2 files changed

Lines changed: 263 additions & 78 deletions

File tree

‎packages/metadata-core/src/customization-pair-lookup.test.ts‎

Lines changed: 127 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,69 +7,95 @@ import {
77
installedManifestsOf,
88
isPairOf,
99
pairOf,
10+
pairsOf,
1011
type InstalledManifestLookup,
1112
} from './customization-pair-lookup.js';
1213

1314
/**
14-
* The lookup answers exactly what the pure id rule answered for every package
15-
* shape on `main` — an App, a module its artifact brings, a standalone
16-
* package, a `sys` kernel package, a manifest with no namespace — because
17-
* `appOf` is the identity until the ensure door resolves a member to its App.
18-
* Each row pins the literal pair, so a resolver that moves one package to
19-
* another App reds that row by name. Every row is asked twice: with nothing
20-
* installed, and with all the rows installed — the module declaring its App
21-
* the way HotCRM's modules do — so the lookup the callers now hand in is
22-
* proven to move nothing while `appOf` stays the identity.
15+
* The App lookup over the package shapes ADR-0146 D3 merge question 11 names.
16+
* Every row is asked twice: with nothing installed — every package is then
17+
* absent, so its own unit, and the lookup answers exactly the old per-package
18+
* derivation — and with every row installed, where a member answers its App's
19+
* pair. Each row pins the literal pair, so a resolver that moves a package to
20+
* another App reds that row by name.
2321
*/
24-
const ROWS: ReadonlyArray<{
25-
shape: string;
26-
manifest: { id: string; type?: string; scope?: string; namespace?: string; dependencies?: Record<string, string> };
27-
pair: string;
28-
}> = [
22+
type Manifest = { id: string; type?: string; scope?: string; namespace?: string; dependencies?: Record<string, string> };
23+
const ROWS: ReadonlyArray<{ shape: string; manifest: Manifest; app: string | undefined }> = [
2924
{
3025
shape: 'an App package',
3126
manifest: { id: 'app.objectstack.hotcrm', type: 'app', namespace: 'crm' },
32-
pair: 'app.objectstack.hotcrm.custom',
27+
app: 'app.objectstack.hotcrm',
3328
},
3429
{
35-
shape: 'a module an App artifact brings (still its own pair today)',
36-
manifest: {
37-
id: 'app.objectstack.hotcrm.service',
38-
type: 'module',
39-
namespace: 'crm',
40-
dependencies: { 'app.objectstack.hotcrm': '^6.1.0' },
41-
},
42-
pair: 'app.objectstack.hotcrm.service.custom',
30+
shape: 'a module declaring its App (HotCRM)',
31+
manifest: { id: 'app.objectstack.hotcrm.service', type: 'module', namespace: 'crm', dependencies: { 'app.objectstack.hotcrm': '^6.1.0' } },
32+
app: 'app.objectstack.hotcrm',
4333
},
4434
{
45-
shape: 'a standalone package',
35+
shape: 'a module declaring its App and a kernel package',
36+
manifest: { id: 'app.objectstack.hotcrm.o2c', type: 'module', namespace: 'crm', dependencies: { 'app.objectstack.hotcrm': '^6.1.0', 'com.objectstack.audit': '^1' } },
37+
app: 'app.objectstack.hotcrm',
38+
},
39+
{
40+
shape: 'a module reaching its App through a declared module',
41+
manifest: { id: 'app.objectstack.hotcrm.service.sla', type: 'module', namespace: 'crm', dependencies: { 'app.objectstack.hotcrm.service': '^6' } },
42+
app: 'app.objectstack.hotcrm',
43+
},
44+
{
45+
shape: 'a plugin declaring two Apps, one holding its namespace',
46+
manifest: { id: 'com.acme.bridge', type: 'plugin', namespace: 'crm', dependencies: { 'app.objectstack.hotcrm': '^6', 'com.example.showcase': '^1' } },
47+
app: 'app.objectstack.hotcrm',
48+
},
49+
{
50+
shape: 'a plugin declaring two Apps, neither holding its namespace',
51+
manifest: { id: 'com.acme.glue', type: 'plugin', namespace: 'glue', dependencies: { 'app.objectstack.hotcrm': '^6', 'com.example.showcase': '^1' } },
52+
app: undefined,
53+
},
54+
{
55+
shape: 'a standalone App',
4656
manifest: { id: 'com.example.showcase', type: 'app', namespace: 'showcase' },
47-
pair: 'com.example.showcase.custom',
57+
app: 'com.example.showcase',
58+
},
59+
{
60+
shape: 'a plugin declaring no App (its own unit)',
61+
manifest: { id: 'com.acme.solo', type: 'plugin', namespace: 'solo', dependencies: { 'com.acme.ring': '^1' } },
62+
app: 'com.acme.solo',
63+
},
64+
{
65+
shape: 'a module in a declared cycle that reaches no App',
66+
manifest: { id: 'com.acme.ring', type: 'module', namespace: 'ring', dependencies: { 'com.acme.solo': '^1' } },
67+
app: 'com.acme.ring',
68+
},
69+
{
70+
shape: 'a module declaring an App that is not installed',
71+
manifest: { id: 'com.acme.orphan', type: 'module', namespace: 'orph', dependencies: { 'com.acme.absent': '^1' } },
72+
app: 'com.acme.orphan',
4873
},
4974
{
5075
shape: 'a sys kernel package',
5176
manifest: { id: 'com.objectstack.plugin-auth', type: 'plugin', scope: 'system', namespace: 'sys' },
52-
pair: 'com.objectstack.plugin-auth.custom',
77+
app: 'com.objectstack.plugin-auth',
78+
},
79+
{
80+
shape: 'a kernel package declaring an App (kernel keeps its own answer)',
81+
manifest: { id: 'com.objectstack.audit', type: 'plugin', scope: 'cloud', namespace: 'sys', dependencies: { 'app.objectstack.hotcrm': '^6' } },
82+
app: 'com.objectstack.audit',
5383
},
5484
{
5585
shape: 'a manifest with no namespace',
5686
manifest: { id: 'hotcrm', type: 'app' },
57-
pair: 'hotcrm.custom',
87+
app: 'hotcrm',
5888
},
5989
];
6090

6191
const NOTHING_INSTALLED = installedManifestsOf(undefined);
62-
const ALL_INSTALLED = installedManifestsOf({
92+
const lookupOf = (manifests: readonly Manifest[]): InstalledManifestLookup => installedManifestsOf({
6393
getPackage: (id: string) => {
64-
const row = ROWS.find((r) => r.manifest.id === id);
65-
return row ? { manifest: row.manifest } : undefined;
94+
const manifest = manifests.find((m) => m.id === id);
95+
return manifest ? { manifest } : undefined;
6696
},
6797
});
68-
const LOOKUPS: ReadonlyArray<{ installed: string; lookup: InstalledManifestLookup }> = [
69-
{ installed: 'nothing installed', lookup: NOTHING_INSTALLED },
70-
{ installed: 'every row installed', lookup: ALL_INSTALLED },
71-
];
72-
const ROWS_BY_LOOKUP = LOOKUPS.flatMap(({ installed, lookup }) => ROWS.map((row) => ({ ...row, installed, lookup })));
98+
const ALL_INSTALLED = lookupOf(ROWS.map((r) => r.manifest));
7399

74100
describe('installedManifestsOf — the registry read the lookup is', () => {
75101
it('reads the installed manifest by id, and nothing for an id not installed', () => {
@@ -90,11 +116,27 @@ describe('installedManifestsOf — the registry read the lookup is', () => {
90116
});
91117
});
92118

93-
describe('pairOf — the pair of a package, looked up through its App', () => {
94-
it.each(ROWS_BY_LOOKUP)('$shape, $installed: answers the old derivation, $pair', ({ manifest, pair, lookup }) => {
95-
expect(appOf(manifest.id, lookup)).toBe(manifest.id);
96-
expect(pairOf(manifest.id, lookup)).toEqual(customizationPackageId(manifest.id));
97-
expect(pairOf(manifest.id, lookup)).toEqual({ ok: true, value: pair });
119+
describe('appOf / pairOf — the pair of a package is the pair of its App (ADR-0146 D3 merge question 11)', () => {
120+
it.each(ROWS)('$shape, nothing installed: its own unit, the old derivation', ({ manifest }) => {
121+
expect(appOf(manifest.id, NOTHING_INSTALLED)).toBe(manifest.id);
122+
expect(pairOf(manifest.id, NOTHING_INSTALLED)).toEqual(customizationPackageId(manifest.id));
123+
});
124+
125+
it.each(ROWS)('$shape, every row installed: App $app', ({ manifest, app }) => {
126+
expect(appOf(manifest.id, ALL_INSTALLED)).toBe(app);
127+
expect(pairOf(manifest.id, ALL_INSTALLED)).toEqual(app === undefined
128+
? { ok: false, reason: 'no_single_app' }
129+
: { ok: true, value: `${app}.custom` });
130+
});
131+
132+
it('HotCRM: the App and its four modules have one pair', () => {
133+
const app = { id: 'app.objectstack.hotcrm', type: 'app', namespace: 'crm' };
134+
const modules = ['service', 'revenue', 'marketing', 'o2c'].map((m) => ({
135+
id: `app.objectstack.hotcrm.${m}`, type: 'module', namespace: 'crm', dependencies: { 'app.objectstack.hotcrm': '^6.1.0' },
136+
}));
137+
const installed = lookupOf([app, ...modules]);
138+
const pairs = new Set([app, ...modules].map((m) => pairOf(m.id, installed)).map((p) => (p.ok ? p.value : p.reason)));
139+
expect([...pairs]).toEqual(['app.objectstack.hotcrm.custom']);
98140
});
99141

100142
it.each([
@@ -104,26 +146,60 @@ describe('pairOf — the pair of a package, looked up through its App', () => {
104146
{ input: 'hotcrm.custom', reason: 'base_is_customization_package' },
105147
{ input: 'Com.Acme', reason: 'base_id_invalid' },
106148
])('refuses $input as the id rule does: $reason', ({ input, reason }) => {
107-
for (const { lookup } of LOOKUPS) {
149+
for (const lookup of [NOTHING_INSTALLED, ALL_INSTALLED]) {
108150
expect(pairOf(input, lookup)).toEqual(customizationPackageId(input));
109151
expect(pairOf(input, lookup)).toEqual({ ok: false, reason });
110152
}
111153
});
154+
155+
it('a pair id answers itself, installed or not', () => {
156+
const pair = { id: 'app.objectstack.hotcrm.custom', type: 'module', namespace: 'crm_custom', dependencies: { 'app.objectstack.hotcrm': '^6' } };
157+
const installed = lookupOf([...ROWS.map((r) => r.manifest), pair]);
158+
expect(appOf(pair.id, installed)).toBe(pair.id);
159+
expect(pairOf(pair.id, installed)).toEqual({ ok: false, reason: 'base_is_customization_package' });
160+
});
112161
});
113162

114-
describe('isPairOf — is this THE pair of that package', () => {
115-
it.each(ROWS_BY_LOOKUP)('$shape, $installed: only $pair is its pair', ({ manifest, pair, lookup }) => {
116-
const candidates = [pair, manifest.id, `${pair}.custom`, 'app.objectstack.hotcrm.custom', 'partner.ext', null, undefined];
163+
describe('isPairOf / pairsOf — which packages hold a package\'s customizations', () => {
164+
it.each(ROWS)('$shape, nothing installed: only the old derivation is its pair', ({ manifest }) => {
165+
const own = customizationPackageId(manifest.id);
166+
const candidates = [own.ok ? own.value : null, manifest.id, 'app.objectstack.hotcrm.custom', 'partner.ext', null, undefined];
117167
for (const candidate of candidates) {
118-
expect(isPairOf(candidate, manifest.id, lookup)).toBe(isCustomizationPairOf(candidate, manifest.id));
168+
expect(isPairOf(candidate, manifest.id, NOTHING_INSTALLED)).toBe(isCustomizationPairOf(candidate, manifest.id));
119169
}
120-
expect(isPairOf(pair, manifest.id, lookup)).toBe(true);
121-
expect(isPairOf(manifest.id, manifest.id, lookup)).toBe(false);
122170
});
123171

124-
it.each(LOOKUPS)('$installed: a pair has no pair, and a missing base has none', ({ lookup }) => {
125-
expect(isPairOf('hotcrm.custom.custom', 'hotcrm.custom', lookup)).toBe(false);
126-
expect(isPairOf('.custom', '', lookup)).toBe(false);
127-
expect(isPairOf('x.custom', undefined, lookup)).toBe(false);
172+
it('a member: its App\'s pair is its pair; its own derivation is not, while nothing holds it', () => {
173+
const service = 'app.objectstack.hotcrm.service';
174+
expect(pairsOf(service, ALL_INSTALLED)).toEqual(['app.objectstack.hotcrm.custom']);
175+
expect(isPairOf('app.objectstack.hotcrm.custom', service, ALL_INSTALLED)).toBe(true);
176+
expect(isPairOf(`${service}.custom`, service, ALL_INSTALLED)).toBe(false);
177+
expect(isPairOf(service, service, ALL_INSTALLED)).toBe(false);
178+
});
179+
180+
it('a member: the per-package pair an earlier version created stays its pair while installed (merge question 12)', () => {
181+
const service = 'app.objectstack.hotcrm.service';
182+
const legacy = { id: `${service}.custom`, type: 'module', namespace: 'crm_custom', dependencies: { [service]: '^6' } };
183+
const installed = lookupOf([...ROWS.map((r) => r.manifest), legacy]);
184+
expect(pairOf(service, installed)).toEqual({ ok: true, value: 'app.objectstack.hotcrm.custom' });
185+
expect(pairsOf(service, installed)).toEqual(['app.objectstack.hotcrm.custom', legacy.id]);
186+
expect(isPairOf(legacy.id, service, installed)).toBe(true);
187+
// …and of its own member only: the App and a sibling do not read it.
188+
expect(isPairOf(legacy.id, 'app.objectstack.hotcrm', installed)).toBe(false);
189+
expect(isPairOf(legacy.id, 'app.objectstack.hotcrm.o2c', installed)).toBe(false);
190+
});
191+
192+
it('a package with no single App: no pair to land in, a legacy pair still served', () => {
193+
expect(pairsOf('com.acme.glue', ALL_INSTALLED)).toEqual([]);
194+
const legacy = { id: 'com.acme.glue.custom', type: 'module', namespace: 'glue_custom', dependencies: { 'com.acme.glue': '^1' } };
195+
expect(pairsOf('com.acme.glue', lookupOf([...ROWS.map((r) => r.manifest), legacy]))).toEqual([legacy.id]);
196+
});
197+
198+
it('a pair has no pair, and a missing base has none', () => {
199+
for (const lookup of [NOTHING_INSTALLED, ALL_INSTALLED]) {
200+
expect(isPairOf('hotcrm.custom.custom', 'hotcrm.custom', lookup)).toBe(false);
201+
expect(isPairOf('.custom', '', lookup)).toBe(false);
202+
expect(isPairOf('x.custom', undefined, lookup)).toBe(false);
203+
}
128204
});
129205
});

0 commit comments

Comments
 (0)