@aestheticfunction/dspack-emit 0.3.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 (62) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +285 -0
  3. package/dist/cli.d.ts +2 -0
  4. package/dist/cli.js +186 -0
  5. package/dist/index.d.ts +16 -0
  6. package/dist/index.js +15 -0
  7. package/dist/targets/a2ui/surface.d.ts +43 -0
  8. package/dist/targets/a2ui/surface.js +219 -0
  9. package/dist/targets/csr.d.ts +15 -0
  10. package/dist/targets/csr.js +9 -0
  11. package/dist/targets/json-render/codegen.d.ts +35 -0
  12. package/dist/targets/json-render/codegen.js +91 -0
  13. package/dist/targets/json-render/emit.d.ts +66 -0
  14. package/dist/targets/json-render/emit.js +142 -0
  15. package/dist/targets/json-render/model.d.ts +57 -0
  16. package/dist/targets/json-render/model.js +99 -0
  17. package/dist/targets/json-render/profile.d.ts +23 -0
  18. package/dist/targets/json-render/profile.js +3 -0
  19. package/dist/transform/color.d.ts +11 -0
  20. package/dist/transform/color.js +53 -0
  21. package/dist/transform/emit.d.ts +15 -0
  22. package/dist/transform/emit.js +100 -0
  23. package/dist/transform/index.d.ts +26 -0
  24. package/dist/transform/index.js +26 -0
  25. package/dist/transform/inline-defs.d.ts +22 -0
  26. package/dist/transform/inline-defs.js +172 -0
  27. package/dist/transform/mapping.d.ts +24 -0
  28. package/dist/transform/mapping.js +322 -0
  29. package/dist/transform/profiles.d.ts +120 -0
  30. package/dist/transform/profiles.js +282 -0
  31. package/dist/transform/report.d.ts +13 -0
  32. package/dist/transform/report.js +76 -0
  33. package/dist/types.d.ts +103 -0
  34. package/dist/types.js +6 -0
  35. package/dist/validate/ajv.d.ts +15 -0
  36. package/dist/validate/ajv.js +132 -0
  37. package/dist/validate/meta/a2ui-catalog.meta.0_9_1.json +39 -0
  38. package/dist/validate/meta/a2ui-catalog.meta.1_0.json +34 -0
  39. package/package.json +41 -0
  40. package/src/cli.ts +197 -0
  41. package/src/index.ts +39 -0
  42. package/src/ingestion.test.ts +156 -0
  43. package/src/json-render-emit.test.ts +196 -0
  44. package/src/surface-emit.test.ts +184 -0
  45. package/src/targets/a2ui/surface.ts +287 -0
  46. package/src/targets/csr.ts +23 -0
  47. package/src/targets/json-render/codegen.ts +135 -0
  48. package/src/targets/json-render/emit.ts +227 -0
  49. package/src/targets/json-render/model.ts +170 -0
  50. package/src/targets/json-render/profile.ts +26 -0
  51. package/src/transform/color.ts +52 -0
  52. package/src/transform/emit.ts +128 -0
  53. package/src/transform/index.ts +54 -0
  54. package/src/transform/inline-defs.ts +209 -0
  55. package/src/transform/mapping.ts +391 -0
  56. package/src/transform/profiles.ts +425 -0
  57. package/src/transform/report.ts +105 -0
  58. package/src/transform.test.ts +112 -0
  59. package/src/types.ts +145 -0
  60. package/src/validate/ajv.ts +151 -0
  61. package/src/validate/meta/a2ui-catalog.meta.0_9_1.json +39 -0
  62. package/src/validate/meta/a2ui-catalog.meta.1_0.json +34 -0
@@ -0,0 +1,425 @@
1
+ /**
2
+ * A mapping *profile* is pure data describing how a dspack contract's components
3
+ * correspond to A2UI catalog components. The transform engine (mapping.ts) is
4
+ * source-agnostic and reads only this profile + the dspack document — never any
5
+ * framework code. Retargeting a different design system = writing a new profile.
6
+ *
7
+ * The profile below targets the shadcn/ui dspack contract. Two kinds of components
8
+ * are emitted:
9
+ * - `components`: derived from dspack component entries.
10
+ * - `synthesized`: A2UI structural/content primitives (Text, Column) that dspack
11
+ * does NOT contain. dspack describes a component *library*, not a layout system
12
+ * (its `layout` block is descriptive: breakpoints/grid/spacing, not renderable
13
+ * components). A2UI surfaces need primitives to be composable/renderable, so we
14
+ * synthesize them and record that as a fidelity finding.
15
+ *
16
+ * `casualtyComponents` are dspack components with no faithful A2UI representation;
17
+ * they are documented and warned about, not emitted.
18
+ */
19
+ import type { FidelityClass, Json } from "../types.js";
20
+
21
+ export interface Profile {
22
+ catalogTitle: string;
23
+ catalogDescription: string;
24
+ /** Versioned catalogId/$id is built from this base + `/<ver>/catalog.json`. */
25
+ catalogIdBase: string;
26
+ /** v1.0 optional top-level `instructions`. */
27
+ instructions: string;
28
+ /** Which dspack token (category.name) supplies theme.primaryColor. */
29
+ primaryColorToken: { category: string; name: string };
30
+ components: ComponentPlan[];
31
+ synthesized: ComponentPlan[];
32
+ casualtyComponents: CasualtyComponent[];
33
+ /** dspack component ids deliberately left out (not mapped, not a casualty). */
34
+ intentionallyOmitted?: string[];
35
+ /**
36
+ * Which synthesized A2UI primitives the surface emitter uses when a dspack
37
+ * surface needs structure the source vocabulary does not express: text
38
+ * leaves become `textComponent` instances; multiple children in a
39
+ * single-child slot are wrapped in `wrapComponent`.
40
+ */
41
+ surfaceSynthesis: {
42
+ textComponent: string;
43
+ textProp: string;
44
+ wrapComponent: string;
45
+ wrapChildrenProp: string;
46
+ };
47
+ }
48
+
49
+ export interface ComponentPlan {
50
+ a2ui: string;
51
+ /** dspack component id this is derived from; omitted for pure synthesized primitives. */
52
+ dspackId?: string;
53
+ /** Overrides dspack description; if omitted and dspackId set, dspack description is used. */
54
+ description?: string;
55
+ /** Inlined `$defs` to compose via allOf (ComponentCommon is always included). */
56
+ commons: string[];
57
+ /** A2UI-native structural slots — synthesis, not dspack props. */
58
+ structural: Record<string, StructuralSlot>;
59
+ /** dspack prop name -> A2UI property mapping. */
60
+ propMap?: Record<string, PropPlan>;
61
+ /** A2UI required property names (the `component` const is added automatically). */
62
+ required: string[];
63
+ /** How the surface emitter projects a dspack-surface node onto this component. */
64
+ surfacePlan?: SurfacePlanDirectives;
65
+ }
66
+
67
+ /**
68
+ * Data-only directives for projecting a dspack-surface (CSR) node onto an
69
+ * emitted A2UI component instance. Compound composition that A2UI cannot
70
+ * represent is flattened here, per the documented casualty mapping in
71
+ * MAPPING.md — the emitter consumes the node's whole subtree when sub-content
72
+ * directives (`subText`/`subButtonText`) are present.
73
+ */
74
+ export interface SurfacePlanDirectives {
75
+ /** Descendant sub-component id -> A2UI prop receiving that node's `text`. */
76
+ subText?: Record<string, string>;
77
+ /**
78
+ * Descendant sub-component id -> A2UI prop receiving the `text` of the first
79
+ * label-bearing component under that sub-component — a component whose own
80
+ * surface plan projects text as a child label via `textChildProp` (e.g.
81
+ * AlertDialogTrigger's button label -> triggerLabel). Incidental text on
82
+ * other descendants is never picked up. The label-bearer's own props are a
83
+ * documented casualty.
84
+ */
85
+ subButtonText?: Record<string, string>;
86
+ /** Synthesize a declarative A2UI Action into this prop (event name is a deterministic slug). */
87
+ actionProp?: string;
88
+ /** Node `text` becomes a synthesized text-primitive child referenced by this ComponentId prop. */
89
+ textChildProp?: string;
90
+ /** Node `text` becomes this DynamicString prop directly. */
91
+ textProp?: string;
92
+ /** Children emit as components; exactly one child id in this prop (>1 children are wrapped). */
93
+ childProp?: string;
94
+ /** Children emit as components; their ids form a ChildList in this prop. */
95
+ childrenProp?: string;
96
+ /** CSR props copied verbatim into same-named structural slots (e.g. Table columns/rows). */
97
+ structuralPassthrough?: string[];
98
+ }
99
+
100
+ export interface StructuralSlot {
101
+ schema: Json;
102
+ description: string;
103
+ synthNote: string;
104
+ }
105
+
106
+ export interface PropPlan {
107
+ a2ui: string;
108
+ kind: "enum" | "string" | "boolean" | "number";
109
+ /** Target (A2UI) enum vocabulary written into the catalog. */
110
+ targetEnum?: string[];
111
+ /** Source-value -> target-value projection. Many-to-one => lossy. */
112
+ valueMap?: Record<string, string>;
113
+ default?: string;
114
+ description?: string;
115
+ }
116
+
117
+ export interface CasualtyComponent {
118
+ dspackId: string;
119
+ attempted: string;
120
+ class: FidelityClass;
121
+ reason: string;
122
+ }
123
+
124
+ const DynStr = { $ref: "#/$defs/DynamicString" };
125
+ const CompId = { $ref: "#/$defs/ComponentId" };
126
+
127
+ export const shadcnProfile: Profile = {
128
+ catalogTitle: "shadcn/ui — A2UI catalog (compiled from dspack)",
129
+ catalogDescription:
130
+ "A2UI catalog compiled from the shadcn/ui dspack v0.2 contract. Component shapes, " +
131
+ "variant enums, and the primary design token are projected onto the A2UI basic " +
132
+ "component vocabulary. See MAPPING.md for per-field fidelity.",
133
+ catalogIdBase: "https://rdombrowski.dev/catalogs/shadcn-ui",
134
+ instructions: "For layout, use the Column component to organize other components.",
135
+ primaryColorToken: { category: "color", name: "primary" },
136
+ surfaceSynthesis: {
137
+ textComponent: "Text",
138
+ textProp: "text",
139
+ wrapComponent: "Column",
140
+ wrapChildrenProp: "children",
141
+ },
142
+
143
+ components: [
144
+ {
145
+ a2ui: "Button",
146
+ dspackId: "button",
147
+ commons: ["ComponentCommon", "Checkable"],
148
+ structural: {
149
+ child: {
150
+ schema: CompId,
151
+ description:
152
+ "The ID of the child component to render inside the button (e.g. a Text). " +
153
+ "Referenced by ID; not defined inline.",
154
+ synthNote:
155
+ "A2UI Buttons render a child component by ID; dspack Button has no equivalent " +
156
+ "slot (children are arbitrary React nodes).",
157
+ },
158
+ action: {
159
+ schema: { $ref: "#/$defs/Action" },
160
+ description: "The interaction dispatched when the button is activated.",
161
+ synthNote:
162
+ "A2UI requires a declarative action; dspack expresses this as an onClick handler prop, " +
163
+ "which is not representable in a declarative catalog.",
164
+ },
165
+ },
166
+ propMap: {
167
+ variant: {
168
+ a2ui: "variant",
169
+ kind: "enum",
170
+ targetEnum: ["default", "primary", "borderless"],
171
+ valueMap: {
172
+ default: "primary",
173
+ destructive: "default",
174
+ outline: "default",
175
+ secondary: "default",
176
+ ghost: "borderless",
177
+ link: "borderless",
178
+ },
179
+ default: "primary",
180
+ description:
181
+ "Button style hint, projected from shadcn variants onto the A2UI basic vocabulary.",
182
+ },
183
+ },
184
+ required: ["child", "action"],
185
+ surfacePlan: { textChildProp: "child", actionProp: "action" },
186
+ },
187
+
188
+ {
189
+ a2ui: "Card",
190
+ dspackId: "card",
191
+ commons: ["ComponentCommon"],
192
+ structural: {
193
+ child: {
194
+ schema: CompId,
195
+ description:
196
+ "The ID of the single child component. Wrap multiple elements in a Column and pass its ID.",
197
+ synthNote:
198
+ "A2UI Card takes exactly one child by ID; dspack Card composes via sub-components " +
199
+ "(CardHeader/CardContent/CardFooter), which collapse to a single child slot.",
200
+ },
201
+ },
202
+ required: ["child"],
203
+ surfacePlan: { childProp: "child" },
204
+ },
205
+
206
+ {
207
+ a2ui: "TextField",
208
+ dspackId: "input",
209
+ commons: ["ComponentCommon", "Checkable"],
210
+ structural: {
211
+ label: {
212
+ schema: DynStr,
213
+ description: "The text label for the input field.",
214
+ synthNote:
215
+ "A2UI TextField owns its label; dspack Input relies on an external <label> element, " +
216
+ "so the label is synthesized.",
217
+ },
218
+ value: {
219
+ schema: DynStr,
220
+ description: "The bound value of the text field.",
221
+ synthNote: "A2UI two-way-binds value; dspack Input has no value prop in the contract.",
222
+ },
223
+ },
224
+ propMap: {
225
+ type: {
226
+ a2ui: "variant",
227
+ kind: "enum",
228
+ targetEnum: ["shortText", "longText", "number", "obscured"],
229
+ valueMap: {
230
+ text: "shortText",
231
+ email: "shortText",
232
+ search: "shortText",
233
+ password: "obscured",
234
+ number: "number",
235
+ },
236
+ default: "shortText",
237
+ description: "Input kind, projected from the HTML input type onto A2UI TextField variants.",
238
+ },
239
+ },
240
+ required: ["label"],
241
+ surfacePlan: { textProp: "label" },
242
+ },
243
+
244
+ {
245
+ // shadcn Badge -> a real A2UI Badge component shape (variant enum carried verbatim).
246
+ a2ui: "Badge",
247
+ dspackId: "badge",
248
+ commons: ["ComponentCommon"],
249
+ structural: {
250
+ label: {
251
+ schema: DynStr,
252
+ description: "The badge text.",
253
+ synthNote: "A2UI has no Badge; the label is synthesized from the badge's text child.",
254
+ },
255
+ },
256
+ propMap: {
257
+ variant: {
258
+ a2ui: "variant",
259
+ kind: "enum",
260
+ targetEnum: ["default", "secondary", "outline", "destructive"],
261
+ default: "default",
262
+ description: "Badge visual treatment (carried verbatim from shadcn; the React visual honors all four).",
263
+ },
264
+ },
265
+ required: ["label"],
266
+ surfacePlan: { textProp: "label" },
267
+ },
268
+
269
+ {
270
+ // shadcn Table -> a synthesized presentational A2UI Table shape (caption/columns/rows).
271
+ // The dspack sub-component composition (header/body/row/cell) cannot be represented and
272
+ // is recorded as a casualty by the per-component composition handling.
273
+ a2ui: "Table",
274
+ dspackId: "table",
275
+ commons: ["ComponentCommon"],
276
+ structural: {
277
+ caption: {
278
+ schema: DynStr,
279
+ description: "Accessible caption naming the table.",
280
+ synthNote: "A2UI has no Table; caption synthesized from TableCaption.",
281
+ },
282
+ columns: {
283
+ schema: { type: "array", items: { type: "string" } },
284
+ description: "Header labels for the data columns (a trailing status column is rendered separately).",
285
+ synthNote: "Static column labels; A2UI has no table-header primitive.",
286
+ },
287
+ rows: {
288
+ schema: { type: "array", items: { type: "object" } },
289
+ description: "Row records, each { cells: string[], status?: { label, variant } }.",
290
+ synthNote: "Row data carried as a static array; A2UI has no tabular data model.",
291
+ },
292
+ },
293
+ required: ["columns", "rows"],
294
+ surfacePlan: { structuralPassthrough: ["caption", "columns", "rows"] },
295
+ },
296
+
297
+ {
298
+ // shadcn AlertDialog -> a synthesized non-dismissible confirmation. Preserves the
299
+ // defining distinction from Dialog; the rich composition is a documented casualty.
300
+ a2ui: "AlertDialog",
301
+ dspackId: "alert-dialog",
302
+ commons: ["ComponentCommon"],
303
+ structural: {
304
+ triggerLabel: {
305
+ schema: DynStr,
306
+ description: "Label of the button that opens the confirmation.",
307
+ synthNote: "Trigger modeled as a label, not an AlertDialogTrigger sub-component.",
308
+ },
309
+ title: {
310
+ schema: DynStr,
311
+ description: "Confirmation title.",
312
+ synthNote: "From AlertDialogTitle.",
313
+ },
314
+ description: {
315
+ schema: DynStr,
316
+ description: "Consequence description shown in the confirmation.",
317
+ synthNote: "From AlertDialogDescription.",
318
+ },
319
+ confirmLabel: {
320
+ schema: DynStr,
321
+ description: "Label of the destructive confirm action.",
322
+ synthNote: "From AlertDialogAction.",
323
+ },
324
+ cancelLabel: {
325
+ schema: DynStr,
326
+ description: "Label of the cancel action.",
327
+ synthNote: "From AlertDialogCancel.",
328
+ },
329
+ action: {
330
+ schema: { $ref: "#/$defs/Action" },
331
+ description: "Event dispatched when the user confirms the destructive action.",
332
+ synthNote: "A2UI declarative action; dspack expresses this as an onClick handler.",
333
+ },
334
+ },
335
+ required: ["triggerLabel", "title", "action"],
336
+ surfacePlan: {
337
+ subText: {
338
+ "alert-dialog-title": "title",
339
+ "alert-dialog-description": "description",
340
+ "alert-dialog-cancel": "cancelLabel",
341
+ "alert-dialog-action": "confirmLabel",
342
+ },
343
+ subButtonText: { "alert-dialog-trigger": "triggerLabel" },
344
+ actionProp: "action",
345
+ },
346
+ },
347
+ ],
348
+
349
+ synthesized: [
350
+ {
351
+ a2ui: "Text",
352
+ commons: ["ComponentCommon"],
353
+ description: "Displays text content. Synthesized A2UI content primitive (not in dspack).",
354
+ structural: {
355
+ text: {
356
+ schema: DynStr,
357
+ description: "The text content to display.",
358
+ synthNote: "A2UI content primitive required to render labels/titles in a surface.",
359
+ },
360
+ },
361
+ propMap: {
362
+ // No dspack source; declared directly as an A2UI-native enum.
363
+ variant: {
364
+ a2ui: "variant",
365
+ kind: "enum",
366
+ targetEnum: ["h1", "h2", "h3", "h4", "h5", "caption", "body"],
367
+ default: "body",
368
+ description: "A hint for the base text style.",
369
+ },
370
+ },
371
+ required: ["text"],
372
+ },
373
+ {
374
+ a2ui: "Column",
375
+ commons: ["ComponentCommon"],
376
+ description:
377
+ "Arranges children vertically. Synthesized A2UI structural primitive (dspack has no " +
378
+ "layout component; its `layout` block is descriptive only).",
379
+ structural: {
380
+ children: {
381
+ schema: { $ref: "#/$defs/ChildList" },
382
+ description: "Child component IDs (or a template).",
383
+ synthNote: "A2UI structural primitive required to compose multiple children.",
384
+ },
385
+ },
386
+ propMap: {
387
+ justify: {
388
+ a2ui: "justify",
389
+ kind: "enum",
390
+ targetEnum: ["start", "center", "end", "spaceBetween", "spaceAround", "spaceEvenly", "stretch"],
391
+ default: "start",
392
+ description: "Arrangement of children along the vertical main axis.",
393
+ },
394
+ align: {
395
+ a2ui: "align",
396
+ kind: "enum",
397
+ targetEnum: ["center", "end", "start", "stretch"],
398
+ default: "stretch",
399
+ description: "Alignment of children along the horizontal cross axis.",
400
+ },
401
+ },
402
+ required: ["children"],
403
+ },
404
+ ],
405
+
406
+ casualtyComponents: [
407
+ {
408
+ dspackId: "dialog",
409
+ attempted: "Modal",
410
+ class: "cannot-represent",
411
+ reason:
412
+ "dspack Dialog is a compound component (DialogTrigger/Content/Header/Title/Description/" +
413
+ "Footer/Close) with required-children composition rules, focus management, and a11y roles. " +
414
+ "A2UI Modal exposes only trigger+content; the composition contract cannot be represented.",
415
+ },
416
+ {
417
+ dspackId: "dropdown-menu",
418
+ attempted: "(none)",
419
+ class: "cannot-represent",
420
+ reason:
421
+ "No A2UI basic component corresponds to a dropdown menu (items, checkbox/radio items, " +
422
+ "sub-menus, separators). Omitted.",
423
+ },
424
+ ],
425
+ };
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Builds the validation + fidelity report (Markdown + JSON) for one emitted catalog.
3
+ */
4
+ import type { A2uiVersion, FidelityClass, FidelityEntry, Warning } from "../types.js";
5
+ import type { MappingResult } from "./mapping.js";
6
+ import type { ValidationReport } from "../validate/ajv.js";
7
+
8
+ const CLASS_ORDER: FidelityClass[] = ["maps-cleanly", "synthesis-defaults", "lossy", "cannot-represent"];
9
+ const CLASS_LABEL: Record<FidelityClass, string> = {
10
+ "maps-cleanly": "maps cleanly",
11
+ "synthesis-defaults": "requires synthesis / defaults",
12
+ lossy: "currently lossy",
13
+ "cannot-represent": "cannot be represented",
14
+ };
15
+
16
+ export interface ReportBundle {
17
+ md: string;
18
+ json: Json;
19
+ }
20
+ type Json = Record<string, unknown>;
21
+
22
+ export function buildReport(
23
+ version: A2uiVersion,
24
+ mapping: MappingResult,
25
+ validation: ValidationReport,
26
+ ): ReportBundle {
27
+ const counts = countByClass(mapping.fidelity);
28
+
29
+ const json: Json = {
30
+ a2uiVersion: version,
31
+ generatedAt: new Date().toISOString(),
32
+ validation: { pass: validation.pass, gates: validation.gates },
33
+ fidelity: {
34
+ summary: counts,
35
+ entries: mapping.fidelity,
36
+ },
37
+ warnings: mapping.warnings,
38
+ emittedComponents: mapping.componentOrder,
39
+ primaryColor: { hex: mapping.primaryColorHex, source: mapping.primaryColorSource },
40
+ coverage: {
41
+ unclassified: mapping.coverage.filter((c) => c.disposition === "unclassified").map((c) => c.id),
42
+ entries: mapping.coverage,
43
+ },
44
+ };
45
+
46
+ const md = [
47
+ `# Validation & fidelity report — A2UI v${version}`,
48
+ "",
49
+ `Emitted components: ${mapping.componentOrder.map((c) => "`" + c + "`").join(", ")}`,
50
+ `Primary color token: ${mapping.primaryColorSource ?? "(none)"} → \`${mapping.primaryColorHex ?? "—"}\``,
51
+ "",
52
+ `## Validation — ${validation.pass ? "✅ PASS" : "❌ FAIL"} (hard gate)`,
53
+ "",
54
+ "| Gate | Result | Detail |",
55
+ "| --- | --- | --- |",
56
+ ...validation.gates.map(
57
+ (g) => `| ${g.name} | ${g.pass ? "✅ pass" : "❌ fail"} | ${escapePipes(g.detail)} |`,
58
+ ),
59
+ "",
60
+ ...validation.gates
61
+ .filter((g) => g.errors?.length)
62
+ .flatMap((g) => [`### ${g.name} — details`, "", ...g.errors!.map((e) => `- ${escapePipes(e)}`), ""]),
63
+ `## Fidelity summary`,
64
+ "",
65
+ "| Class | Count |",
66
+ "| --- | --- |",
67
+ ...CLASS_ORDER.map((c) => `| ${CLASS_LABEL[c]} | ${counts[c]} |`),
68
+ "",
69
+ `## Fidelity — per field`,
70
+ "",
71
+ "| Source (dspack) | Target (A2UI) | Class | Note |",
72
+ "| --- | --- | --- | --- |",
73
+ ...mapping.fidelity.map(
74
+ (f) =>
75
+ `| \`${f.source}\` | \`${f.target}\` | ${CLASS_LABEL[f.class]} | ${escapePipes(f.note)} |`,
76
+ ),
77
+ "",
78
+ `## Component coverage (every input dspack component accounted for)`,
79
+ "",
80
+ "| dspack component | Disposition | Detail |",
81
+ "| --- | --- | --- |",
82
+ ...mapping.coverage.map(
83
+ (c) => `| \`${c.id}\` | ${c.disposition === "unclassified" ? "⚠️ **unclassified**" : c.disposition} | ${escapePipes(c.detail ?? "")} |`,
84
+ ),
85
+ "",
86
+ `## Warnings (unsupported dspack constructs)`,
87
+ "",
88
+ mapping.warnings.length === 0
89
+ ? "_None._"
90
+ : mapping.warnings.map((w: Warning) => `- **${w.code}** — ${escapePipes(w.message)}`).join("\n"),
91
+ "",
92
+ ].join("\n");
93
+
94
+ return { md, json };
95
+ }
96
+
97
+ function countByClass(entries: FidelityEntry[]): Record<FidelityClass, number> {
98
+ const counts = { "maps-cleanly": 0, "synthesis-defaults": 0, lossy: 0, "cannot-represent": 0 };
99
+ for (const e of entries) counts[e.class]++;
100
+ return counts;
101
+ }
102
+
103
+ function escapePipes(s: string): string {
104
+ return s.replace(/\|/g, "\\|").replace(/\n/g, " ");
105
+ }
@@ -0,0 +1,112 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { readFileSync } from "node:fs";
3
+ import { fileURLToPath } from "node:url";
4
+ import { toHex6, hslToHex } from "./transform/color.js";
5
+ import { transform } from "./transform/index.js";
6
+ import { validateCatalog } from "./validate/ajv.js";
7
+ import type { DspackDoc } from "./types.js";
8
+
9
+ const repo = (p: string) => fileURLToPath(new URL(`../${p}`, import.meta.url));
10
+ const doc = JSON.parse(readFileSync(repo("input/shadcn-ui.dspack.json"), "utf8")) as DspackDoc;
11
+ const surface = JSON.parse(readFileSync(repo("surface/settings-card.surface.json"), "utf8"));
12
+
13
+ describe("color synthesis", () => {
14
+ it("converts the shadcn primary HSL to the expected hex", () => {
15
+ expect(toHex6("hsl(222.2, 47.4%, 11.2%)")).toBe("#0f172a");
16
+ });
17
+ it("passes hex through and expands shorthand", () => {
18
+ expect(toHex6("#00BFFF")).toBe("#00bfff");
19
+ expect(toHex6("#abc")).toBe("#aabbcc");
20
+ });
21
+ it("returns null for unconvertible values", () => {
22
+ expect(toHex6("calc(0.5rem - 4px)")).toBeNull();
23
+ });
24
+ it("hslToHex handles primary/secondary hues", () => {
25
+ expect(hslToHex(0, 0, 100)).toBe("#ffffff");
26
+ expect(hslToHex(0, 0, 0)).toBe("#000000");
27
+ });
28
+ });
29
+
30
+ describe("profile mapping", () => {
31
+ const { catalog, mapping } = transform(doc, "0.9.1", surface);
32
+ it("emits the expected component set", () => {
33
+ expect(Object.keys(catalog.components)).toEqual([
34
+ "Button",
35
+ "Card",
36
+ "TextField",
37
+ "Badge",
38
+ "Table",
39
+ "AlertDialog",
40
+ "Text",
41
+ "Column",
42
+ ]);
43
+ });
44
+ it("projects the 6 shadcn Button variants onto 3 A2UI variants (lossy)", () => {
45
+ const variant = (catalog.components.Button as any).allOf.at(-1).properties.variant;
46
+ expect(variant.enum).toEqual(["default", "primary", "borderless"]);
47
+ expect(variant["x-dspack-source"].sourceEnum).toHaveLength(6);
48
+ const lossy = mapping.fidelity.find(
49
+ (f) => f.source === "components.button.props.variant",
50
+ );
51
+ expect(lossy?.class).toBe("lossy");
52
+ });
53
+ it("records dialog/dropdown-menu as cannot-represent casualties", () => {
54
+ for (const id of ["dialog", "dropdown-menu"]) {
55
+ const f = mapping.fidelity.find((e) => e.source === `components.${id}`);
56
+ expect(f?.class).toBe("cannot-represent");
57
+ }
58
+ });
59
+ it("emits Table/Badge/AlertDialog but records their composition loss as a casualty", () => {
60
+ // alert-dialog is now emitted as a component, yet its dspack composition cannot survive.
61
+ const composition = mapping.fidelity.find(
62
+ (e) => e.source === "components.alert-dialog.composition",
63
+ );
64
+ expect(composition?.class).toBe("cannot-represent");
65
+ });
66
+ it("warns about the unsupported dspack knowledge layer", () => {
67
+ const codes = mapping.warnings.map((w) => w.code);
68
+ expect(codes).toContain("unsupported-section:patterns");
69
+ expect(codes).toContain("unsupported-section:antiPatterns");
70
+ });
71
+ });
72
+
73
+ describe("validation gates", () => {
74
+ it("passes all three gates for both emitted versions", () => {
75
+ for (const v of ["0.9.1", "1.0"] as const) {
76
+ const { validation } = transform(doc, v, surface);
77
+ expect(validation.pass, `v${v} should pass`).toBe(true);
78
+ expect(validation.gates.map((g) => g.name)).toEqual([
79
+ "schema-compile + no-external-ref",
80
+ "catalog-shape",
81
+ "instance",
82
+ ]);
83
+ }
84
+ });
85
+
86
+ it("the instance gate is not vacuous: rejects an invalid Button instance", () => {
87
+ const { catalog } = transform(doc, "0.9.1", surface);
88
+ const bad = {
89
+ messages: [
90
+ {
91
+ updateComponents: {
92
+ components: [
93
+ // invalid enum value + missing required `action`
94
+ { id: "b", component: "Button", child: "x", variant: "destructive" },
95
+ ],
96
+ },
97
+ },
98
+ ],
99
+ };
100
+ const report = validateCatalog(catalog, "0.9.1", bad);
101
+ const instance = report.gates.find((g) => g.name === "instance")!;
102
+ expect(instance.pass).toBe(false);
103
+ expect(report.pass).toBe(false);
104
+ });
105
+
106
+ it("the catalog-shape gate is version-distinct (a v1.0 catalog fails the v0.9.1 meta-schema)", () => {
107
+ const { catalog: v1 } = transform(doc, "1.0", surface);
108
+ const asNineOne = validateCatalog(v1, "0.9.1");
109
+ const shape = asNineOne.gates.find((g) => g.name === "catalog-shape")!;
110
+ expect(shape.pass).toBe(false);
111
+ });
112
+ });