Skip to content

Commit efbca75

Browse files
committed
test(metadata-protocol): pin the shapes of the five overlay types that cannot carry the override declaration
Also counts a union's refusal only when every branch still diagnosing the body names the key, so a view body claiming no member is not answered true. Claude-Session: https://claude.ai/code/session_01Utp6CbC5BpV41NMLrJMt8Q Co-authored-by: Claude <noreply@anthropic.com>
1 parent ef126bd commit efbca75

2 files changed

Lines changed: 91 additions & 11 deletions

File tree

‎packages/metadata-protocol/src/protocol-packaged-base-refusal.ts‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,15 @@ export function overlayPairFixAdmits(
443443
* so a member that gains or loses the key is answered by its own schema.
444444
*
445445
* Only the refusal of the KEY is read: an `unrecognized_keys` issue naming
446-
* `overrides` at the body's root, or at the root inside a union's branch
447-
* issues (the view union keeps the branch the body claims and mutes the
448-
* others, `focusClaimedBranch`). Every other finding on the body is the save
449-
* door's to answer, as before, and answers `false` here. The body's own
450-
* `overrides` value, if any, is replaced by a well-formed probe: the key
451-
* alone decides.
446+
* `overrides` at the body's root. Inside a union it must be named by EVERY
447+
* branch still diagnosing the body — the view union keeps the branch the body
448+
* claims and mutes the others to a kind mismatch at the root
449+
* (`focusClaimedBranch`), which is dropped here as every consumer of a failed
450+
* union drops it — so a body that claims no branch, refused by the container
451+
* member and by a member that carries the key, is not answered `true`. Every
452+
* other finding on the body is the save door's to answer, as before, and
453+
* answers `false` here. The body's own `overrides` value, if any, is replaced
454+
* by a well-formed probe: the key alone decides.
452455
*/
453456
export function overrideDeclarationRefusedByShape(type: string, body: Record<string, unknown>): boolean {
454457
const schema = resolveOverlaySchema(type, body);
@@ -460,15 +463,25 @@ export function overrideDeclarationRefusedByShape(type: string, body: Record<str
460463
/** A well-formed declaration: the probe asks whether the KEY is carried, so its value raises nothing. */
461464
const OVERRIDES_KEY_PROBE = Object.freeze({ package: 'com.example.base', basedOn: '1.0.0' });
462465

463-
/** An `unrecognized_keys` issue naming `overrides` at the parsed body's root, here or in a union branch's issues. */
466+
type ParseIssue = { code?: unknown; path?: unknown; keys?: unknown; errors?: unknown } | null;
467+
468+
/** An issue at the parsed value's root: a `path` of length 0. */
469+
const atRoot = (issue: ParseIssue): boolean => Array.isArray(issue?.path) && issue.path.length === 0;
470+
471+
/**
472+
* An `unrecognized_keys` issue naming `overrides` at the parsed body's root;
473+
* for a union, named by every branch whose issues are not a bare kind
474+
* mismatch at the root (a muted branch, or a member of another kind).
475+
*/
464476
function namesOverridesAtRoot(issue: unknown): boolean {
465-
const candidate = issue as { code?: unknown; path?: unknown; keys?: unknown; errors?: unknown } | null;
477+
const candidate = issue as ParseIssue;
466478
if (candidate?.code === 'unrecognized_keys') {
467-
return Array.isArray(candidate.path) && candidate.path.length === 0
468-
&& Array.isArray(candidate.keys) && candidate.keys.includes('overrides');
479+
return atRoot(candidate) && Array.isArray(candidate.keys) && candidate.keys.includes('overrides');
469480
}
470481
if (candidate?.code === 'invalid_union' && Array.isArray(candidate.errors)) {
471-
return candidate.errors.some((branch) => Array.isArray(branch) && branch.some(namesOverridesAtRoot));
482+
const diagnosing = candidate.errors.filter((branch): branch is unknown[] => Array.isArray(branch)
483+
&& !branch.every((i) => (i as ParseIssue)?.code === 'invalid_type' && atRoot(i as ParseIssue)));
484+
return diagnosing.length > 0 && diagnosing.every((branch) => branch.some(namesOverridesAtRoot));
472485
}
473486
return false;
474487
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* [#30261, ADR-0146 D2, D11] `overrideDeclarationRefusedByShape` — whether the
5+
* schema the save door judges a body by refuses an `overrides` key on it, so
6+
* that no save declaring the override can land.
7+
*
8+
* The census of `os migrate packageless-overlays` lists such a row
9+
* `cannot_declare` (never `movable`), and the two overlay-write closes leave
10+
* its write open (`overlayPairFixAdmits`). Pinned over the five overlay types
11+
* the ceremony moves, one well-formed body per shape: the view CONTAINER is the
12+
* one shape that answers `true`, in each of its spellings (`list`,
13+
* `listViews`, `form`, `formViews`); every other shape spreads
14+
* `OverrideDeclarationFields` and answers `false`. So does a `view` body that
15+
* claims no member of the union (refused by the container member and by a
16+
* member that carries the key alike), and a type the door validates with no
17+
* schema.
18+
*/
19+
import { describe, expect, it } from 'vitest';
20+
import { DEFAULT_METADATA_TYPE_REGISTRY } from '@objectstack/spec/kernel';
21+
import { overrideDeclarationRefusedByShape } from './protocol-packaged-base-refusal.js';
22+
23+
const grid = (columns: readonly string[]) => ({
24+
type: 'grid', data: { provider: 'object', object: 'task' }, columns: columns.map((field) => ({ field })),
25+
});
26+
27+
/** One well-formed body per shape of the five overlay types, and whether its shape refuses the key. */
28+
const SHAPES: ReadonlyArray<{ shape: string; type: string; body: Record<string, unknown>; refused: boolean }> = [
29+
{ shape: 'view container (list)', type: 'view', refused: true, body: { object: 'task', list: { label: 'All', ...grid(['title']) } } },
30+
{ shape: 'view container (listViews)', type: 'view', refused: true, body: { object: 'task', listViews: { mine: { label: 'Mine', ...grid(['title']) } } } },
31+
{ shape: 'view container (form)', type: 'view', refused: true, body: { object: 'task', form: { type: 'simple', fields: ['title'] } } },
32+
{ shape: 'view container (formViews)', type: 'view', refused: true, body: { object: 'task', formViews: { quick: { type: 'simple', fields: ['title'] } } } },
33+
{ shape: 'single view', type: 'view', refused: false, body: { name: 'task.all', object: 'task', viewKind: 'list', label: 'All', config: grid(['title']) } },
34+
{ shape: 'list overlay', type: 'view', refused: false, body: { name: 'task.all', label: 'All', object: 'task', viewKind: 'list', columns: [{ field: 'title', label: 'Title' }] } },
35+
{ shape: 'dashboard', type: 'dashboard', refused: false, body: { name: 'crm_board', label: 'Board', widgets: [] } },
36+
{ shape: 'report', type: 'report', refused: false, body: { name: 'crm_report', label: 'Report', type: 'summary', dataset: 'crm_metrics', rows: ['month'], values: ['amount_sum'] } },
37+
{ shape: 'translation', type: 'translation', refused: false, body: { name: 'crm_fr', label: 'French', locale: 'fr', messages: { save: 'Enregistrer' } } },
38+
{ shape: 'email template', type: 'email_template', refused: false, body: { name: 'crm_welcome', label: 'Welcome', subject: 'Welcome', bodyHtml: '<p>Hi</p>' } },
39+
];
40+
41+
describe('[#30261] overrideDeclarationRefusedByShape: the shapes of the five overlay types that cannot carry the declaration', () => {
42+
it('the fixture covers every overlay type the ceremony moves', () => {
43+
const overlayTypes = DEFAULT_METADATA_TYPE_REGISTRY.filter((entry) => entry.allowEnvironmentOverlay).map((entry) => entry.type).sort();
44+
expect([...new Set(SHAPES.map((s) => s.type))].sort()).toEqual(overlayTypes);
45+
});
46+
47+
for (const { shape, type, body, refused } of SHAPES) {
48+
it(`${shape}: ${refused ? 'refuses' : 'carries'} the \`overrides\` key`, () => {
49+
expect(overrideDeclarationRefusedByShape(type, body)).toBe(refused);
50+
// The answer is the key's, not the body's value: a body already
51+
// carrying a declaration is answered the same way.
52+
expect(overrideDeclarationRefusedByShape(type, { ...body, overrides: { package: 'com.acme.crm' } })).toBe(refused);
53+
});
54+
}
55+
56+
it('a view body that claims no member of the union is not answered `true` (the container member alone names the key)', () => {
57+
expect(overrideDeclarationRefusedByShape('view', { name: 'task.all', label: 'All' })).toBe(false);
58+
});
59+
60+
it('another finding on a container body does not hide the key: a container with an unknown key of its own still answers `true`', () => {
61+
expect(overrideDeclarationRefusedByShape('view', { object: 'task', list: { label: 'All', ...grid(['title']) }, colour: 'red' })).toBe(true);
62+
});
63+
64+
it('a type the save door validates with no schema answers `false`', () => {
65+
expect(overrideDeclarationRefusedByShape('function', { name: 'f' })).toBe(false);
66+
});
67+
});

0 commit comments

Comments
 (0)