@mapomodule/mcp 0.0.0-stage → 1.0.0-beta.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.
- package/README.md +479 -2
- package/bin/mapo-mcp.mjs +3 -0
- package/dist/build.d.mts +18 -0
- package/dist/build.d.ts +18 -0
- package/dist/build.mjs +351 -0
- package/dist/cli.d.mts +7 -0
- package/dist/cli.d.ts +7 -0
- package/dist/cli.mjs +263 -0
- package/dist/index.d.mts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.mjs +1 -0
- package/dist/knowledge/api.json +1 -0
- package/dist/knowledge/docs.json +1 -0
- package/dist/module.d.mts +16 -0
- package/dist/module.d.ts +16 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +121 -0
- package/dist/runtime/core/doctor.d.ts +0 -0
- package/dist/runtime/core/doctor.js +128 -0
- package/dist/runtime/core/indexer.d.ts +0 -0
- package/dist/runtime/core/indexer.js +193 -0
- package/dist/runtime/core/knowledge.d.ts +0 -0
- package/dist/runtime/core/knowledge.js +76 -0
- package/dist/runtime/core/openapi.d.ts +0 -0
- package/dist/runtime/core/openapi.js +229 -0
- package/dist/runtime/core/prompts.d.ts +0 -0
- package/dist/runtime/core/prompts.js +108 -0
- package/dist/runtime/core/recipes.d.ts +0 -0
- package/dist/runtime/core/recipes.js +297 -0
- package/dist/runtime/core/resources.d.ts +0 -0
- package/dist/runtime/core/resources.js +88 -0
- package/dist/runtime/core/scaffold/fields.d.ts +0 -0
- package/dist/runtime/core/scaffold/fields.js +64 -0
- package/dist/runtime/core/scaffold/index.d.ts +0 -0
- package/dist/runtime/core/scaffold/index.js +74 -0
- package/dist/runtime/core/scaffold/templates.d.ts +0 -0
- package/dist/runtime/core/scaffold/templates.js +349 -0
- package/dist/runtime/core/schema-source.d.ts +0 -0
- package/dist/runtime/core/schema-source.js +70 -0
- package/dist/runtime/core/search.d.ts +0 -0
- package/dist/runtime/core/search.js +173 -0
- package/dist/runtime/core/tokenize.d.ts +0 -0
- package/dist/runtime/core/tokenize.js +96 -0
- package/dist/runtime/core/tool-spec.d.ts +0 -0
- package/dist/runtime/core/tool-spec.js +3 -0
- package/dist/runtime/core/tools/api.d.ts +0 -0
- package/dist/runtime/core/tools/api.js +192 -0
- package/dist/runtime/core/tools/app.d.ts +0 -0
- package/dist/runtime/core/tools/app.js +141 -0
- package/dist/runtime/core/tools/backend.d.ts +0 -0
- package/dist/runtime/core/tools/backend.js +143 -0
- package/dist/runtime/core/tools/docs.d.ts +0 -0
- package/dist/runtime/core/tools/docs.js +150 -0
- package/dist/runtime/core/tools/scaffold.d.ts +0 -0
- package/dist/runtime/core/tools/scaffold.js +127 -0
- package/dist/runtime/core/types.d.ts +0 -0
- package/dist/runtime/core/types.js +0 -0
- package/dist/runtime/core/write.d.ts +0 -0
- package/dist/runtime/core/write.js +53 -0
- package/dist/runtime/mcp/handlers/mapo/index.js +13 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/crud-page.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/custom-field.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/debug-form.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/migrate-v1.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/prompts/theme.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/resources/app-manifest.js +19 -0
- package/dist/runtime/mcp/handlers/mapo/resources/docs.js +34 -0
- package/dist/runtime/mcp/handlers/mapo/resources/fields.js +34 -0
- package/dist/runtime/mcp/handlers/mapo/resources/knowledge-index.js +22 -0
- package/dist/runtime/mcp/handlers/mapo/tools/backend-schema.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/component-api.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/composable-api.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/doctor.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/get-doc.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/inspect-app.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/list-field-types.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/list-recipes.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/scaffold.js +3 -0
- package/dist/runtime/mcp/handlers/mapo/tools/search-docs.js +3 -0
- package/dist/runtime/nitro/context.d.ts +0 -0
- package/dist/runtime/nitro/context.js +21 -0
- package/dist/runtime/nitro/to-mcp-prompt.d.ts +0 -0
- package/dist/runtime/nitro/to-mcp-prompt.js +23 -0
- package/dist/runtime/nitro/to-mcp-tool.d.ts +0 -0
- package/dist/runtime/nitro/to-mcp-tool.js +28 -0
- package/dist/runtime/types/virtual-modules.d.ts +6 -0
- package/dist/types.d.mts +9 -0
- package/package.json +73 -4
- package/src/__tests__/cli.test.ts +121 -0
- package/src/__tests__/doctor.test.ts +145 -0
- package/src/__tests__/fixtures/drf-schema.json +142 -0
- package/src/__tests__/fixtures.ts +198 -0
- package/src/__tests__/indexer.test.ts +114 -0
- package/src/__tests__/knowledge.test.ts +209 -0
- package/src/__tests__/openapi.test.ts +256 -0
- package/src/__tests__/recipes.test.ts +44 -0
- package/src/__tests__/resources.test.ts +69 -0
- package/src/__tests__/scaffold.test.ts +153 -0
- package/src/__tests__/search.test.ts +105 -0
- package/src/__tests__/tools.test.ts +226 -0
- package/src/__tests__/write.test.ts +134 -0
- package/src/build/extract-components.ts +135 -0
- package/src/build/extract-composables.ts +125 -0
- package/src/build/extract-fields.ts +250 -0
- package/src/build/index.ts +103 -0
- package/src/cli.ts +364 -0
- package/src/index.ts +65 -0
- package/src/module.ts +214 -0
- package/src/runtime/core/doctor.ts +204 -0
- package/src/runtime/core/indexer.ts +279 -0
- package/src/runtime/core/knowledge.ts +121 -0
- package/src/runtime/core/openapi.ts +417 -0
- package/src/runtime/core/prompts.ts +150 -0
- package/src/runtime/core/recipes.ts +361 -0
- package/src/runtime/core/resources.ts +144 -0
- package/src/runtime/core/scaffold/fields.ts +127 -0
- package/src/runtime/core/scaffold/index.ts +120 -0
- package/src/runtime/core/scaffold/templates.ts +394 -0
- package/src/runtime/core/schema-source.ts +123 -0
- package/src/runtime/core/search.ts +282 -0
- package/src/runtime/core/tokenize.ts +146 -0
- package/src/runtime/core/tool-spec.ts +74 -0
- package/src/runtime/core/tools/api.ts +300 -0
- package/src/runtime/core/tools/app.ts +200 -0
- package/src/runtime/core/tools/backend.ts +197 -0
- package/src/runtime/core/tools/docs.ts +215 -0
- package/src/runtime/core/tools/scaffold.ts +176 -0
- package/src/runtime/core/types.ts +133 -0
- package/src/runtime/core/write.ts +119 -0
- package/src/runtime/mcp/handlers/mapo/index.ts +22 -0
- package/src/runtime/mcp/handlers/mapo/prompts/crud-page.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/custom-field.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/debug-form.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/migrate-v1.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/prompts/theme.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/resources/app-manifest.ts +28 -0
- package/src/runtime/mcp/handlers/mapo/resources/docs.ts +41 -0
- package/src/runtime/mcp/handlers/mapo/resources/fields.ts +37 -0
- package/src/runtime/mcp/handlers/mapo/resources/knowledge-index.ts +24 -0
- package/src/runtime/mcp/handlers/mapo/tools/backend-schema.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/component-api.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/composable-api.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/doctor.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/get-doc.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/inspect-app.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/list-field-types.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/list-recipes.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/scaffold.ts +4 -0
- package/src/runtime/mcp/handlers/mapo/tools/search-docs.ts +4 -0
- package/src/runtime/nitro/context.ts +41 -0
- package/src/runtime/nitro/to-mcp-prompt.ts +30 -0
- package/src/runtime/nitro/to-mcp-tool.ts +35 -0
- package/src/runtime/types/virtual-modules.d.ts +6 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
4
|
+
import {
|
|
5
|
+
endpointsByModel,
|
|
6
|
+
listModels,
|
|
7
|
+
renderFields,
|
|
8
|
+
resolveSchema,
|
|
9
|
+
toFields,
|
|
10
|
+
toScaffoldSpecs,
|
|
11
|
+
} from "../runtime/core/openapi.js";
|
|
12
|
+
import {
|
|
13
|
+
SchemaUnavailableError,
|
|
14
|
+
clearSchemaCache,
|
|
15
|
+
loadOpenApiDocument,
|
|
16
|
+
} from "../runtime/core/schema-source.js";
|
|
17
|
+
import type { OpenApiDocument } from "../runtime/core/openapi.js";
|
|
18
|
+
|
|
19
|
+
const fixturePath = fileURLToPath(
|
|
20
|
+
new URL("./fixtures/drf-schema.json", import.meta.url),
|
|
21
|
+
);
|
|
22
|
+
const doc = JSON.parse(readFileSync(fixturePath, "utf-8")) as OpenApiDocument;
|
|
23
|
+
|
|
24
|
+
/** The mapped field for one Article property. */
|
|
25
|
+
function field(key: string) {
|
|
26
|
+
return toFields(doc, "Article", { includeReadOnly: true })!.fields.find(
|
|
27
|
+
(entry) => entry.key === key,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
afterEach(() => clearSchemaCache());
|
|
32
|
+
|
|
33
|
+
describe("resolveSchema", () => {
|
|
34
|
+
it("follows a $ref to its component", () => {
|
|
35
|
+
const { schema, ref } = resolveSchema(doc, {
|
|
36
|
+
$ref: "#/components/schemas/Tag",
|
|
37
|
+
});
|
|
38
|
+
expect(ref).toBe("Tag");
|
|
39
|
+
expect(schema.properties?.label?.maxLength).toBe(40);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("unwraps the allOf DRF emits for nullable relations, keeping siblings", () => {
|
|
43
|
+
const { schema, ref } = resolveSchema(doc, {
|
|
44
|
+
allOf: [{ $ref: "#/components/schemas/Author" }],
|
|
45
|
+
nullable: true,
|
|
46
|
+
readOnly: true,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
expect(ref).toBe("Author");
|
|
50
|
+
expect(schema.nullable).toBe(true);
|
|
51
|
+
expect(schema.readOnly).toBe(true); // the sibling wins over the component
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("endpointsByModel", () => {
|
|
56
|
+
it("finds collection endpoints, paginated or plain", () => {
|
|
57
|
+
const endpoints = endpointsByModel(doc);
|
|
58
|
+
|
|
59
|
+
expect(endpoints.get("Article")).toBe("/api/articles/"); // {count, results}
|
|
60
|
+
expect(endpoints.get("Author")).toBe("/api/authors/"); // bare array
|
|
61
|
+
expect(endpoints.get("Tag")).toBe("/api/tags/");
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("ignores detail routes", () => {
|
|
65
|
+
// `/api/articles/{id}/` must not win over the collection route.
|
|
66
|
+
expect(endpointsByModel(doc).get("Article")).not.toContain("{id}");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("reports nothing for a component no route serves", () => {
|
|
70
|
+
expect(endpointsByModel(doc).has("Editor")).toBe(false);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("mapProperty", () => {
|
|
75
|
+
it.each([
|
|
76
|
+
["title", "text"],
|
|
77
|
+
["body", "textarea"],
|
|
78
|
+
["summary", "textarea"],
|
|
79
|
+
["status", "select"],
|
|
80
|
+
["published_at", "datetime"],
|
|
81
|
+
["release_date", "date"],
|
|
82
|
+
["is_featured", "boolean"],
|
|
83
|
+
["priority", "number"],
|
|
84
|
+
["contact_email", "email"],
|
|
85
|
+
["canonical_url", "url"],
|
|
86
|
+
["cover", "file"],
|
|
87
|
+
["author", "fks"],
|
|
88
|
+
["tags", "m2m"],
|
|
89
|
+
])("maps %j to %j", (key, type) => {
|
|
90
|
+
expect(field(key)?.type).toBe(type);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("keeps the length limit of a short string and lengthens the rest", () => {
|
|
94
|
+
expect(field("title")?.attrs).toEqual({ maxLength: 200 });
|
|
95
|
+
expect(field("summary")?.type).toBe("textarea");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("turns choices into select items with readable labels", () => {
|
|
99
|
+
expect(field("status")?.attrs).toEqual({
|
|
100
|
+
items: [
|
|
101
|
+
{ label: "Draft", value: "draft" },
|
|
102
|
+
{ label: "Published", value: "published" },
|
|
103
|
+
{ label: "Archived", value: "archived" },
|
|
104
|
+
],
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("prefers the schema title over the derived label", () => {
|
|
109
|
+
expect(field("status")?.label).toBe("Publication status");
|
|
110
|
+
expect(field("published_at")?.label).toBe("Published at");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("carries numeric bounds", () => {
|
|
114
|
+
expect(field("priority")?.attrs).toEqual({ min: 1, max: 10 });
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("resolves the endpoint of a relation from the paths section", () => {
|
|
118
|
+
expect(field("author")?.attrs).toEqual({
|
|
119
|
+
endpoint: "/api/authors/",
|
|
120
|
+
itemValue: "id",
|
|
121
|
+
});
|
|
122
|
+
expect(field("tags")?.attrs).toEqual({
|
|
123
|
+
endpoint: "/api/tags/",
|
|
124
|
+
itemValue: "id",
|
|
125
|
+
});
|
|
126
|
+
expect(field("author")?.note).toBeUndefined();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("says so when the endpoint could not be inferred", () => {
|
|
130
|
+
const editor = field("editor")!;
|
|
131
|
+
expect(editor.type).toBe("fks");
|
|
132
|
+
expect(editor.note).toContain("no list endpoint");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("flags an array of plain values instead of pretending it is a repeater", () => {
|
|
136
|
+
expect(field("keywords")?.type).toBe("repeater");
|
|
137
|
+
expect(field("keywords")?.note).toContain("review");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("marks required and read-only from the schema", () => {
|
|
141
|
+
expect(field("title")?.required).toBe(true);
|
|
142
|
+
expect(field("body")?.required).toBeUndefined();
|
|
143
|
+
expect(field("created_at")?.readonly).toBe(true);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
describe("toFields", () => {
|
|
148
|
+
it("skips read-only properties by default and explains why", () => {
|
|
149
|
+
const result = toFields(doc, "Article")!;
|
|
150
|
+
|
|
151
|
+
expect(result.fields.map((entry) => entry.key)).not.toContain("id");
|
|
152
|
+
expect(result.skipped.map((entry) => entry.key)).toEqual(
|
|
153
|
+
expect.arrayContaining(["id", "created_at"]),
|
|
154
|
+
);
|
|
155
|
+
expect(result.notes.join(" ")).toContain("includeReadOnly");
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("reports the model's own endpoint", () => {
|
|
159
|
+
expect(toFields(doc, "Article")!.endpoint).toBe("/api/articles/");
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it("never emits a type the app does not register", () => {
|
|
163
|
+
const result = toFields(doc, "Article", {
|
|
164
|
+
knownTypes: new Set(["text", "textarea", "select"]),
|
|
165
|
+
})!;
|
|
166
|
+
|
|
167
|
+
expect(
|
|
168
|
+
result.fields.every((entry) =>
|
|
169
|
+
["text", "textarea", "select"].includes(entry.type),
|
|
170
|
+
),
|
|
171
|
+
).toBe(true);
|
|
172
|
+
expect(
|
|
173
|
+
result.skipped.find((entry) => entry.key === "tags")?.reason,
|
|
174
|
+
).toContain("m2m");
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("returns null for an unknown model", () => {
|
|
178
|
+
expect(toFields(doc, "Nope")).toBeNull();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("rendering", () => {
|
|
183
|
+
it("emits descriptors as TypeScript, with the uncertainty as a comment", () => {
|
|
184
|
+
const code = renderFields(toFields(doc, "Article")!);
|
|
185
|
+
|
|
186
|
+
expect(code).toContain("const fields: FieldDescriptor<Article>[] = [");
|
|
187
|
+
expect(code).toContain(
|
|
188
|
+
'{ key: "title", type: "text", label: "Title", required: true',
|
|
189
|
+
);
|
|
190
|
+
expect(code).toContain("// no list endpoint found for Editor");
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("produces specs mapo_scaffold accepts", () => {
|
|
194
|
+
const specs = toScaffoldSpecs(toFields(doc, "Article")!);
|
|
195
|
+
expect(specs).toContain("title:text:Title");
|
|
196
|
+
expect(specs).toContain("status:select:Publication status");
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
describe("listModels", () => {
|
|
201
|
+
it("lists components with their endpoint and size", () => {
|
|
202
|
+
const models = listModels(doc);
|
|
203
|
+
expect(models.map((model) => model.name)).toEqual([
|
|
204
|
+
"Article",
|
|
205
|
+
"Author",
|
|
206
|
+
"Editor",
|
|
207
|
+
"Tag",
|
|
208
|
+
]);
|
|
209
|
+
expect(models[0]!.endpoint).toBe("/api/articles/");
|
|
210
|
+
expect(models[0]!.fieldCount).toBeGreaterThan(10);
|
|
211
|
+
expect(
|
|
212
|
+
models.find((model) => model.name === "Editor")!.endpoint,
|
|
213
|
+
).toBeUndefined();
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
describe("loadOpenApiDocument", () => {
|
|
218
|
+
it("reads a local file", async () => {
|
|
219
|
+
const loaded = await loadOpenApiDocument(fixturePath);
|
|
220
|
+
expect(loaded.info?.title).toBe("Example CMS API");
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("reports a missing file", async () => {
|
|
224
|
+
await expect(loadOpenApiDocument("/nope/schema.json")).rejects.toThrow(
|
|
225
|
+
SchemaUnavailableError,
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("recognises YAML and points at the JSON query param", async () => {
|
|
230
|
+
const { writeFileSync, mkdtempSync } = await import("node:fs");
|
|
231
|
+
const { tmpdir } = await import("node:os");
|
|
232
|
+
const { join } = await import("node:path");
|
|
233
|
+
const path = join(
|
|
234
|
+
mkdtempSync(join(tmpdir(), "mapo-schema-")),
|
|
235
|
+
"schema.yaml",
|
|
236
|
+
);
|
|
237
|
+
writeFileSync(path, "openapi: 3.0.3\ninfo:\n title: x\n");
|
|
238
|
+
|
|
239
|
+
await expect(loadOpenApiDocument(path)).rejects.toThrow(/format=json/);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it("rejects a document without component schemas", async () => {
|
|
243
|
+
const { writeFileSync, mkdtempSync } = await import("node:fs");
|
|
244
|
+
const { tmpdir } = await import("node:os");
|
|
245
|
+
const { join } = await import("node:path");
|
|
246
|
+
const path = join(
|
|
247
|
+
mkdtempSync(join(tmpdir(), "mapo-schema-")),
|
|
248
|
+
"empty.json",
|
|
249
|
+
);
|
|
250
|
+
writeFileSync(path, JSON.stringify({ openapi: "3.0.3", paths: {} }));
|
|
251
|
+
|
|
252
|
+
await expect(loadOpenApiDocument(path)).rejects.toThrow(
|
|
253
|
+
/components\.schemas/,
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { matchRecipes, RECIPES } from "../runtime/core/recipes.js";
|
|
3
|
+
|
|
4
|
+
describe("recipe table", () => {
|
|
5
|
+
it("has unique ids and non-empty metadata", () => {
|
|
6
|
+
const ids = RECIPES.map((recipe) => recipe.id);
|
|
7
|
+
expect(new Set(ids).size).toBe(ids.length);
|
|
8
|
+
|
|
9
|
+
for (const recipe of RECIPES) {
|
|
10
|
+
expect(recipe.doc.endsWith(".md")).toBe(true);
|
|
11
|
+
expect(recipe.keywords.length).toBeGreaterThan(2);
|
|
12
|
+
expect(recipe.summary.length).toBeGreaterThan(20);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe("matchRecipes", () => {
|
|
18
|
+
it.each([
|
|
19
|
+
["register a custom form field component", "custom-field"],
|
|
20
|
+
["how do I paginate a list of articles with filters", "crud-list"],
|
|
21
|
+
["let editors reorder the navigation entries", "menu-manager"],
|
|
22
|
+
["port a v1 project to v2", "migration"],
|
|
23
|
+
["protect the dashboard route with permissions", "auth-permissions"],
|
|
24
|
+
])("maps %j to the %j recipe", (query, expectedId) => {
|
|
25
|
+
expect(matchRecipes(query)[0]?.id).toBe(expectedId);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("surfaces both plausible recipes for an ambiguous request", () => {
|
|
29
|
+
// "upload an image" points at media, "in a form" at field types: show both.
|
|
30
|
+
const ids = matchRecipes("upload an image and pick it in a form").map(
|
|
31
|
+
(recipe) => recipe.id,
|
|
32
|
+
);
|
|
33
|
+
expect(ids).toContain("media-manager");
|
|
34
|
+
expect(ids.length).toBeGreaterThan(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("stays silent when the query is unrelated", () => {
|
|
38
|
+
expect(matchRecipes("what is the weather today")).toEqual([]);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("caps the number of matches", () => {
|
|
42
|
+
expect(matchRecipes("form field list page", 1)).toHaveLength(1);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
DOCS_URI_PREFIX,
|
|
4
|
+
FIELD_URI_PREFIX,
|
|
5
|
+
docResourceList,
|
|
6
|
+
fieldResourceList,
|
|
7
|
+
renderDocResource,
|
|
8
|
+
renderFieldResource,
|
|
9
|
+
renderKnowledgeIndex,
|
|
10
|
+
} from "../runtime/core/resources.js";
|
|
11
|
+
import { makeApi, makeContext } from "./fixtures.js";
|
|
12
|
+
|
|
13
|
+
const context = makeContext();
|
|
14
|
+
|
|
15
|
+
describe("renderKnowledgeIndex", () => {
|
|
16
|
+
it("maps out recipes, pages and every extracted symbol", () => {
|
|
17
|
+
const index = renderKnowledgeIndex(context.knowledge(), context.api());
|
|
18
|
+
expect(index).toContain("## Recipes");
|
|
19
|
+
expect(index).toContain("howto/crud-list.md");
|
|
20
|
+
expect(index).toContain("MapoList, MapoDetail");
|
|
21
|
+
expect(index).toContain("select, text");
|
|
22
|
+
expect(index).toContain("useCrud");
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("renderDocResource", () => {
|
|
27
|
+
it("returns the whole page", () => {
|
|
28
|
+
const text = renderDocResource(context.knowledge(), "howto/crud-list.md")!;
|
|
29
|
+
expect(text).toContain("# CRUD list view");
|
|
30
|
+
expect(text).toContain("Bulk actions");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("returns null for an unknown path", () => {
|
|
34
|
+
expect(renderDocResource(context.knowledge(), "nope.md")).toBeNull();
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("renderFieldResource", () => {
|
|
39
|
+
it("describes the contract of a field type", () => {
|
|
40
|
+
const text = renderFieldResource(makeApi(), "select")!;
|
|
41
|
+
expect(text).toContain("# Field type `select`");
|
|
42
|
+
expect(text).toContain("component: NuiSelectMenu");
|
|
43
|
+
expect(text).toContain("`items`: readonly string[] (required)");
|
|
44
|
+
expect(text).toContain("## Shared descriptor properties");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("returns null for an unregistered type", () => {
|
|
48
|
+
expect(renderFieldResource(makeApi(), "nope")).toBeNull();
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("resource listings", () => {
|
|
53
|
+
it("addresses every doc page under the docs prefix", () => {
|
|
54
|
+
const resources = docResourceList(context.knowledge());
|
|
55
|
+
expect(resources.length).toBeGreaterThan(0);
|
|
56
|
+
for (const resource of resources) {
|
|
57
|
+
expect(resource.uri.startsWith(DOCS_URI_PREFIX)).toBe(true);
|
|
58
|
+
expect(resource.mimeType).toBe("text/markdown");
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("addresses every field type under the fields prefix", () => {
|
|
63
|
+
const resources = fieldResourceList(makeApi());
|
|
64
|
+
expect(resources.map((resource) => resource.uri)).toEqual([
|
|
65
|
+
`${FIELD_URI_PREFIX}select`,
|
|
66
|
+
`${FIELD_URI_PREFIX}text`,
|
|
67
|
+
]);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
humanise,
|
|
4
|
+
parseFields,
|
|
5
|
+
UnknownFieldTypeError,
|
|
6
|
+
} from "../runtime/core/scaffold/fields.js";
|
|
7
|
+
import { SCAFFOLD_KINDS, scaffold } from "../runtime/core/scaffold/index.js";
|
|
8
|
+
import { makeApi } from "./fixtures.js";
|
|
9
|
+
|
|
10
|
+
const api = makeApi();
|
|
11
|
+
|
|
12
|
+
describe("field specs", () => {
|
|
13
|
+
it("parses key, type and label", () => {
|
|
14
|
+
expect(parseFields(["title:text:Article title"])).toEqual([
|
|
15
|
+
{ key: "title", type: "text", label: "Article title" },
|
|
16
|
+
]);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("defaults the type to text and derives a readable label", () => {
|
|
20
|
+
expect(parseFields(["published_at"])).toEqual([
|
|
21
|
+
{ key: "published_at", type: "text", label: "Published at" },
|
|
22
|
+
]);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("rejects a type the registry does not know", () => {
|
|
26
|
+
expect(() => parseFields(["title:wysiwyg"], api)).toThrow(
|
|
27
|
+
UnknownFieldTypeError,
|
|
28
|
+
);
|
|
29
|
+
try {
|
|
30
|
+
parseFields(["title:wysiwyg"], api);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
// The message must let the caller recover on its own.
|
|
33
|
+
expect((error as Error).message).toContain("select, text");
|
|
34
|
+
expect((error as Error).message).toContain("defineFormField");
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("humanises identifiers", () => {
|
|
39
|
+
expect(humanise("is_featured")).toBe("Is featured");
|
|
40
|
+
expect(humanise("coverImage")).toBe("Cover image");
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
describe("scaffold", () => {
|
|
45
|
+
it("covers every advertised kind", () => {
|
|
46
|
+
for (const kind of SCAFFOLD_KINDS) {
|
|
47
|
+
const result = scaffold({ kind, name: "articles", api });
|
|
48
|
+
expect(result.files.length, kind).toBeGreaterThan(0);
|
|
49
|
+
expect(result.docs.length, kind).toBeGreaterThan(0);
|
|
50
|
+
for (const file of result.files) {
|
|
51
|
+
expect(file.content.length, `${kind} → ${file.path}`).toBeGreaterThan(
|
|
52
|
+
50,
|
|
53
|
+
);
|
|
54
|
+
expect(file.path.startsWith("/"), `${kind} → ${file.path}`).toBe(false);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("derives model, route and endpoint from the resource name", () => {
|
|
60
|
+
const [file] = scaffold({ kind: "list-page", name: "articles", api }).files;
|
|
61
|
+
|
|
62
|
+
expect(file!.path).toBe("app/pages/articles/index.vue");
|
|
63
|
+
expect(file!.content).toContain("interface Article {");
|
|
64
|
+
expect(file!.content).toContain('endpoint="/api/articles"');
|
|
65
|
+
expect(file!.content).toContain('detail-base="/articles"');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it.each([
|
|
69
|
+
["categories", "Category"],
|
|
70
|
+
["boxes", "Box"],
|
|
71
|
+
["articles", "Article"],
|
|
72
|
+
// Irregular plurals are left alone rather than guessed at.
|
|
73
|
+
["media", "Media"],
|
|
74
|
+
["star-rating", "StarRating"],
|
|
75
|
+
])("singularises %j into %j", (name, model) => {
|
|
76
|
+
const result = scaffold({ kind: "detail-page", name, api });
|
|
77
|
+
expect(result.files[0]!.content).toContain(`interface ${model} {`);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("honours explicit endpoint and route", () => {
|
|
81
|
+
const [file] = scaffold({
|
|
82
|
+
kind: "list-page",
|
|
83
|
+
name: "articles",
|
|
84
|
+
endpoint: "/api/cms/posts",
|
|
85
|
+
route: "/content/posts",
|
|
86
|
+
api,
|
|
87
|
+
}).files;
|
|
88
|
+
|
|
89
|
+
expect(file!.path).toBe("app/pages/content/posts/index.vue");
|
|
90
|
+
expect(file!.content).toContain('endpoint="/api/cms/posts"');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("emits descriptors with the requested types and a typed model", () => {
|
|
94
|
+
const [file] = scaffold({
|
|
95
|
+
kind: "detail-page",
|
|
96
|
+
name: "articles",
|
|
97
|
+
fields: ["title:text:Title", "featured:select:Featured"],
|
|
98
|
+
api,
|
|
99
|
+
}).files;
|
|
100
|
+
|
|
101
|
+
expect(file!.content).toContain('key: "title"');
|
|
102
|
+
expect(file!.content).toContain('type: "select"');
|
|
103
|
+
expect(file!.content).toContain("title: string;");
|
|
104
|
+
// `select` declares a required `items` attr: the skeleton must show it.
|
|
105
|
+
expect(file!.content).toContain("attrs: { items: undefined /* TODO */ }");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("generates both the component and its registration for a custom field", () => {
|
|
109
|
+
const result = scaffold({ kind: "custom-field", name: "star-rating", api });
|
|
110
|
+
|
|
111
|
+
expect(result.files.map((file) => file.path)).toEqual([
|
|
112
|
+
"app/components/StarRatingField.vue",
|
|
113
|
+
"app/plugins/star-rating-field.ts",
|
|
114
|
+
]);
|
|
115
|
+
expect(result.files[1]!.content).toContain('defineFormField("star-rating"');
|
|
116
|
+
expect(result.notes.join(" ")).toContain("mapo_inspect_app");
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("keeps the login page out of the admin layout", () => {
|
|
120
|
+
const [file] = scaffold({ kind: "login-page", api }).files;
|
|
121
|
+
|
|
122
|
+
expect(file!.path).toBe("app/pages/login.vue");
|
|
123
|
+
expect(file!.content).toContain("layout: false");
|
|
124
|
+
expect(file!.content).toContain("<MapoLogin />");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("imports types from the aggregate package a consumer installs", () => {
|
|
128
|
+
for (const kind of [
|
|
129
|
+
"list-page",
|
|
130
|
+
"detail-page",
|
|
131
|
+
"standalone-form",
|
|
132
|
+
] as const) {
|
|
133
|
+
const content = scaffold({ kind, name: "articles", api }).files[0]!
|
|
134
|
+
.content;
|
|
135
|
+
expect(content, kind).toContain('from "mapomodule/types"');
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("puts every page behind the auth middleware and the admin layout", () => {
|
|
140
|
+
for (const kind of [
|
|
141
|
+
"list-page",
|
|
142
|
+
"detail-page",
|
|
143
|
+
"standalone-form",
|
|
144
|
+
"menu-page",
|
|
145
|
+
"media-page",
|
|
146
|
+
] as const) {
|
|
147
|
+
const content = scaffold({ kind, name: "articles", api }).files[0]!
|
|
148
|
+
.content;
|
|
149
|
+
expect(content, kind).toContain('layout: "mapo-default"');
|
|
150
|
+
expect(content, kind).toContain('middleware: ["auth"]');
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
extractSnippet,
|
|
4
|
+
getDoc,
|
|
5
|
+
listDocs,
|
|
6
|
+
searchDocs,
|
|
7
|
+
} from "../runtime/core/search.js";
|
|
8
|
+
import { CRUD_LIST_DOC, CUSTOM_FIELDS_DOC, makeKnowledge } from "./fixtures.js";
|
|
9
|
+
|
|
10
|
+
const knowledge = makeKnowledge({
|
|
11
|
+
"howto/crud-list.md": CRUD_LIST_DOC,
|
|
12
|
+
"uikit/form/custom-fields.md": CUSTOM_FIELDS_DOC,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe("searchDocs", () => {
|
|
16
|
+
it("ranks the section that actually answers the question first", () => {
|
|
17
|
+
const [top] = searchDocs(
|
|
18
|
+
knowledge,
|
|
19
|
+
"how do I run bulk actions on selected rows",
|
|
20
|
+
);
|
|
21
|
+
expect(top?.path).toBe("howto/crud-list.md");
|
|
22
|
+
expect(top?.heading).toBe("Bulk actions");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("finds a page through an identifier mentioned in its body", () => {
|
|
26
|
+
const [top] = searchDocs(knowledge, "defineFormField");
|
|
27
|
+
expect(top?.path).toBe("uikit/form/custom-fields.md");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("honours the section filter", () => {
|
|
31
|
+
const hits = searchDocs(knowledge, "field registry", { section: "howto" });
|
|
32
|
+
expect(hits.length).toBeGreaterThan(0);
|
|
33
|
+
expect(hits.every((hit) => hit.section === "howto")).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("honours the package filter, with or without the scope", () => {
|
|
37
|
+
const scoped = searchDocs(knowledge, "field", { pkg: "@mapomodule/form" });
|
|
38
|
+
const bare = searchDocs(knowledge, "field", { pkg: "form" });
|
|
39
|
+
expect(scoped.map((hit) => hit.path)).toEqual(bare.map((hit) => hit.path));
|
|
40
|
+
expect(bare.every((hit) => hit.path.startsWith("uikit/form/"))).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("respects the limit and returns nothing for gibberish", () => {
|
|
44
|
+
expect(searchDocs(knowledge, "field", { limit: 1 })).toHaveLength(1);
|
|
45
|
+
expect(searchDocs(knowledge, "zzzqqq")).toEqual([]);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("extractSnippet", () => {
|
|
50
|
+
it("centres the excerpt on the matched term", () => {
|
|
51
|
+
const text = `${"padding ".repeat(60)}NEEDLE${" trailing".repeat(60)}`;
|
|
52
|
+
const snippet = extractSnippet(text, "needle");
|
|
53
|
+
expect(snippet).toContain("NEEDLE");
|
|
54
|
+
expect(snippet.length).toBeLessThan(text.length);
|
|
55
|
+
expect(snippet.startsWith("…")).toBe(true);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("falls back to the head of the text when nothing matches", () => {
|
|
59
|
+
expect(extractSnippet("short body", "absent")).toBe("short body");
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("getDoc", () => {
|
|
64
|
+
it("returns the whole page with its heading list", () => {
|
|
65
|
+
const doc = getDoc(knowledge, "howto/crud-list.md")!;
|
|
66
|
+
expect(doc.title).toBe("CRUD list view");
|
|
67
|
+
expect(doc.headings).toContain("Bulk actions");
|
|
68
|
+
expect(doc.content).toContain("MapoList");
|
|
69
|
+
expect(doc.truncated).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("returns a single section when asked, by heading or anchor", () => {
|
|
73
|
+
const byHeading = getDoc(knowledge, "howto/crud-list.md", {
|
|
74
|
+
heading: "Bulk actions",
|
|
75
|
+
})!;
|
|
76
|
+
const byAnchor = getDoc(knowledge, "howto/crud-list.md", {
|
|
77
|
+
heading: "bulk-actions",
|
|
78
|
+
})!;
|
|
79
|
+
expect(byHeading.content).toBe(byAnchor.content);
|
|
80
|
+
expect(byHeading.content).toContain("## Bulk actions");
|
|
81
|
+
expect(byHeading.content).not.toContain("Minimal example");
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("tolerates a leading docs/ prefix and reports unknown pages", () => {
|
|
85
|
+
expect(getDoc(knowledge, "docs/howto/crud-list.md")).not.toBeNull();
|
|
86
|
+
expect(getDoc(knowledge, "nope.md")).toBeNull();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("truncates beyond maxChars", () => {
|
|
90
|
+
const doc = getDoc(knowledge, "howto/crud-list.md", { maxChars: 100 })!;
|
|
91
|
+
expect(doc.truncated).toBe(true);
|
|
92
|
+
expect(doc.content).toContain("[truncated]");
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
describe("listDocs", () => {
|
|
97
|
+
it("groups chunks back into pages", () => {
|
|
98
|
+
const docs = listDocs(knowledge);
|
|
99
|
+
expect(docs.map((doc) => doc.path)).toEqual([
|
|
100
|
+
"howto/crud-list.md",
|
|
101
|
+
"uikit/form/custom-fields.md",
|
|
102
|
+
]);
|
|
103
|
+
expect(docs[0]!.headings).toContain("Quick edit");
|
|
104
|
+
});
|
|
105
|
+
});
|