Skip to content

Commit 34928cb

Browse files
committed
test(metadata-protocol): the save door refuses a controlled_by_parent detail whose one master is not writable
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B2EeooonSXrANpkpgUSfFZ
1 parent 4d1ca8d commit 34928cb

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* #25092 — the object save door refuses a `controlled_by_parent` detail whose
5+
* one required `master_detail` is marked `readonly: true` or `system: true`.
6+
*
7+
* ADR-0055 §1 derives such an object's master from exactly one required
8+
* `master_detail` that every write of a detail supplies. The rule lives in the
9+
* spec parse (`controlledByParentMasterRefusal`, shape `master-not-writable`),
10+
* and `saveMetaItem` parses with `ObjectSchema`, so the door answers
11+
* `422 INVALID_METADATA` at `fields` and stores nothing — the same refusal
12+
* `os validate` and `os build` meet, from the same rule.
13+
*/
14+
import { describe, expect, it } from 'vitest';
15+
import { assertEngineDeleteDispatch, assertEngineUpdateDispatch, assertEngineFindOnePredicate, type EngineFindOneQueryInput } from '@objectstack/metadata-core';
16+
import { ObjectStackProtocolImplementation } from './protocol.js';
17+
18+
/** The engine surface the repository write path touches (as the container-issue-descent harness). */
19+
function makeProtocol() {
20+
const rows = new Map<string, Record<string, unknown>>();
21+
let nextId = 0;
22+
const engine: any = {
23+
async findOne(object: string, query?: EngineFindOneQueryInput) {
24+
assertEngineFindOnePredicate(object, query); return null; },
25+
async find() { return []; },
26+
async insert(table: string, data: Record<string, unknown>) {
27+
nextId += 1;
28+
if (table === 'sys_metadata') rows.set(`${data.type}|${data.name}`, data);
29+
return { id: `r_${nextId}` };
30+
},
31+
async update(_t: string, data: Record<string, unknown>, opts?: Record<string, unknown>) {
32+
assertEngineUpdateDispatch(data, opts);
33+
return { id: null };
34+
},
35+
async delete(_t: string, opts?: Record<string, unknown>) {
36+
assertEngineDeleteDispatch(opts);
37+
return { deleted: 0 };
38+
},
39+
registry: { registerItem: () => {}, registerObject: () => {} },
40+
};
41+
const protocol: any = new ObjectStackProtocolImplementation(engine, () => new Map());
42+
return { protocol, rows };
43+
}
44+
45+
async function rejection(promise: Promise<unknown>): Promise<any> {
46+
try {
47+
await promise;
48+
} catch (err) {
49+
return err;
50+
}
51+
throw new Error('expected the save to be rejected, but it resolved');
52+
}
53+
54+
const detailWith = (flags: Record<string, unknown>) => ({
55+
name: 'work_order_item',
56+
label: 'Work Order Item',
57+
sharingModel: 'controlled_by_parent',
58+
fields: {
59+
order: { name: 'order', type: 'master_detail', label: 'Order', reference: 'work_order', required: true, ...flags },
60+
note: { name: 'note', type: 'text', label: 'Note' },
61+
},
62+
});
63+
64+
describe('#25092 the save door refuses a controlled_by_parent detail whose one master is not writable', () => {
65+
it.each([
66+
['readonly: true', { readonly: true }, '`readonly: true`'],
67+
['system: true', { system: true }, '`system: true`'],
68+
])('a one master marked %s answers 422 INVALID_METADATA at `fields`, and nothing is stored', async (_label, flags, named) => {
69+
const { protocol, rows } = makeProtocol();
70+
71+
const err = await rejection(protocol.saveMetaItem({
72+
type: 'object', name: 'work_order_item', item: detailWith(flags),
73+
}));
74+
75+
expect(err.code).toBe('INVALID_METADATA');
76+
expect(err.status).toBe(422);
77+
expect(err.issues).toEqual([expect.objectContaining({ path: 'fields', code: 'custom' })]);
78+
expect(err.issues[0].message.startsWith(
79+
"Object 'work_order_item' declares sharingModel 'controlled_by_parent' with its one required "
80+
+ `\`master_detail\`, \`order\`, marked ${named}.`,
81+
)).toBe(true);
82+
expect(rows.size).toBe(0);
83+
});
84+
85+
it('control: the same detail with a writable master saves', async () => {
86+
const { protocol, rows } = makeProtocol();
87+
88+
const result = await protocol.saveMetaItem({
89+
type: 'object', name: 'work_order_item', item: detailWith({}),
90+
});
91+
92+
expect(result.success).toBe(true);
93+
expect(rows.size).toBe(1);
94+
});
95+
});

‎scripts/engine-double-contract.pinned.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,21 @@
466466
"verb": "update",
467467
"pinned": 1
468468
},
469+
{
470+
"file": "packages/metadata-protocol/src/protocol.cbp-master-not-writable-refusal.test.ts",
471+
"verb": "delete",
472+
"pinned": 1
473+
},
474+
{
475+
"file": "packages/metadata-protocol/src/protocol.cbp-master-not-writable-refusal.test.ts",
476+
"verb": "findOne",
477+
"pinned": 1
478+
},
479+
{
480+
"file": "packages/metadata-protocol/src/protocol.cbp-master-not-writable-refusal.test.ts",
481+
"verb": "update",
482+
"pinned": 1
483+
},
469484
{
470485
"file": "packages/metadata-protocol/src/protocol.cluster-mutation-fanout.test.ts",
471486
"verb": "delete",

0 commit comments

Comments
 (0)