@aestheticfunction/dspack-emit 0.5.0 → 0.7.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 (39) hide show
  1. package/README.md +5 -3
  2. package/dist/targets/a2ui/surface.js +580 -36
  3. package/dist/transform/mapping.js +21 -7
  4. package/dist/transform/model.d.ts +94 -1
  5. package/dist/transform/model.js +25 -4
  6. package/dist/transform/parse-v2.d.ts +55 -24
  7. package/dist/transform/parse-v2.js +176 -2
  8. package/dist/transform/profile-schema-v2.js +224 -46
  9. package/dist/transform/shadcn-v2-respelling.d.ts +65 -0
  10. package/dist/transform/shadcn-v2-respelling.js +407 -0
  11. package/dist/transform/validate-v2.js +39 -3
  12. package/dist/types.d.ts +5 -1
  13. package/dist/validate/ajv.d.ts +19 -0
  14. package/dist/validate/ajv.js +78 -13
  15. package/package.json +6 -2
  16. package/src/a0.test.ts +235 -0
  17. package/src/engine-boundary.test.ts +10 -0
  18. package/src/eval-fixture.test.ts +27 -14
  19. package/src/instance-diagnostics.test.ts +194 -0
  20. package/src/layered-dissolution.test.ts +139 -0
  21. package/src/profile-v2.test.ts +9 -3
  22. package/src/review-findings.test.ts +4 -1
  23. package/src/surface-emit.test.ts +43 -11
  24. package/src/surface-fidelity.test.ts +12 -0
  25. package/src/t1.test.ts +370 -0
  26. package/src/t2.test.ts +222 -0
  27. package/src/t3.test.ts +304 -0
  28. package/src/t4.test.ts +278 -0
  29. package/src/targets/a2ui/surface.ts +747 -72
  30. package/src/transform/mapping.ts +22 -6
  31. package/src/transform/model.ts +107 -5
  32. package/src/transform/parse-v2.ts +228 -17
  33. package/src/transform/profile-schema-v2.ts +224 -46
  34. package/src/transform/profile.v2.schema.json +224 -46
  35. package/src/transform/shadcn-v2-respelling.ts +414 -0
  36. package/src/transform/validate-v2.ts +39 -3
  37. package/src/types.ts +13 -1
  38. package/src/validate/ajv.ts +96 -12
  39. package/src/validate-v2.test.ts +1 -1
package/src/t4.test.ts ADDED
@@ -0,0 +1,278 @@
1
+ /**
2
+ * T4: multi-slot compounds — `sub(x).children -> slot:name`.
3
+ *
4
+ * Fail-first (/tmp/t4-failfirst.txt, captured at main e21e0bf before any
5
+ * code): the spelling was refused by the v2 grammar ("must match pattern");
6
+ * the 50-unresolved ledger carried dialog 7 + sheet 7 + popover 6 +
7
+ * dropdown-menu 12; ex.invite-teammates-dialog and
8
+ * ex.customer-context-sheet refused end-to-end; and the containment
9
+ * re-baseline's overlay-task produced a fully lint-clean surface the
10
+ * emitter refused on unmapped 'dialog'.
11
+ *
12
+ * The primitive is ONE closed spelling: the ordered children of exactly one
13
+ * descendant instance of the named region become instances; the slot
14
+ * carries the reference (multi wraps in Column — T3's joined-children
15
+ * discipline). Regions have no fallback chains, one region feeds one slot,
16
+ * zero regions leave the destination absent for gate A3, several refuse.
17
+ * Claimed subs (text harvests, sibling slots) are skipped at any
18
+ * dissolution depth — their consumption is their own route's ledger line.
19
+ *
20
+ * The measured surprise, followed rather than the roadmap: dropdown-menu is
21
+ * DATA-shaped (a T2 collect + scalar text routes) and needs no slot at all.
22
+ */
23
+ import { readFileSync } from "node:fs";
24
+ import { fileURLToPath } from "node:url";
25
+ import { describe, expect, it } from "vitest";
26
+ import { transformFromJson } from "./transform/index.js";
27
+ import { emitSurface, EmitSurfaceError } from "./targets/a2ui/surface.js";
28
+ import { loadProfile, ProfileLoadError } from "./transform/profile-load.js";
29
+ import { SHADCN_V3_T4_PLANS } from "./transform/shadcn-v2-respelling.js";
30
+ import type { DspackDoc, DspackSurface } from "./types.js";
31
+
32
+ const repo = (p: string) => fileURLToPath(new URL(`../${p}`, import.meta.url));
33
+ const contract = JSON.parse(readFileSync(repo("eval/shadcn-v3.dspack.json"), "utf8")) as DspackDoc & {
34
+ examples: Array<{ id: string; surface: DspackSurface }>;
35
+ };
36
+ const fixture = JSON.parse(readFileSync(repo("eval/shadcn-v3.eval.profile.json"), "utf8"));
37
+
38
+ const KEEP = ["card", "badge", "button", "input", "textarea", "select", "field", "field-set", "field-group", "scroll-area", "dialog", "sheet", "popover", "dropdown-menu"];
39
+
40
+ const t4Profile = (mutate?: (doc: Record<string, unknown>) => void) => {
41
+ const base = structuredClone(fixture) as Record<string, unknown> & {
42
+ components: Array<Record<string, unknown> & { dspackId: string }>;
43
+ };
44
+ base.components = base.components.map((plan) => {
45
+ const t4 = SHADCN_V3_T4_PLANS[plan.dspackId];
46
+ return t4 ? (structuredClone(t4) as typeof plan) : plan;
47
+ });
48
+ const keepSet = new Set(KEEP);
49
+ base.intentionallyOmitted = base.components.filter((c) => !keepSet.has(c.dspackId)).map((c) => c.dspackId).sort();
50
+ base.components = base.components.filter((c) => keepSet.has(c.dspackId));
51
+ delete (base as Record<string, unknown>)["x-scaffold"];
52
+ mutate?.(base);
53
+ return loadProfile(base);
54
+ };
55
+
56
+ const surfaceOf = (root: unknown, intent = "overlay-task"): DspackSurface =>
57
+ ({ dspackSurface: "0.1", system: contract.name as string, intent, root }) as DspackSurface;
58
+
59
+ const emit = (root: unknown, profile = t4Profile()) => emitSurface(surfaceOf(root), contract, { profile });
60
+
61
+ const fromExample = (exampleId: string, component: string): Record<string, unknown> => {
62
+ const ex = contract.examples.find((e) => e.id === exampleId)!;
63
+ let hit: Record<string, unknown> | undefined;
64
+ const walk = (n: Record<string, unknown>): void => {
65
+ if (hit) return;
66
+ if (n.component === component) { hit = n; return; }
67
+ for (const c of (n.children as Record<string, unknown>[] | undefined) ?? []) walk(c);
68
+ };
69
+ walk(ex.surface.root as unknown as Record<string, unknown>);
70
+ expect(hit, `${component} in ${exampleId}`).toBeDefined();
71
+ return structuredClone(hit!);
72
+ };
73
+
74
+ const instancesOf = (messages: unknown[]) =>
75
+ (messages[1] as { updateComponents: { components: Array<Record<string, unknown>> } }).updateComponents.components;
76
+
77
+ describe("T4 grammar and gates", () => {
78
+ it("the slot spelling loads (the fail-first refusal, inverted)", () => {
79
+ expect(t4Profile().language).toBe("v2");
80
+ });
81
+
82
+ it("a region has no fallback chain: sub(a|b).children refuses", () => {
83
+ expect(() =>
84
+ t4Profile((doc) => {
85
+ const d = (doc.components as Array<Record<string, unknown>>).find((c) => c.dspackId === "dialog")!;
86
+ (d.surface as { routes: unknown[] }).routes = [{ from: ["sub(dialog-content|dialog-footer).children"], to: "slot:child" }];
87
+ }),
88
+ ).toThrowError(ProfileLoadError);
89
+ });
90
+
91
+ it("one region, one slot: the same sub sourcing two slot routes refuses", () => {
92
+ expect(() =>
93
+ t4Profile((doc) => {
94
+ const d = (doc.components as Array<Record<string, unknown>>).find((c) => c.dspackId === "dialog")!;
95
+ (d.surface as { routes: unknown[] }).routes = [
96
+ { from: ["sub(dialog-content).children"], to: "slot:child" },
97
+ { from: ["sub(dialog-content).children"], to: "slot:footer" },
98
+ ];
99
+ }),
100
+ ).toThrowError(ProfileLoadError);
101
+ });
102
+
103
+ it("sub(x).children lands only in a slot: prop destination refuses", () => {
104
+ expect(() =>
105
+ t4Profile((doc) => {
106
+ const d = (doc.components as Array<Record<string, unknown>>).find((c) => c.dspackId === "dialog")!;
107
+ (d.surface as { routes: unknown[] }).routes = [{ from: ["sub(dialog-content).children"], to: "prop:title" }];
108
+ }),
109
+ ).toThrowError(ProfileLoadError);
110
+ });
111
+ });
112
+
113
+ describe("T4 — dialog (the production invite-teammates shape, verbatim)", () => {
114
+ it("emits {title, description, triggerLabel, closeLabel, child, footer}; fields inside the slot dissolve and donate", () => {
115
+ const { messages, fidelity } = emit(fromExample("ex.invite-teammates-dialog", "dialog"));
116
+ const comps = instancesOf(messages);
117
+ const dialog = comps.find((c) => c.component === "Dialog")!;
118
+ expect(dialog.title).toBe("Invite people to Northwind");
119
+ expect(dialog.triggerLabel).toBe("Invite people");
120
+ expect(dialog.closeLabel).toBe("Cancel");
121
+ expect(typeof dialog.description).toBe("string");
122
+
123
+ // The body slot: two fields dissolve to Textarea + Select, each followed
124
+ // by its field-description as a Text caption — four instances,
125
+ // Column-wrapped, in reading order.
126
+ const body = comps.find((c) => c.id === dialog.child)!;
127
+ expect(body.component).toBe("Column");
128
+ const textarea = comps.find((c) => c.component === "Textarea")!;
129
+ const select = comps.find((c) => c.component === "Select")!;
130
+ expect(textarea.label).toBe("Email addresses");
131
+ expect(select.label).toBe("Role");
132
+ expect((select.options as unknown[]).length).toBe(3);
133
+ const bodyIds = body.children as string[];
134
+ expect(bodyIds).toHaveLength(4);
135
+ expect(bodyIds[0]).toBe(textarea.id);
136
+ expect(bodyIds[2]).toBe(select.id);
137
+ for (const i of [1, 3]) {
138
+ const t = comps.find((c) => c.id === bodyIds[i])!;
139
+ expect(t.component).toBe("Text");
140
+ expect(t.variant).toBe("caption");
141
+ }
142
+
143
+ // The footer slot: close is a claimed text lift; the send button remains.
144
+ const footerRef = comps.find((c) => c.id === dialog.footer)!;
145
+ expect(footerRef.component).toBe("Button");
146
+ expect(footerRef.child).toBeDefined();
147
+
148
+ // No wrapper instance for header; the regions ledger as moved.
149
+ expect(comps.some((c) => c.component === "DialogHeader")).toBe(false);
150
+ expect(fidelity.some((f) => f.kind === "moved" && f.source.includes("dialog-content children"))).toBe(true);
151
+
152
+ const profile = t4Profile();
153
+ const check = transformFromJson(contract, { profile, surface: { messages } });
154
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(true);
155
+ });
156
+
157
+ it("two instances of a region refuse: a slot names ONE region", () => {
158
+ expect(() =>
159
+ emit({
160
+ component: "dialog",
161
+ children: [
162
+ { component: "dialog-content", children: [{ component: "dialog-title", text: "A" }] },
163
+ { component: "dialog-content", children: [{ component: "badge", text: "B" }] },
164
+ ],
165
+ }),
166
+ ).toThrowError(/ONE region/);
167
+ });
168
+
169
+ it("an unclaimed direct child of a slot compound refuses — fail-closed, never approximated", () => {
170
+ expect(() =>
171
+ emit({
172
+ component: "dialog",
173
+ children: [
174
+ { component: "dialog-content", children: [{ component: "dialog-title", text: "T" }] },
175
+ { component: "badge", text: "stray" },
176
+ ],
177
+ }),
178
+ ).toThrowError(/no destination/);
179
+ });
180
+
181
+ it("a casualty inside a slot still refuses (casualty enforcement is live inside regions)", () => {
182
+ const profile = t4Profile((doc) => {
183
+ (doc.casualtyComponents as unknown[]) = [
184
+ { dspackId: "badge", attempted: "(none)", class: "cannot-represent", reason: "test casualty" },
185
+ ];
186
+ (doc.components as Array<Record<string, unknown>>).splice(
187
+ (doc.components as Array<Record<string, unknown>>).findIndex((c) => c.dspackId === "badge"),
188
+ 1,
189
+ );
190
+ });
191
+ expect(() =>
192
+ emit(
193
+ {
194
+ component: "dialog",
195
+ children: [
196
+ { component: "dialog-content", children: [{ component: "dialog-title", text: "T" }, { component: "badge", text: "x" }] },
197
+ ],
198
+ },
199
+ profile,
200
+ ),
201
+ ).toThrowError(/declared casualty/);
202
+ });
203
+ });
204
+
205
+ describe("T4 — sheet composes with shipped primitives (the customer-context shape, verbatim)", () => {
206
+ it("scroll-area DISSOLVES inside the body slot; cards emit; footer keeps the non-close action", () => {
207
+ const { messages } = emit(fromExample("ex.customer-context-sheet", "sheet"));
208
+ const comps = instancesOf(messages);
209
+ const sheet = comps.find((c) => c.component === "Sheet")!;
210
+ expect(sheet.title).toBe("Amara Okafor");
211
+ expect(sheet.triggerLabel).toBe("Customer context");
212
+ expect(sheet.closeLabel).toBe("Close");
213
+ expect(comps.some((c) => c.component === "ScrollArea")).toBe(false);
214
+ const body = comps.find((c) => c.id === sheet.child)!;
215
+ expect(body.component).toBe("Column");
216
+ expect((body.children as string[]).length).toBe(3);
217
+ for (const cid of body.children as string[]) {
218
+ expect(comps.find((c) => c.id === cid)!.component).toBe("Card");
219
+ }
220
+ const check = transformFromJson(contract, { profile: t4Profile(), surface: { messages } });
221
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(true);
222
+ });
223
+ });
224
+
225
+ describe("T4 — popover's measured empty body stays absent", () => {
226
+ it("emits {triggerLabel, title, description}; the child slot is absent and A3 accepts it", () => {
227
+ const { messages } = emit(fromExample("ex.usage-help-affordances", "popover"));
228
+ const comps = instancesOf(messages);
229
+ const pop = comps.find((c) => c.component === "Popover")!;
230
+ expect(pop.triggerLabel).toBe("How usage is measured");
231
+ expect(pop.title).toBe("How usage is measured");
232
+ expect(pop.child).toBeUndefined();
233
+ const check = transformFromJson(contract, { profile: t4Profile(), surface: { messages } });
234
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(true);
235
+ });
236
+ });
237
+
238
+ describe("T4 finding — dropdown-menu is data, not slots (measurement over roadmap)", () => {
239
+ it("the production workspace menu emits {triggerLabel, menuLabel, items}; the separator drops with record", () => {
240
+ const { messages, fidelity } = emit(fromExample("ex.workspace-members-directory", "dropdown-menu"));
241
+ const comps = instancesOf(messages);
242
+ const menu = comps.find((c) => c.component === "DropdownMenu")!;
243
+ expect(menu.triggerLabel).toBe("Member actions");
244
+ expect(menu.menuLabel).toBe("Selected members");
245
+ expect(menu.items).toEqual([
246
+ { label: "Resend invitation" },
247
+ { label: "Change role" },
248
+ { label: "Export as CSV" },
249
+ ]);
250
+ // The nested separator's loss rides the collect's flatten record (it is
251
+ // inside dropdown-menu-content, not a direct child); the authored drop
252
+ // reason lives on the coverage row — the same two-channel honesty as A0.
253
+ expect(fidelity.some((f) => f.kind === "flattened" && f.class === "lossy")).toBe(true);
254
+ const out = transformFromJson(contract, { profile: t4Profile() });
255
+ expect(out.mapping.coverage.find((c) => c.id === "dropdown-menu.dropdown-menu-separator")?.detail).toContain("divider");
256
+ const check = transformFromJson(contract, { profile: t4Profile(), surface: { messages } });
257
+ expect(check.validation.gates.find((g) => g.name === "instance")?.pass).toBe(true);
258
+ });
259
+ });
260
+
261
+ describe("T4 — ledger and determinism", () => {
262
+ it("all four families derive zero unresolved subs", () => {
263
+ const out = transformFromJson(contract, { profile: t4Profile() });
264
+ const unresolved = out.mapping.coverage.filter(
265
+ (c) => c.disposition === "unclassified" &&
266
+ ["dialog.", "sheet.", "popover.", "dropdown-menu."].some((p) => c.id.startsWith(p)),
267
+ );
268
+ expect(unresolved).toEqual([]);
269
+ expect(out.mapping.coverage.find((c) => c.id === "dialog.dialog-content")?.detail).toContain("slot");
270
+ });
271
+
272
+ it("emission is deterministic across runs", () => {
273
+ const subtree = fromExample("ex.invite-teammates-dialog", "dialog");
274
+ const a = emit(subtree);
275
+ const b = emit(subtree);
276
+ expect(JSON.stringify([a.messages, a.warnings, a.fidelity])).toBe(JSON.stringify([b.messages, b.warnings, b.fidelity]));
277
+ });
278
+ });