@aestheticfunction/dspack-emit 0.4.1 → 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.
Files changed (72) hide show
  1. package/README.md +16 -6
  2. package/dist/cli.js +56 -0
  3. package/dist/index.d.ts +4 -1
  4. package/dist/index.js +3 -1
  5. package/dist/targets/a2ui/diagnostics.d.ts +110 -0
  6. package/dist/targets/a2ui/diagnostics.js +94 -0
  7. package/dist/targets/a2ui/surface.d.ts +28 -9
  8. package/dist/targets/a2ui/surface.js +1004 -222
  9. package/dist/transform/desugar.d.ts +32 -0
  10. package/dist/transform/desugar.js +156 -0
  11. package/dist/transform/emit.js +59 -0
  12. package/dist/transform/index.js +4 -0
  13. package/dist/transform/mapping.d.ts +0 -6
  14. package/dist/transform/mapping.js +66 -2
  15. package/dist/transform/model.d.ts +368 -0
  16. package/dist/transform/model.js +169 -0
  17. package/dist/transform/parse-v2.d.ts +109 -0
  18. package/dist/transform/parse-v2.js +479 -0
  19. package/dist/transform/profile-load.d.ts +7 -6
  20. package/dist/transform/profile-load.js +99 -20
  21. package/dist/transform/profile-schema-v2.d.ts +9 -0
  22. package/dist/transform/profile-schema-v2.js +608 -0
  23. package/dist/transform/profiles.d.ts +31 -1
  24. package/dist/transform/scaffold.d.ts +29 -12
  25. package/dist/transform/scaffold.js +91 -31
  26. package/dist/transform/shadcn-v2-respelling.d.ts +77 -0
  27. package/dist/transform/shadcn-v2-respelling.js +456 -0
  28. package/dist/transform/validate-v2.d.ts +36 -0
  29. package/dist/transform/validate-v2.js +136 -0
  30. package/dist/types.d.ts +23 -0
  31. package/dist/validate/ajv.js +96 -25
  32. package/dist/validate/meta/a2ui-catalog.meta.0_9_1.json +39 -0
  33. package/dist/validate/meta/a2ui-catalog.meta.1_0.json +34 -0
  34. package/package.json +2 -2
  35. package/src/__snapshots__/byte-neutral.test.ts.snap +66 -0
  36. package/src/a0.test.ts +235 -0
  37. package/src/byte-neutral.test.ts +246 -0
  38. package/src/cli.ts +60 -0
  39. package/src/dangling-ref.test.ts +118 -0
  40. package/src/desugar.test.ts +162 -0
  41. package/src/engine-boundary.test.ts +248 -0
  42. package/src/eval-fixture.test.ts +102 -0
  43. package/src/functions.test.ts +182 -0
  44. package/src/index.ts +4 -1
  45. package/src/layered-dissolution.test.ts +139 -0
  46. package/src/profile-load.test.ts +99 -1
  47. package/src/profile-v2.test.ts +369 -0
  48. package/src/review-findings.test.ts +292 -0
  49. package/src/schema-mirrors.test.ts +5 -0
  50. package/src/surface-fidelity.test.ts +213 -0
  51. package/src/t1.test.ts +369 -0
  52. package/src/t2.test.ts +222 -0
  53. package/src/t3.test.ts +304 -0
  54. package/src/t4.test.ts +278 -0
  55. package/src/targets/a2ui/diagnostics.ts +136 -0
  56. package/src/targets/a2ui/surface.ts +1468 -235
  57. package/src/transform/desugar.ts +194 -0
  58. package/src/transform/emit.ts +65 -0
  59. package/src/transform/index.ts +5 -0
  60. package/src/transform/mapping.ts +60 -2
  61. package/src/transform/model.ts +427 -0
  62. package/src/transform/parse-v2.ts +608 -0
  63. package/src/transform/profile-load.ts +104 -22
  64. package/src/transform/profile-schema-v2.ts +608 -0
  65. package/src/transform/profile.v2.schema.json +600 -0
  66. package/src/transform/profiles.ts +32 -1
  67. package/src/transform/scaffold.ts +124 -47
  68. package/src/transform/shadcn-v2-respelling.ts +475 -0
  69. package/src/transform/validate-v2.ts +174 -0
  70. package/src/types.ts +32 -0
  71. package/src/validate/ajv.ts +89 -26
  72. package/src/validate-v2.test.ts +173 -0
@@ -0,0 +1,139 @@
1
+ /**
2
+ * Layered dissolution contexts (emit#37, ratified 2026-08-07).
3
+ *
4
+ * A dissolving boundary shadows the enclosing compound contexts where they
5
+ * overlap, but does not erase them: dispositions remain available through
6
+ * the ancestor chain. This closes the seam the post-T4 Build matrix measured
7
+ * (eval/t4-build-matrix.json, preference-settings) — an outer compound's sub
8
+ * inside a transparent inner boundary.
9
+ *
10
+ * Fail-first (recorded on main d3aab9f, before the change): the shape below
11
+ * refused at emit with
12
+ *
13
+ * unknown component 'card-footer': not a mapped component of the '…' profile
14
+ *
15
+ * because T1 dissolution governed the inner boundary's subtree by ITS OWN
16
+ * model alone, so `card-footer` (a sub of the OUTER `card`) found no
17
+ * disposition in the inner `field` model and fell through to emitNode. The
18
+ * surface was S1/S2/S3-clean — spec v0.4 §5.1 accepts it (a `card` ancestor
19
+ * exists at any depth) — so this was purely an emitter seam.
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 } 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 profileOf = (keep: string[]) => {
36
+ const doc = structuredClone(fixture) as Record<string, unknown> & { components: Array<{ dspackId: string }> };
37
+ const keepSet = new Set(keep);
38
+ (doc as Record<string, unknown>).intentionallyOmitted = doc.components.filter((c) => !keepSet.has(c.dspackId)).map((c) => c.dspackId).sort();
39
+ doc.components = doc.components.filter((c) => keepSet.has(c.dspackId));
40
+ delete (doc as Record<string, unknown>)["x-scaffold"];
41
+ return loadProfile(doc);
42
+ };
43
+
44
+ const surfaceOf = (root: unknown, intent = "preference-settings"): DspackSurface =>
45
+ ({ dspackSurface: "0.1", system: contract.name as string, intent, root }) as DspackSurface;
46
+
47
+ const instancesOf = (messages: unknown[]) =>
48
+ (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } }).updateComponents.components;
49
+
50
+ describe("layered dissolution: an outer compound's sub survives inside a dissolved inner boundary", () => {
51
+ const profile = profileOf(["card", "field", "field-set", "field-group", "switch", "button", "badge"]);
52
+
53
+ it("card-footer inside a transparent field resolves against card's model and its button rises", () => {
54
+ const { messages } = emitSurface(
55
+ surfaceOf({
56
+ component: "card",
57
+ children: [
58
+ { component: "card-header", children: [{ component: "card-title", text: "Preferences" }] },
59
+ {
60
+ component: "card-content",
61
+ children: [
62
+ {
63
+ component: "field",
64
+ children: [
65
+ { component: "field-label", text: "Weekly digest" },
66
+ { component: "switch", props: { name: "digest" } },
67
+ { component: "card-footer", children: [{ component: "button", text: "Save" }] },
68
+ ],
69
+ },
70
+ ],
71
+ },
72
+ ],
73
+ }),
74
+ contract,
75
+ { profile },
76
+ );
77
+ const comps = instancesOf(messages);
78
+ // No wrapper instance survives for any dissolved boundary.
79
+ for (const gone of ["Card_footer", "Field", "CardFooter", "CardHeader"]) {
80
+ expect(comps.some((c) => c.component === gone)).toBe(false);
81
+ }
82
+ // The field donated its label onto the switch (T1 still works through the layering).
83
+ const sw = comps.find((c) => c.component === "Switch")!;
84
+ expect(sw.label).toBe("Weekly digest");
85
+ // card-footer dissolved (card's disposition, reached through the ancestor
86
+ // chain) and its button rose as a real instance.
87
+ const button = comps.find((c) => c.component === "Button")!;
88
+ expect(button.child).toBeDefined();
89
+ // The whole thing is emittable and A3-valid.
90
+ const check = transformFromJson(contract, { profile, surface: { messages } });
91
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(true);
92
+ });
93
+
94
+ it("the inner boundary's own dispositions resolve first (innermost-first walk)", () => {
95
+ // Genuine same-id overlap between two frames is structurally impossible —
96
+ // sub-component ids are unique document-wide (enforced by S2), so the
97
+ // ratified "inner shadows outer" is defined but unreachable for real
98
+ // contracts. What IS observable is that the inner frame is consulted
99
+ // first: field-title inside a dissolved field takes FIELD's asText (h4),
100
+ // never a fallthrough to some outer default.
101
+ const { messages } = emitSurface(
102
+ surfaceOf({
103
+ component: "card",
104
+ children: [
105
+ {
106
+ component: "card-content",
107
+ children: [
108
+ {
109
+ component: "field",
110
+ children: [
111
+ { component: "field-title", text: "Section" },
112
+ { component: "switch", props: { name: "x" } },
113
+ ],
114
+ },
115
+ ],
116
+ },
117
+ ],
118
+ }),
119
+ contract,
120
+ { profile },
121
+ );
122
+ const comps = instancesOf(messages);
123
+ const text = comps.find((c) => c.component === "Text" && c.text === "Section")!;
124
+ expect(text).toBeDefined();
125
+ expect(text.variant).toBe("h4"); // field's asText, not card's h3
126
+ });
127
+
128
+ it("determinism holds across the layered walk", () => {
129
+ const root = {
130
+ component: "card",
131
+ children: [
132
+ { component: "card-content", children: [{ component: "field", children: [{ component: "field-label", text: "L" }, { component: "switch" }, { component: "card-footer", children: [{ component: "button", text: "Go" }] }] }] },
133
+ ],
134
+ };
135
+ const a = emitSurface(surfaceOf(root), contract, { profile });
136
+ const b = emitSurface(surfaceOf(root), contract, { profile });
137
+ expect(JSON.stringify([a.messages, a.warnings, a.fidelity])).toBe(JSON.stringify([b.messages, b.warnings, b.fidelity]));
138
+ });
139
+ });
@@ -103,12 +103,75 @@ describe("casualty refusal cites the authored reason", () => {
103
103
  });
104
104
  });
105
105
 
106
+ describe("a declared casualty cannot be consumed into a parent", () => {
107
+ /**
108
+ * The plan-lookup gate refuses a casualty emitted as its OWN node. A
109
+ * consuming strategy (subText / subButtonText / subTable / subFlatten)
110
+ * walks the subtree directly, so without an equivalent gate an author
111
+ * could fold a casualty's text into a parent prop and the authored
112
+ * refusal would vanish behind a warning. Consumption is how compounds
113
+ * carry their parts — never an escape hatch around "cannot represent".
114
+ */
115
+ it("refuses a casualty folded in by a parent's subText, citing the authored reason", () => {
116
+ const withFold = {
117
+ ...shadcnProfile,
118
+ components: shadcnProfile.components.map((plan) =>
119
+ plan.dspackId === "card"
120
+ ? {
121
+ ...plan,
122
+ surfacePlan: {
123
+ ...(plan.surfacePlan ?? {}),
124
+ subText: { ...(plan.surfacePlan?.subText ?? {}), "dropdown-menu": "title" },
125
+ },
126
+ }
127
+ : plan,
128
+ ),
129
+ };
130
+ const surface: DspackSurface = {
131
+ dspackSurface: "0.1",
132
+ system: shadcnDoc.name as string,
133
+ intent: "record-collection",
134
+ root: {
135
+ component: "card",
136
+ children: [{ component: "dropdown-menu", text: "Row actions" }],
137
+ },
138
+ } as DspackSurface;
139
+ try {
140
+ emitSurface(surface, shadcnDoc, { profile: withFold });
141
+ expect.unreachable("a consumed casualty must refuse, not fold silently");
142
+ } catch (e) {
143
+ expect(e).toBeInstanceOf(EmitSurfaceError);
144
+ const msg = (e as EmitSurfaceError).message;
145
+ expect(msg).toContain("declared casualty");
146
+ expect(msg).toContain("cannot be consumed into");
147
+ expect(msg).toContain("dropdown menu"); // the authored reason survives
148
+ // The refusal points at the casualty itself, not at the parent that
149
+ // would have consumed it — same locating precision as direct emission.
150
+ expect((e as EmitSurfaceError).path).toBe("$.root.children[0]");
151
+ }
152
+ });
153
+
154
+ it("still consumes ordinary sub-components (the gate is casualty-scoped)", () => {
155
+ const surface: DspackSurface = {
156
+ dspackSurface: "0.1",
157
+ system: shadcnDoc.name as string,
158
+ intent: "record-collection",
159
+ root: {
160
+ component: "card",
161
+ children: [{ component: "card-header", children: [{ component: "card-title", text: "Members" }] }],
162
+ },
163
+ } as DspackSurface;
164
+ const result = emitSurface(surface, shadcnDoc, { profile: shadcnProfile });
165
+ expect(JSON.stringify(result.messages)).toContain("Members");
166
+ });
167
+ });
168
+
106
169
  describe("scaffoldProfile", () => {
107
170
  it("produces a loadProfile-valid profile from a bare contract", () => {
108
171
  const { profile } = scaffoldProfile(astryxDoc, {
109
172
  catalogIdBase: "https://example.test/catalogs/astryx-scaffold",
110
173
  });
111
- // The scaffold is itself a valid v1 profile document.
174
+ // The scaffold is itself a valid v2 profile document.
112
175
  const loaded = loadProfile(profile);
113
176
  expect(loaded.catalogIdBase).toBe("https://example.test/catalogs/astryx-scaffold");
114
177
  });
@@ -128,6 +191,41 @@ describe("scaffoldProfile", () => {
128
191
  expect(notes.length).toBeGreaterThan(0);
129
192
  });
130
193
 
194
+ it("a scaffold's unresolved decisions, once resolved, transform — the authoring loop closes", () => {
195
+ const { profile, notes } = scaffoldProfile(shadcnDoc, {
196
+ catalogIdBase: "https://example.test/catalogs/shadcn-scaffold",
197
+ });
198
+ // The scaffold names every decision it refused to make...
199
+ const pending = notes.filter((n) => n.note.startsWith("unresolved sub-component"));
200
+ expect(pending.length).toBeGreaterThanOrEqual(10);
201
+ const xs = (profile["x-scaffold"] as { unresolved: Record<string, string[]> }).unresolved;
202
+ expect(Object.keys(xs)).toEqual(expect.arrayContaining(["card", "table", "alert-dialog"]));
203
+
204
+ // ...and an author resolving ONE compound's subs makes that compound emit.
205
+ const components = profile.components as Array<Record<string, unknown>>;
206
+ const card = components.find((c) => c.dspackId === "card")!;
207
+ card.surface = {
208
+ routes: [{ from: ["children"], to: "slots:children" }],
209
+ subs: {
210
+ "card-header": "transparent",
211
+ "card-content": "transparent",
212
+ "card-footer": "transparent",
213
+ "card-title": { asText: "h3" },
214
+ "card-description": { asText: "caption" },
215
+ },
216
+ };
217
+ // The other compounds stay undecided, so transform still refuses — but no
218
+ // longer about card: its family is resolved and gone from the findings.
219
+ try {
220
+ transformFromJson(shadcnDoc, { profile: loadProfile(profile) });
221
+ expect.unreachable("table and alert-dialog are still undecided");
222
+ } catch (e) {
223
+ const issues = (e as { issues: Array<{ message: string }> }).issues;
224
+ expect(issues.some((i) => i.message.includes("of 'card'"))).toBe(false);
225
+ expect(issues.some((i) => i.message.includes("of 'table'"))).toBe(true);
226
+ }
227
+ });
228
+
131
229
  it("seeds enum props verbatim (identity projection, no valueMap)", () => {
132
230
  const { profile } = scaffoldProfile(shadcnDoc, {
133
231
  catalogIdBase: "https://example.test/catalogs/shadcn-scaffold",
@@ -0,0 +1,369 @@
1
+ /**
2
+ * Profile v2: the primitive language, proven against the v1 byte contract.
3
+ *
4
+ * The keystone assertion: the ENTIRE shadcn profile re-spelled in v2 —
5
+ * Identity / Route / Collect strings instead of the ten v1 directives —
6
+ * loads through the v2 dispatcher and emits BYTE-IDENTICALLY to the v1
7
+ * profile: same catalog digests (both A2UI versions), same messages, same
8
+ * warnings, for every pinned directive surface and worked example. v2 is
9
+ * not a new emitter; it is a new spelling for the same transformation.
10
+ *
11
+ * Also here: the version dispatcher's refusals, and the load-time refusal
12
+ * matrix for the v2 grammar — every way a v2 surface block can be wrong,
13
+ * refused with a pathed finding at load, before any emission runs.
14
+ */
15
+ import { createHash } from "node:crypto";
16
+ import { readFileSync } from "node:fs";
17
+ import { fileURLToPath } from "node:url";
18
+ import { describe, expect, it } from "vitest";
19
+ import { transformFromJson } from "./transform/index.js";
20
+ import { emitSurface } from "./targets/a2ui/surface.js";
21
+ import { loadProfile, ProfileLoadError } from "./transform/profile-load.js";
22
+ import { shadcnProfile, type ComponentPlan } from "./transform/profiles.js";
23
+ import { SHADCN_V2_SURFACES as V2_SURFACES } from "./transform/shadcn-v2-respelling.js";
24
+ import type { A2uiVersion, DspackDoc, DspackSurface } from "./types.js";
25
+
26
+ const repo = (p: string) => fileURLToPath(new URL(`../${p}`, import.meta.url));
27
+ const doc = JSON.parse(readFileSync(repo("input/shadcn-ui.dspack.json"), "utf8")) as DspackDoc;
28
+
29
+ const digest = (value: unknown): string =>
30
+ createHash("sha256").update(JSON.stringify(value)).digest("hex").slice(0, 16);
31
+
32
+ // ---------------------------------------------------------------------------
33
+ // The v2 re-spelling: every v1 directive the shadcn profile uses, as
34
+ // primitives. Hand-authored — this is what a v2 profile author would write.
35
+ // ---------------------------------------------------------------------------
36
+
37
+ /**
38
+ * The v2 document, built from the TS profile so every non-surface field is
39
+ * identical by construction — the only authored difference is the language.
40
+ */
41
+ function shadcnV2Document(): Record<string, unknown> {
42
+ const strip = (plan: ComponentPlan): Record<string, unknown> => {
43
+ const { surfacePlan: _v1, subCoverage: _prose, ...rest } = structuredClone(plan) as Record<string, unknown> & {
44
+ surfacePlan?: unknown;
45
+ subCoverage?: unknown;
46
+ };
47
+ const dspackId = (rest as { dspackId?: string }).dspackId;
48
+ if (dspackId && V2_SURFACES[dspackId]) rest.surface = V2_SURFACES[dspackId];
49
+ return rest;
50
+ };
51
+ const base = structuredClone(shadcnProfile) as unknown as Record<string, unknown>;
52
+ return {
53
+ profileVersion: "2",
54
+ ...base,
55
+ components: shadcnProfile.components.map(strip),
56
+ synthesized: shadcnProfile.synthesized.map(strip),
57
+ };
58
+ }
59
+
60
+ const VERSIONS: A2uiVersion[] = ["0.9.1", "1.0"];
61
+
62
+ /** The v1 pins this file must reproduce — same values as byte-neutral.test.ts. */
63
+ const V1_CATALOG_DIGESTS: Record<string, string> = {
64
+ "0.9.1": "7c1b72dfd83dde14",
65
+ "1.0": "2dde439bbf643834",
66
+ };
67
+
68
+ describe("the v2 re-spelling emits byte-identically to the v1 profile", () => {
69
+ const v2 = () => loadProfile(shadcnV2Document());
70
+
71
+ it("loads through the dispatcher and is stamped as the v2 language", () => {
72
+ const profile = v2();
73
+ expect(profile.language).toBe("v2");
74
+ expect(profile.catalogTitle).toBe(shadcnProfile.catalogTitle);
75
+ });
76
+
77
+ for (const version of VERSIONS) {
78
+ it(`catalog digest equals the pinned v1 digest (a2ui ${version})`, () => {
79
+ const out = transformFromJson(doc, { profile: v2(), a2uiVersion: version });
80
+ expect(digest(out.catalog)).toBe(V1_CATALOG_DIGESTS[version]);
81
+ expect(out.validation.pass).toBe(true);
82
+ });
83
+ }
84
+
85
+ const surface = (root: unknown, intent = "record-collection"): DspackSurface =>
86
+ ({ dspackSurface: "0.1", system: doc.name as string, intent, root }) as DspackSurface;
87
+
88
+ /** Every directive-exercising surface from the byte gate, plus the worked examples. */
89
+ const cases: Array<[string, DspackSurface]> = [
90
+ ["textChild + action (button)", surface({ component: "button", text: "Save changes", props: { variant: "default" } })],
91
+ [
92
+ "self.text routes (input, badge)",
93
+ surface({
94
+ component: "card",
95
+ children: [
96
+ { component: "input", text: "Email" },
97
+ { component: "badge", text: "New", props: { variant: "secondary" } },
98
+ ],
99
+ }),
100
+ ],
101
+ [
102
+ "transparent + asText subs (card family)",
103
+ surface({
104
+ component: "card",
105
+ children: [
106
+ {
107
+ component: "card-header",
108
+ children: [
109
+ { component: "card-title", text: "Members" },
110
+ { component: "card-description", text: "Everyone with access." },
111
+ ],
112
+ },
113
+ { component: "card-content", text: "Three people." },
114
+ ],
115
+ }),
116
+ ],
117
+ [
118
+ "collects (table family)",
119
+ surface({
120
+ component: "table",
121
+ children: [
122
+ { component: "table-caption", text: "Recent orders" },
123
+ { component: "table-header", children: [{ component: "table-row", children: [{ component: "table-head", text: "Order" }, { component: "table-head", text: "Status" }] }] },
124
+ {
125
+ component: "table-body",
126
+ children: [
127
+ { component: "table-row", children: [{ component: "table-cell", text: "#1001" }, { component: "table-cell", text: "Shipped" }] },
128
+ { component: "table-row", children: [{ component: "table-cell", text: "#1002" }, { component: "table-cell", text: "Pending" }] },
129
+ ],
130
+ },
131
+ ],
132
+ }),
133
+ ],
134
+ [
135
+ "consuming routes + label fallback (alert-dialog)",
136
+ surface(
137
+ {
138
+ component: "alert-dialog",
139
+ children: [
140
+ { component: "alert-dialog-trigger", children: [{ component: "button", text: "Delete" }] },
141
+ {
142
+ component: "alert-dialog-content",
143
+ children: [
144
+ { component: "alert-dialog-title", text: "Delete project?" },
145
+ { component: "alert-dialog-description", text: "This cannot be undone." },
146
+ ],
147
+ },
148
+ ],
149
+ },
150
+ "destructive-action",
151
+ ),
152
+ ],
153
+ [
154
+ "wrap synthesis (>1 children in a slot)",
155
+ surface({
156
+ component: "card",
157
+ children: [
158
+ { component: "badge", text: "One" },
159
+ { component: "badge", text: "Two" },
160
+ ],
161
+ }),
162
+ ],
163
+ [
164
+ "id deduplication",
165
+ surface({
166
+ component: "card",
167
+ children: [
168
+ { component: "badge", id: "tag", text: "One" },
169
+ { component: "badge", id: "tag", text: "Two" },
170
+ ],
171
+ }),
172
+ ],
173
+ ...((doc as unknown as { examples?: Array<{ id: string; surface: DspackSurface }> }).examples ?? []).map(
174
+ (e) => [`worked example ${e.id}`, e.surface] as [string, DspackSurface],
175
+ ),
176
+ ];
177
+
178
+ it.each(cases)("%s — identical messages and warnings", (_name, s) => {
179
+ const fromV1 = emitSurface(s, doc, { profile: shadcnProfile });
180
+ const fromV2 = emitSurface(s, doc, { profile: v2() });
181
+ expect(digest(fromV2.messages)).toBe(digest(fromV1.messages));
182
+ expect(digest(fromV2.warnings)).toBe(digest(fromV1.warnings));
183
+ });
184
+ });
185
+
186
+ describe("the version dispatcher", () => {
187
+ const v1Doc = () => ({ profileVersion: "1", ...structuredClone(shadcnProfile) });
188
+
189
+ it("v1 documents keep loading exactly as before", () => {
190
+ const profile = loadProfile(v1Doc());
191
+ expect(profile.language).toBeUndefined();
192
+ });
193
+
194
+ it("an unknown version refuses with a pathed finding naming the supported set", () => {
195
+ for (const bad of ["3", "2.0", 2, undefined, null]) {
196
+ const attempt = { ...v1Doc(), profileVersion: bad } as unknown;
197
+ try {
198
+ loadProfile(attempt);
199
+ expect.unreachable(`version ${JSON.stringify(bad)} must refuse`);
200
+ } catch (e) {
201
+ expect(e).toBeInstanceOf(ProfileLoadError);
202
+ const issue = (e as ProfileLoadError).issues[0];
203
+ expect(issue.path).toBe("/profileVersion");
204
+ expect(issue.message).toContain('"1"');
205
+ expect(issue.message).toContain('"2"');
206
+ }
207
+ }
208
+ });
209
+
210
+ it("one language per document: v1 syntax inside a v2 document refuses", () => {
211
+ const mixed = shadcnV2Document();
212
+ (mixed.components as Array<Record<string, unknown>>)[0].surfacePlan = { textProp: "child" };
213
+ expect(() => loadProfile(mixed)).toThrowError(ProfileLoadError);
214
+ });
215
+
216
+ it("one language per document: v2 syntax inside a v1 document refuses", () => {
217
+ const mixed = v1Doc() as unknown as { components: Array<Record<string, unknown>> };
218
+ mixed.components[0].surface = { routes: [] };
219
+ expect(() => loadProfile(mixed)).toThrowError(ProfileLoadError);
220
+ });
221
+ });
222
+
223
+ describe("load-time refusal matrix (v2 grammar)", () => {
224
+ /** A minimal valid v2 document with one plan whose surface block is under test. */
225
+ const docWith = (surface: unknown, plan: Partial<ComponentPlan> = {}): Record<string, unknown> => ({
226
+ profileVersion: "2",
227
+ catalogTitle: "t",
228
+ catalogDescription: "d",
229
+ catalogIdBase: "https://example.test/catalogs/x",
230
+ instructions: "",
231
+ primaryColorToken: { category: "color", name: "primary" },
232
+ surfaceSynthesis: { textComponent: "Text", textProp: "text", wrapComponent: "Column", wrapChildrenProp: "children" },
233
+ synthesized: [],
234
+ casualtyComponents: [],
235
+ components: [
236
+ {
237
+ a2ui: "Widget",
238
+ dspackId: "widget",
239
+ commons: ["ComponentCommon"],
240
+ structural: {
241
+ label: { schema: { type: "string" }, description: "d", synthNote: "s" },
242
+ child: { schema: { type: "string" }, description: "d", synthNote: "s" },
243
+ items: { schema: { type: "array" }, description: "d", synthNote: "s" },
244
+ go: { schema: { $ref: "#/$defs/Action" }, description: "d", synthNote: "s" },
245
+ },
246
+ required: [],
247
+ surface,
248
+ ...plan,
249
+ },
250
+ ],
251
+ });
252
+
253
+ const refusal = (surface: unknown, plan?: Partial<ComponentPlan>): string => {
254
+ try {
255
+ loadProfile(docWith(surface, plan));
256
+ return "LOADED";
257
+ } catch (e) {
258
+ if (e instanceof ProfileLoadError) return e.issues.map((i) => `${i.path}: ${i.message}`).join("\n");
259
+ throw e;
260
+ }
261
+ };
262
+
263
+ it("a well-formed block loads", () => {
264
+ expect(
265
+ refusal({
266
+ routes: [
267
+ { from: ["self.text"], to: "prop:label" },
268
+ { from: ["children"], to: "slot:child" },
269
+ ],
270
+ }),
271
+ ).toBe("LOADED");
272
+ });
273
+
274
+ it("an unknown selector refuses at its exact path", () => {
275
+ const out = refusal({ routes: [{ from: ["self.magic"], to: "prop:label" }] });
276
+ expect(out).toContain("/routes/0/from/0");
277
+ expect(out).toContain("pattern");
278
+ });
279
+
280
+ it("an arbitrary JSON path is not a selector", () => {
281
+ expect(refusal({ routes: [{ from: ["$.children[0].text"], to: "prop:label" }] })).toContain("/from/0");
282
+ });
283
+
284
+ it("a computed destination is refused by the grammar", () => {
285
+ expect(refusal({ routes: [{ from: ["self.text"], to: "prop:${name}" }] })).toContain("/to");
286
+ });
287
+
288
+ it("an undeclared destination refuses: inventing properties dies here, not at A3", () => {
289
+ const out = refusal({ routes: [{ from: ["self.text"], to: "prop:invented" }] });
290
+ expect(out).toContain("not declared by this plan");
291
+ });
292
+
293
+ it("selector/destination incompatibility refuses (action fed by text)", () => {
294
+ const out = refusal({ routes: [{ from: ["self.text"], to: "action:go" }] });
295
+ expect(out).toContain("cannot feed destination kind 'action'");
296
+ });
297
+
298
+ it("a fallback chain that mixes write phases refuses", () => {
299
+ const out = refusal({
300
+ routes: [{ from: ["sub(a).text", "self.props.label"], to: "prop:label" }],
301
+ });
302
+ expect(out).toContain("mixes write phases");
303
+ });
304
+
305
+ it("a same-phase double-claim refuses; distinct-phase layering loads", () => {
306
+ const clash = refusal({
307
+ routes: [
308
+ { from: ["sub(a).text"], to: "prop:label" },
309
+ { from: ["sub(b).text"], to: "prop:label" },
310
+ ],
311
+ });
312
+ expect(clash).toContain("same write phase");
313
+ const layered = refusal({
314
+ routes: [
315
+ { from: ["self.props.label"], to: "prop:label" },
316
+ { from: ["sub(a).text"], to: "prop:label" },
317
+ ],
318
+ });
319
+ expect(layered).toBe("LOADED");
320
+ });
321
+
322
+ it("a second children route refuses (multi-slot arrives with T4)", () => {
323
+ const out = refusal({
324
+ routes: [
325
+ { from: ["children"], to: "slot:child" },
326
+ { from: ["children"], to: "slots:items" },
327
+ ],
328
+ });
329
+ expect(out).toContain("T4");
330
+ });
331
+
332
+ it("children routing plus subtree consumption is a contradiction and refuses", () => {
333
+ const out = refusal({
334
+ routes: [
335
+ { from: ["sub(a).text"], to: "prop:label" },
336
+ { from: ["children"], to: "slot:child" },
337
+ ],
338
+ });
339
+ expect(out).toContain("contradictory");
340
+ });
341
+
342
+ it("a records collect without a field name refuses", () => {
343
+ const out = refusal({
344
+ collects: [{ of: ["sec"], into: "prop:items", shape: "records", row: ["row"], cells: ["cell"] }],
345
+ });
346
+ expect(out).toContain("field");
347
+ });
348
+
349
+ it("a sub with two dispositions refuses", () => {
350
+ const out = refusal({
351
+ routes: [{ from: ["sub(part).text"], to: "prop:label" }],
352
+ subs: { part: "transparent" },
353
+ });
354
+ expect(out).toContain("one sub, one disposition");
355
+ });
356
+
357
+ it("unimplemented capability spellings are refused; T1's are now implemented with their own rules", () => {
358
+ // T1 landed: `transparent` is a real spelling — on THIS plan it refuses
359
+ // for its true reason (a transparent plan declares no catalog surface,
360
+ // and Widget declares structural slots), not because the grammar is
361
+ // closed to it.
362
+ expect(refusal({ transparent: true })).toContain("declares no catalog surface");
363
+ // `onto` remains INTERNAL vocabulary: donation is authored via
364
+ // transparent.donate blocks; a raw onto key on a route stays refused.
365
+ expect(refusal({ routes: [{ from: ["self.text"], to: "prop:label", onto: "control" }] })).not.toBe("LOADED");
366
+ // T3 joins have not landed and stay refused, never reinterpreted.
367
+ expect(refusal({ collects: [{ of: ["s"], into: "prop:items", shape: "records", field: "f", row: ["r"], cells: ["c"], joinOn: "id" }] })).not.toBe("LOADED");
368
+ });
369
+ });