@mapomodule/form 2.0.0-beta.8 → 2.0.0-experimental.1

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 (145) hide show
  1. package/README.md +38 -119
  2. package/dist/index.d.mts +2 -1
  3. package/dist/index.d.ts +3 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.mjs +1 -0
  6. package/dist/module.d.mts +1 -0
  7. package/dist/module.d.ts.map +1 -1
  8. package/dist/module.json +1 -1
  9. package/dist/module.mjs +9 -0
  10. package/dist/runtime/components/MapoFocusPortal.vue +9 -2
  11. package/dist/runtime/components/MapoFocusPortal.vue.d.ts.map +1 -1
  12. package/dist/runtime/components/MapoForm.vue +28 -6
  13. package/dist/runtime/components/MapoForm.vue.d.ts +13 -1
  14. package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
  15. package/dist/runtime/components/MapoFormAnnotation.vue +79 -0
  16. package/dist/runtime/components/MapoFormAnnotation.vue.d.ts +27 -0
  17. package/dist/runtime/components/MapoFormAnnotation.vue.d.ts.map +1 -0
  18. package/dist/runtime/components/MapoFormAnnotationBadges.vue +28 -0
  19. package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts +13 -0
  20. package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts.map +1 -0
  21. package/dist/runtime/components/MapoFormField.vue +47 -11
  22. package/dist/runtime/components/MapoFormField.vue.d.ts +15 -9
  23. package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
  24. package/dist/runtime/components/MapoFormGroup.vue +18 -2
  25. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  26. package/dist/runtime/components/MapoFormTabs.vue +50 -8
  27. package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
  28. package/dist/runtime/components/fields/MapoMediaField.vue +5 -3
  29. package/dist/runtime/components/fields/MapoMediaField.vue.d.ts.map +1 -1
  30. package/dist/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  31. package/dist/runtime/components/fields/MapoMediaM2mField.vue.d.ts.map +1 -1
  32. package/dist/runtime/components/fields/MapoRepeater.vue +73 -14
  33. package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
  34. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +167 -0
  35. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
  36. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
  37. package/dist/runtime/components/fields/MapoRepeaterItem.vue +46 -10
  38. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
  39. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  40. package/dist/runtime/components/fields/MapoWygEditor.vue +19 -13
  41. package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
  42. package/dist/runtime/composables/useFieldAnnotation.d.ts +18 -0
  43. package/dist/runtime/composables/useFieldAnnotation.d.ts.map +1 -0
  44. package/dist/runtime/composables/useFieldAnnotation.js +16 -0
  45. package/dist/runtime/composables/useFocusMode.d.ts +3 -1
  46. package/dist/runtime/composables/useFocusMode.d.ts.map +1 -1
  47. package/dist/runtime/composables/useFormFromSchema.d.ts +23 -1
  48. package/dist/runtime/composables/useFormFromSchema.d.ts.map +1 -1
  49. package/dist/runtime/composables/useFormFromSchema.js +173 -32
  50. package/dist/runtime/composables/useMapoForm.d.ts +21 -3
  51. package/dist/runtime/composables/useMapoForm.d.ts.map +1 -1
  52. package/dist/runtime/composables/useMapoForm.js +49 -15
  53. package/dist/runtime/types/annotation.d.ts +43 -0
  54. package/dist/runtime/types/annotation.d.ts.map +1 -0
  55. package/dist/runtime/types/annotation.js +0 -0
  56. package/dist/runtime/types/descriptor.d.ts +81 -4
  57. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  58. package/dist/runtime/types/index.d.ts +2 -1
  59. package/dist/runtime/types/index.d.ts.map +1 -1
  60. package/dist/runtime/utils/annotations.d.ts +40 -0
  61. package/dist/runtime/utils/annotations.d.ts.map +1 -0
  62. package/dist/runtime/utils/annotations.js +78 -0
  63. package/dist/runtime/utils/draft.d.ts +21 -0
  64. package/dist/runtime/utils/draft.d.ts.map +1 -0
  65. package/dist/runtime/utils/draft.js +108 -0
  66. package/dist/runtime/utils/fieldComponent.d.ts +9 -0
  67. package/dist/runtime/utils/fieldComponent.d.ts.map +1 -0
  68. package/dist/runtime/utils/fieldComponent.js +12 -0
  69. package/dist/runtime/utils/fieldFocus.d.ts +24 -0
  70. package/dist/runtime/utils/fieldFocus.d.ts.map +1 -0
  71. package/dist/runtime/utils/fieldFocus.js +41 -0
  72. package/dist/runtime/utils/fieldsToJsonSchema.d.ts +38 -0
  73. package/dist/runtime/utils/fieldsToJsonSchema.d.ts.map +1 -0
  74. package/dist/runtime/utils/fieldsToJsonSchema.js +248 -0
  75. package/dist/runtime/utils/repeaterTemplates.d.ts +26 -0
  76. package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
  77. package/dist/runtime/utils/repeaterTemplates.js +51 -0
  78. package/dist/runtime/utils/reveal.d.ts +34 -0
  79. package/dist/runtime/utils/reveal.d.ts.map +1 -0
  80. package/dist/runtime/utils/reveal.js +29 -0
  81. package/dist/runtime/utils/tabVisibility.d.ts +18 -0
  82. package/dist/runtime/utils/tabVisibility.d.ts.map +1 -0
  83. package/dist/runtime/utils/tabVisibility.js +18 -0
  84. package/package.json +5 -5
  85. package/src/__tests__/MapoFocusPortal.component.test.ts +97 -0
  86. package/src/__tests__/MapoForm.component.test.ts +79 -0
  87. package/src/__tests__/MapoFormAnnotation.component.test.ts +244 -0
  88. package/src/__tests__/MapoFormField.component.test.ts +508 -0
  89. package/src/__tests__/MapoFormGroup.component.test.ts +141 -0
  90. package/src/__tests__/MapoFormTabs.component.test.ts +254 -0
  91. package/src/__tests__/MapoRepeaterItem.component.test.ts +286 -0
  92. package/src/__tests__/MapoWygEditor.component.test.ts +95 -0
  93. package/src/__tests__/annotations.test.ts +227 -0
  94. package/src/__tests__/article.schema.json +193 -0
  95. package/src/__tests__/camomillaRoundTrip.test.ts +73 -0
  96. package/src/__tests__/draft.test.ts +221 -0
  97. package/src/__tests__/fieldComponent.test.ts +31 -0
  98. package/src/__tests__/fieldExtensions.test.ts +63 -0
  99. package/src/__tests__/fieldFocus.test.ts +178 -0
  100. package/src/__tests__/fieldsToJsonSchema.test.ts +630 -0
  101. package/src/__tests__/headlessRenderer.ts +85 -0
  102. package/src/__tests__/repeaterTemplates.test.ts +154 -0
  103. package/src/__tests__/reveal.test.ts +89 -0
  104. package/src/__tests__/schemaSources.test.ts +132 -0
  105. package/src/__tests__/tabVisibility.test.ts +102 -0
  106. package/src/__tests__/useFormFromSchema.test.ts +387 -0
  107. package/src/__tests__/useMapoForm.test.ts +213 -2
  108. package/src/index.ts +9 -0
  109. package/src/module.ts +9 -0
  110. package/src/runtime/components/MapoFocusPortal.vue +15 -2
  111. package/src/runtime/components/MapoForm.vue +47 -4
  112. package/src/runtime/components/MapoFormAnnotation.vue +113 -0
  113. package/src/runtime/components/MapoFormAnnotationBadges.vue +41 -0
  114. package/src/runtime/components/MapoFormField.vue +72 -11
  115. package/src/runtime/components/MapoFormGroup.vue +20 -2
  116. package/src/runtime/components/MapoFormTabs.vue +64 -8
  117. package/src/runtime/components/fields/MapoMediaField.vue +5 -3
  118. package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  119. package/src/runtime/components/fields/MapoRepeater.vue +82 -14
  120. package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +188 -0
  121. package/src/runtime/components/fields/MapoRepeaterItem.vue +56 -8
  122. package/src/runtime/components/fields/MapoWygEditor.vue +22 -14
  123. package/src/runtime/composables/useFieldAnnotation.ts +38 -0
  124. package/src/runtime/composables/useFocusMode.ts +7 -1
  125. package/src/runtime/composables/useFormFromSchema.ts +306 -42
  126. package/src/runtime/composables/useMapoForm.ts +103 -23
  127. package/src/runtime/types/annotation.ts +53 -0
  128. package/src/runtime/types/descriptor.ts +94 -4
  129. package/src/runtime/types/index.ts +10 -0
  130. package/src/runtime/utils/annotations.ts +169 -0
  131. package/src/runtime/utils/draft.ts +158 -0
  132. package/src/runtime/utils/fieldComponent.ts +34 -0
  133. package/src/runtime/utils/fieldFocus.ts +73 -0
  134. package/src/runtime/utils/fieldsToJsonSchema.ts +405 -0
  135. package/src/runtime/utils/repeaterTemplates.ts +103 -0
  136. package/src/runtime/utils/reveal.ts +68 -0
  137. package/src/runtime/utils/tabVisibility.ts +44 -0
  138. package/dist/__tests__/__mocks__/nuxt-imports.d.ts +0 -4
  139. package/dist/__tests__/__mocks__/nuxt-imports.d.ts.map +0 -1
  140. package/dist/__tests__/registry.test.d.ts +0 -2
  141. package/dist/__tests__/registry.test.d.ts.map +0 -1
  142. package/dist/__tests__/useFormFromSchema.test.d.ts +0 -2
  143. package/dist/__tests__/useFormFromSchema.test.d.ts.map +0 -1
  144. package/dist/__tests__/useMapoForm.test.d.ts +0 -2
  145. package/dist/__tests__/useMapoForm.test.d.ts.map +0 -1
@@ -0,0 +1,227 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import {
3
+ annotationFor,
4
+ entriesUnder,
5
+ tabAnnotationGroups,
6
+ valuePath,
7
+ } from "../runtime/utils/annotations.js";
8
+ import type {
9
+ AnyFieldDescriptor,
10
+ FieldAnnotation,
11
+ FieldAnnotations,
12
+ } from "../runtime/types/index.js";
13
+ import type { TabEntry } from "../runtime/types/layout.js";
14
+
15
+ const ai: FieldAnnotation = {
16
+ label: "Suggested by AI",
17
+ icon: "i-lucide-sparkles",
18
+ color: "info",
19
+ };
20
+ const toCheck: FieldAnnotation = { ...ai, label: "To check", color: "warning" };
21
+ const note: FieldAnnotation = { label: "Note" };
22
+
23
+ /** A tab with one flat group of fields, plus optional sub-tabs and child tabs. */
24
+ function tabOf(
25
+ fields: AnyFieldDescriptor[],
26
+ extra: {
27
+ subtabs?: Record<string, AnyFieldDescriptor[]>;
28
+ children?: TabEntry[];
29
+ } = {},
30
+ ): TabEntry {
31
+ return {
32
+ name: "content",
33
+ groups: new Map([
34
+ [
35
+ "__flat__",
36
+ { fields, subtabs: new Map(Object.entries(extra.subtabs ?? {})) },
37
+ ],
38
+ ]),
39
+ children: new Map((extra.children ?? []).map((tab) => [tab.name, tab])),
40
+ };
41
+ }
42
+
43
+ /** How many marks the badges of a tab count, all groups together. */
44
+ const countOf = (
45
+ tab: TabEntry,
46
+ annotations: FieldAnnotations,
47
+ model: Record<string, unknown> = {},
48
+ lang = "",
49
+ ) =>
50
+ tabAnnotationGroups(tab, annotations, model, lang).reduce(
51
+ (sum, group) => sum + group.count,
52
+ 0,
53
+ );
54
+
55
+ describe("valuePath", () => {
56
+ it("is the key of a field", () => {
57
+ expect(valuePath({ key: "title", type: "text" }, "it")).toBe("title");
58
+ });
59
+
60
+ it("is the translation in the current language for a translatable field", () => {
61
+ const field: AnyFieldDescriptor = {
62
+ key: "title",
63
+ type: "text",
64
+ translatable: true,
65
+ };
66
+ expect(valuePath(field, "it")).toBe("translations.it.title");
67
+ expect(valuePath(field, "")).toBe("title");
68
+ });
69
+ });
70
+
71
+ describe("annotationFor", () => {
72
+ it("finds the annotation at the path of the value", () => {
73
+ expect(annotationFor({ title: ai }, "title", "title")).toBe(ai);
74
+ });
75
+
76
+ it("prefers the current language, then the bare key of a translatable field", () => {
77
+ const annotations = { "translations.it.title": toCheck, title: ai };
78
+ expect(annotationFor(annotations, "title", "translations.it.title")).toBe(
79
+ toCheck,
80
+ );
81
+ expect(annotationFor({ title: ai }, "title", "translations.it.title")).toBe(
82
+ ai,
83
+ );
84
+ });
85
+
86
+ it("finds nothing for a field without annotations", () => {
87
+ expect(annotationFor({ slug: ai }, "title", "title")).toBeUndefined();
88
+ });
89
+ });
90
+
91
+ describe("entriesUnder", () => {
92
+ it("keeps the entries under the prefix, keyed relative to it", () => {
93
+ const record = {
94
+ "blocks.0.title": ai,
95
+ "blocks.1.title": note,
96
+ "blocks.0": toCheck,
97
+ title: note,
98
+ };
99
+ expect(entriesUnder(record, "blocks.0")).toEqual({ title: ai });
100
+ });
101
+ });
102
+
103
+ describe("tabAnnotationGroups", () => {
104
+ it("groups the annotations of a tab by icon and color", () => {
105
+ const tab = tabOf([
106
+ { key: "title", type: "text" },
107
+ { key: "summary", type: "textarea" },
108
+ { key: "slug", type: "text" },
109
+ { key: "body", type: "editor" },
110
+ ]);
111
+ const annotations = { title: ai, summary: ai, slug: toCheck, body: note };
112
+
113
+ expect(tabAnnotationGroups(tab, annotations, {}, "")).toEqual([
114
+ {
115
+ icon: "i-lucide-sparkles",
116
+ color: "info",
117
+ count: 2,
118
+ labels: [ai.label],
119
+ },
120
+ {
121
+ icon: "i-lucide-sparkles",
122
+ color: "warning",
123
+ count: 1,
124
+ labels: [toCheck.label],
125
+ },
126
+ { icon: "i-lucide-info", color: "neutral", count: 1, labels: ["Note"] },
127
+ ]);
128
+ });
129
+
130
+ it("counts the marks in sub-tabs and child tabs", () => {
131
+ const tab = tabOf([{ key: "title", type: "text" }], {
132
+ subtabs: { seo: [{ key: "seo_title", type: "text" }] },
133
+ children: [tabOf([{ key: "author", type: "text" }])],
134
+ });
135
+ const annotations = { title: ai, seo_title: ai, author: ai };
136
+
137
+ expect(countOf(tab, annotations)).toBe(3);
138
+ });
139
+
140
+ it("counts the marks of the items a repeater renders, as they render", () => {
141
+ const blocks: AnyFieldDescriptor = {
142
+ key: "blocks",
143
+ type: "repeater",
144
+ fields: [
145
+ { key: "title", type: "text" },
146
+ { key: "note", type: "text", show: ({ model }) => !!model.withNote },
147
+ ],
148
+ };
149
+ const model = { blocks: [{ title: "A" }, { title: "B", withNote: true }] };
150
+ const annotations = {
151
+ "blocks.0.title": ai,
152
+ "blocks.1.title": ai,
153
+ "blocks.0.note": ai, // hidden in the first item
154
+ "blocks.1.note": ai,
155
+ "blocks.9.title": ai, // no such item
156
+ };
157
+
158
+ expect(countOf(tabOf([blocks]), annotations, model)).toBe(3);
159
+ });
160
+
161
+ it("counts a translatable field in the current language, inside items too", () => {
162
+ const blocks: AnyFieldDescriptor = {
163
+ key: "blocks",
164
+ type: "repeater",
165
+ fields: [{ key: "title", type: "text", translatable: true }],
166
+ };
167
+ const tab = tabOf([
168
+ { key: "title", type: "text", translatable: true },
169
+ blocks,
170
+ ]);
171
+ const annotations = {
172
+ "translations.it.title": ai,
173
+ "translations.en.title": ai,
174
+ "blocks.0.translations.it.title": ai,
175
+ "blocks.1.translations.en.title": ai,
176
+ };
177
+
178
+ expect(countOf(tab, annotations, { blocks: [{}, {}] }, "it")).toBe(2);
179
+ expect(countOf(tab, annotations, { blocks: [{}, {}] }, "fr")).toBe(0);
180
+ });
181
+
182
+ it("finds the items of a translatable repeater under its key, as the repeater does", () => {
183
+ const blocks: AnyFieldDescriptor = {
184
+ key: "blocks",
185
+ type: "repeater",
186
+ translatable: true,
187
+ fields: [{ key: "title", type: "text" }],
188
+ };
189
+ const model = { translations: { it: { blocks: [{ title: "A" }] } } };
190
+
191
+ expect(
192
+ countOf(tabOf([blocks]), { "blocks.0.title": ai }, model, "it"),
193
+ ).toBe(1);
194
+ });
195
+
196
+ it("uses the fields of each item's template", () => {
197
+ const blocks: AnyFieldDescriptor = {
198
+ key: "blocks",
199
+ type: "repeater",
200
+ attrs: {
201
+ templates: {
202
+ text: [{ key: "body", type: "textarea" }],
203
+ image: [{ key: "alt", type: "text" }],
204
+ },
205
+ },
206
+ };
207
+ const model = { blocks: [{ _template: "image" }, {}] };
208
+ const annotations = {
209
+ "blocks.0.alt": ai,
210
+ "blocks.0.body": ai, // not a field of the image template
211
+ "blocks.1.body": ai, // no template: the first one, text
212
+ };
213
+
214
+ expect(countOf(tabOf([blocks]), annotations, model)).toBe(2);
215
+ });
216
+
217
+ it("leaves out the fields the model hides", () => {
218
+ const tab = tabOf([
219
+ { key: "title", type: "text" },
220
+ { key: "price", type: "number", show: ({ model }) => !!model.paid },
221
+ ]);
222
+ const annotations = { title: ai, price: ai };
223
+
224
+ expect(countOf(tab, annotations)).toBe(1);
225
+ expect(countOf(tab, annotations, { paid: true })).toBe(2);
226
+ });
227
+ });
@@ -0,0 +1,193 @@
1
+ {
2
+ "type": "object",
3
+ "properties": {
4
+ "indexable": {
5
+ "type": "boolean",
6
+ "translatable": true,
7
+ "title": "Indexable"
8
+ },
9
+ "ordering": {
10
+ "type": "integer",
11
+ "maximum": 9223372036854775807,
12
+ "format": "int64",
13
+ "minimum": 0,
14
+ "title": "Ordering"
15
+ },
16
+ "is_public": { "type": "boolean", "readOnly": true, "title": "Is public" },
17
+ "meta": { "type": "object", "title": "Meta" },
18
+ "routerlink": { "type": "string", "readOnly": true, "title": "Routerlink" },
19
+ "og_url": {
20
+ "type": "string",
21
+ "nullable": true,
22
+ "maxLength": 200,
23
+ "translatable": true,
24
+ "title": "Og url"
25
+ },
26
+ "tags": {
27
+ "type": "relation",
28
+ "model": "camomilla.Tag",
29
+ "multiple": true,
30
+ "endpoint": "/api/camomilla/tags/",
31
+ "title": "Tags"
32
+ },
33
+ "published_at": {
34
+ "type": "string",
35
+ "format": "date-time",
36
+ "nullable": true,
37
+ "translatable": true,
38
+ "title": "Published at"
39
+ },
40
+ "keywords": {
41
+ "type": "string",
42
+ "nullable": true,
43
+ "maxLength": 200,
44
+ "translatable": true,
45
+ "title": "Keywords"
46
+ },
47
+ "breadcrumbs_title": {
48
+ "type": "string",
49
+ "nullable": true,
50
+ "maxLength": 128,
51
+ "translatable": true,
52
+ "title": "Breadcrumbs title"
53
+ },
54
+ "content": {
55
+ "type": "string",
56
+ "translatable": true,
57
+ "title": "Content",
58
+ "format": "textarea"
59
+ },
60
+ "identifier": {
61
+ "type": "string",
62
+ "nullable": true,
63
+ "maxLength": 200,
64
+ "title": "Identifier"
65
+ },
66
+ "template": {
67
+ "type": "string",
68
+ "nullable": true,
69
+ "maxLength": 500,
70
+ "title": "Template"
71
+ },
72
+ "description": {
73
+ "type": "string",
74
+ "nullable": true,
75
+ "translatable": true,
76
+ "title": "Description",
77
+ "format": "textarea"
78
+ },
79
+ "id": { "type": "integer", "readOnly": true, "title": "ID" },
80
+ "og_title": {
81
+ "type": "string",
82
+ "nullable": true,
83
+ "maxLength": 200,
84
+ "translatable": true,
85
+ "title": "Og title"
86
+ },
87
+ "canonical": {
88
+ "type": "string",
89
+ "nullable": true,
90
+ "maxLength": 200,
91
+ "translatable": true,
92
+ "title": "Canonical"
93
+ },
94
+ "author": {
95
+ "type": "relation",
96
+ "model": "auth.User",
97
+ "multiple": false,
98
+ "endpoint": "/api/camomilla/users/",
99
+ "nullable": true,
100
+ "title": "Author"
101
+ },
102
+ "og_image": {
103
+ "type": "relation",
104
+ "model": "camomilla.Media",
105
+ "multiple": false,
106
+ "endpoint": "/api/camomilla/media/",
107
+ "nullable": true,
108
+ "title": "Og image"
109
+ },
110
+ "parent_page": {
111
+ "type": "relation",
112
+ "model": "camomilla.Article",
113
+ "multiple": false,
114
+ "endpoint": "/api/camomilla/articles/",
115
+ "nullable": true,
116
+ "title": "Parent page"
117
+ },
118
+ "highlight_image": {
119
+ "type": "relation",
120
+ "model": "camomilla.Media",
121
+ "multiple": false,
122
+ "endpoint": "/api/camomilla/media/",
123
+ "nullable": true,
124
+ "title": "Highlight image"
125
+ },
126
+ "template_file": {
127
+ "type": "string",
128
+ "readOnly": true,
129
+ "title": "Template file"
130
+ },
131
+ "date_updated_at": {
132
+ "type": "string",
133
+ "format": "date-time",
134
+ "readOnly": true,
135
+ "title": "Date updated at"
136
+ },
137
+ "breadcrumbs": {
138
+ "type": "string",
139
+ "readOnly": true,
140
+ "title": "Breadcrumbs"
141
+ },
142
+ "og_description": {
143
+ "type": "string",
144
+ "nullable": true,
145
+ "translatable": true,
146
+ "title": "Og description",
147
+ "format": "textarea"
148
+ },
149
+ "template_data": {
150
+ "type": "object",
151
+ "translatable": true,
152
+ "title": "Template data"
153
+ },
154
+ "date_created": {
155
+ "type": "string",
156
+ "format": "date-time",
157
+ "readOnly": true,
158
+ "title": "Date created"
159
+ },
160
+ "autopermalink": {
161
+ "type": "boolean",
162
+ "translatable": true,
163
+ "title": "Autopermalink"
164
+ },
165
+ "title": {
166
+ "type": "string",
167
+ "nullable": true,
168
+ "maxLength": 200,
169
+ "translatable": true,
170
+ "title": "Title"
171
+ },
172
+ "permalink": {
173
+ "type": "string",
174
+ "translatable": true,
175
+ "title": "Permalink"
176
+ },
177
+ "og_type": {
178
+ "type": "string",
179
+ "nullable": true,
180
+ "maxLength": 200,
181
+ "translatable": true,
182
+ "title": "Og type"
183
+ },
184
+ "status": { "type": "string", "readOnly": true, "title": "Status" },
185
+ "deleted_at": {
186
+ "type": "string",
187
+ "format": "date-time",
188
+ "readOnly": true,
189
+ "nullable": true,
190
+ "title": "Deleted at"
191
+ }
192
+ }
193
+ }
@@ -0,0 +1,73 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import type { AnyFieldDescriptor } from "../runtime/types/index.js";
4
+ import { useFormFromSchema } from "../runtime/composables/useFormFromSchema";
5
+ import articleSchema from "./article.schema.json";
6
+
7
+ // Captured from camomilla: FormAutoSchema().map_serializer(ArticleSerializer()).
8
+ // This is the contract between the two repos — if camomilla changes the shape,
9
+ // this fails here rather than as an empty form in someone's browser.
10
+ describe("camomilla OPTIONS schema -> mapo descriptors", () => {
11
+ const fields = useFormFromSchema(articleSchema as never);
12
+ const byKey = Object.fromEntries(fields.map((f) => [f.key, f]));
13
+ // `attrs` is not on every member of the AnyFieldDescriptor union.
14
+ const attrsOf = (f: AnyFieldDescriptor | undefined) =>
15
+ (f as { attrs?: Record<string, unknown> } | undefined)?.attrs;
16
+
17
+ it("produces a descriptor per field, not an empty array", () => {
18
+ expect(fields.length).toBeGreaterThan(20);
19
+ });
20
+
21
+ it("maps m2m relations with a usable endpoint", () => {
22
+ expect(byKey.tags.type).toBe("m2m");
23
+ expect(attrsOf(byKey.tags)?.endpoint).toBe("/api/camomilla/tags/");
24
+ expect(attrsOf(byKey.tags)?.multiple).toBe(true);
25
+ });
26
+
27
+ it("maps fk relations with a usable endpoint", () => {
28
+ expect(byKey.author.type).toBe("fks");
29
+ expect(attrsOf(byKey.author)?.endpoint).toBe("/api/camomilla/users/");
30
+ });
31
+
32
+ it("does not leave any relation with an empty endpoint", () => {
33
+ // An empty endpoint is the silent failure: MapoFksField rewrites "" to "/",
34
+ // fetches the API root, and swallows the error as an empty picker.
35
+ const relations = fields.filter(
36
+ (f) => f.type === "fks" || f.type === "m2m",
37
+ );
38
+ expect(relations.length).toBeGreaterThan(0);
39
+ for (const rel of relations) expect(attrsOf(rel)?.endpoint).toBeTruthy();
40
+ });
41
+
42
+ it("marks server-computed fields readonly rather than dropping them", () => {
43
+ // Without this they rendered as ordinary inputs and the backend discarded
44
+ // whatever was typed, with nothing to notice.
45
+ expect(byKey.id.readonly).toBe(true);
46
+ expect(byKey.status.readonly).toBe(true);
47
+ expect(byKey.date_created.readonly).toBe(true);
48
+ // An editable field must NOT be flagged.
49
+ expect(byKey.title.readonly).toBeUndefined();
50
+ });
51
+
52
+ it("respects the multiline hint for unbounded text", () => {
53
+ // A Django TextField has no maxLength, so the >255 heuristic never fires.
54
+ expect(byKey.content.type).toBe("textarea");
55
+ expect(byKey.description.type).toBe("textarea");
56
+ });
57
+
58
+ it("carries labels", () => {
59
+ expect(byKey.title.label).toBe("Title");
60
+ });
61
+
62
+ it("types scalars correctly", () => {
63
+ expect(byKey.title.type).toBe("text");
64
+ expect(byKey.published_at.type).toBe("datetime");
65
+ });
66
+
67
+ it("has no translations envelope and no localized twins", () => {
68
+ expect(byKey.translations).toBeUndefined();
69
+ expect(fields.filter((f) => /_(it|en)$/.test(String(f.key)))).toHaveLength(
70
+ 0,
71
+ );
72
+ });
73
+ });