Skip to content

Commit deac6d1

Browse files
committed
test(metadata-protocol): pin the overlay save door's whole-collection list-view verdict
Claude-Session: https://claude.ai/code/session_019oFY6Hi35J7atvgVFnmoaK Co-authored-by: Claude <noreply@anthropic.com>
1 parent 5fed01d commit deac6d1

1 file changed

Lines changed: 164 additions & 4 deletions

File tree

‎packages/metadata-protocol/src/protocol.runtime-authoring-gate.test.ts‎

Lines changed: 164 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3629,17 +3629,21 @@ describe('runtime authoring gate on VIEW writes into a package — the closure v
36293629
}
36303630
});
36313631

3632-
it('(c) a write that names no package is unchanged — the overlay door refuses the stranger column and stays silent on nowhere', async () => {
3632+
it('(c) a write that names no package gets no closure verdict — the overlay door refuses the stranger column, and nowhere as nowhere', async () => {
36333633
const overlay = makePackagedProtocol();
36343634
const err = await saveView(overlay.protocol, listView('showcase_task')).catch((e: any) => e);
36353635
expect({ code: err?.code, status: err?.status }).toEqual({ code: 'INVALID_METADATA', status: 422 });
36363636
expect(err.issues.map((i: any) => [i.rule, i.path])).toEqual([[FIELD_UNKNOWN, 'views[0].columns[1].field']]);
36373637
expect(activeViewRows(overlay.rows)).toEqual([]);
36383638

3639+
// [#24668] No closure, so no closure verdict: the binding is judged
3640+
// against the whole collection and refused as existing nowhere (the
3641+
// block below pins that door in full).
36393642
const nowhere = makePackagedProtocol();
3640-
const result = await saveView(nowhere.protocol, listView('nosuch_obj'));
3641-
expect(result).toMatchObject({ success: true });
3642-
expect(rulesOf(result.advisories)).toEqual([]);
3643+
const refused = await saveView(nowhere.protocol, listView('nosuch_obj')).catch((e: any) => e);
3644+
expect({ code: refused?.code, status: refused?.status }).toEqual({ code: 'INVALID_METADATA', status: 422 });
3645+
expect(rulesOf(refused.issues)).toEqual(['list-view-object-unknown']);
3646+
expect(activeViewRows(nowhere.rows)).toEqual([]);
36433647
});
36443648

36453649
it('(d) the draft → active promotion refuses the out-of-closure binding, and the in-closure unknown field', async () => {
@@ -3721,3 +3725,159 @@ describe('runtime authoring gate on VIEW writes into a package — the closure v
37213725
expect(rulesOf(causal.issues)).toEqual([FIELD_UNKNOWN]);
37223726
});
37233727
});
3728+
3729+
// [#24668] The overlay twin of the block above: a list view written with NO
3730+
// package is judged against the WHOLE collection the host serves — every
3731+
// registry object (every package's and the platform's) folded with the stored
3732+
// rows — so a binding missing from it exists nowhere, and the real
3733+
// `saveMetaItem` refuses it `422 INVALID_METADATA` at the binding
3734+
// (`list-view-object-unknown`), with nothing stored. The verdict the closure
3735+
// door gives the same body (`list-view-object-outside-closure`) and the dataset
3736+
// door gives an unknown base object (`object-reference-unknown`). A binding
3737+
// that resolves — a package's object, a platform object, a stored overlay
3738+
// object — is admitted with its field verdict unchanged. Drafts are not gated;
3739+
// a stored row is judged at its next save.
3740+
describe('runtime authoring gate on VIEW writes that name no package — the whole-collection verdict (#24668)', () => {
3741+
const OWN = 'com.acme.crm';
3742+
const STRANGER = 'com.other.analytics';
3743+
const PLATFORM = 'com.objectstack.platform';
3744+
const UNKNOWN = 'list-view-object-unknown';
3745+
const FIELD_UNKNOWN = 'list-view-field-unknown';
3746+
3747+
const object = (name: string, packageId?: string) => ({
3748+
name,
3749+
label: name,
3750+
...(packageId === undefined ? {} : { _packageId: packageId }),
3751+
fields: [{ name: 'title', type: 'text', label: 'Title' }],
3752+
});
3753+
/** The card's body: a flattened list overlay bound to `bound`, one known and by default one unknown column. */
3754+
const listView = (bound: string, columns: readonly string[] = ['title', 'zzz_nope']) => ({
3755+
name: `${bound}.qavfy_r1`,
3756+
object: bound,
3757+
viewKind: 'list',
3758+
label: 'r1',
3759+
type: 'grid',
3760+
data: { provider: 'object', object: bound },
3761+
columns: columns.map((field) => ({ field })),
3762+
});
3763+
3764+
/** A registry holding two packages' objects and a platform object; `tenant_ticket` lives only in the store. */
3765+
function makeOverlayProtocol() {
3766+
const { engine, rows } = makeStubEngine();
3767+
const packages: Record<string, { manifest: Record<string, unknown> }> = {
3768+
[OWN]: { manifest: { name: OWN, version: '1.0.0' } },
3769+
[STRANGER]: { manifest: { name: STRANGER, version: '1.0.0' } },
3770+
};
3771+
engine.registry.listItems = (type: string) =>
3772+
type === 'object'
3773+
? [object('crm_account', OWN), object('showcase_task', STRANGER), object('sys_widget_log', PLATFORM)]
3774+
: [];
3775+
engine.registry.getPackage = (id: string) => packages[id];
3776+
// A tenant-authored object, stored and never registered: the gate's
3777+
// universe is the registry FOLDED with the store, so it is there.
3778+
rows.set(keyOf({ type: 'object', name: 'tenant_ticket', organization_id: null, state: 'active' }), {
3779+
id: 'r_obj', type: 'object', name: 'tenant_ticket', organization_id: null, state: 'active',
3780+
metadata: JSON.stringify(object('tenant_ticket')),
3781+
});
3782+
const protocol = new ObjectStackProtocolImplementation(engine, () => new Map(), 'env_test') as any;
3783+
return { protocol, rows };
3784+
}
3785+
3786+
const saveView = (protocol: any, item: { name: string }, extra: Record<string, unknown> = {}) =>
3787+
protocol.saveMetaItem({ type: 'view', name: item.name, item, ...extra });
3788+
const activeViewRows = (rows: Map<string, Row>) =>
3789+
Array.from(rows.values()).filter((r) => r.type === 'view' && r.state === 'active');
3790+
const rulesOf = (findings: any[] | undefined) => (findings ?? []).map((f: any) => f.rule);
3791+
/** A refused publish-mode write: the 422 envelope, carrying the one unknown-object issue at the binding. */
3792+
const expectRefusedUnknown = (err: any, what: string) => {
3793+
expect(err, `${what}: the write resolved — the overlay door still accepts a binding that exists nowhere`).toBeInstanceOf(Error);
3794+
expect({ code: err.code, status: err.status }).toEqual({ code: 'INVALID_METADATA', status: 422 });
3795+
expect(rulesOf(err.issues), JSON.stringify(err.issues)).toEqual([UNKNOWN]);
3796+
expect(err.issues[0]).toMatchObject({ severity: 'error', path: 'views[0].data.object' });
3797+
expect(err.issues[0].message).toContain('"nosuch_obj"');
3798+
};
3799+
3800+
let warn: ReturnType<typeof vi.spyOn>;
3801+
beforeEach(() => {
3802+
warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
3803+
delete process.env.OS_ALLOW_UNLINTED_METADATA_WRITES;
3804+
});
3805+
afterEach(() => {
3806+
warn.mockRestore();
3807+
delete process.env.OS_ALLOW_UNLINTED_METADATA_WRITES;
3808+
});
3809+
3810+
it('(a) REFUSES an active overlay save bound to an object that exists nowhere — 422 at the binding, nothing stored', async () => {
3811+
const { protocol, rows } = makeOverlayProtocol();
3812+
3813+
const err = await saveView(protocol, listView('nosuch_obj')).catch((e: any) => e);
3814+
3815+
expectRefusedUnknown(err, 'overlay write');
3816+
// A gate that refuses after persisting is a log line.
3817+
expect(activeViewRows(rows)).toEqual([]);
3818+
});
3819+
3820+
it('(a) the control: a binding that resolves is admitted — package, stranger, platform and stored overlay objects', async () => {
3821+
for (const bound of ['crm_account', 'showcase_task', 'sys_widget_log', 'tenant_ticket']) {
3822+
const { protocol, rows } = makeOverlayProtocol();
3823+
3824+
const result = await saveView(protocol, listView(bound, ['title']));
3825+
3826+
expect(result, bound).toMatchObject({ success: true });
3827+
expect(rulesOf(result.advisories), bound).toEqual([]);
3828+
expect(activeViewRows(rows), bound).toHaveLength(1);
3829+
}
3830+
});
3831+
3832+
it('(b) a binding that resolves keeps its field verdict: an unknown column is refused as before', async () => {
3833+
for (const bound of ['crm_account', 'tenant_ticket']) {
3834+
const { protocol, rows } = makeOverlayProtocol();
3835+
3836+
const err = await saveView(protocol, listView(bound)).catch((e: any) => e);
3837+
3838+
expect({ code: err?.code, status: err?.status }, bound).toEqual({ code: 'INVALID_METADATA', status: 422 });
3839+
expect(err.issues.map((i: any) => [i.rule, i.path]), bound).toEqual([[FIELD_UNKNOWN, 'views[0].columns[1].field']]);
3840+
expect(activeViewRows(rows), bound).toEqual([]);
3841+
}
3842+
});
3843+
3844+
it('(c) a DRAFT save of the same body is not gated; its promotion is judged and refused', async () => {
3845+
const { protocol, rows } = makeOverlayProtocol();
3846+
3847+
await expect(saveView(protocol, listView('nosuch_obj'), { mode: 'draft' })).resolves.toMatchObject({ success: true });
3848+
const promoted = await protocol
3849+
.publishMetaItem({ type: 'view', name: 'nosuch_obj.qavfy_r1' })
3850+
.catch((e: any) => e);
3851+
3852+
expectRefusedUnknown(promoted, 'promotion');
3853+
expect(activeViewRows(rows)).toEqual([]);
3854+
});
3855+
3856+
it('(d) a write naming a package the registry cannot produce narrows nothing — the whole-collection verdict', async () => {
3857+
const { protocol, rows } = makeOverlayProtocol();
3858+
3859+
const err = await saveView(protocol, listView('nosuch_obj'), { packageId: 'com.unregistered.pkg' }).catch((e: any) => e);
3860+
3861+
expectRefusedUnknown(err, 'unresolvable package');
3862+
expect(activeViewRows(rows)).toEqual([]);
3863+
});
3864+
3865+
it('(e) a stored view with the binding keeps loading; its next save is refused, the row untouched', async () => {
3866+
const { protocol, rows } = makeOverlayProtocol();
3867+
// Stored before the crossing, seeded straight into the store because the
3868+
// save door now refuses this body.
3869+
const stored = listView('nosuch_obj', ['title']);
3870+
rows.set(keyOf({ type: 'view', name: stored.name, organization_id: null, state: 'active' }), {
3871+
id: 'r_seed', type: 'view', name: stored.name, organization_id: null, state: 'active',
3872+
metadata: JSON.stringify(stored),
3873+
});
3874+
3875+
// The boot path runs no authoring rule.
3876+
await expect(protocol.loadMetaFromDb()).resolves.toMatchObject({ errors: 0, invalid: 0 });
3877+
3878+
// The next save is a write, and is judged.
3879+
const err = await saveView(protocol, stored).catch((e: any) => e);
3880+
expectRefusedUnknown(err, 'next save of a stored row');
3881+
expect(JSON.parse(activeViewRows(rows)[0]!.metadata)).toEqual(stored);
3882+
});
3883+
});

0 commit comments

Comments
 (0)