@specverse/engines 4.1.28 → 4.2.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 (237) hide show
  1. package/assets/examples/manifests/frontend-only.yaml +3 -6
  2. package/assets/examples/manifests/fullstack-app.yaml +5 -7
  3. package/assets/examples/manifests/fullstack-monorepo.yaml +3 -6
  4. package/dist/inference/comprehensive-engine.d.ts.map +1 -1
  5. package/dist/inference/comprehensive-engine.js +3 -19
  6. package/dist/inference/comprehensive-engine.js.map +1 -1
  7. package/dist/inference/core/rule-engine.d.ts +31 -0
  8. package/dist/inference/core/rule-engine.d.ts.map +1 -1
  9. package/dist/inference/core/rule-engine.js +117 -33
  10. package/dist/inference/core/rule-engine.js.map +1 -1
  11. package/dist/inference/core/rule-file-types.d.ts +0 -2
  12. package/dist/inference/core/rule-file-types.d.ts.map +1 -1
  13. package/dist/inference/core/rule-file-types.js +3 -6
  14. package/dist/inference/core/rule-file-types.js.map +1 -1
  15. package/dist/inference/core/rule-loader.d.ts +5 -15
  16. package/dist/inference/core/rule-loader.d.ts.map +1 -1
  17. package/dist/inference/core/rule-loader.js +43 -132
  18. package/dist/inference/core/rule-loader.js.map +1 -1
  19. package/dist/inference/core/types.d.ts +0 -6
  20. package/dist/inference/core/types.d.ts.map +1 -1
  21. package/dist/inference/core/types.js +0 -4
  22. package/dist/inference/core/types.js.map +1 -1
  23. package/dist/inference/logical/generators/component-type-resolver.d.ts +0 -26
  24. package/dist/inference/logical/generators/component-type-resolver.d.ts.map +1 -1
  25. package/dist/inference/logical/generators/component-type-resolver.js +0 -19
  26. package/dist/inference/logical/generators/component-type-resolver.js.map +1 -1
  27. package/dist/inference/logical/generators/specialist-view-expander.d.ts +1 -17
  28. package/dist/inference/logical/generators/specialist-view-expander.d.ts.map +1 -1
  29. package/dist/inference/logical/generators/specialist-view-expander.js +0 -15
  30. package/dist/inference/logical/generators/specialist-view-expander.js.map +1 -1
  31. package/dist/inference/logical/generators/view-generator.d.ts +4 -14
  32. package/dist/inference/logical/generators/view-generator.d.ts.map +1 -1
  33. package/dist/inference/logical/generators/view-generator.js +6 -26
  34. package/dist/inference/logical/generators/view-generator.js.map +1 -1
  35. package/dist/inference/logical/index.d.ts +2 -2
  36. package/dist/inference/logical/index.d.ts.map +1 -1
  37. package/dist/inference/logical/logical-engine.d.ts.map +1 -1
  38. package/dist/inference/logical/logical-engine.js +17 -80
  39. package/dist/inference/logical/logical-engine.js.map +1 -1
  40. package/dist/inference/quint-transpiler.d.ts +5 -3
  41. package/dist/inference/quint-transpiler.d.ts.map +1 -1
  42. package/dist/inference/quint-transpiler.js +11 -6
  43. package/dist/inference/quint-transpiler.js.map +1 -1
  44. package/dist/libs/instance-factories/applications/templates/generic/main-generator.js +3 -3
  45. package/dist/libs/instance-factories/applications/templates/react/api-client-generator.js +16 -6
  46. package/dist/libs/instance-factories/applications/templates/react-starter/app-tsx-generator.js +110 -0
  47. package/dist/libs/instance-factories/applications/templates/react-starter/dashboard-body-composer.js +121 -0
  48. package/dist/libs/instance-factories/applications/templates/react-starter/detail-body-composer.js +78 -0
  49. package/dist/libs/instance-factories/applications/templates/react-starter/form-body-composer.js +190 -0
  50. package/dist/libs/instance-factories/applications/templates/react-starter/helpers-emitter.js +45 -0
  51. package/dist/libs/instance-factories/applications/templates/react-starter/html-to-jsx.js +192 -0
  52. package/dist/libs/instance-factories/applications/templates/react-starter/list-body-composer.js +46 -0
  53. package/dist/libs/instance-factories/applications/templates/react-starter/orchestrator.js +30 -0
  54. package/dist/libs/instance-factories/applications/templates/react-starter/package-json-generator.js +38 -0
  55. package/dist/libs/instance-factories/applications/templates/react-starter/regen-safety.js +89 -0
  56. package/dist/libs/instance-factories/applications/templates/react-starter/view-emitter.js +56 -0
  57. package/dist/libs/instance-factories/applications/templates/react-starter/views-generator.js +66 -0
  58. package/dist/libs/instance-factories/cli/templates/commander/command-generator.js +14 -11
  59. package/dist/libs/instance-factories/controllers/templates/fastify/server-generator.js +11 -3
  60. package/dist/libs/instance-factories/services/templates/prisma/controller-generator.js +27 -17
  61. package/dist/libs/instance-factories/shared/path-resolver.js +1 -1
  62. package/dist/realize/index.d.ts.map +1 -1
  63. package/dist/realize/index.js +15 -22
  64. package/dist/realize/index.js.map +1 -1
  65. package/dist/registry/utils/manifest-adapter.d.ts +8 -1
  66. package/dist/registry/utils/manifest-adapter.d.ts.map +1 -1
  67. package/dist/registry/utils/manifest-adapter.js +8 -1
  68. package/dist/registry/utils/manifest-adapter.js.map +1 -1
  69. package/libs/instance-factories/applications/react-app-starter.yaml +150 -0
  70. package/libs/instance-factories/applications/templates/generic/main-generator.ts +3 -3
  71. package/libs/instance-factories/applications/templates/react/api-client-generator.ts +16 -6
  72. package/libs/instance-factories/applications/templates/react-starter/README.md +211 -0
  73. package/libs/instance-factories/applications/templates/react-starter/__tests__/dashboard-body-composer.test.ts +153 -0
  74. package/libs/instance-factories/applications/templates/react-starter/__tests__/detail-body-composer.test.ts +145 -0
  75. package/libs/instance-factories/applications/templates/react-starter/__tests__/form-body-composer.test.ts +175 -0
  76. package/libs/instance-factories/applications/templates/react-starter/__tests__/helpers-emitter.test.ts +55 -0
  77. package/libs/instance-factories/applications/templates/react-starter/__tests__/html-to-jsx.test.ts +140 -0
  78. package/libs/instance-factories/applications/templates/react-starter/__tests__/list-body-composer.test.ts +146 -0
  79. package/libs/instance-factories/applications/templates/react-starter/__tests__/orchestrator.test.ts +163 -0
  80. package/libs/instance-factories/applications/templates/react-starter/__tests__/parity-p2-factory-imports.test.ts +116 -0
  81. package/libs/instance-factories/applications/templates/react-starter/__tests__/parity-p3-rendered-output.test.ts +183 -0
  82. package/libs/instance-factories/applications/templates/react-starter/__tests__/regen-safety.test.ts +144 -0
  83. package/libs/instance-factories/applications/templates/react-starter/__tests__/starter-generators.test.ts +114 -0
  84. package/libs/instance-factories/applications/templates/react-starter/__tests__/view-emitter.test.ts +107 -0
  85. package/libs/instance-factories/applications/templates/react-starter/__tests__/views-generator.test.ts +139 -0
  86. package/libs/instance-factories/applications/templates/react-starter/app-tsx-generator.ts +141 -0
  87. package/libs/instance-factories/applications/templates/react-starter/dashboard-body-composer.ts +174 -0
  88. package/libs/instance-factories/applications/templates/react-starter/detail-body-composer.ts +135 -0
  89. package/libs/instance-factories/applications/templates/react-starter/form-body-composer.ts +306 -0
  90. package/libs/instance-factories/applications/templates/react-starter/helpers-emitter.ts +60 -0
  91. package/libs/instance-factories/applications/templates/react-starter/html-to-jsx.ts +334 -0
  92. package/libs/instance-factories/applications/templates/react-starter/list-body-composer.ts +120 -0
  93. package/libs/instance-factories/applications/templates/react-starter/orchestrator.ts +80 -0
  94. package/libs/instance-factories/applications/templates/react-starter/package-json-generator.ts +57 -0
  95. package/libs/instance-factories/applications/templates/react-starter/regen-safety.ts +157 -0
  96. package/libs/instance-factories/applications/templates/react-starter/skeletons/dashboard.tsx.template +47 -0
  97. package/libs/instance-factories/applications/templates/react-starter/skeletons/detail.tsx.template +94 -0
  98. package/libs/instance-factories/applications/templates/react-starter/skeletons/form.tsx.template +114 -0
  99. package/libs/instance-factories/applications/templates/react-starter/skeletons/list.tsx.template +72 -0
  100. package/libs/instance-factories/applications/templates/react-starter/view-emitter.ts +151 -0
  101. package/libs/instance-factories/applications/templates/react-starter/views-generator.ts +137 -0
  102. package/libs/instance-factories/cli/templates/commander/command-generator.ts +14 -11
  103. package/libs/instance-factories/controllers/templates/fastify/server-generator.ts +11 -3
  104. package/libs/instance-factories/services/templates/prisma/controller-generator.ts +27 -17
  105. package/libs/instance-factories/shared/path-resolver.ts +8 -2
  106. package/package.json +3 -3
  107. package/dist/libs/instance-factories/applications/templates/react/_view-components-source.js +0 -530
  108. package/dist/libs/instance-factories/applications/templates/react/app-tsx-generator.js +0 -73
  109. package/dist/libs/instance-factories/applications/templates/react/field-helpers-generator.js +0 -99
  110. package/dist/libs/instance-factories/applications/templates/react/package-json-generator.js +0 -49
  111. package/dist/libs/instance-factories/applications/templates/react/pattern-adapter-generator.js +0 -156
  112. package/dist/libs/instance-factories/applications/templates/react/react-pattern-adapter.js +0 -935
  113. package/dist/libs/instance-factories/applications/templates/react/relationship-field-generator.js +0 -143
  114. package/dist/libs/instance-factories/applications/templates/react/tailwind-adapter-generator.js +0 -646
  115. package/dist/libs/instance-factories/applications/templates/react/tailwind-adapter-wrapper-generator.js +0 -65
  116. package/dist/libs/instance-factories/applications/templates/react/view-dashboard-generator.js +0 -143
  117. package/dist/libs/instance-factories/applications/templates/react/view-detail-generator.js +0 -143
  118. package/dist/libs/instance-factories/applications/templates/react/view-form-generator.js +0 -355
  119. package/dist/libs/instance-factories/applications/templates/react/view-list-generator.js +0 -91
  120. package/dist/libs/instance-factories/applications/templates/react/view-router-generator.js +0 -79
  121. package/dist/libs/instance-factories/tools/templates/vscode/vscode-extension-generator.js.bak +0 -244
  122. package/dist/libs/instance-factories/views/index.js +0 -48
  123. package/dist/libs/instance-factories/views/templates/react/adapters/antd-adapter.js +0 -742
  124. package/dist/libs/instance-factories/views/templates/react/adapters/mui-adapter.js +0 -824
  125. package/dist/libs/instance-factories/views/templates/react/adapters/shadcn-adapter.js +0 -719
  126. package/dist/libs/instance-factories/views/templates/react/app-generator.js +0 -45
  127. package/dist/libs/instance-factories/views/templates/react/components-generator.js +0 -820
  128. package/dist/libs/instance-factories/views/templates/react/forms-generator.js +0 -275
  129. package/dist/libs/instance-factories/views/templates/react/frontend-package-json-generator.js +0 -46
  130. package/dist/libs/instance-factories/views/templates/react/hooks-generator.js +0 -81
  131. package/dist/libs/instance-factories/views/templates/react/index-css-generator.js +0 -9
  132. package/dist/libs/instance-factories/views/templates/react/index-html-generator.js +0 -23
  133. package/dist/libs/instance-factories/views/templates/react/main-tsx-generator.js +0 -21
  134. package/dist/libs/instance-factories/views/templates/react/react-component-generator.js +0 -299
  135. package/dist/libs/instance-factories/views/templates/react/router-generator.js +0 -136
  136. package/dist/libs/instance-factories/views/templates/react/router-generic-generator.js +0 -107
  137. package/dist/libs/instance-factories/views/templates/react/shared-utils-generator.js +0 -187
  138. package/dist/libs/instance-factories/views/templates/react/spec-json-generator.js +0 -7
  139. package/dist/libs/instance-factories/views/templates/react/types-generator.js +0 -56
  140. package/dist/libs/instance-factories/views/templates/react/views-metadata-generator.js +0 -27
  141. package/dist/libs/instance-factories/views/templates/react/vite-config-generator.js +0 -29
  142. package/dist/libs/instance-factories/views/templates/runtime/runtime-view-renderer.js +0 -261
  143. package/dist/libs/instance-factories/views/templates/shared/adapter-types.js +0 -34
  144. package/dist/libs/instance-factories/views/templates/shared/atomic-components-registry.js +0 -800
  145. package/dist/libs/instance-factories/views/templates/shared/base-generator.js +0 -305
  146. package/dist/libs/instance-factories/views/templates/shared/component-metadata.js +0 -517
  147. package/dist/libs/instance-factories/views/templates/shared/composite-pattern-types.js +0 -0
  148. package/dist/libs/instance-factories/views/templates/shared/composite-patterns.js +0 -445
  149. package/dist/libs/instance-factories/views/templates/shared/index.js +0 -80
  150. package/dist/libs/instance-factories/views/templates/shared/pattern-validator.js +0 -210
  151. package/dist/libs/instance-factories/views/templates/shared/property-mapper.js +0 -492
  152. package/dist/libs/instance-factories/views/templates/shared/syntax-mapper.js +0 -321
  153. package/dist/realize/index.js.bak +0 -758
  154. package/libs/instance-factories/applications/react-app.yaml +0 -186
  155. package/libs/instance-factories/applications/templates/react/_view-components-source.ts +0 -555
  156. package/libs/instance-factories/applications/templates/react/app-tsx-generator.ts +0 -94
  157. package/libs/instance-factories/applications/templates/react/field-helpers-generator.ts +0 -106
  158. package/libs/instance-factories/applications/templates/react/package-json-generator.ts +0 -57
  159. package/libs/instance-factories/applications/templates/react/pattern-adapter-generator.ts +0 -179
  160. package/libs/instance-factories/applications/templates/react/react-pattern-adapter.tsx +0 -1347
  161. package/libs/instance-factories/applications/templates/react/relationship-field-generator.ts +0 -150
  162. package/libs/instance-factories/applications/templates/react/tailwind-adapter-generator.ts +0 -704
  163. package/libs/instance-factories/applications/templates/react/tailwind-adapter-wrapper-generator.ts +0 -84
  164. package/libs/instance-factories/applications/templates/react/view-dashboard-generator.ts +0 -150
  165. package/libs/instance-factories/applications/templates/react/view-detail-generator.ts +0 -150
  166. package/libs/instance-factories/applications/templates/react/view-form-generator.ts +0 -362
  167. package/libs/instance-factories/applications/templates/react/view-list-generator.ts +0 -98
  168. package/libs/instance-factories/applications/templates/react/view-router-generator.ts +0 -89
  169. package/libs/instance-factories/views/README.md +0 -62
  170. package/libs/instance-factories/views/index.d.ts +0 -13
  171. package/libs/instance-factories/views/index.d.ts.map +0 -1
  172. package/libs/instance-factories/views/index.js +0 -18
  173. package/libs/instance-factories/views/index.js.map +0 -1
  174. package/libs/instance-factories/views/index.ts +0 -45
  175. package/libs/instance-factories/views/react-components.yaml +0 -129
  176. package/libs/instance-factories/views/templates/ARCHITECTURE.md +0 -198
  177. package/libs/instance-factories/views/templates/react/adapters/antd-adapter.ts +0 -869
  178. package/libs/instance-factories/views/templates/react/adapters/mui-adapter.ts +0 -953
  179. package/libs/instance-factories/views/templates/react/adapters/shadcn-adapter.ts +0 -806
  180. package/libs/instance-factories/views/templates/react/app-generator.ts +0 -55
  181. package/libs/instance-factories/views/templates/react/components-generator.ts +0 -938
  182. package/libs/instance-factories/views/templates/react/forms-generator.ts +0 -325
  183. package/libs/instance-factories/views/templates/react/frontend-package-json-generator.ts +0 -57
  184. package/libs/instance-factories/views/templates/react/hooks-generator.ts +0 -106
  185. package/libs/instance-factories/views/templates/react/index-css-generator.ts +0 -14
  186. package/libs/instance-factories/views/templates/react/index-html-generator.ts +0 -34
  187. package/libs/instance-factories/views/templates/react/main-tsx-generator.ts +0 -29
  188. package/libs/instance-factories/views/templates/react/react-component-generator.d.ts +0 -152
  189. package/libs/instance-factories/views/templates/react/react-component-generator.d.ts.map +0 -1
  190. package/libs/instance-factories/views/templates/react/react-component-generator.js +0 -398
  191. package/libs/instance-factories/views/templates/react/react-component-generator.js.map +0 -1
  192. package/libs/instance-factories/views/templates/react/react-component-generator.ts +0 -533
  193. package/libs/instance-factories/views/templates/react/router-generator.ts +0 -197
  194. package/libs/instance-factories/views/templates/react/router-generic-generator.ts +0 -132
  195. package/libs/instance-factories/views/templates/react/shared-utils-generator.ts +0 -196
  196. package/libs/instance-factories/views/templates/react/spec-json-generator.ts +0 -17
  197. package/libs/instance-factories/views/templates/react/types-generator.ts +0 -76
  198. package/libs/instance-factories/views/templates/react/views-metadata-generator.ts +0 -42
  199. package/libs/instance-factories/views/templates/react/vite-config-generator.ts +0 -38
  200. package/libs/instance-factories/views/templates/runtime/runtime-view-renderer.d.ts.map +0 -1
  201. package/libs/instance-factories/views/templates/runtime/runtime-view-renderer.js.map +0 -1
  202. package/libs/instance-factories/views/templates/runtime/runtime-view-renderer.ts +0 -474
  203. package/libs/instance-factories/views/templates/shared/__tests__/composite-patterns.test.ts +0 -242
  204. package/libs/instance-factories/views/templates/shared/adapter-types.d.ts +0 -77
  205. package/libs/instance-factories/views/templates/shared/adapter-types.d.ts.map +0 -1
  206. package/libs/instance-factories/views/templates/shared/adapter-types.js +0 -47
  207. package/libs/instance-factories/views/templates/shared/adapter-types.js.map +0 -1
  208. package/libs/instance-factories/views/templates/shared/adapter-types.ts +0 -142
  209. package/libs/instance-factories/views/templates/shared/atomic-components-registry.d.ts +0 -63
  210. package/libs/instance-factories/views/templates/shared/atomic-components-registry.d.ts.map +0 -1
  211. package/libs/instance-factories/views/templates/shared/atomic-components-registry.js +0 -822
  212. package/libs/instance-factories/views/templates/shared/atomic-components-registry.js.map +0 -1
  213. package/libs/instance-factories/views/templates/shared/atomic-components-registry.ts +0 -908
  214. package/libs/instance-factories/views/templates/shared/base-generator.d.ts +0 -247
  215. package/libs/instance-factories/views/templates/shared/base-generator.d.ts.map +0 -1
  216. package/libs/instance-factories/views/templates/shared/base-generator.js +0 -363
  217. package/libs/instance-factories/views/templates/shared/base-generator.js.map +0 -1
  218. package/libs/instance-factories/views/templates/shared/base-generator.ts +0 -608
  219. package/libs/instance-factories/views/templates/shared/component-metadata.d.ts +0 -254
  220. package/libs/instance-factories/views/templates/shared/component-metadata.d.ts.map +0 -1
  221. package/libs/instance-factories/views/templates/shared/component-metadata.js +0 -602
  222. package/libs/instance-factories/views/templates/shared/component-metadata.js.map +0 -1
  223. package/libs/instance-factories/views/templates/shared/component-metadata.ts +0 -803
  224. package/libs/instance-factories/views/templates/shared/composite-pattern-types.ts +0 -250
  225. package/libs/instance-factories/views/templates/shared/composite-patterns.ts +0 -535
  226. package/libs/instance-factories/views/templates/shared/index.ts +0 -68
  227. package/libs/instance-factories/views/templates/shared/pattern-validator.ts +0 -279
  228. package/libs/instance-factories/views/templates/shared/property-mapper.d.ts +0 -149
  229. package/libs/instance-factories/views/templates/shared/property-mapper.d.ts.map +0 -1
  230. package/libs/instance-factories/views/templates/shared/property-mapper.js +0 -580
  231. package/libs/instance-factories/views/templates/shared/property-mapper.js.map +0 -1
  232. package/libs/instance-factories/views/templates/shared/property-mapper.ts +0 -700
  233. package/libs/instance-factories/views/templates/shared/syntax-mapper.d.ts +0 -143
  234. package/libs/instance-factories/views/templates/shared/syntax-mapper.d.ts.map +0 -1
  235. package/libs/instance-factories/views/templates/shared/syntax-mapper.js +0 -420
  236. package/libs/instance-factories/views/templates/shared/syntax-mapper.js.map +0 -1
  237. package/libs/instance-factories/views/templates/shared/syntax-mapper.ts +0 -539
@@ -0,0 +1,114 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import * as ts from 'typescript';
3
+ import { generate as generateAppTsx } from '../app-tsx-generator.js';
4
+ import { generate as generatePackageJson } from '../package-json-generator.js';
5
+
6
+ function assertValidTsx(source: string, label: string): void {
7
+ const result = ts.transpileModule(source, {
8
+ compilerOptions: {
9
+ jsx: ts.JsxEmit.Preserve,
10
+ target: ts.ScriptTarget.ES2022,
11
+ module: ts.ModuleKind.ESNext,
12
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
13
+ strict: false,
14
+ },
15
+ reportDiagnostics: true,
16
+ });
17
+ const errors = result.diagnostics?.filter(d => d.category === ts.DiagnosticCategory.Error) ?? [];
18
+ if (errors.length > 0) {
19
+ const message = errors
20
+ .map(d => ts.flattenDiagnosticMessageText(d.messageText, '\n'))
21
+ .join('\n');
22
+ throw new Error(`${label} failed to parse:\n${message}\n\n--- source ---\n${source}`);
23
+ }
24
+ }
25
+
26
+ describe('app-tsx-generator', () => {
27
+ it('produces valid TSX with one branch per model and view type', async () => {
28
+ const source = await generateAppTsx({
29
+ spec: {
30
+ models: {
31
+ Post: { name: 'Post', attributes: {} },
32
+ Author: { name: 'Author', attributes: {} },
33
+ },
34
+ },
35
+ });
36
+ assertValidTsx(source, 'App.tsx');
37
+ // One nav section per model
38
+ expect(source).toContain('Post');
39
+ expect(source).toContain('Author');
40
+ // One import per (model, view) pair — 2 models × 4 view types = 8 imports
41
+ const importLines = source.match(/^import \{ \w+View \} from '\.\/views\//gm) ?? [];
42
+ expect(importLines.length).toBe(8);
43
+ // Navigation wiring
44
+ expect(source).toContain("select('Post', 'list')");
45
+ expect(source).toContain("select('Author', 'dashboard')");
46
+ });
47
+
48
+ it('handles an empty spec gracefully', async () => {
49
+ const source = await generateAppTsx({ spec: { models: {}, views: {} } });
50
+ assertValidTsx(source, 'App.tsx (empty spec)');
51
+ expect(source).toContain('No models in this spec.');
52
+ // Still renders a QueryClientProvider
53
+ expect(source).toContain('QueryClientProvider');
54
+ });
55
+
56
+ it('uses @tanstack/react-query, not @specverse/runtime', async () => {
57
+ const source = await generateAppTsx({
58
+ spec: { models: { Post: { name: 'Post', attributes: {} } } },
59
+ });
60
+ expect(source).not.toContain('@specverse/runtime');
61
+ expect(source).toContain('@tanstack/react-query');
62
+ });
63
+ });
64
+
65
+ describe('package-json-generator', () => {
66
+ it('omits @specverse/runtime from dependencies', async () => {
67
+ const json = await generatePackageJson({ spec: { name: 'test' } });
68
+ const pkg = JSON.parse(json);
69
+ const allDeps = { ...pkg.dependencies, ...pkg.devDependencies };
70
+ expect(allDeps['@specverse/runtime']).toBeUndefined();
71
+ expect(allDeps['@specverse/engines']).toBeUndefined();
72
+ expect(allDeps['@specverse/types']).toBeUndefined();
73
+ });
74
+
75
+ it('includes the deps the emitted views actually need', async () => {
76
+ const json = await generatePackageJson({ spec: { name: 'test' } });
77
+ const pkg = JSON.parse(json);
78
+ expect(pkg.dependencies).toMatchObject({
79
+ react: expect.any(String),
80
+ 'react-dom': expect.any(String),
81
+ '@tanstack/react-query': expect.any(String),
82
+ });
83
+ expect(pkg.devDependencies).toMatchObject({
84
+ typescript: expect.any(String),
85
+ vite: expect.any(String),
86
+ tailwindcss: expect.any(String),
87
+ });
88
+ });
89
+
90
+ it('slugifies the app name', async () => {
91
+ const json = await generatePackageJson({
92
+ spec: { metadata: { name: 'My Fancy App!' } },
93
+ });
94
+ const pkg = JSON.parse(json);
95
+ expect(pkg.name).toBe('my-fancy-app');
96
+ });
97
+
98
+ it('falls back to a default name when none is provided', async () => {
99
+ const json = await generatePackageJson({ spec: {} });
100
+ const pkg = JSON.parse(json);
101
+ expect(pkg.name).toBe('specverse-starter-app');
102
+ });
103
+
104
+ it('parses as valid JSON with the expected shape', async () => {
105
+ const json = await generatePackageJson({ spec: { name: 'test' } });
106
+ const pkg = JSON.parse(json);
107
+ expect(pkg.type).toBe('module');
108
+ expect(pkg.scripts).toMatchObject({
109
+ dev: 'vite',
110
+ build: 'tsc && vite build',
111
+ preview: 'vite preview',
112
+ });
113
+ });
114
+ });
@@ -0,0 +1,107 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import * as ts from 'typescript';
3
+ import { emitView, type EmitContext, type ModelSpec, type ViewSpec } from '../view-emitter.js';
4
+
5
+ /**
6
+ * Build a minimal context. The `renderBody` is a stub that returns a
7
+ * fixed Tailwind table — the point of these tests is the emitter
8
+ * orchestration + substitutions + JSX validity, not the body's content.
9
+ * Parity with the real Tailwind adapter is covered by Phase 3 (P3).
10
+ */
11
+ function makeContext(overrides: Partial<EmitContext> = {}): EmitContext {
12
+ const defaultModel: ModelSpec = {
13
+ name: 'Post',
14
+ attributes: {
15
+ id: { type: 'UUID', required: true },
16
+ title: { type: 'String', required: true },
17
+ body: { type: 'Text', required: false },
18
+ },
19
+ };
20
+
21
+ const defaultView: ViewSpec = {
22
+ type: 'list',
23
+ model: 'Post',
24
+ };
25
+
26
+ return {
27
+ view: defaultView,
28
+ viewName: 'PostListView',
29
+ model: defaultModel,
30
+ modelSchemas: { Post: defaultModel },
31
+ // renderBody returns JSX-ready source that will be injected verbatim
32
+ // into {{BODY}}. NOT raw HTML — that's the composer's job.
33
+ renderBody: () =>
34
+ '<table className="w-full"><thead><tr><th className="px-4 py-2">Title</th></tr></thead>' +
35
+ '<tbody><tr><td className="px-4 py-2">Example</td></tr></tbody></table>',
36
+ ...overrides,
37
+ };
38
+ }
39
+
40
+ function assertValidTsx(source: string, label: string): void {
41
+ const result = ts.transpileModule(source, {
42
+ compilerOptions: {
43
+ jsx: ts.JsxEmit.Preserve,
44
+ target: ts.ScriptTarget.ES2022,
45
+ module: ts.ModuleKind.ESNext,
46
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
47
+ strict: false,
48
+ },
49
+ reportDiagnostics: true,
50
+ });
51
+ const errors = result.diagnostics?.filter(d => d.category === ts.DiagnosticCategory.Error) ?? [];
52
+ if (errors.length > 0) {
53
+ const message = errors
54
+ .map(d => ts.flattenDiagnosticMessageText(d.messageText, '\n'))
55
+ .join('\n');
56
+ throw new Error(`${label} failed to parse as TSX:\n${message}\n\n--- source ---\n${source}`);
57
+ }
58
+ }
59
+
60
+ describe('emitView — list', () => {
61
+ it('produces TSX that parses cleanly', () => {
62
+ const context = makeContext();
63
+ const source = emitView(context);
64
+ assertValidTsx(source, 'emitted list view');
65
+ });
66
+
67
+ it('substitutes model names into the skeleton', () => {
68
+ const source = emitView(makeContext());
69
+ expect(source).toContain('PostListView'); // component name
70
+ expect(source).toContain('usePostsQuery'); // generated hook name
71
+ expect(source).toContain('useDeletePostMutation'); // generated hook name
72
+ expect(source).toContain("Search posts…"); // pluralized lowercase
73
+ expect(source).toContain('+ New Post'); // action button label
74
+ expect(source).toContain('No posts yet.'); // empty state
75
+ });
76
+
77
+ it('substitutes a non-trivial plural correctly', () => {
78
+ const m: ModelSpec = { name: 'Category', attributes: {} };
79
+ const source = emitView(makeContext({
80
+ model: m,
81
+ view: { type: 'list', model: 'Category' },
82
+ viewName: 'CategoryListView',
83
+ modelSchemas: { Category: m },
84
+ }));
85
+ expect(source).toContain('CategoryListView');
86
+ expect(source).toContain('useCategoriesQuery'); // "Category" → "Categories"
87
+ expect(source).toContain('Search categories…');
88
+ });
89
+
90
+ it('injects the rendered body at {{BODY}}', () => {
91
+ const source = emitView(makeContext({
92
+ renderBody: () => '<table className="my-table"><tbody><tr><td>Custom</td></tr></tbody></table>',
93
+ }));
94
+ expect(source).toContain('<table className="my-table">');
95
+ expect(source).toContain('Custom');
96
+ // The placeholder itself should be gone.
97
+ expect(source).not.toContain('{{BODY}}');
98
+ // All other placeholders should be gone too.
99
+ expect(source).not.toMatch(/\{\{[A-Z_]+\}\}/);
100
+ });
101
+
102
+ it('throws for an unknown view type', () => {
103
+ expect(() =>
104
+ emitView(makeContext({ view: { type: 'unknown-type-xyz' } }))
105
+ ).toThrow(/No skeleton registered/);
106
+ });
107
+ });
@@ -0,0 +1,139 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
2
+ import * as ts from 'typescript';
3
+ import { generate, type ExpandedSpec } from '../views-generator.js';
4
+
5
+ function makeSpec(): ExpandedSpec {
6
+ return {
7
+ models: {
8
+ Post: {
9
+ name: 'Post',
10
+ attributes: {
11
+ id: { type: 'UUID', required: true, auto: 'uuid4' },
12
+ title: { type: 'String', required: true },
13
+ body: { type: 'Text', required: false },
14
+ },
15
+ },
16
+ Author: {
17
+ name: 'Author',
18
+ attributes: {
19
+ id: { type: 'UUID', required: true, auto: 'uuid4' },
20
+ name: { type: 'String', required: true },
21
+ },
22
+ },
23
+ },
24
+ views: {},
25
+ };
26
+ }
27
+
28
+ function assertValidTsx(source: string, label: string): void {
29
+ const result = ts.transpileModule(source, {
30
+ compilerOptions: {
31
+ jsx: ts.JsxEmit.Preserve,
32
+ target: ts.ScriptTarget.ES2022,
33
+ module: ts.ModuleKind.ESNext,
34
+ moduleResolution: ts.ModuleResolutionKind.Bundler,
35
+ strict: false,
36
+ },
37
+ reportDiagnostics: true,
38
+ });
39
+ const errors = result.diagnostics?.filter(d => d.category === ts.DiagnosticCategory.Error) ?? [];
40
+ if (errors.length > 0) {
41
+ const message = errors
42
+ .map(d => ts.flattenDiagnosticMessageText(d.messageText, '\n'))
43
+ .join('\n');
44
+ throw new Error(`${label} failed to parse as TSX:\n${message}\n\n--- source ---\n${source}`);
45
+ }
46
+ }
47
+
48
+ describe('views-generator.generate', () => {
49
+ it('emits 4 view files per model plus the helpers file', async () => {
50
+ const files = await generate({ spec: makeSpec() });
51
+ const paths = Object.keys(files);
52
+ expect(paths).toContain('src/views/PostListView.tsx');
53
+ expect(paths).toContain('src/views/PostDetailView.tsx');
54
+ expect(paths).toContain('src/views/PostFormView.tsx');
55
+ expect(paths).toContain('src/views/PostDashboardView.tsx');
56
+ expect(paths).toContain('src/views/AuthorListView.tsx');
57
+ expect(paths).toContain('src/views/AuthorDetailView.tsx');
58
+ expect(paths).toContain('src/views/AuthorFormView.tsx');
59
+ expect(paths).toContain('src/views/AuthorDashboardView.tsx');
60
+ expect(paths).toContain('src/lib/entity-display.ts');
61
+ // 2 models × 4 view types + 1 helper = 9 files.
62
+ expect(paths.length).toBe(9);
63
+ });
64
+
65
+ it('every emitted view file parses as valid TSX', async () => {
66
+ const files = await generate({ spec: makeSpec() });
67
+ for (const [path, source] of Object.entries(files)) {
68
+ if (path.endsWith('.tsx')) assertValidTsx(source, path);
69
+ }
70
+ });
71
+
72
+ it('uses a user-declared view spec in preference to a synthesized default', async () => {
73
+ const spec: ExpandedSpec = {
74
+ models: {
75
+ Post: {
76
+ name: 'Post',
77
+ attributes: {
78
+ id: { type: 'UUID', auto: 'uuid4' },
79
+ title: { type: 'String', required: true },
80
+ },
81
+ },
82
+ },
83
+ views: {
84
+ PostListView: {
85
+ type: 'list',
86
+ model: 'Post',
87
+ uiComponents: {
88
+ customTable: {
89
+ type: 'table',
90
+ properties: {
91
+ columns: ['SUPER CUSTOM COLUMN'],
92
+ },
93
+ },
94
+ },
95
+ },
96
+ },
97
+ };
98
+ const files = await generate({ spec });
99
+ const out = files['src/views/PostListView.tsx'];
100
+ // The user's custom column header is not directly visible because
101
+ // the current composer ignores user uiComponents (it infers
102
+ // columns from the model). But the view spec flows through — if
103
+ // we later teach the composer to honour uiComponents, this test
104
+ // anchors the contract.
105
+ expect(out).toBeDefined();
106
+ expect(out).toContain('export function PostListView');
107
+ });
108
+
109
+ it('warns on specialist view types and skips them', async () => {
110
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
111
+ try {
112
+ const spec: ExpandedSpec = {
113
+ models: {
114
+ Post: { name: 'Post', attributes: { id: { auto: 'uuid4' }, title: {} } },
115
+ },
116
+ views: {
117
+ PostBoardView: { type: 'board', model: 'Post' },
118
+ PostTimelineView: { type: 'timeline', model: 'Post' },
119
+ },
120
+ };
121
+ const files = await generate({ spec });
122
+ expect(files['src/views/PostBoardView.tsx']).toBeUndefined();
123
+ expect(files['src/views/PostTimelineView.tsx']).toBeUndefined();
124
+ expect(warn).toHaveBeenCalled();
125
+ const warning = warn.mock.calls[0][0] as string;
126
+ expect(warning).toContain('Skipped');
127
+ expect(warning).toContain('board');
128
+ expect(warning).toContain('timeline');
129
+ } finally {
130
+ warn.mockRestore();
131
+ }
132
+ });
133
+
134
+ it('handles an empty models map gracefully', async () => {
135
+ const files = await generate({ spec: { models: {}, views: {} } });
136
+ // Only the helpers file is emitted; no view files.
137
+ expect(Object.keys(files)).toEqual(['src/lib/entity-display.ts']);
138
+ });
139
+ });
@@ -0,0 +1,141 @@
1
+ /**
2
+ * App.tsx generator for ReactAppStarter
3
+ *
4
+ * Generates a simple App shell that routes between the emitted view
5
+ * components. Not using react-router — the starter output is
6
+ * deliberately low-magic. Users can add routing when they need
7
+ * URL-level view selection; for now, internal state drives it.
8
+ *
9
+ * Output:
10
+ * - sidebar nav listing every (model × view-type)
11
+ * - a main area rendering the selected view
12
+ * - QueryClientProvider at the root
13
+ */
14
+
15
+ import type { ExpandedSpec } from './views-generator.js';
16
+
17
+ export interface AppGeneratorContext {
18
+ spec: ExpandedSpec;
19
+ manifest?: unknown;
20
+ }
21
+
22
+ export async function generate(context: AppGeneratorContext): Promise<string> {
23
+ const models = Object.keys(context.spec.models ?? {});
24
+ const imports = buildImports(models);
25
+ const navEntries = buildNavEntries(models);
26
+ const viewSwitch = buildViewSwitch(models);
27
+
28
+ return `/**
29
+ * App.tsx — generated by @specverse/realize (ReactAppStarter)
30
+ *
31
+ * Safe to edit. Edits are preserved across regeneration via content
32
+ * hashing. This is a minimal shell: a sidebar listing every
33
+ * (model, view-type) plus a main area that renders the selection.
34
+ * Swap in react-router or your preferred routing library when the
35
+ * app needs URL-driven navigation.
36
+ */
37
+ import { useState } from 'react';
38
+ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
39
+ ${imports}
40
+
41
+ const queryClient = new QueryClient({
42
+ defaultOptions: {
43
+ queries: { staleTime: 5 * 60 * 1000, retry: 1 },
44
+ },
45
+ });
46
+
47
+ type Selection = {
48
+ model: string;
49
+ view: 'list' | 'detail' | 'form' | 'dashboard';
50
+ entityId?: string | number;
51
+ };
52
+
53
+ function Inner() {
54
+ const [selection, setSelection] = useState<Selection>(${models.length > 0 ? `{ model: '${models[0]}', view: 'list' }` : `{ model: '', view: 'list' }`});
55
+
56
+ const select = (model: string, view: Selection['view']) =>
57
+ setSelection({ model, view });
58
+
59
+ return (
60
+ <div className="min-h-screen flex bg-gray-50 dark:bg-gray-950">
61
+ <aside className="w-64 shrink-0 border-r border-gray-200 dark:border-gray-800 bg-white dark:bg-gray-900">
62
+ <div className="p-4 border-b border-gray-200 dark:border-gray-800">
63
+ <h1 className="text-lg font-semibold text-gray-900 dark:text-gray-100">App</h1>
64
+ </div>
65
+ <nav className="p-2 space-y-4">
66
+ ${navEntries}
67
+ </nav>
68
+ </aside>
69
+
70
+ <main className="flex-1 overflow-auto">
71
+ ${viewSwitch}
72
+ </main>
73
+ </div>
74
+ );
75
+ }
76
+
77
+ export default function App() {
78
+ return (
79
+ <QueryClientProvider client={queryClient}>
80
+ <Inner />
81
+ </QueryClientProvider>
82
+ );
83
+ }
84
+ `;
85
+ }
86
+
87
+ // ──────────────────────────────────────────────────────────────────────
88
+ // Helpers
89
+ // ──────────────────────────────────────────────────────────────────────
90
+
91
+ function buildImports(models: string[]): string {
92
+ return models.flatMap(m => [
93
+ `import { ${m}ListView } from './views/${m}ListView';`,
94
+ `import { ${m}DetailView } from './views/${m}DetailView';`,
95
+ `import { ${m}FormView } from './views/${m}FormView';`,
96
+ `import { ${m}DashboardView } from './views/${m}DashboardView';`,
97
+ ]).join('\n');
98
+ }
99
+
100
+ function buildNavEntries(models: string[]): string {
101
+ if (models.length === 0) {
102
+ return ' <p className="text-sm text-gray-400 px-2">No models in this spec.</p>';
103
+ }
104
+ return models.map(m => ` <div>
105
+ <div className="px-2 pb-1 text-xs font-semibold uppercase tracking-wider text-gray-500 dark:text-gray-400">
106
+ ${m}
107
+ </div>
108
+ <button type="button" onClick={() => select('${m}', 'list')} className="${navButtonCls('list')}">List</button>
109
+ <button type="button" onClick={() => select('${m}', 'dashboard')} className="${navButtonCls('dashboard')}">Dashboard</button>
110
+ <button type="button" onClick={() => select('${m}', 'form')} className="${navButtonCls('form')}">New</button>
111
+ </div>`).join('\n');
112
+ }
113
+
114
+ function navButtonCls(_view: string): string {
115
+ return (
116
+ 'block w-full text-left rounded px-2 py-1 text-sm ' +
117
+ 'text-gray-700 hover:bg-gray-100 ' +
118
+ 'dark:text-gray-300 dark:hover:bg-gray-800'
119
+ );
120
+ }
121
+
122
+ function buildViewSwitch(models: string[]): string {
123
+ if (models.length === 0) {
124
+ return ' <p className="p-6 text-sm text-gray-400">No models — add one to your .specly file and run <code>spv realize</code>.</p>';
125
+ }
126
+ const branches = models.flatMap(m => [
127
+ ` {selection.model === '${m}' && selection.view === 'list' && (
128
+ <${m}ListView onSelect={item => setSelection({ model: '${m}', view: 'detail', entityId: (item as any).id })} onCreate={() => setSelection({ model: '${m}', view: 'form' })} />
129
+ )}`,
130
+ ` {selection.model === '${m}' && selection.view === 'detail' && selection.entityId !== undefined && (
131
+ <${m}DetailView entityId={selection.entityId} onEdit={item => setSelection({ model: '${m}', view: 'form', entityId: (item as any).id })} onBack={() => setSelection({ model: '${m}', view: 'list' })} onDeleted={() => setSelection({ model: '${m}', view: 'list' })} />
132
+ )}`,
133
+ ` {selection.model === '${m}' && selection.view === 'form' && (
134
+ <${m}FormView mode={selection.entityId ? 'update' : 'create'} entityId={selection.entityId} onSuccess={() => setSelection({ model: '${m}', view: 'list' })} onCancel={() => setSelection({ model: '${m}', view: 'list' })} />
135
+ )}`,
136
+ ` {selection.model === '${m}' && selection.view === 'dashboard' && (
137
+ <${m}DashboardView onSelect={item => setSelection({ model: '${m}', view: 'detail', entityId: (item as any).id })} />
138
+ )}`,
139
+ ]);
140
+ return branches.join('\n');
141
+ }
@@ -0,0 +1,174 @@
1
+ /**
2
+ * Dashboard-view body composer for ReactAppStarter
3
+ *
4
+ * Minimal starter-kit dashboard: metric cards derived from the list
5
+ * query (total count; enum-field breakdowns if the model has a status-
6
+ * like attribute), plus a compact preview of recent records.
7
+ *
8
+ * Aggregation / charts are deferred — they need backend endpoints
9
+ * (`/api/posts/metrics`, `/api/posts/by-month`, …) that the starter
10
+ * kit doesn't assume. TODO comments flag the extension points.
11
+ */
12
+
13
+ import { METADATA_FIELDS } from '@specverse/runtime/views/core';
14
+ import type { EmitContext, ModelSpec } from './view-emitter.js';
15
+
16
+ const METADATA_FIELD_NAMES = new Set(METADATA_FIELDS);
17
+
18
+ export function composeDashboardBody(context: EmitContext): string {
19
+ const modelName = context.model.name;
20
+ const previewColumns = inferPreviewColumns(context.model);
21
+ const enumFields = inferEnumFields(context.model);
22
+
23
+ const lines: string[] = [];
24
+
25
+ // ── Metrics row ────────────────────────────────────────────────────
26
+ lines.push('<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4">');
27
+ lines.push(...renderTotalCard(modelName));
28
+ for (const enumField of enumFields) {
29
+ lines.push(...renderEnumBreakdownCard(enumField));
30
+ }
31
+ if (enumFields.length === 0) {
32
+ lines.push(...renderPlaceholderCard());
33
+ }
34
+ lines.push('</div>');
35
+
36
+ // ── TODO: aggregation metrics ──────────────────────────────────────
37
+ lines.push('');
38
+ lines.push('{/* TODO: add aggregation metrics (averages / sums / time series)');
39
+ lines.push(' by adding backend endpoints and per-metric hooks. Wire them in');
40
+ lines.push(' alongside the count cards above. */}');
41
+
42
+ // ── Recent-items preview ───────────────────────────────────────────
43
+ lines.push('');
44
+ lines.push('<div className="rounded-lg border border-gray-200 bg-white dark:border-gray-700 dark:bg-gray-900">');
45
+ lines.push(' <div className="border-b border-gray-200 dark:border-gray-700 px-6 py-3">');
46
+ lines.push(' <h3 className="text-sm font-semibold text-gray-700 dark:text-gray-200 uppercase tracking-wider">');
47
+ lines.push(` Recent ${humanize(modelName)}s`);
48
+ lines.push(' </h3>');
49
+ lines.push(' </div>');
50
+
51
+ if (previewColumns.length === 0) {
52
+ lines.push(' <div className="px-6 py-4 text-sm text-gray-400">No displayable fields.</div>');
53
+ } else {
54
+ lines.push(' <table className="min-w-full divide-y divide-gray-200 dark:divide-gray-700">');
55
+ lines.push(' <thead>');
56
+ lines.push(' <tr>');
57
+ for (const col of previewColumns) {
58
+ lines.push(
59
+ ` <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">` +
60
+ humanize(col) +
61
+ `</th>`
62
+ );
63
+ }
64
+ lines.push(' </tr>');
65
+ lines.push(' </thead>');
66
+ lines.push(' <tbody className="divide-y divide-gray-200 dark:divide-gray-700">');
67
+ lines.push(' {preview.map((item, idx) => (');
68
+ lines.push(' <tr');
69
+ lines.push(' key={idx}');
70
+ lines.push(' onClick={() => onSelect?.(item)}');
71
+ lines.push(' className="hover:bg-gray-50 dark:hover:bg-gray-800 cursor-pointer"');
72
+ lines.push(' >');
73
+ for (const col of previewColumns) {
74
+ lines.push(
75
+ ` <td className="px-6 py-4 text-sm text-gray-700 dark:text-gray-300">` +
76
+ `{String((item as any).${col} ?? '')}</td>`
77
+ );
78
+ }
79
+ lines.push(' </tr>');
80
+ lines.push(' ))}');
81
+ lines.push(' {preview.length === 0 && (');
82
+ lines.push(` <tr><td colSpan={${previewColumns.length}} className="px-6 py-4 text-sm text-gray-400">No records yet.</td></tr>`);
83
+ lines.push(' )}');
84
+ lines.push(' </tbody>');
85
+ lines.push(' </table>');
86
+ }
87
+ lines.push('</div>');
88
+
89
+ return lines.join('\n');
90
+ }
91
+
92
+ // ──────────────────────────────────────────────────────────────────────
93
+ // Card rendering
94
+ // ──────────────────────────────────────────────────────────────────────
95
+
96
+ function renderTotalCard(modelName: string): string[] {
97
+ const pluralLower = humanize(modelName).toLowerCase() + 's';
98
+ return [
99
+ ' <div className="rounded-lg border border-gray-200 bg-white p-6 shadow-sm dark:border-gray-700 dark:bg-gray-900">',
100
+ ' <p className="text-xs font-medium uppercase tracking-wide text-gray-500 dark:text-gray-400">',
101
+ ` Total ${pluralLower}`,
102
+ ' </p>',
103
+ ' <p className="mt-2 text-3xl font-semibold text-gray-900 dark:text-gray-100">',
104
+ ' {items.length}',
105
+ ' </p>',
106
+ ' </div>',
107
+ ];
108
+ }
109
+
110
+ function renderEnumBreakdownCard(field: EnumField): string[] {
111
+ // One card per enum value — shows count of records where the field
112
+ // equals that value.
113
+ const cards: string[] = [];
114
+ for (const value of field.values) {
115
+ cards.push(
116
+ ' <div className="rounded-lg border border-gray-200 bg-white p-6 shadow-sm dark:border-gray-700 dark:bg-gray-900">',
117
+ ' <p className="text-xs font-medium uppercase tracking-wide text-gray-500 dark:text-gray-400">',
118
+ ` ${humanize(field.name)}: ${humanize(value)}`,
119
+ ' </p>',
120
+ ' <p className="mt-2 text-3xl font-semibold text-gray-900 dark:text-gray-100">',
121
+ ` {items.filter((i: any) => i.${field.name} === ${JSON.stringify(value)}).length}`,
122
+ ' </p>',
123
+ ' </div>',
124
+ );
125
+ }
126
+ return cards;
127
+ }
128
+
129
+ function renderPlaceholderCard(): string[] {
130
+ return [
131
+ ' <div className="rounded-lg border border-dashed border-gray-300 p-6 text-sm text-gray-400 dark:border-gray-600">',
132
+ ' Add a metric here — e.g. a sum, average, or time-windowed count.',
133
+ ' </div>',
134
+ ];
135
+ }
136
+
137
+ // ──────────────────────────────────────────────────────────────────────
138
+ // Field inference
139
+ // ──────────────────────────────────────────────────────────────────────
140
+
141
+ interface EnumField {
142
+ name: string;
143
+ values: string[];
144
+ }
145
+
146
+ function inferEnumFields(model: ModelSpec): EnumField[] {
147
+ const out: EnumField[] = [];
148
+ const attrs = model.attributes ?? {};
149
+ for (const [name, rawDef] of Object.entries(attrs)) {
150
+ const def = rawDef as { values?: string[] };
151
+ const values = def?.values;
152
+ if (Array.isArray(values) && values.length > 0 && values.length <= 6) {
153
+ out.push({ name, values });
154
+ }
155
+ }
156
+ // Cap: one enum breakdown in the card row to avoid card sprawl. The
157
+ // remaining enum fields stay available in the list view; users can
158
+ // promote more breakdowns manually.
159
+ return out.slice(0, 1);
160
+ }
161
+
162
+ function inferPreviewColumns(model: ModelSpec): string[] {
163
+ const attrs = model.attributes ?? {};
164
+ return Object.keys(attrs)
165
+ .filter(n => !METADATA_FIELD_NAMES.has(n))
166
+ .slice(0, 4); // cap to keep the table readable
167
+ }
168
+
169
+ function humanize(name: string): string {
170
+ return name
171
+ .replace(/([A-Z])/g, ' $1')
172
+ .replace(/^./, c => c.toUpperCase())
173
+ .trim();
174
+ }