@aestheticfunction/dspack-emit 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/t1.test.ts ADDED
@@ -0,0 +1,369 @@
1
+ /**
2
+ * T1: transparent identity + constrained control donation.
3
+ *
4
+ * The named production failure this exists to fix, measured across three
5
+ * milestones: a fully governed `card > form > form-field > form-item >
6
+ * form-label + input` surface refused solely because `form` was a casualty.
7
+ * Fail-first evidence (captured before any T1 code, /tmp/t1-failfirst.txt):
8
+ *
9
+ * 1. the eval fixture refuses the v3 contract — form family: 6 unresolved;
10
+ * 2. the T1 spellings were refused by the v2 grammar (7 pathed issues);
11
+ * 3. the shipped profile refuses the example: unknown component 'form'.
12
+ *
13
+ * T1's rules, each pinned here:
14
+ * - a transparent plan emits NO instance; children rise; the root may not
15
+ * be transparent;
16
+ * - donation is scoped to its dissolving BOUNDARY (form-item), which is
17
+ * what lets a three-field form donate three labels to three inputs
18
+ * instead of refusing;
19
+ * - exactly one eligible control per boundary — zero and several refuse;
20
+ * - eligibility is declared, never guessed (the plan must declare every
21
+ * donated destination);
22
+ * - the control's own content beats donated context (first-wins below the
23
+ * control's writes);
24
+ * - nothing to donate is not an error: the destination stays absent and
25
+ * gate A3 arbitrates — relocation, never synthesis;
26
+ * - provenance identifies donor and receiver in the ledger.
27
+ */
28
+ import { readFileSync } from "node:fs";
29
+ import { fileURLToPath } from "node:url";
30
+ import { describe, expect, it } from "vitest";
31
+ import { transformFromJson } from "./transform/index.js";
32
+ import { emitSurface, EmitSurfaceError } from "./targets/a2ui/surface.js";
33
+ import { loadProfile, ProfileLoadError } from "./transform/profile-load.js";
34
+ import { ProfileContractError } from "./transform/validate-v2.js";
35
+ import type { DspackDoc, DspackSurface } from "./types.js";
36
+
37
+ const repo = (p: string) => fileURLToPath(new URL(`../${p}`, import.meta.url));
38
+ const contract = JSON.parse(readFileSync(repo("eval/shadcn-v3.dspack.json"), "utf8")) as DspackDoc & {
39
+ examples: Array<{ id: string; surface: DspackSurface }>;
40
+ };
41
+ const fixture = JSON.parse(readFileSync(repo("eval/shadcn-v3.eval.profile.json"), "utf8"));
42
+
43
+ import { SHADCN_V3_T1_SURFACES } from "./transform/shadcn-v2-respelling.js";
44
+
45
+ /** The T1 resolution of the form family — shared with the eval fixture builder. */
46
+ const FORM_T1_SURFACE = SHADCN_V3_T1_SURFACES.form;
47
+
48
+ /**
49
+ * A hermetic T1 profile: ONLY the plans the target failure needs, taken from
50
+ * the eval fixture (card and input are the transplanted shipped plans; input
51
+ * declares `label`, which is what makes it donation-eligible), plus the
52
+ * transparent form. The full fixture deliberately keeps 80 other unresolved
53
+ * decisions and correctly refuses — the eval-overlay measurement covers that
54
+ * side; these tests isolate T1 itself.
55
+ */
56
+ const t1Profile = (mutate?: (doc: Record<string, unknown>) => void) => {
57
+ const base = structuredClone(fixture) as Record<string, unknown> & { components: Array<Record<string, unknown>> };
58
+ const pick = (id: string) => structuredClone(base.components.find((c) => c.dspackId === id)!);
59
+ const form = pick("form");
60
+ form.surface = structuredClone(FORM_T1_SURFACE);
61
+ // The form's contract props are React-side function/state machinery with no
62
+ // declarative projection; a transparent plan declares no catalog surface —
63
+ // the required componentPlan fields stay present and EMPTY.
64
+ delete form.propMap;
65
+ form.structural = {};
66
+ form.required = [];
67
+ const doc: Record<string, unknown> & { components: Array<Record<string, unknown>> } = {
68
+ ...base,
69
+ components: [pick("card"), form, pick("input")],
70
+ "x-scaffold": undefined,
71
+ };
72
+ delete (doc as Record<string, unknown>)["x-scaffold"];
73
+ mutate?.(doc);
74
+ return loadProfile(doc);
75
+ };
76
+
77
+ const expense = contract.examples.find((e) => e.id.includes("expense"))!;
78
+
79
+ describe("T1 grammar and gates", () => {
80
+ it("the T1 spellings load (the fail-first refusal, inverted)", () => {
81
+ const profile = t1Profile();
82
+ expect(profile.language).toBe("v2");
83
+ });
84
+
85
+ it("a transparent plan with structural, propMap, required, routes, or collects refuses", () => {
86
+ const out: string[] = [];
87
+ try {
88
+ t1Profile((doc) => {
89
+ const form = (doc.components as Array<Record<string, unknown>>).find((c) => c.dspackId === "form")!;
90
+ form.structural = { x: { schema: {}, description: "d", synthNote: "s" } };
91
+ });
92
+ } catch (e) {
93
+ out.push(...(e as ProfileLoadError).issues.map((i) => i.message));
94
+ }
95
+ expect(out.join("\n")).toContain("transparent plan declares no catalog surface");
96
+ });
97
+
98
+ it("a donation writing a destination no receiver plan declares refuses at the contract gate", () => {
99
+ expect(() =>
100
+ transformFromJson(contract, {
101
+ profile: t1Profile((doc) => {
102
+ const form = (doc.components as Array<Record<string, unknown>>).find((c) => c.dspackId === "form")!;
103
+ (form.surface as typeof FORM_T1_SURFACE).subs["form-item"] = {
104
+ transparent: { donate: [{ from: "sub(form-label).text", to: "prop:nonexistentDest" }] },
105
+ } as never;
106
+ }),
107
+ }),
108
+ ).toThrowError(ProfileContractError);
109
+ });
110
+
111
+ it("a donation source that is not a declared sub refuses at the contract gate", () => {
112
+ expect(() =>
113
+ transformFromJson(contract, {
114
+ profile: t1Profile((doc) => {
115
+ const form = (doc.components as Array<Record<string, unknown>>).find((c) => c.dspackId === "form")!;
116
+ (form.surface as typeof FORM_T1_SURFACE).subs["form-item"] = {
117
+ transparent: { donate: [{ from: "sub(imaginary-label).text", to: "prop:label" }] },
118
+ } as never;
119
+ }),
120
+ }),
121
+ ).toThrowError(ProfileContractError);
122
+ });
123
+ });
124
+
125
+ /**
126
+ * A measured discovery worth its own frame: ex.expense-report-form is NOT a
127
+ * pure T1 shape. Its first three fields are the named chain (label + input);
128
+ * fields 4-6 are radio-group (T2's territory) and textarea (T5's). T1 alone
129
+ * therefore cannot emit the full example — what it does, and what these tests
130
+ * pin, is (a) emit the NAMED CHAIN completely, and (b) move the full
131
+ * example's first blocker from "unknown component 'form'" to the unresolved
132
+ * T2/T5 controls, exactly the failure-shift the architecture predicted.
133
+ */
134
+ const namedChain = (): DspackSurface => {
135
+ const root = structuredClone(expense.surface.root) as { children: unknown[] };
136
+ root.children = root.children.slice(0, 3); // the three input-controlled fields
137
+ return { ...structuredClone(expense.surface), root } as DspackSurface;
138
+ };
139
+
140
+ describe("T1 emits the named production failure", () => {
141
+ it("the named chain (form > form-field > form-item > form-label + input) emits: wrappers dissolve, labels donate per field", () => {
142
+ const { messages, warnings, fidelity } = emitSurface(namedChain(), contract, { profile: t1Profile() });
143
+ const components = (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } })
144
+ .updateComponents.components;
145
+
146
+ // No wrapper survives: no instance for form / form-field / form-item /
147
+ // form-control, and no invented FormAsAnything component.
148
+ const names = components.map((c) => c.component);
149
+ expect(names.every((n) => !String(n).startsWith("Form"))).toBe(true);
150
+
151
+ // Every input carries the label its field's form-label donated.
152
+ const fields = components.filter((c) => c.component === "TextField");
153
+ expect(fields.length).toBeGreaterThanOrEqual(3);
154
+ const labels = fields.map((f) => f.label);
155
+ expect(labels).toEqual(expect.arrayContaining(["Merchant", "Amount in USD"]));
156
+ expect(labels.every((l) => typeof l === "string" && l.length > 0)).toBe(true);
157
+
158
+ // Descriptions rise as caption Text; the description text survives.
159
+ expect(JSON.stringify(components)).toContain("Use the name printed on the receipt");
160
+
161
+ // Provenance: the ledger names donor and receiver for each donation.
162
+ const donated = fidelity.filter((f) => f.kind === "donated");
163
+ expect(donated.length).toBe(fields.length);
164
+ expect(donated[0].source).toContain("form-label");
165
+ expect(donated[0].destination).toContain("label @");
166
+ expect(donated[0].note).toContain("single eligible control");
167
+
168
+ // form-message drops with its authored reason.
169
+ expect(warnings.some((w) => w.code === "surface-sub-dropped" && w.message.includes("validation state"))).toBe(true);
170
+
171
+ // The transparent root dissolved into the synthesized layout root.
172
+ expect(components[0]).toMatchObject({ id: "root", component: "Column" });
173
+ expect(warnings.some((w) => w.code === "surface-synthesized-wrap" && w.message.includes("transparent root"))).toBe(true);
174
+
175
+ // And the emitted instances validate against the catalog (gate A3).
176
+ const check = transformFromJson(contract, { profile: t1Profile(), surface: { messages } });
177
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(true);
178
+ });
179
+
180
+ it("the FULL example's first blocker shifts from 'unknown form' to the T2/T5 controls", () => {
181
+ // Before T1 (fail-first record): unknown component 'form'. After T1: the
182
+ // wrappers dissolve fine and the refusal names the radio-group field's
183
+ // boundary — the frontier moved to exactly where T2 begins.
184
+ try {
185
+ emitSurface(expense.surface, contract, { profile: t1Profile() });
186
+ expect.unreachable("fields 4-6 need T2/T5; the full example must still refuse");
187
+ } catch (e) {
188
+ expect(e).toBeInstanceOf(EmitSurfaceError);
189
+ expect((e as Error).message).toContain("exactly one eligible control");
190
+ expect((e as Error).message).not.toContain("unknown component 'form'");
191
+ }
192
+ });
193
+
194
+ it("emission is deterministic: two runs produce identical bytes", () => {
195
+ const a = emitSurface(namedChain(), contract, { profile: t1Profile() });
196
+ const b = emitSurface(namedChain(), contract, { profile: t1Profile() });
197
+ expect(JSON.stringify(a.messages)).toBe(JSON.stringify(b.messages));
198
+ expect(JSON.stringify(a.warnings)).toBe(JSON.stringify(b.warnings));
199
+ expect(JSON.stringify(a.fidelity)).toBe(JSON.stringify(b.fidelity));
200
+ });
201
+ });
202
+
203
+ describe("T1 fail-closed boundaries", () => {
204
+ const surfaceOf = (root: unknown): DspackSurface =>
205
+ ({ dspackSurface: "0.1", system: contract.name as string, intent: "data-submission", root }) as DspackSurface;
206
+
207
+ it("zero eligible controls refuses, naming the boundary", () => {
208
+ const s = surfaceOf({
209
+ component: "card",
210
+ children: [
211
+ {
212
+ component: "form",
213
+ children: [
214
+ {
215
+ component: "form-item",
216
+ children: [{ component: "form-label", text: "Orphan label" }],
217
+ },
218
+ ],
219
+ },
220
+ ],
221
+ });
222
+ try {
223
+ emitSurface(s, contract, { profile: t1Profile() });
224
+ expect.unreachable("a donation with no control must refuse");
225
+ } catch (e) {
226
+ expect(e).toBeInstanceOf(EmitSurfaceError);
227
+ expect((e as Error).message).toContain("exactly one eligible control");
228
+ expect((e as Error).message).toContain("found 0");
229
+ }
230
+ });
231
+
232
+ it("several eligible controls refuse — donation never broadcasts", () => {
233
+ const s = surfaceOf({
234
+ component: "card",
235
+ children: [
236
+ {
237
+ component: "form",
238
+ children: [
239
+ {
240
+ component: "form-item",
241
+ children: [
242
+ { component: "form-label", text: "Which one?" },
243
+ { component: "input", props: { type: "text" } },
244
+ { component: "input", props: { type: "email" } },
245
+ ],
246
+ },
247
+ ],
248
+ },
249
+ ],
250
+ });
251
+ try {
252
+ emitSurface(s, contract, { profile: t1Profile() });
253
+ expect.unreachable("two candidate controls must refuse");
254
+ } catch (e) {
255
+ expect((e as Error).message).toContain("found 2");
256
+ expect((e as Error).message).toContain("never broadcasts");
257
+ }
258
+ });
259
+
260
+ it("root-transparency, single survivor: the descendant IS the root (no transport wrap)", () => {
261
+ const s = surfaceOf({
262
+ component: "form",
263
+ children: [
264
+ {
265
+ component: "form-item",
266
+ children: [
267
+ { component: "form-label", text: "Lone" },
268
+ { component: "input", props: { type: "text" } },
269
+ ],
270
+ },
271
+ ],
272
+ });
273
+ const { messages, fidelity } = emitSurface(s, contract, { profile: t1Profile() });
274
+ const components = (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } })
275
+ .updateComponents.components;
276
+ expect(components[0]).toMatchObject({ id: "root", component: "TextField", label: "Lone" });
277
+ expect(components.some((c) => c.component === "Column")).toBe(false);
278
+ expect(fidelity.some((f) => f.note?.includes("single surviving descendant is preserved as the root"))).toBe(true);
279
+ });
280
+
281
+ it("root-transparency, several survivors: the synthesized wrap is structural transport", () => {
282
+ const s = surfaceOf({
283
+ component: "form",
284
+ children: [
285
+ { component: "form-item", children: [{ component: "form-label", text: "A" }, { component: "input", props: { type: "text" } }] },
286
+ { component: "form-item", children: [{ component: "form-label", text: "B" }, { component: "input", props: { type: "email" } }] },
287
+ ],
288
+ });
289
+ const { messages, warnings, fidelity } = emitSurface(s, contract, { profile: t1Profile() });
290
+ const components = (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } })
291
+ .updateComponents.components;
292
+ expect(components[0]).toMatchObject({ id: "root", component: "Column" });
293
+ expect(warnings.some((w) => w.code === "surface-synthesized-wrap" && w.message.includes("transparent root"))).toBe(true);
294
+ expect(fidelity.some((f) => f.kind === "wrapped" && f.note?.includes("structural transport"))).toBe(true);
295
+ });
296
+
297
+ it("root-transparency, nothing survives: refuse — never fabricate", () => {
298
+ const s = surfaceOf({ component: "form", children: [{ component: "form-message" }] });
299
+ try {
300
+ emitSurface(s, contract, { profile: t1Profile() });
301
+ expect.unreachable("an empty transparent root must refuse");
302
+ } catch (e) {
303
+ expect(e).toBeInstanceOf(EmitSurfaceError);
304
+ expect((e as Error).message).toContain("dissolved to nothing");
305
+ }
306
+ });
307
+
308
+ it("the control's own content beats donated context, and the loss is ledgered", () => {
309
+ const s = surfaceOf({
310
+ component: "card",
311
+ children: [
312
+ {
313
+ component: "form",
314
+ children: [
315
+ {
316
+ component: "form-item",
317
+ children: [
318
+ { component: "form-label", text: "Donated label" },
319
+ { component: "input", text: "Own label", props: { type: "text" } },
320
+ ],
321
+ },
322
+ ],
323
+ },
324
+ ],
325
+ });
326
+ const { messages, fidelity } = emitSurface(s, contract, { profile: t1Profile() });
327
+ const field = (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } })
328
+ .updateComponents.components.find((c) => c.component === "TextField")!;
329
+ expect(field.label).toBe("Own label");
330
+ const lost = fidelity.find((f) => f.kind === "dropped" && f.note?.includes("donated value did not land"));
331
+ expect(lost).toBeDefined();
332
+ });
333
+
334
+ it("nothing to donate leaves the destination to gate A3 — relocation, never synthesis", () => {
335
+ const s = surfaceOf({
336
+ component: "card",
337
+ children: [
338
+ {
339
+ component: "form",
340
+ children: [
341
+ {
342
+ component: "form-item",
343
+ children: [{ component: "input", props: { type: "text" } }],
344
+ },
345
+ ],
346
+ },
347
+ ],
348
+ });
349
+ const { messages } = emitSurface(s, contract, { profile: t1Profile() });
350
+ const field = (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } })
351
+ .updateComponents.components.find((c) => c.component === "TextField")!;
352
+ expect(field.label).toBeUndefined();
353
+ // The shipped TextField requires `label`, so A3 refuses the omission.
354
+ const check = transformFromJson(contract, { profile: t1Profile(), surface: { messages } });
355
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(false);
356
+ });
357
+
358
+ it("a transparent plan emits no catalog entry, and coverage says so", () => {
359
+ const out = transformFromJson(contract, { profile: t1Profile() });
360
+ expect((out.catalog.components as Record<string, unknown>)["Form"]).toBeUndefined();
361
+ const row = out.mapping.coverage.find((c) => c.id === "form");
362
+ expect(row?.detail).toContain("transparent");
363
+ // The form family's derived sub-coverage is fully resolved.
364
+ const unresolved = out.mapping.coverage.filter(
365
+ (c) => c.disposition === "unclassified" && c.id.startsWith("form."),
366
+ );
367
+ expect(unresolved).toEqual([]);
368
+ });
369
+ });
package/src/t2.test.ts ADDED
@@ -0,0 +1,222 @@
1
+ /**
2
+ * T2: homogeneous repeated sub-structure → data array (item-mode collection).
3
+ *
4
+ * Fail-first (captured before any code, /tmp/t2-failfirst.txt): the eval
5
+ * ledger carries radio-group-item and select's seven subs among its 80
6
+ * unresolved; the item-mode spelling was refused by the v2 grammar; and
7
+ * ex.notification-preferences refused end-to-end.
8
+ *
9
+ * The measured shapes that scoped this milestone:
10
+ * - `select-item` carries its label as its OWN text — the pure homogeneous
11
+ * shape, collected completely by T2;
12
+ * - `radio-group-item` carries ONLY its id; its label is a SIBLING `label`
13
+ * joined by htmlFor→id — a DECLARED KEY JOIN, which is T3's mechanism.
14
+ * T2 collects what each item itself carries ({value: self.id}) and the
15
+ * sibling labels are dropped WITH RECORD — the measured T3 frontier, not
16
+ * a T2 defect. Position-based pairing is banned by the ratified model.
17
+ * - Neither item declares a `value` prop in the contract (the example
18
+ * encodes value into ids by convention) — a contract-modeling gap noted
19
+ * for the taxonomy trail, not papered over here.
20
+ */
21
+ import { readFileSync } from "node:fs";
22
+ import { fileURLToPath } from "node:url";
23
+ import { describe, expect, it } from "vitest";
24
+ import { transformFromJson } from "./transform/index.js";
25
+ import { emitSurface } from "./targets/a2ui/surface.js";
26
+ import { loadProfile, ProfileLoadError } from "./transform/profile-load.js";
27
+ import type { DspackDoc, DspackSurface } from "./types.js";
28
+
29
+ const repo = (p: string) => fileURLToPath(new URL(`../${p}`, import.meta.url));
30
+ const contract = JSON.parse(readFileSync(repo("eval/shadcn-v3.dspack.json"), "utf8")) as DspackDoc & {
31
+ examples: Array<{ id: string; surface: DspackSurface }>;
32
+ };
33
+ const fixture = JSON.parse(readFileSync(repo("eval/shadcn-v3.eval.profile.json"), "utf8"));
34
+
35
+ const RG_T2_SURFACE = {
36
+ collects: [{ of: ["radio-group-item"], into: "prop:options", item: { value: "self.id" } }],
37
+ };
38
+ const SELECT_T2_SURFACE = {
39
+ collects: [{ of: ["select-item"], into: "prop:options", item: { label: "self.text" } }],
40
+ subs: {
41
+ "select-trigger": { drop: "the trigger renders from the bound value, not authored content" },
42
+ "select-value": { drop: "the visible value is runtime state derived from the selection" },
43
+ "select-label": { drop: "group headings are not carried by the flat options shape" },
44
+ "select-separator": { drop: "visual grouping chrome with no data meaning" },
45
+ },
46
+ };
47
+
48
+ /** Hermetic T2 profile: radio-group + select plans over the eval fixture base. */
49
+ const t2Profile = (mutate?: (doc: Record<string, unknown>) => void) => {
50
+ const base = structuredClone(fixture) as Record<string, unknown> & { components: Array<Record<string, unknown>> };
51
+ const pick = (id: string) => structuredClone(base.components.find((c) => c.dspackId === id)!);
52
+ const radioGroup = {
53
+ a2ui: "RadioGroup",
54
+ dspackId: "radio-group",
55
+ commons: ["ComponentCommon"],
56
+ structural: {
57
+ options: {
58
+ schema: { type: "array", items: { type: "object", properties: { value: { type: "string" }, label: { type: "string" } }, required: ["value"], additionalProperties: false } },
59
+ description: "The selectable options, one record per item.",
60
+ synthNote: "A2UI models repeated options as data on the group; dspack models them as repeated sub-components.",
61
+ },
62
+ },
63
+ propMap: { name: { a2ui: "name", kind: "string" }, defaultValue: { a2ui: "defaultValue", kind: "string" } },
64
+ required: ["options"],
65
+ surface: structuredClone(RG_T2_SURFACE),
66
+ };
67
+ const select = {
68
+ a2ui: "Select",
69
+ dspackId: "select",
70
+ commons: ["ComponentCommon"],
71
+ structural: {
72
+ options: {
73
+ schema: { type: "array", items: { type: "object", properties: { label: { type: "string" } }, required: ["label"], additionalProperties: false } },
74
+ description: "The selectable options.",
75
+ synthNote: "A2UI models repeated options as data on the control.",
76
+ },
77
+ },
78
+ propMap: { defaultValue: { a2ui: "defaultValue", kind: "string" } },
79
+ required: ["options"],
80
+ surface: structuredClone(SELECT_T2_SURFACE),
81
+ };
82
+ const doc: Record<string, unknown> & { components: Array<Record<string, unknown>> } = {
83
+ ...base,
84
+ components: [pick("card"), radioGroup, select],
85
+ };
86
+ delete (doc as Record<string, unknown>)["x-scaffold"];
87
+ mutate?.(doc);
88
+ return loadProfile(doc);
89
+ };
90
+
91
+ const surfaceOf = (root: unknown): DspackSurface =>
92
+ ({ dspackSurface: "0.1", system: contract.name as string, intent: "preference-settings", root }) as DspackSurface;
93
+
94
+ /** The production notification-preferences radio-group, verbatim shape. */
95
+ const RG_SURFACE = {
96
+ component: "radio-group",
97
+ props: { name: "digest", defaultValue: "monday" },
98
+ children: [
99
+ { component: "radio-group-item", id: "digest-monday" },
100
+ { component: "label", props: { htmlFor: "digest-monday" }, text: "Monday morning" },
101
+ { component: "radio-group-item", id: "digest-friday" },
102
+ { component: "label", props: { htmlFor: "digest-friday" }, text: "Friday afternoon" },
103
+ ],
104
+ };
105
+
106
+ describe("T2 grammar and gates", () => {
107
+ it("the item-mode spelling loads (the fail-first refusal, inverted)", () => {
108
+ expect(t2Profile().language).toBe("v2");
109
+ });
110
+
111
+ it("mixing item-mode and table-mode keys refuses", () => {
112
+ expect(() =>
113
+ t2Profile((doc) => {
114
+ const rg = (doc.components as Array<Record<string, unknown>>).find((c) => c.dspackId === "radio-group")!;
115
+ rg.surface = { collects: [{ of: ["radio-group-item"], into: "prop:options", item: { value: "self.id" }, shape: "flat", row: ["x"], cells: ["y"] }] };
116
+ }),
117
+ ).toThrowError(ProfileLoadError);
118
+ });
119
+
120
+ it("item fields are item-local: a sub() read refuses at the item field's path", () => {
121
+ // The schema's closed item-selector pattern fires first (self.text |
122
+ // self.id | self.props.<prop>); the parse layer carries the same rule
123
+ // with a T3-naming message as defense-in-depth behind it.
124
+ try {
125
+ t2Profile((doc) => {
126
+ const rg = (doc.components as Array<Record<string, unknown>>).find((c) => c.dspackId === "radio-group")!;
127
+ rg.surface = { collects: [{ of: ["radio-group-item"], into: "prop:options", item: { label: "sub(label).text" } }] };
128
+ });
129
+ expect.unreachable("a join read must refuse");
130
+ } catch (e) {
131
+ const issues = (e as ProfileLoadError).issues;
132
+ expect(issues.some((i) => i.path.includes("/item/label") || i.path.includes("collects"))).toBe(true);
133
+ expect(issues.map((i) => i.message).join("\n")).toContain("pattern");
134
+ }
135
+ });
136
+ });
137
+
138
+ describe("T2 collects the measured shapes", () => {
139
+ it("select: the pure homogeneous shape collects completely", () => {
140
+ const s = surfaceOf({
141
+ component: "select",
142
+ props: { defaultValue: "all" },
143
+ children: [
144
+ {
145
+ component: "select-content",
146
+ children: [
147
+ { component: "select-item", text: "All roles" },
148
+ { component: "select-item", text: "Admins" },
149
+ { component: "select-item", text: "Editors" },
150
+ ],
151
+ },
152
+ ],
153
+ });
154
+ const { messages } = emitSurface(s, contract, { profile: t2Profile() });
155
+ const [sel] = (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } })
156
+ .updateComponents.components;
157
+ expect(sel.component).toBe("Select");
158
+ expect(sel.options).toEqual([{ label: "All roles" }, { label: "Admins" }, { label: "Editors" }]);
159
+ const check = transformFromJson(contract, { profile: t2Profile(), surface: { messages } });
160
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(true);
161
+ });
162
+
163
+ it("radio-group: items collect what they carry; sibling labels are the recorded T3 frontier", () => {
164
+ const { messages, warnings, fidelity } = emitSurface(surfaceOf(RG_SURFACE), contract, { profile: t2Profile() });
165
+ const [rg] = (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } })
166
+ .updateComponents.components;
167
+ expect(rg.component).toBe("RadioGroup");
168
+ expect(rg.name).toBe("digest");
169
+ expect(rg.options).toEqual([{ value: "digest-monday" }, { value: "digest-friday" }]);
170
+
171
+ // The htmlFor-joined sibling labels are dropped WITH RECORD, naming T3.
172
+ const labelDrops = warnings.filter((w) => w.code === "surface-sub-dropped" && w.message.includes("'label'"));
173
+ expect(labelDrops).toHaveLength(2);
174
+ expect(labelDrops[0].message).toContain("T3");
175
+ expect(fidelity.filter((f) => f.kind === "dropped" && f.source.includes("label"))).toHaveLength(2);
176
+
177
+ // Each collected record is ledgered with its source item.
178
+ const collected = fidelity.filter((f) => f.kind === "moved" && f.note?.includes("item record"));
179
+ expect(collected).toHaveLength(2);
180
+ expect(collected[0].source).toContain("radio-group-item");
181
+
182
+ const check = transformFromJson(contract, { profile: t2Profile(), surface: { messages } });
183
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(true);
184
+ });
185
+
186
+ it("emission is deterministic across runs", () => {
187
+ const a = emitSurface(surfaceOf(RG_SURFACE), contract, { profile: t2Profile() });
188
+ const b = emitSurface(surfaceOf(RG_SURFACE), contract, { profile: t2Profile() });
189
+ expect(JSON.stringify([a.messages, a.warnings, a.fidelity])).toBe(JSON.stringify([b.messages, b.warnings, b.fidelity]));
190
+ });
191
+
192
+ it("an item missing its field omits it, and A3 arbitrates — never synthesis", () => {
193
+ const s = surfaceOf({
194
+ component: "radio-group",
195
+ children: [{ component: "radio-group-item" }], // no id at all
196
+ });
197
+ const { messages } = emitSurface(s, contract, { profile: t2Profile() });
198
+ const [rg] = (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } })
199
+ .updateComponents.components;
200
+ expect(rg.options).toEqual([{}]);
201
+ // The record schema requires `value`, so A3 refuses the emission.
202
+ const check = transformFromJson(contract, { profile: t2Profile(), surface: { messages } });
203
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(false);
204
+ });
205
+
206
+ it("derived coverage resolves both families through the collects", () => {
207
+ const out = (() => {
208
+ try {
209
+ return transformFromJson(contract, { profile: t2Profile() });
210
+ } catch {
211
+ return undefined;
212
+ }
213
+ })();
214
+ // The hermetic profile passes the contract gate (card + both T2 plans).
215
+ expect(out).toBeDefined();
216
+ const unresolved = out!.mapping.coverage.filter(
217
+ (c) => c.disposition === "unclassified" && (c.id.startsWith("radio-group.") || c.id.startsWith("select.")),
218
+ );
219
+ expect(unresolved).toEqual([]);
220
+ expect(out!.mapping.coverage.find((c) => c.id === "radio-group.radio-group-item")?.detail).toContain("collected");
221
+ });
222
+ });