@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,198 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildIndex,
|
|
3
|
+
chunkMarkdown,
|
|
4
|
+
KNOWLEDGE_VERSION,
|
|
5
|
+
} from "../runtime/core/indexer.js";
|
|
6
|
+
import type { MapoAppManifest } from "../index.js";
|
|
7
|
+
import type { MapoToolContext } from "../runtime/core/tool-spec.js";
|
|
8
|
+
import type { ApiKnowledge, DocsKnowledge } from "../runtime/core/types.js";
|
|
9
|
+
|
|
10
|
+
/** Builds an in-memory knowledge base from `path → markdown` pairs. */
|
|
11
|
+
export function makeKnowledge(files: Record<string, string>): DocsKnowledge {
|
|
12
|
+
const chunks = Object.entries(files).flatMap(([path, markdown]) =>
|
|
13
|
+
chunkMarkdown(path, markdown),
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
version: KNOWLEDGE_VERSION,
|
|
18
|
+
generatedAt: new Date(0).toISOString(),
|
|
19
|
+
docsRoot: "docs",
|
|
20
|
+
chunks,
|
|
21
|
+
index: buildIndex(chunks),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const CRUD_LIST_DOC = `# CRUD list view
|
|
26
|
+
|
|
27
|
+
Build a resource list page using MapoList.
|
|
28
|
+
|
|
29
|
+
## Minimal example
|
|
30
|
+
|
|
31
|
+
\`\`\`vue
|
|
32
|
+
<MapoList endpoint="/api/articles" detail-base="/articles" />
|
|
33
|
+
\`\`\`
|
|
34
|
+
|
|
35
|
+
## Bulk actions
|
|
36
|
+
|
|
37
|
+
Pass \`bulkActions\` to run an operation on every selected row.
|
|
38
|
+
|
|
39
|
+
## Quick edit
|
|
40
|
+
|
|
41
|
+
Inline editing of a row uses the field registry with \`type: "text"\`.
|
|
42
|
+
`;
|
|
43
|
+
|
|
44
|
+
export const CUSTOM_FIELDS_DOC = `# Custom fields
|
|
45
|
+
|
|
46
|
+
Register your own field component in the form registry.
|
|
47
|
+
|
|
48
|
+
## Registering a component
|
|
49
|
+
|
|
50
|
+
Use \`defineFormField\` in a plugin to add a new field type.
|
|
51
|
+
|
|
52
|
+
## Overriding defaults
|
|
53
|
+
|
|
54
|
+
Set \`mapoForm.fields\` in nuxt.config to override attrs per type.
|
|
55
|
+
`;
|
|
56
|
+
|
|
57
|
+
/** Minimal API surface fixture, shaped like the real extraction output. */
|
|
58
|
+
export function makeApi(): ApiKnowledge {
|
|
59
|
+
return {
|
|
60
|
+
version: 1,
|
|
61
|
+
generatedAt: new Date(0).toISOString(),
|
|
62
|
+
components: [
|
|
63
|
+
{
|
|
64
|
+
name: "MapoList",
|
|
65
|
+
pkg: "@mapomodule/uikit",
|
|
66
|
+
file: "packages/@mapomodule/uikit/src/runtime/components/MapoList.vue",
|
|
67
|
+
description: "Paginated resource list.",
|
|
68
|
+
props: [
|
|
69
|
+
{ name: "endpoint", type: "string", required: true },
|
|
70
|
+
{
|
|
71
|
+
name: "columns",
|
|
72
|
+
type: "ListColumn<T>[]",
|
|
73
|
+
required: false,
|
|
74
|
+
default: "[]",
|
|
75
|
+
description: "Columns to render.",
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
events: [{ name: "update:items" }],
|
|
79
|
+
slots: [{ name: "head" }],
|
|
80
|
+
exposed: [],
|
|
81
|
+
docs: ["uikit/list.md#mapolist"],
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: "MapoDetail",
|
|
85
|
+
pkg: "@mapomodule/uikit",
|
|
86
|
+
file: "packages/@mapomodule/uikit/src/runtime/components/MapoDetail.vue",
|
|
87
|
+
props: [{ name: "id", type: "string | number", required: true }],
|
|
88
|
+
events: [{ name: "saved" }],
|
|
89
|
+
slots: [],
|
|
90
|
+
exposed: [],
|
|
91
|
+
docs: [],
|
|
92
|
+
},
|
|
93
|
+
],
|
|
94
|
+
fieldTypes: [
|
|
95
|
+
{
|
|
96
|
+
type: "select",
|
|
97
|
+
component: "NuiSelectMenu",
|
|
98
|
+
descriptor: "SelectDescriptor",
|
|
99
|
+
description: "Single or multi-value select menu.",
|
|
100
|
+
attrs: [
|
|
101
|
+
{
|
|
102
|
+
name: "items",
|
|
103
|
+
type: "readonly string[]",
|
|
104
|
+
required: true,
|
|
105
|
+
description: "Choices.",
|
|
106
|
+
},
|
|
107
|
+
{ name: "multiple", type: "boolean", required: false },
|
|
108
|
+
],
|
|
109
|
+
defaultAttrs: { labelKey: "text" },
|
|
110
|
+
docs: ["uikit/form/add-fields.md#select"],
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
type: "text",
|
|
114
|
+
component: "NuiInput",
|
|
115
|
+
descriptor: "TextDescriptor",
|
|
116
|
+
attrs: [{ name: "placeholder", type: "string", required: false }],
|
|
117
|
+
docs: [],
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
fieldCommon: [
|
|
121
|
+
{ name: "key", type: "DeepKeyOf<T>", required: true },
|
|
122
|
+
{ name: "label", type: "string", required: false },
|
|
123
|
+
],
|
|
124
|
+
composables: [
|
|
125
|
+
{
|
|
126
|
+
name: "useCrud",
|
|
127
|
+
pkg: "@mapomodule/core",
|
|
128
|
+
file: "packages/@mapomodule/core/src/runtime/api/crud.ts",
|
|
129
|
+
signature: "<T>(endpoint: string) => CrudRepository<T>",
|
|
130
|
+
description: "Typed CRUD factory.",
|
|
131
|
+
docs: ["modules/core.md#usecrud"],
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
name: "useMediaStore",
|
|
135
|
+
pkg: "@mapomodule/uikit",
|
|
136
|
+
file: "packages/@mapomodule/uikit/src/runtime/stores/media.ts",
|
|
137
|
+
docs: [],
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A healthy app snapshot: `@nuxt/ui` first, a login page, real auth endpoints,
|
|
145
|
+
* one admin route, bundled locales only. Doctor rules should stay silent on it.
|
|
146
|
+
*/
|
|
147
|
+
export function makeManifest(
|
|
148
|
+
overrides: Partial<MapoAppManifest> = {},
|
|
149
|
+
): MapoAppManifest {
|
|
150
|
+
return {
|
|
151
|
+
rootDir: "/tmp/app",
|
|
152
|
+
dev: true,
|
|
153
|
+
modules: [
|
|
154
|
+
{ name: "@mapomodule/core", version: "0.0.0" },
|
|
155
|
+
{ name: "@mapomodule/uikit", version: "0.0.0" },
|
|
156
|
+
{ name: "@mapomodule/form", version: "0.0.0" },
|
|
157
|
+
],
|
|
158
|
+
moduleOrder: ["@nuxt/ui", "mapomodule"],
|
|
159
|
+
config: {
|
|
160
|
+
mapoCore: {
|
|
161
|
+
authLoginUrl: "/api/backend/login",
|
|
162
|
+
userInfoApi: "/api/backend/me/",
|
|
163
|
+
logoutUrl: "/api/backend/logout",
|
|
164
|
+
loginUrl: "/login",
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
fieldTypes: [
|
|
168
|
+
{ type: "text", source: "default" },
|
|
169
|
+
{ type: "select", source: "default" },
|
|
170
|
+
],
|
|
171
|
+
components: ["MapoList", "MapoDetail"],
|
|
172
|
+
routes: [{ path: "/login" }, { path: "/articles" }],
|
|
173
|
+
locales: ["en", "it"],
|
|
174
|
+
...overrides,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** Tool context backed by the fixtures above. */
|
|
179
|
+
export function makeContext(
|
|
180
|
+
files: Record<string, string> = {
|
|
181
|
+
"howto/crud-list.md": CRUD_LIST_DOC,
|
|
182
|
+
"uikit/form/custom-fields.md": CUSTOM_FIELDS_DOC,
|
|
183
|
+
},
|
|
184
|
+
manifest: MapoAppManifest | null = makeManifest(),
|
|
185
|
+
overrides: Partial<MapoToolContext> = {},
|
|
186
|
+
): MapoToolContext {
|
|
187
|
+
const docs = makeKnowledge(files);
|
|
188
|
+
const api = makeApi();
|
|
189
|
+
return {
|
|
190
|
+
knowledge: () => docs,
|
|
191
|
+
api: () => api,
|
|
192
|
+
manifest: () => manifest,
|
|
193
|
+
rootDir: () => manifest?.rootDir ?? null,
|
|
194
|
+
canWrite: () => manifest?.dev ?? false,
|
|
195
|
+
backend: () => ({ schemaUrl: null, token: null }),
|
|
196
|
+
...overrides,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
buildIndex,
|
|
4
|
+
chunkMarkdown,
|
|
5
|
+
extractTags,
|
|
6
|
+
slugify,
|
|
7
|
+
splitSections,
|
|
8
|
+
} from "../runtime/core/indexer.js";
|
|
9
|
+
import { stem, tokenize } from "../runtime/core/tokenize.js";
|
|
10
|
+
import { CRUD_LIST_DOC } from "./fixtures.js";
|
|
11
|
+
|
|
12
|
+
describe("tokenize", () => {
|
|
13
|
+
it("splits identifiers into their sub-words", () => {
|
|
14
|
+
expect(tokenize("MapoListFilters")).toEqual([
|
|
15
|
+
"mapolistfilter",
|
|
16
|
+
"mapo",
|
|
17
|
+
"list",
|
|
18
|
+
"filter",
|
|
19
|
+
]);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("drops stopwords and de-pluralises", () => {
|
|
23
|
+
expect(tokenize("how do I add the fields")).toEqual(["add", "field"]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("leaves short and -ss words alone", () => {
|
|
27
|
+
expect(stem("css")).toBe("css");
|
|
28
|
+
expect(stem("has")).toBe("has");
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("splitSections", () => {
|
|
33
|
+
it("extracts the title and one section per H2", () => {
|
|
34
|
+
const { title, sections } = splitSections(CRUD_LIST_DOC);
|
|
35
|
+
expect(title).toBe("CRUD list view");
|
|
36
|
+
expect(sections.map((section) => section.heading)).toEqual([
|
|
37
|
+
"CRUD list view",
|
|
38
|
+
"Minimal example",
|
|
39
|
+
"Bulk actions",
|
|
40
|
+
"Quick edit",
|
|
41
|
+
]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("ignores headings inside fenced code blocks", () => {
|
|
45
|
+
const { sections } = splitSections(
|
|
46
|
+
"# T\n\n## Real\n\n```md\n## Fake\n```\n",
|
|
47
|
+
);
|
|
48
|
+
expect(sections.map((section) => section.heading)).toEqual(["Real"]);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("chunkMarkdown", () => {
|
|
53
|
+
const chunks = chunkMarkdown("howto/crud-list.md", CRUD_LIST_DOC);
|
|
54
|
+
|
|
55
|
+
it("carries path metadata on every chunk", () => {
|
|
56
|
+
expect(chunks.every((chunk) => chunk.path === "howto/crud-list.md")).toBe(
|
|
57
|
+
true,
|
|
58
|
+
);
|
|
59
|
+
expect(chunks[0]!.section).toBe("howto");
|
|
60
|
+
expect(chunks[0]!.title).toBe("CRUD list view");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("anchors sections like VitePress", () => {
|
|
64
|
+
const bulk = chunks.find((chunk) => chunk.heading === "Bulk actions");
|
|
65
|
+
expect(bulk?.anchor).toBe("bulk-actions");
|
|
66
|
+
expect(bulk?.id).toBe("howto/crud-list.md#bulk-actions");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("maps nested paths to their package", () => {
|
|
70
|
+
const [chunk] = chunkMarkdown(
|
|
71
|
+
"uikit/form/validation.md",
|
|
72
|
+
"# V\n\n## A\n\ntext\n",
|
|
73
|
+
);
|
|
74
|
+
expect(chunk?.pkg).toBe("@mapomodule/form");
|
|
75
|
+
expect(chunk?.group).toBe("form");
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("extractTags", () => {
|
|
80
|
+
it("picks up components, composables and field types", () => {
|
|
81
|
+
const tags = extractTags('Use <MapoList> with useCrud and type: "editor"');
|
|
82
|
+
expect(tags).toContain("MapoList");
|
|
83
|
+
expect(tags).toContain("useCrud");
|
|
84
|
+
expect(tags).toContain("editor");
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe("buildIndex", () => {
|
|
89
|
+
const chunks = chunkMarkdown("howto/crud-list.md", CRUD_LIST_DOC);
|
|
90
|
+
const index = buildIndex(chunks);
|
|
91
|
+
|
|
92
|
+
it("indexes every chunk", () => {
|
|
93
|
+
expect(index.total).toBe(chunks.length);
|
|
94
|
+
expect(index.lengths).toHaveLength(chunks.length);
|
|
95
|
+
expect(index.avgLength).toBeGreaterThan(0);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("boosts heading terms above body terms", () => {
|
|
99
|
+
const bulkIndex = chunks.findIndex(
|
|
100
|
+
(chunk) => chunk.heading === "Bulk actions",
|
|
101
|
+
);
|
|
102
|
+
const posting = index.terms["bulk"]!.find(
|
|
103
|
+
([chunkIndex]) => chunkIndex === bulkIndex,
|
|
104
|
+
)!;
|
|
105
|
+
expect(posting[1]).toBeGreaterThan(1);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe("slugify", () => {
|
|
110
|
+
it("matches GitHub/VitePress anchors", () => {
|
|
111
|
+
expect(slugify("Bulk actions")).toBe("bulk-actions");
|
|
112
|
+
expect(slugify("`useCrud()` options")).toBe("usecrud-options");
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regression tests against the real generated knowledge base.
|
|
3
|
+
* Skipped when the package has not been built yet (`pnpm build`).
|
|
4
|
+
*/
|
|
5
|
+
import { mkdtempSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import { afterEach, describe, expect, it } from "vitest";
|
|
9
|
+
import { KNOWLEDGE_VERSION } from "../runtime/core/indexer.js";
|
|
10
|
+
import {
|
|
11
|
+
API_KNOWLEDGE_VERSION,
|
|
12
|
+
clearKnowledgeCache,
|
|
13
|
+
loadApiKnowledge,
|
|
14
|
+
loadDocsKnowledge,
|
|
15
|
+
resolveKnowledgeDir,
|
|
16
|
+
} from "../runtime/core/knowledge.js";
|
|
17
|
+
import { RECIPES } from "../runtime/core/recipes.js";
|
|
18
|
+
import { getDoc, searchDocs } from "../runtime/core/search.js";
|
|
19
|
+
|
|
20
|
+
const knowledgeDir = resolveKnowledgeDir(import.meta.url);
|
|
21
|
+
const describeBuilt = knowledgeDir ? describe : describe.skip;
|
|
22
|
+
|
|
23
|
+
// The loaders memoise per directory; tests below swap directories on purpose.
|
|
24
|
+
afterEach(() => clearKnowledgeCache());
|
|
25
|
+
|
|
26
|
+
describeBuilt("generated knowledge base", () => {
|
|
27
|
+
const knowledge = loadDocsKnowledge(knowledgeDir);
|
|
28
|
+
|
|
29
|
+
it("indexes the real docs tree", () => {
|
|
30
|
+
const paths = new Set(knowledge.chunks.map((chunk) => chunk.path));
|
|
31
|
+
expect(paths.has("howto/crud-list.md")).toBe(true);
|
|
32
|
+
expect(paths.has("uikit/form/custom-fields.md")).toBe(true);
|
|
33
|
+
expect(knowledge.chunks.length).toBeGreaterThan(100);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("excludes internal planning docs", () => {
|
|
37
|
+
const roadmap = knowledge.chunks.filter((chunk) =>
|
|
38
|
+
chunk.path.startsWith("roadmap/"),
|
|
39
|
+
);
|
|
40
|
+
expect(roadmap).toEqual([]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it.each([
|
|
44
|
+
["how do I build a paginated list of articles", "howto/crud-list.md"],
|
|
45
|
+
["add a new field type to the registry", "uikit/form/registry.md"],
|
|
46
|
+
["translate the admin interface", "howto/i18n.md"],
|
|
47
|
+
["protect a page with permissions", "howto/auth-permissions.md"],
|
|
48
|
+
["show a toast notification", "howto/feedback.md"],
|
|
49
|
+
["connect a django backend", "howto/backend-integration.md"],
|
|
50
|
+
])("ranks %j near the top of the results", (query, expectedPath) => {
|
|
51
|
+
const hits = searchDocs(knowledge, query, { limit: 3 });
|
|
52
|
+
expect(hits.map((hit) => hit.path)).toContain(expectedPath);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("keeps every curated recipe pointing at a real page and section", () => {
|
|
56
|
+
const pages = new Map(
|
|
57
|
+
knowledge.chunks.map((chunk) => [chunk.path, new Set<string>()] as const),
|
|
58
|
+
);
|
|
59
|
+
for (const chunk of knowledge.chunks)
|
|
60
|
+
pages.get(chunk.path)!.add(chunk.heading);
|
|
61
|
+
|
|
62
|
+
for (const recipe of RECIPES) {
|
|
63
|
+
expect(
|
|
64
|
+
pages.has(recipe.doc),
|
|
65
|
+
`recipe "${recipe.id}" → missing doc ${recipe.doc}`,
|
|
66
|
+
).toBe(true);
|
|
67
|
+
if (recipe.heading) {
|
|
68
|
+
expect(
|
|
69
|
+
[...pages.get(recipe.doc)!],
|
|
70
|
+
`recipe "${recipe.id}" → missing heading in ${recipe.doc}`,
|
|
71
|
+
).toContain(recipe.heading);
|
|
72
|
+
}
|
|
73
|
+
for (const related of recipe.seeAlso ?? []) {
|
|
74
|
+
expect(
|
|
75
|
+
pages.has(related),
|
|
76
|
+
`recipe "${recipe.id}" → missing seeAlso ${related}`,
|
|
77
|
+
).toBe(true);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("reads a real page back", () => {
|
|
83
|
+
const doc = getDoc(knowledge, "howto/crud-list.md")!;
|
|
84
|
+
expect(doc.title).toBeTruthy();
|
|
85
|
+
expect(doc.content).toContain("MapoList");
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describeBuilt("generated API surface", () => {
|
|
90
|
+
const api = loadApiKnowledge(knowledgeDir);
|
|
91
|
+
|
|
92
|
+
it("extracts the Mapo components from source", () => {
|
|
93
|
+
const names = api.components.map((component) => component.name);
|
|
94
|
+
expect(names).toContain("MapoList");
|
|
95
|
+
expect(names).toContain("MapoDetail");
|
|
96
|
+
expect(names).toContain("MapoForm");
|
|
97
|
+
expect(api.components.length).toBeGreaterThan(30);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("reports real prop types and JSDoc, not documentation prose", () => {
|
|
101
|
+
const list = api.components.find(
|
|
102
|
+
(component) => component.name === "MapoList",
|
|
103
|
+
)!;
|
|
104
|
+
const endpoint = list.props.find((prop) => prop.name === "endpoint")!;
|
|
105
|
+
|
|
106
|
+
expect(endpoint.type).toContain("string");
|
|
107
|
+
expect(list.props.length).toBeGreaterThan(10);
|
|
108
|
+
expect(list.props.some((prop) => prop.description)).toBe(true);
|
|
109
|
+
expect(list.slots.length).toBeGreaterThan(0);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("covers every field type of the registry, with its component", () => {
|
|
113
|
+
const types = api.fieldTypes.map((field) => field.type);
|
|
114
|
+
for (const expected of [
|
|
115
|
+
"text",
|
|
116
|
+
"select",
|
|
117
|
+
"editor",
|
|
118
|
+
"repeater",
|
|
119
|
+
"fks",
|
|
120
|
+
"date",
|
|
121
|
+
]) {
|
|
122
|
+
expect(types).toContain(expected);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const select = api.fieldTypes.find((field) => field.type === "select")!;
|
|
126
|
+
expect(select.component).toBeTruthy();
|
|
127
|
+
expect(select.attrs.map((attr) => attr.name)).toContain("items");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("keeps descriptor types readable instead of expanding mapped types", () => {
|
|
131
|
+
const key = api.fieldCommon.find((member) => member.name === "key")!;
|
|
132
|
+
expect(key.type).toBe("DeepKeyOf<T>");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("lists the auto-imported composables with their signatures", () => {
|
|
136
|
+
const names = api.composables.map((composable) => composable.name);
|
|
137
|
+
expect(names).toContain("useCrud");
|
|
138
|
+
expect(names).toContain("useMapoAuth");
|
|
139
|
+
|
|
140
|
+
const crud = api.composables.find(
|
|
141
|
+
(composable) => composable.name === "useCrud",
|
|
142
|
+
)!;
|
|
143
|
+
expect(crud.pkg).toBe("@mapomodule/core");
|
|
144
|
+
expect(crud.signature).toContain("endpoint");
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
describe("knowledge loaders", () => {
|
|
149
|
+
/**
|
|
150
|
+
* Guards the silent-wrong-data case: chunk indices in the index are only
|
|
151
|
+
* meaningful against the chunks they were built from, so a file written by a
|
|
152
|
+
* different generator must fail loudly instead of scoring against stale
|
|
153
|
+
* postings.
|
|
154
|
+
*/
|
|
155
|
+
function writeKnowledge(docsVersion: number, apiVersion: number): string {
|
|
156
|
+
const dir = mkdtempSync(join(tmpdir(), "mapo-mcp-"));
|
|
157
|
+
writeFileSync(
|
|
158
|
+
join(dir, "docs.json"),
|
|
159
|
+
JSON.stringify({
|
|
160
|
+
version: docsVersion,
|
|
161
|
+
generatedAt: "",
|
|
162
|
+
docsRoot: "docs",
|
|
163
|
+
chunks: [],
|
|
164
|
+
index: { terms: {}, lengths: [], avgLength: 0, total: 0 },
|
|
165
|
+
}),
|
|
166
|
+
);
|
|
167
|
+
writeFileSync(
|
|
168
|
+
join(dir, "api.json"),
|
|
169
|
+
JSON.stringify({
|
|
170
|
+
version: apiVersion,
|
|
171
|
+
generatedAt: "",
|
|
172
|
+
components: [],
|
|
173
|
+
fieldTypes: [],
|
|
174
|
+
fieldCommon: [],
|
|
175
|
+
composables: [],
|
|
176
|
+
}),
|
|
177
|
+
);
|
|
178
|
+
return dir;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
it("accepts files written by the current generator", () => {
|
|
182
|
+
const dir = writeKnowledge(KNOWLEDGE_VERSION, API_KNOWLEDGE_VERSION);
|
|
183
|
+
clearKnowledgeCache();
|
|
184
|
+
|
|
185
|
+
expect(loadDocsKnowledge(dir).chunks).toEqual([]);
|
|
186
|
+
expect(loadApiKnowledge(dir).components).toEqual([]);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("rejects a stale docs index instead of scoring against it", () => {
|
|
190
|
+
const dir = writeKnowledge(KNOWLEDGE_VERSION + 1, API_KNOWLEDGE_VERSION);
|
|
191
|
+
clearKnowledgeCache();
|
|
192
|
+
|
|
193
|
+
expect(() => loadDocsKnowledge(dir)).toThrow(/version mismatch/i);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("rejects a stale API surface", () => {
|
|
197
|
+
const dir = writeKnowledge(KNOWLEDGE_VERSION, API_KNOWLEDGE_VERSION + 1);
|
|
198
|
+
clearKnowledgeCache();
|
|
199
|
+
|
|
200
|
+
expect(() => loadApiKnowledge(dir)).toThrow(/version mismatch/i);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("reports a missing knowledge directory with a directive message", () => {
|
|
204
|
+
clearKnowledgeCache();
|
|
205
|
+
expect(() =>
|
|
206
|
+
loadDocsKnowledge(join(tmpdir(), "mapo-mcp-does-not-exist")),
|
|
207
|
+
).toThrow(/pnpm --filter @mapomodule\/mcp knowledge/);
|
|
208
|
+
});
|
|
209
|
+
});
|