|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #21412 — the divergent view-container `name` refusal, as ONE judge with two |
| 5 | + * entries: the source registrars' (the key is DERIVED from the binding) and |
| 6 | + * the runtime save door's (the key is the name the row is SAVED under). |
| 7 | + * |
| 8 | + * The boot registrar's and `os validate`'s use of the derived entry stays |
| 9 | + * pinned where it always was (`packages/objectql`'s |
| 10 | + * `view-container-name-refusal.test.ts`, `packages/cli`'s |
| 11 | + * `validate-view-container-name.test.ts`), unedited: their words did not move. |
| 12 | + * The save door's use is pinned at the door, in `packages/metadata-protocol`'s |
| 13 | + * `view-container-runtime-expansion.test.ts`. This file pins the judge's two |
| 14 | + * predicates against each other on the shapes that tell them apart, and the |
| 15 | + * artifact/HMR door's container branch, which lives in this package. |
| 16 | + * |
| 17 | + * Shapes, named as the card's measurement named them (row = the save name): |
| 18 | + * P1 row crm_lead, body `name` lead_views, bound to crm_lead — both refuse; |
| 19 | + * P2 row lead_views, body `name` lead_views, bound to crm_lead — the derived |
| 20 | + * entry refuses, the save door must NOT (#13407 keeps such a container, |
| 21 | + * #21334 expands one under its own name); |
| 22 | + * P3 row lead_views, body `name` crm_lead, bound to crm_lead — the derived |
| 23 | + * entry passes, the save door must refuse; |
| 24 | + * P4 row crm_lead, body `name` lead_views, no other binding — the derived |
| 25 | + * entry passes (the binding falls back to `name`), the save door must |
| 26 | + * refuse. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect, vi } from 'vitest'; |
| 30 | +import { savedViewContainerNameRefusal, viewContainerNameRefusal } from './view-container-name.js'; |
| 31 | +import { MetadataPlugin } from './plugin.js'; |
| 32 | + |
| 33 | +const PKG = 'com.acme.crm'; |
| 34 | + |
| 35 | +const listArm = { label: 'All Leads', type: 'grid', columns: [{ field: 'name' }] }; |
| 36 | +/** Bound to crm_lead through its own `object`, with no `data` on any arm. */ |
| 37 | +const boundBody = (name?: string) => ({ ...(name === undefined ? {} : { name }), object: 'crm_lead', list: listArm }); |
| 38 | +/** No binding but its own `name`. */ |
| 39 | +const nameOnlyBody = (name: string) => ({ name, list: listArm }); |
| 40 | + |
| 41 | +/** The minimum a rejection pin asserts: the ADR-0112 envelope, not "it threw". */ |
| 42 | +function expectEnvelope(refusal: unknown): void { |
| 43 | + expect(refusal).toBeInstanceOf(Error); |
| 44 | + expect((refusal as any).code).toBe('VALIDATION_ERROR'); |
| 45 | + expect((refusal as any).status).toBe(400); |
| 46 | + expect((refusal as any).httpStatus).toBe(400); |
| 47 | +} |
| 48 | + |
| 49 | +describe('#21412 — the save door\'s entry judges the name the row is saved under', () => { |
| 50 | + it('P1: a body `name` that is neither the row nor the binding is refused, naming both values', () => { |
| 51 | + const refusal = savedViewContainerNameRefusal(boundBody('lead_views'), 'crm_lead'); |
| 52 | + expectEnvelope(refusal); |
| 53 | + expect(refusal!.message).toContain("'lead_views'"); |
| 54 | + expect(refusal!.message).toContain("'crm_lead'"); |
| 55 | + }); |
| 56 | + |
| 57 | + it('P3: a body `name` equal to the binding but not to the row is refused', () => { |
| 58 | + expectEnvelope(savedViewContainerNameRefusal(boundBody('crm_lead'), 'lead_views')); |
| 59 | + }); |
| 60 | + |
| 61 | + it('P4: a body `name` that is the only binding, but not the row, is refused', () => { |
| 62 | + expectEnvelope(savedViewContainerNameRefusal(nameOnlyBody('lead_views'), 'crm_lead')); |
| 63 | + }); |
| 64 | + |
| 65 | + it('P2: a body `name` equal to the row passes, though the container binds elsewhere', () => { |
| 66 | + expect(savedViewContainerNameRefusal(boundBody('lead_views'), 'lead_views')).toBeUndefined(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('an absent, empty or non-string `name` refuses nothing — the door stamps or the schema judges it', () => { |
| 70 | + expect(savedViewContainerNameRefusal(boundBody(), 'lead_views')).toBeUndefined(); |
| 71 | + expect(savedViewContainerNameRefusal(boundBody(''), 'lead_views')).toBeUndefined(); |
| 72 | + expect(savedViewContainerNameRefusal({ ...boundBody(), name: 7 }, 'lead_views')).toBeUndefined(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('SCOPE: a standalone ViewItem is not judged here — the every-type half is #21470', () => { |
| 76 | + const record = { name: 'crm_lead.other', object: 'crm_lead', viewKind: 'list', list: listArm }; |
| 77 | + expect(savedViewContainerNameRefusal(record, 'crm_lead.mine')).toBeUndefined(); |
| 78 | + expect(savedViewContainerNameRefusal({ name: 'b', label: 'not a container' }, 'a')).toBeUndefined(); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('#21412 — the two entries are one judgement keyed differently, not two rules', () => { |
| 83 | + it('on P1 both entries refuse, in one envelope', () => { |
| 84 | + const derived = viewContainerNameRefusal(boundBody('lead_views'), 'manifest', PKG); |
| 85 | + const saved = savedViewContainerNameRefusal(boundBody('lead_views'), 'crm_lead'); |
| 86 | + expectEnvelope(derived); |
| 87 | + expectEnvelope(saved); |
| 88 | + }); |
| 89 | + |
| 90 | + it('they part exactly where the derived key and the save name part (P2, P3, P4)', () => { |
| 91 | + // P2: derived refuses (the name is not the binding); saved passes. |
| 92 | + expectEnvelope(viewContainerNameRefusal(boundBody('lead_views'), 'manifest', PKG)); |
| 93 | + expect(savedViewContainerNameRefusal(boundBody('lead_views'), 'lead_views')).toBeUndefined(); |
| 94 | + // P3: derived passes (the name IS the binding); saved refuses. |
| 95 | + expect(viewContainerNameRefusal(boundBody('crm_lead'), 'manifest', PKG)).toBeUndefined(); |
| 96 | + expectEnvelope(savedViewContainerNameRefusal(boundBody('crm_lead'), 'lead_views')); |
| 97 | + // P4: derived passes (the binding falls back to the name); saved refuses. |
| 98 | + expect(viewContainerNameRefusal(nameOnlyBody('lead_views'), 'manifest', PKG)).toBeUndefined(); |
| 99 | + expectEnvelope(savedViewContainerNameRefusal(nameOnlyBody('lead_views'), 'crm_lead')); |
| 100 | + }); |
| 101 | + |
| 102 | + it('the derived entry keeps boot\'s precondition: a falsy derived key refuses nothing', () => { |
| 103 | + // `deriveViewContainerObject`'s `??` chain keeps an empty string, and |
| 104 | + // boot warns and skips such an entry instead of refusing it. |
| 105 | + const emptyBinding = { name: 'x', list: { ...listArm, data: { provider: 'object', object: '' } } }; |
| 106 | + expect(viewContainerNameRefusal(emptyBinding, 'manifest', PKG)).toBeUndefined(); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +// --------------------------------------------------------------------------- |
| 111 | +// The artifact/HMR door's container branch, driven as its own #13912 pin |
| 112 | +// drives it (`plugin-artifact-view-container-object.test.ts`). |
| 113 | +// --------------------------------------------------------------------------- |
| 114 | + |
| 115 | +function fakeCtx() { |
| 116 | + return { |
| 117 | + logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() }, |
| 118 | + registerService: vi.fn(), |
| 119 | + getService: vi.fn(() => undefined), |
| 120 | + trigger: vi.fn(), |
| 121 | + } as any; |
| 122 | +} |
| 123 | + |
| 124 | +async function loadThroughArtifactDoor(container: unknown): Promise<{ plugin: any; error: any }> { |
| 125 | + const plugin = new MetadataPlugin({ watch: false, config: { bootstrap: 'lazy' } }) as any; |
| 126 | + const definition = JSON.parse(JSON.stringify({ |
| 127 | + manifest: { id: PKG, name: 'CRM', version: '1.0.0', type: 'app' }, |
| 128 | + views: [container], |
| 129 | + })); |
| 130 | + let error: any = null; |
| 131 | + try { |
| 132 | + await plugin._parseAndRegisterArtifact(fakeCtx(), definition, 'fixture-21412'); |
| 133 | + } catch (e) { |
| 134 | + error = e; |
| 135 | + } |
| 136 | + return { plugin, error }; |
| 137 | +} |
| 138 | + |
| 139 | +describe('#21412 — the artifact/HMR door\'s container branch refuses through the judge', () => { |
| 140 | + it('refuses the probe document with the judge\'s refusal, and files nothing', async () => { |
| 141 | + const { plugin, error } = await loadThroughArtifactDoor(boundBody('lead_views')); |
| 142 | + expectEnvelope(error); |
| 143 | + // THROUGH the judge: the door throws what the derived entry returns |
| 144 | + // for the same document, word for word — not the generic register |
| 145 | + // contract's refusal (#7378 row 1), which this branch no longer reaches. |
| 146 | + expect(error.message).toBe(viewContainerNameRefusal(boundBody('lead_views'), 'artifact', PKG)!.message); |
| 147 | + expect(await plugin.manager.get('view', 'crm_lead')).toBeFalsy(); |
| 148 | + expect(await plugin.manager.get('view', 'lead_views')).toBeFalsy(); |
| 149 | + expect(await plugin.manager.get('view', 'crm_lead.default')).toBeFalsy(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('CONTROL: an agreeing or absent `name` still registers under the derived key', async () => { |
| 153 | + for (const body of [boundBody('crm_lead'), boundBody()]) { |
| 154 | + const { plugin, error } = await loadThroughArtifactDoor(body); |
| 155 | + expect(error).toBeNull(); |
| 156 | + expect(await plugin.manager.get('view', 'crm_lead')).toBeTruthy(); |
| 157 | + expect(await plugin.manager.get('view', 'crm_lead.default')).toBeTruthy(); |
| 158 | + } |
| 159 | + }); |
| 160 | +}); |
0 commit comments