Skip to content

Commit e4764f8

Browse files
committed
test(objectql): pin a refused upgrade leaving the installed version serving (red first)
1 parent c064d3a commit e4764f8

1 file changed

Lines changed: 248 additions & 0 deletions

File tree

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#28697] A refused upgrade leaves the installed version serving, whole.
5+
*
6+
* ## What this file pins
7+
*
8+
* An installed `1.0.0` of a package (a v1-only object, a shared object, an
9+
* object a tenant has overlaid, an object another package extends, an app, a
10+
* page, a picklist, a datasource) is re-registered as a `2.0.0` that the
11+
* registry refuses part-way, once for each refusal the registry raises after
12+
* the new package record is written:
13+
*
14+
* - the refusal keeps its code and status;
15+
* - the package record, every object (as served and as layered), every item,
16+
* picklist options, namespaces, security-catalog names, default record page
17+
* holders and datasource definitions read exactly as before;
18+
* - the other package's extension and the tenant's overlay still serve.
19+
*
20+
* CONTROLS: a clean `2.0.0` upgrades as before (and keeps the extension and
21+
* the overlay); a refusal the package door raises before its first write leaves
22+
* v1 as before; the other package's own entries are not touched by a refused
23+
* upgrade of this one.
24+
*/
25+
26+
import { describe, it, expect } from 'vitest';
27+
import { ObjectQL } from './engine.js';
28+
29+
const PKG = 'com.probe.upgrade';
30+
const HOST = 'com.probe.host';
31+
const EXT = 'com.probe.extender';
32+
const NS = 'probeup';
33+
34+
const field = (label: string) => ({ type: 'text', label });
35+
const object = (name: string, extra: Record<string, unknown> = {}) => ({
36+
name,
37+
label: name,
38+
fields: { title: field('Title') },
39+
...extra,
40+
});
41+
const app = (name: string, objectName: string) => ({
42+
name,
43+
label: name,
44+
navigation: [{ id: `nav_${objectName}`, type: 'object', label: objectName, objectName }],
45+
});
46+
47+
const V1 = () => ({
48+
id: PKG,
49+
name: PKG,
50+
namespace: NS,
51+
version: '1.0.0',
52+
objects: [object('probeup_a'), object('probeup_shared', { label: 'v1 shared' }), object('probeup_old')],
53+
apps: [app('probeup_app', 'probeup_a')],
54+
pages: [{ name: 'probeup_v1_page', type: 'app' }],
55+
picklists: [{ name: 'probeup_tier', label: 'Tier', options: [{ label: 'Gold', value: 'gold' }] }],
56+
datasources: [{ name: 'probeup_ds_v1', driver: 'memory' }],
57+
});
58+
59+
/** A `2.0.0` that rewrites, adds and drops, with `fault` appended where the registry refuses it. */
60+
const V2 = (fault: Record<string, unknown> = {}) => {
61+
const { objects: faultObjects = [], ...rest } = fault as { objects?: unknown[] };
62+
return {
63+
id: PKG,
64+
name: PKG,
65+
namespace: NS,
66+
version: '2.0.0',
67+
objects: [
68+
object('probeup_a', { label: 'v2 a' }),
69+
object('probeup_shared', { label: 'v2 shared' }),
70+
object('probeup_new'),
71+
...faultObjects,
72+
],
73+
apps: [app('probeup_app_v2', 'probeup_new')],
74+
pages: [{ name: 'probeup_v2_page', type: 'app' }],
75+
datasources: [{ name: 'probeup_ds_v2', driver: 'memory' }],
76+
...rest,
77+
};
78+
};
79+
80+
const INDUSTRY = { name: 'probeup_industry', label: 'Industry', options: [{ label: 'Finance', value: 'finance' }] };
81+
const RECORD_AMOUNT = {
82+
title: field('Title'),
83+
currency_code: { type: 'text', label: 'Currency', valueDomain: 'iso_4217_currency' },
84+
amount: { type: 'currency', label: 'Amount', currencyConfig: { currencyMode: 'record', currencyField: 'currency_code' } },
85+
};
86+
87+
/** An engine holding HOST, PKG@1.0.0, EXT's extension of a v1 object and a tenant overlay of another. */
88+
function installed(): ObjectQL {
89+
const engine = new ObjectQL();
90+
engine.registerApp({
91+
id: HOST,
92+
name: HOST,
93+
objects: [object('probeup_held', { label: 'Host held' })],
94+
picklists: [INDUSTRY],
95+
picklistExtensions: [{ extend: 'probeup_tier', options: [{ label: 'Silver', value: 'silver' }] }],
96+
} as any);
97+
engine.registerApp(V1() as any);
98+
engine.registerApp({
99+
id: EXT,
100+
name: EXT,
101+
objectExtensions: [{ extend: 'probeup_old', fields: { ext_note: field('Extender note') } }],
102+
} as any);
103+
engine.registry.registerObject(
104+
{ ...object('probeup_a', { label: 'Tenant label' }), _provenance: 'org' } as any,
105+
'sys_metadata',
106+
undefined,
107+
'overlay',
108+
);
109+
return engine;
110+
}
111+
112+
/** Everything the engine serves, read through its public verbs. */
113+
function served(engine: ObjectQL) {
114+
const registry = engine.registry;
115+
const objects = registry.getAllObjects().map((o: any) => o.name as string).filter((n) => n.startsWith('probeup_')).sort();
116+
return {
117+
packages: Object.fromEntries([PKG, HOST, EXT].map((id) => [id, registry.getPackage(id)])),
118+
objects: Object.fromEntries(objects.map((name) => [name, {
119+
served: registry.getObject(name),
120+
layers: registry.getObjectContributors(name).map((c) => ({ packageId: c.packageId, ownership: c.ownership, priority: c.priority, definition: c.definition })),
121+
}])),
122+
items: Object.fromEntries(registry.getRegisteredTypes().filter((t) => t !== 'object').sort().map((type) => [type, registry.listItems(type)])),
123+
picklists: Object.fromEntries(['probeup_tier', 'probeup_industry', 'probeup_list'].map((p) => [p, registry.resolvePicklistOptions(p)])),
124+
namespaces: Object.fromEntries([NS, 'probeupinner'].map((ns) => [ns, registry.getNamespaceOwners(ns)])),
125+
catalog: (['permission', 'position', 'capability'] as const).map((type) => registry.securityCatalogNamesHeldBy(PKG, type as any)),
126+
defaultPages: ['probeup_a', 'probeup_held'].map((o) => registry.defaultRecordPageHoldersOf(o)),
127+
datasources: engine.listDatasourceDefs().map((d) => d.name).filter((n) => n.startsWith('probeup_')).sort(),
128+
};
129+
}
130+
131+
function refusal(fn: () => void): any {
132+
try {
133+
fn();
134+
} catch (e) {
135+
return e;
136+
}
137+
throw new Error('expected the upgrade to be refused');
138+
}
139+
140+
// ── refusals the registry raises after the 2.0.0 record is written ─────────
141+
142+
const PART_WAY: Array<{ label: string; fault: Record<string, unknown>; code: string; status: number }> = [
143+
{
144+
label: 'an object field with no `type`',
145+
fault: { objects: [{ name: 'probeup_bad', fields: { t: { label: 'No type' } } }] },
146+
code: 'INVALID_METADATA', status: 422,
147+
},
148+
{
149+
label: 'an object declaring its own `queue` field',
150+
fault: { objects: [object('probeup_queued', { fields: { title: field('Title'), queue: field('Queue') } })] },
151+
code: 'INVALID_METADATA', status: 422,
152+
},
153+
{
154+
label: 'an object action its schema refuses',
155+
fault: { objects: [object('probeup_acted', { actions: [{ name: 'probeup_go', type: 'nonsense' }] })] },
156+
code: 'INVALID_METADATA', status: 422,
157+
},
158+
{
159+
label: 'a roll-up of a record-currency amount in an object registered just before it',
160+
fault: {
161+
objects: [
162+
object('probeup_line', { fields: RECORD_AMOUNT }),
163+
object('probeup_rollup', { fields: { title: field('Title'), total: { type: 'summary', label: 'Total', summaryOperations: { object: 'probeup_line', field: 'amount', function: 'sum' } } } }),
164+
],
165+
},
166+
code: 'INVALID_METADATA', status: 422,
167+
},
168+
{
169+
label: 'an object another package owns',
170+
fault: { objects: [object('probeup_held')] },
171+
code: 'OBJECT_OWNERSHIP_CONFLICT', status: 422,
172+
},
173+
{
174+
label: 'an extension field with no `type`',
175+
fault: { objectExtensions: [{ extend: 'probeup_held', fields: { t: { label: 'No type' } } }] },
176+
code: 'INVALID_METADATA', status: 422,
177+
},
178+
{
179+
label: 'a standalone action its schema refuses',
180+
fault: { actions: [{ name: 'probeup_loose', type: 'nonsense' }] },
181+
code: 'INVALID_METADATA', status: 422,
182+
},
183+
{
184+
label: 'a picklist repeating a value an extension already adds to it',
185+
fault: { picklistExtensions: [{ extend: 'probeup_list', options: [{ label: 'X', value: 'x' }] }], picklists: [{ name: 'probeup_list', label: 'L', options: [{ label: 'X again', value: 'x' }] }] },
186+
code: 'INVALID_METADATA', status: 422,
187+
},
188+
{
189+
label: 'a picklist extension repeating a value another package\'s list carries',
190+
fault: { picklistExtensions: [{ extend: 'probeup_industry', options: [{ label: 'Fintech', value: 'finance' }] }] },
191+
code: 'INVALID_METADATA', status: 422,
192+
},
193+
{
194+
label: 'the same from a nested plugin, after the body registered',
195+
fault: { plugins: [{ name: 'probeup_inner', namespace: 'probeupinner', objects: [object('probeup_inner_thing')], picklistExtensions: [{ extend: 'probeup_industry', options: [{ label: 'Fintech', value: 'finance' }] }] }] },
196+
code: 'INVALID_METADATA', status: 422,
197+
},
198+
];
199+
200+
describe('[#28697] an upgrade the registry refuses part-way leaves the installed version serving', () => {
201+
for (const c of PART_WAY) {
202+
it(c.label, () => {
203+
const engine = installed();
204+
const before = served(engine);
205+
const err = refusal(() => engine.registerApp(V2(c.fault) as any));
206+
expect(err).toMatchObject({ code: c.code, status: c.status });
207+
expect(served(engine)).toEqual(before);
208+
expect(engine.registry.getPackage(PKG)?.manifest.version).toBe('1.0.0');
209+
expect((engine.registry.getObject('probeup_shared') as any)?.label).toBe('v1 shared');
210+
expect((engine.registry.getObject('probeup_a') as any)?.label).toBe('Tenant label');
211+
expect(Object.keys((engine.registry.getObject('probeup_old') as any)?.fields ?? {})).toContain('ext_note');
212+
expect(engine.registry.getObject('probeup_new')).toBeUndefined();
213+
});
214+
}
215+
});
216+
217+
// ── controls ───────────────────────────────────────────────────────────────
218+
219+
describe('[#28697] refused-upgrade controls', () => {
220+
it('a clean 2.0.0 upgrades as before, keeping the extension and the overlay', () => {
221+
const engine = installed();
222+
engine.registerApp(V2() as any);
223+
const registry = engine.registry;
224+
expect(registry.getPackage(PKG)?.manifest.version).toBe('2.0.0');
225+
expect((registry.getObject('probeup_shared') as any)?.label).toBe('v2 shared');
226+
expect(registry.getObject('probeup_new')).toBeDefined();
227+
expect((registry.getObject('probeup_a') as any)?.label).toBe('Tenant label');
228+
expect(Object.keys((registry.getObject('probeup_old') as any)?.fields ?? {})).toContain('ext_note');
229+
expect(registry.listItems('app').map((a: any) => a.name)).toContain('probeup_app_v2');
230+
expect(engine.listDatasourceDefs().map((d) => d.name)).toContain('probeup_ds_v2');
231+
});
232+
233+
it('a refusal the package door raises before its first write leaves v1 as before', () => {
234+
const engine = installed();
235+
const before = served(engine);
236+
const err = refusal(() => engine.registerApp({ ...V2(), pages: [{ name: 'probeup_misplaced', type: 'app', isDefault: true }] } as any));
237+
expect(err).toMatchObject({ status: 422 });
238+
expect(served(engine)).toEqual(before);
239+
});
240+
241+
it('a refused upgrade does not move the other package\'s entries', () => {
242+
const engine = installed();
243+
const hostBefore = { pkg: engine.registry.getPackage(HOST), held: engine.registry.getObjectContributors('probeup_held'), industry: engine.registry.resolvePicklistOptions('probeup_industry') };
244+
refusal(() => engine.registerApp(V2({ objects: [object('probeup_held')] }) as any));
245+
expect({ pkg: engine.registry.getPackage(HOST), held: engine.registry.getObjectContributors('probeup_held'), industry: engine.registry.resolvePicklistOptions('probeup_industry') }).toEqual(hostBefore);
246+
expect(engine.registry.getObjectOwner('probeup_held')?.packageId).toBe(HOST);
247+
});
248+
});

0 commit comments

Comments
 (0)