|
| 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 | +}); |
0 commit comments