@makerkit/prd 0.0.4 → 0.0.5
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/.turbo/turbo-build.log +78 -0
- package/.turbo/turbo-test.log +19 -0
- package/.turbo/turbo-typecheck.log +6 -0
- package/README.md +7 -1
- package/dist/README.md +71 -0
- package/dist/attachment-paths-BAhRGiib.js +112 -0
- package/dist/attachment-paths-C-SWgoSx.mjs +77 -0
- package/dist/index.d.ts +81 -0
- package/{index.js → dist/index.js} +155 -562
- package/{index.mjs → dist/index.mjs} +87 -497
- package/{mcp.d.ts → dist/mcp.d.ts} +2 -4
- package/{mcp.js → dist/mcp.js} +24 -24
- package/{mcp.mjs → dist/mcp.mjs} +24 -24
- package/{node.d.ts → dist/node.d.ts} +4 -4
- package/{node.js → dist/node.js} +28 -4
- package/{node.mjs → dist/node.mjs} +30 -6
- package/dist/operations.d.ts +141 -0
- package/dist/operations.js +22 -0
- package/dist/operations.mjs +1 -0
- package/dist/package.json +54 -0
- package/dist/prd-operations-BvFxo9bg.mjs +459 -0
- package/dist/prd-operations-CcahfbOy.js +584 -0
- package/dist/prd.service-CTwKZV-3.d.ts +114 -0
- package/{prompts-oI_6dxvx.js → dist/prompts-1qhax_dj.js} +113 -164
- package/{prompts-CPlFVutB.mjs → dist/prompts-BlsYD0gf.mjs} +96 -129
- package/{prd.service-CV0-aQHk.d.ts → dist/schema-BwTnt0B9.d.ts} +424 -336
- package/package.json +26 -2
- package/src/__tests__/agent-workflow.test.ts +417 -0
- package/src/__tests__/attachments.test.ts +505 -0
- package/src/__tests__/node.test.ts +199 -0
- package/src/__tests__/plan-check.test.ts +390 -0
- package/src/__tests__/planning.test.ts +1137 -0
- package/src/__tests__/prd-operations.test.ts +571 -0
- package/src/__tests__/prd.service.test.ts +652 -0
- package/src/__tests__/prd.tool.test.ts +282 -0
- package/src/__tests__/templates.test.ts +145 -0
- package/src/applicability.ts +73 -0
- package/src/attachment-paths.ts +83 -0
- package/src/attachments.ts +166 -0
- package/src/edge-cases.ts +61 -0
- package/src/index.ts +33 -0
- package/src/markdown.ts +297 -0
- package/src/mcp/index.ts +875 -0
- package/src/mcp/tool-response.ts +85 -0
- package/src/node.ts +184 -0
- package/src/open-questions.ts +98 -0
- package/src/plan-check.ts +385 -0
- package/src/planning-operations.ts +524 -0
- package/src/prd-operations.ts +769 -0
- package/src/prd.service.ts +1165 -0
- package/src/prompts.ts +161 -0
- package/src/safe-file-name.ts +42 -0
- package/src/schema.ts +1090 -0
- package/src/templates.ts +173 -0
- package/src/testing/fake-mcp-server.ts +81 -0
- package/src/types.ts +381 -0
- package/tsconfig.json +23 -0
- package/tsconfig.lib.json +8 -0
- package/tsconfig.spec.json +7 -0
- package/tsdown.config.mts +12 -0
- package/vitest.config.mts +8 -0
- package/index.d.ts +0 -233
- /package/{LICENSE → dist/LICENSE} +0 -0
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { formatPrdMarkdown } from '../markdown';
|
|
4
|
+
import { registerPRDTools } from '../mcp';
|
|
5
|
+
import { type PrdDeps, createPrdService, normalizePrd } from '../prd.service';
|
|
6
|
+
import { implementStoryPrompt, shapePrdPrompt } from '../prompts';
|
|
7
|
+
import { createFakeMcpServer } from '../testing/fake-mcp-server';
|
|
8
|
+
import type { StructuredPRD } from '../types';
|
|
9
|
+
|
|
10
|
+
const normalize = (path: string) => path.replace(/\\/g, '/');
|
|
11
|
+
|
|
12
|
+
/** In-memory files keyed by their full path, so nested folders are real. */
|
|
13
|
+
function createDeps(files: Record<string, string>) {
|
|
14
|
+
return {
|
|
15
|
+
rootPath: '/repo',
|
|
16
|
+
async readdir(path) {
|
|
17
|
+
const prefix = `${normalize(path)}/`;
|
|
18
|
+
|
|
19
|
+
return Object.keys(files)
|
|
20
|
+
.filter((file) => file.startsWith(prefix))
|
|
21
|
+
.map((file) => file.slice(prefix.length));
|
|
22
|
+
},
|
|
23
|
+
async readFile(path) {
|
|
24
|
+
const content = files[normalize(path)];
|
|
25
|
+
|
|
26
|
+
if (content === undefined) {
|
|
27
|
+
throw Object.assign(new Error('ENOENT'), { code: 'ENOENT' });
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return content;
|
|
31
|
+
},
|
|
32
|
+
async writeFile(path, content) {
|
|
33
|
+
files[normalize(path)] = content;
|
|
34
|
+
},
|
|
35
|
+
async mkdir() {},
|
|
36
|
+
async unlink(path) {
|
|
37
|
+
delete files[normalize(path)];
|
|
38
|
+
},
|
|
39
|
+
async fileExists(path) {
|
|
40
|
+
return normalize(path) in files;
|
|
41
|
+
},
|
|
42
|
+
now: () => new Date('2026-10-14T10:00:00Z'),
|
|
43
|
+
verifyCompletion: async () => ({
|
|
44
|
+
passed: true,
|
|
45
|
+
checks: [],
|
|
46
|
+
errors: 0,
|
|
47
|
+
warnings: 0,
|
|
48
|
+
diagnostics: [],
|
|
49
|
+
messages: [],
|
|
50
|
+
}),
|
|
51
|
+
} satisfies PrdDeps;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const STORY = {
|
|
55
|
+
userType: 'member',
|
|
56
|
+
benefit: 'it helps',
|
|
57
|
+
acceptanceCriteria: ['works'],
|
|
58
|
+
priority: 'P2' as const,
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
async function seed(prdsDir = '/repo/.prds') {
|
|
62
|
+
const files: Record<string, string> = {
|
|
63
|
+
[`${prdsDir}/notes/mockups/editor.png`]: 'png',
|
|
64
|
+
};
|
|
65
|
+
const service = createPrdService({ ...createDeps(files), prdsDir });
|
|
66
|
+
|
|
67
|
+
const { filename } = await service.create({
|
|
68
|
+
title: 'Notes',
|
|
69
|
+
overview: 'Let members keep notes',
|
|
70
|
+
problemStatement: 'No place for context',
|
|
71
|
+
marketOpportunity: 'Retention',
|
|
72
|
+
targetUsers: ['Members'],
|
|
73
|
+
solutionDescription: 'Notes per account',
|
|
74
|
+
keyFeatures: ['Create note'],
|
|
75
|
+
successMetrics: ['Notes created'],
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
await service.addUserStory({ ...STORY, filename, action: 'create a note' });
|
|
79
|
+
await service.addUserStory({ ...STORY, filename, action: 'share a note' });
|
|
80
|
+
|
|
81
|
+
const read = () =>
|
|
82
|
+
JSON.parse(files[`${prdsDir}/${filename}`]!) as StructuredPRD;
|
|
83
|
+
|
|
84
|
+
return { files, filename, service, read };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
describe('attachments', () => {
|
|
88
|
+
it('uses custom PRD directories for local files and agent-facing paths', async () => {
|
|
89
|
+
for (const [prdsDir, prdsPath] of [
|
|
90
|
+
['/repo/docs/product plans', 'docs/product plans'],
|
|
91
|
+
['/external/plans', '/external/plans'],
|
|
92
|
+
] as const) {
|
|
93
|
+
const { filename, service } = await seed(prdsDir);
|
|
94
|
+
await service.addAttachment({
|
|
95
|
+
filename,
|
|
96
|
+
title: 'Editor',
|
|
97
|
+
path: 'mockups/editor.png',
|
|
98
|
+
storyIds: ['US001'],
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect((await service.get({ filename })).path).toBe(
|
|
102
|
+
`${prdsDir}/notes.json`,
|
|
103
|
+
);
|
|
104
|
+
expect((await service.list()).prds_dir).toBe(prdsDir);
|
|
105
|
+
const story = await service.getStory({ filename, storyId: 'US001' });
|
|
106
|
+
expect(story.attachments[0]?.path).toBe(
|
|
107
|
+
`${prdsPath}/notes/mockups/editor.png`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('records a file in the PRD folder and links it to stories', async () => {
|
|
113
|
+
const { filename, service, read } = await seed();
|
|
114
|
+
|
|
115
|
+
expect(filename).toBe('notes.json');
|
|
116
|
+
|
|
117
|
+
const result = await service.addAttachment({
|
|
118
|
+
filename,
|
|
119
|
+
title: 'Editor mockup',
|
|
120
|
+
path: 'mockups/editor.png',
|
|
121
|
+
note: 'Layout only',
|
|
122
|
+
storyIds: ['US001'],
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
expect(result).toEqual({
|
|
126
|
+
attachment_id: 'ATT001',
|
|
127
|
+
path: '.prds/notes/mockups/editor.png',
|
|
128
|
+
stories_updated: ['US001'],
|
|
129
|
+
total_attachments: 1,
|
|
130
|
+
});
|
|
131
|
+
expect(read().attachments).toEqual([
|
|
132
|
+
{
|
|
133
|
+
id: 'ATT001',
|
|
134
|
+
title: 'Editor mockup',
|
|
135
|
+
path: 'mockups/editor.png',
|
|
136
|
+
note: 'Layout only',
|
|
137
|
+
},
|
|
138
|
+
]);
|
|
139
|
+
expect(read().userStories[0]!.attachmentIds).toEqual(['ATT001']);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('rejects files that are missing or outside the PRD folder', async () => {
|
|
143
|
+
const { filename, service } = await seed();
|
|
144
|
+
|
|
145
|
+
await expect(
|
|
146
|
+
service.addAttachment({ filename, title: 'x', path: 'missing.png' }),
|
|
147
|
+
).rejects.toThrow(/not found\. Save it in \.prds\/notes\//);
|
|
148
|
+
|
|
149
|
+
for (const path of [
|
|
150
|
+
'../notes.json',
|
|
151
|
+
'mockups/../../secret',
|
|
152
|
+
'/etc/passwd',
|
|
153
|
+
'.env',
|
|
154
|
+
'mockups//editor.png',
|
|
155
|
+
'mockups\\editor.png',
|
|
156
|
+
'C:/editor.png',
|
|
157
|
+
]) {
|
|
158
|
+
await expect(
|
|
159
|
+
service.addAttachment({ filename, title: 'x', path }),
|
|
160
|
+
).rejects.toThrow(/Invalid attachment path/);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('takes either a path or a url, and only known stories', async () => {
|
|
165
|
+
const { filename, service } = await seed();
|
|
166
|
+
|
|
167
|
+
await expect(
|
|
168
|
+
service.addAttachment({ filename, title: 'x' }),
|
|
169
|
+
).rejects.toThrow(/either path or url/);
|
|
170
|
+
await expect(
|
|
171
|
+
service.addAttachment({
|
|
172
|
+
filename,
|
|
173
|
+
title: 'x',
|
|
174
|
+
path: 'mockups/editor.png',
|
|
175
|
+
url: 'https://example.com',
|
|
176
|
+
}),
|
|
177
|
+
).rejects.toThrow(/either path or url/);
|
|
178
|
+
await expect(
|
|
179
|
+
service.addAttachment({
|
|
180
|
+
filename,
|
|
181
|
+
title: 'x',
|
|
182
|
+
url: 'https://example.com',
|
|
183
|
+
storyIds: ['US999'],
|
|
184
|
+
}),
|
|
185
|
+
).rejects.toThrow(/US999/);
|
|
186
|
+
|
|
187
|
+
for (const url of ['javascript:alert(1)', 'file:///etc/passwd']) {
|
|
188
|
+
await expect(
|
|
189
|
+
service.addAttachment({ filename, title: 'x', url }),
|
|
190
|
+
).rejects.toThrow(/http:\/\/ or https:\/\//);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it('returns story and PRD attachments from get_story', async () => {
|
|
195
|
+
const { filename, service } = await seed();
|
|
196
|
+
|
|
197
|
+
await service.addAttachment({
|
|
198
|
+
filename,
|
|
199
|
+
title: 'Editor mockup',
|
|
200
|
+
path: 'mockups/editor.png',
|
|
201
|
+
storyIds: ['US001'],
|
|
202
|
+
});
|
|
203
|
+
await service.addAttachment({
|
|
204
|
+
filename,
|
|
205
|
+
title: 'Brand guide',
|
|
206
|
+
url: 'https://example.com/brand',
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const context = await service.getStory({ filename, storyId: 'US001' });
|
|
210
|
+
|
|
211
|
+
expect(context.attachments).toEqual([
|
|
212
|
+
{
|
|
213
|
+
id: 'ATT001',
|
|
214
|
+
title: 'Editor mockup',
|
|
215
|
+
path: '.prds/notes/mockups/editor.png',
|
|
216
|
+
},
|
|
217
|
+
]);
|
|
218
|
+
expect(context.prd.attachments.map((item) => item.id)).toEqual([
|
|
219
|
+
'ATT001',
|
|
220
|
+
'ATT002',
|
|
221
|
+
]);
|
|
222
|
+
|
|
223
|
+
const other = await service.getStory({ filename, storyId: 'US002' });
|
|
224
|
+
|
|
225
|
+
expect(other.attachments).toEqual([]);
|
|
226
|
+
expect(other.prd.attachments).toHaveLength(2);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('links attachments when adding or updating a story', async () => {
|
|
230
|
+
const { filename, service, read } = await seed();
|
|
231
|
+
|
|
232
|
+
await service.addAttachment({
|
|
233
|
+
filename,
|
|
234
|
+
title: 'Brand guide',
|
|
235
|
+
url: 'https://example.com/brand',
|
|
236
|
+
});
|
|
237
|
+
await service.addUserStory({
|
|
238
|
+
...STORY,
|
|
239
|
+
filename,
|
|
240
|
+
action: 'pin a note',
|
|
241
|
+
attachmentIds: ['ATT001'],
|
|
242
|
+
});
|
|
243
|
+
await service.updateUserStory({
|
|
244
|
+
filename,
|
|
245
|
+
storyId: 'US002',
|
|
246
|
+
attachmentIds: ['ATT001', 'ATT001'],
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
expect(read().userStories.map((story) => story.attachmentIds)).toEqual([
|
|
250
|
+
undefined,
|
|
251
|
+
['ATT001'],
|
|
252
|
+
['ATT001'],
|
|
253
|
+
]);
|
|
254
|
+
await expect(
|
|
255
|
+
service.updateUserStory({
|
|
256
|
+
filename,
|
|
257
|
+
storyId: 'US001',
|
|
258
|
+
attachmentIds: ['ATT404'],
|
|
259
|
+
}),
|
|
260
|
+
).rejects.toThrow(/Attachment "ATT404" not found/);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('removes an attachment from every story and never reissues its id', async () => {
|
|
264
|
+
const { filename, service, read } = await seed();
|
|
265
|
+
|
|
266
|
+
await service.addAttachment({
|
|
267
|
+
filename,
|
|
268
|
+
title: 'Editor mockup',
|
|
269
|
+
path: 'mockups/editor.png',
|
|
270
|
+
storyIds: ['US001', 'US002'],
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const removed = await service.removeAttachment({
|
|
274
|
+
filename,
|
|
275
|
+
attachmentId: 'ATT001',
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
expect(removed.stories_updated).toEqual(['US001', 'US002']);
|
|
279
|
+
|
|
280
|
+
const added = await service.addAttachment({
|
|
281
|
+
filename,
|
|
282
|
+
title: 'Brand guide',
|
|
283
|
+
url: 'https://example.com/brand',
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
expect(added.attachment_id).toBe('ATT002');
|
|
287
|
+
expect(
|
|
288
|
+
read().userStories.flatMap((story) => story.attachmentIds ?? []),
|
|
289
|
+
).toEqual([]);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('does not reuse ids still linked from stories after an older writer dropped the list', async () => {
|
|
293
|
+
const { files, filename, service } = await seed();
|
|
294
|
+
|
|
295
|
+
await service.addAttachment({
|
|
296
|
+
filename,
|
|
297
|
+
title: 'Editor mockup',
|
|
298
|
+
path: 'mockups/editor.png',
|
|
299
|
+
storyIds: ['US001'],
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
// What a version without attachments leaves behind when it saves.
|
|
303
|
+
const path = `/repo/.prds/${filename}`;
|
|
304
|
+
const stale = JSON.parse(files[path]!);
|
|
305
|
+
delete stale.attachments;
|
|
306
|
+
delete stale.metadata.lastAttachmentNumber;
|
|
307
|
+
files[path] = JSON.stringify(stale);
|
|
308
|
+
|
|
309
|
+
const added = await service.addAttachment({
|
|
310
|
+
filename,
|
|
311
|
+
title: 'Brand guide',
|
|
312
|
+
url: 'https://example.com/brand',
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
expect(added.attachment_id).toBe('ATT002');
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it('lists attachments in the markdown export', async () => {
|
|
319
|
+
const { filename, service, read } = await seed();
|
|
320
|
+
|
|
321
|
+
await service.addAttachment({
|
|
322
|
+
filename,
|
|
323
|
+
title: 'Editor mockup',
|
|
324
|
+
path: 'mockups/editor.png',
|
|
325
|
+
note: 'Layout only',
|
|
326
|
+
storyIds: ['US001'],
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
const markdown = formatPrdMarkdown(read(), filename);
|
|
330
|
+
|
|
331
|
+
expect(markdown).toContain(
|
|
332
|
+
'- ATT001: [Editor mockup](notes/mockups/editor.png) - Layout only',
|
|
333
|
+
);
|
|
334
|
+
expect(markdown).toContain('**Attachments:** ATT001');
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it('keeps attachment titles and links from breaking the markdown', () => {
|
|
338
|
+
const markdown = formatPrdMarkdown(
|
|
339
|
+
normalizePrd(
|
|
340
|
+
{
|
|
341
|
+
attachments: [
|
|
342
|
+
{
|
|
343
|
+
id: 'ATT001',
|
|
344
|
+
title: 'x](javascript:alert(1)) [y\n## Injected',
|
|
345
|
+
url: 'https://example.com/a (b)',
|
|
346
|
+
},
|
|
347
|
+
],
|
|
348
|
+
},
|
|
349
|
+
'2026-10-14',
|
|
350
|
+
),
|
|
351
|
+
'notes.json',
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
expect(markdown).not.toContain('\n## Injected');
|
|
355
|
+
expect(markdown).toContain(
|
|
356
|
+
'- ATT001: [x\\](javascript:alert(1)) \\[y ## Injected](https://example.com/a%20%28b%29)',
|
|
357
|
+
);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it('registers the attachment tools', async () => {
|
|
361
|
+
const { filename, service } = await seed();
|
|
362
|
+
const fake = createFakeMcpServer();
|
|
363
|
+
|
|
364
|
+
registerPRDTools(fake.server, service);
|
|
365
|
+
|
|
366
|
+
const added = await fake.call('add_attachment', {
|
|
367
|
+
filename,
|
|
368
|
+
title: 'Brand guide',
|
|
369
|
+
url: 'https://example.com/brand',
|
|
370
|
+
storyIds: ['US001'],
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
expect(added.isError).toBeFalsy();
|
|
374
|
+
expect(added.structuredContent).toMatchObject({ attachment_id: 'ATT001' });
|
|
375
|
+
|
|
376
|
+
const invalid = await fake.call('add_attachment', {
|
|
377
|
+
filename,
|
|
378
|
+
title: 'x',
|
|
379
|
+
url: 'not a url',
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
expect(invalid.isError).toBe(true);
|
|
383
|
+
|
|
384
|
+
const removed = await fake.call('remove_attachment', {
|
|
385
|
+
filename,
|
|
386
|
+
attachmentId: 'ATT001',
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
expect(removed.structuredContent).toMatchObject({
|
|
390
|
+
stories_updated: ['US001'],
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
describe('prompts', () => {
|
|
396
|
+
it('tells the agent to open a story’s attachments and to record them', () => {
|
|
397
|
+
expect(implementStoryPrompt('notes.json', 'US001')).toMatch(
|
|
398
|
+
/Open the files and links in its attachments/,
|
|
399
|
+
);
|
|
400
|
+
expect(shapePrdPrompt('A CRM')).toContain('add_attachment');
|
|
401
|
+
expect(shapePrdPrompt('A CRM')).toContain('PRD text may use Markdown');
|
|
402
|
+
});
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
describe('normalizePrd', () => {
|
|
406
|
+
it('keeps fields it does not know, so newer files survive older writers', () => {
|
|
407
|
+
const prd = normalizePrd(
|
|
408
|
+
{ introduction: { title: 'T' }, futureField: { keep: true } },
|
|
409
|
+
'2026-10-14',
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
expect(prd.attachments).toEqual([]);
|
|
413
|
+
expect(JSON.parse(JSON.stringify(prd)).futureField).toEqual({
|
|
414
|
+
keep: true,
|
|
415
|
+
});
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it('drops hand-edited attachments that are malformed or escape the folder', () => {
|
|
419
|
+
const prd = normalizePrd(
|
|
420
|
+
{
|
|
421
|
+
attachments: [
|
|
422
|
+
{ id: 'ATT001', title: 'Mockup', path: 'mockups/a.png' },
|
|
423
|
+
{
|
|
424
|
+
id: 'ATT002',
|
|
425
|
+
title: 'Docs',
|
|
426
|
+
url: 'https://example.com',
|
|
427
|
+
note: null,
|
|
428
|
+
},
|
|
429
|
+
{ id: 'ATT003', title: 'Broken', url: null },
|
|
430
|
+
{ id: 'ATT004', title: 'Escape', path: '../../../etc/passwd' },
|
|
431
|
+
{ id: 'ATT005', title: 'Script', url: 'javascript:alert(1)' },
|
|
432
|
+
{ id: 'ATT006', title: 'Both', path: 'a.png', url: 'https://x.y' },
|
|
433
|
+
null,
|
|
434
|
+
'ATT007',
|
|
435
|
+
],
|
|
436
|
+
},
|
|
437
|
+
'2026-10-14',
|
|
438
|
+
);
|
|
439
|
+
|
|
440
|
+
expect(prd.attachments).toEqual([
|
|
441
|
+
{ id: 'ATT001', title: 'Mockup', path: 'mockups/a.png' },
|
|
442
|
+
{ id: 'ATT002', title: 'Docs', url: 'https://example.com' },
|
|
443
|
+
]);
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
it('ignores files that are not an object, and a malformed attachment list', () => {
|
|
447
|
+
expect(normalizePrd(['a', 'b'], '2026-10-14')).not.toHaveProperty('0');
|
|
448
|
+
expect(
|
|
449
|
+
normalizePrd({ attachments: 'x' }, '2026-10-14').attachments,
|
|
450
|
+
).toEqual([]);
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
describe('update_attachment and revision checks', () => {
|
|
455
|
+
it('changes the title, clears the note and sets exactly the linked stories', async () => {
|
|
456
|
+
const { filename, service, read } = await seed();
|
|
457
|
+
|
|
458
|
+
await service.addAttachment({
|
|
459
|
+
filename,
|
|
460
|
+
title: 'Editor mockup',
|
|
461
|
+
path: 'mockups/editor.png',
|
|
462
|
+
note: 'Layout only',
|
|
463
|
+
storyIds: ['US001'],
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
expect(
|
|
467
|
+
await service.updateAttachment({
|
|
468
|
+
filename,
|
|
469
|
+
attachmentId: 'ATT001',
|
|
470
|
+
title: 'Editor (v2)',
|
|
471
|
+
note: '',
|
|
472
|
+
storyIds: ['US002'],
|
|
473
|
+
}),
|
|
474
|
+
).toEqual({ attachment_id: 'ATT001', stories_updated: ['US001', 'US002'] });
|
|
475
|
+
expect(read().attachments[0]).toEqual({
|
|
476
|
+
id: 'ATT001',
|
|
477
|
+
title: 'Editor (v2)',
|
|
478
|
+
path: 'mockups/editor.png',
|
|
479
|
+
});
|
|
480
|
+
expect(
|
|
481
|
+
read().userStories.map((story) => story.attachmentIds ?? []),
|
|
482
|
+
).toEqual([[], ['ATT001']]);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it('rejects adding or removing based on an older revision', async () => {
|
|
486
|
+
const { filename, service, read } = await seed();
|
|
487
|
+
const seen = read().metadata.revision ?? 0;
|
|
488
|
+
|
|
489
|
+
await service.updatePrdSection({
|
|
490
|
+
filename,
|
|
491
|
+
section: 'nonGoals',
|
|
492
|
+
value: ['x'],
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
await expect(
|
|
496
|
+
service.addAttachment({
|
|
497
|
+
filename,
|
|
498
|
+
title: 'Docs',
|
|
499
|
+
url: 'https://example.com',
|
|
500
|
+
expectedRevision: seen,
|
|
501
|
+
}),
|
|
502
|
+
).rejects.toThrow(/changed by someone else/);
|
|
503
|
+
expect(read().attachments).toEqual([]);
|
|
504
|
+
});
|
|
505
|
+
});
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { createNodePrdDeps, createScriptVerifier } from '../node';
|
|
4
|
+
import { createPrdService } from '../prd.service';
|
|
5
|
+
|
|
6
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
7
|
+
import { tmpdir } from 'node:os';
|
|
8
|
+
import { join } from 'node:path';
|
|
9
|
+
|
|
10
|
+
const directories: string[] = [];
|
|
11
|
+
|
|
12
|
+
async function createProject(scripts: Record<string, string>) {
|
|
13
|
+
const root = await mkdtemp(join(tmpdir(), 'makerkit-prd-'));
|
|
14
|
+
directories.push(root);
|
|
15
|
+
|
|
16
|
+
await writeFile(
|
|
17
|
+
join(root, 'package.json'),
|
|
18
|
+
JSON.stringify({ name: 'fixture', private: true, scripts }),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
return root;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
afterEach(async () => {
|
|
25
|
+
await Promise.all(
|
|
26
|
+
directories.splice(0).map((dir) => rm(dir, { recursive: true })),
|
|
27
|
+
);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('createScriptVerifier', () => {
|
|
31
|
+
it('passes with no checks when the project has no package.json', async () => {
|
|
32
|
+
const root = await mkdtemp(join(tmpdir(), 'prd-no-package-'));
|
|
33
|
+
directories.push(root);
|
|
34
|
+
|
|
35
|
+
expect(await createScriptVerifier(root)()).toEqual({
|
|
36
|
+
passed: true,
|
|
37
|
+
checks: [],
|
|
38
|
+
errors: 0,
|
|
39
|
+
warnings: 0,
|
|
40
|
+
diagnostics: [],
|
|
41
|
+
messages: ['No package.json: there are no project checks to run.'],
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('passes when every defined check exits 0', async () => {
|
|
46
|
+
const root = await createProject({
|
|
47
|
+
typecheck: 'node -e "process.exit(0)"',
|
|
48
|
+
lint: 'node -e "process.exit(0)"',
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const result = await createScriptVerifier(root)();
|
|
52
|
+
|
|
53
|
+
expect(result).toMatchObject({
|
|
54
|
+
passed: true,
|
|
55
|
+
checks: ['typecheck', 'lint'],
|
|
56
|
+
errors: 0,
|
|
57
|
+
messages: [],
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('fails and reports the output of a failing check', async () => {
|
|
62
|
+
const root = await createProject({
|
|
63
|
+
typecheck:
|
|
64
|
+
'node -e "console.error(\'TS2322 bad type\'); process.exit(1)"',
|
|
65
|
+
lint: 'node -e "process.exit(0)"',
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const result = await createScriptVerifier(root)();
|
|
69
|
+
|
|
70
|
+
expect(result.passed).toBe(false);
|
|
71
|
+
expect(result.errors).toBe(1);
|
|
72
|
+
expect(result.messages).toHaveLength(1);
|
|
73
|
+
expect(result.messages[0]).toMatch(/^typecheck: fail/);
|
|
74
|
+
expect(result.messages[0]).toContain('TS2322 bad type');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('skips checks the project does not define', async () => {
|
|
78
|
+
const root = await createProject({ lint: 'node -e "process.exit(0)"' });
|
|
79
|
+
|
|
80
|
+
const result = await createScriptVerifier(root)();
|
|
81
|
+
|
|
82
|
+
expect(result.passed).toBe(true);
|
|
83
|
+
expect(result.checks).toEqual(['lint']);
|
|
84
|
+
expect(result.messages).toEqual([
|
|
85
|
+
'typecheck: skipped (no "typecheck" script in package.json)',
|
|
86
|
+
]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('runs only the scripts it is given', async () => {
|
|
90
|
+
const root = await createProject({
|
|
91
|
+
test: 'node -e "process.exit(0)"',
|
|
92
|
+
lint: 'node -e "process.exit(1)"',
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const result = await createScriptVerifier(root, ['test'])();
|
|
96
|
+
|
|
97
|
+
expect(result).toMatchObject({ passed: true, checks: ['test'] });
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('createScriptVerifier diagnostics', () => {
|
|
102
|
+
it('returns file-and-line diagnostics for a failing typecheck', async () => {
|
|
103
|
+
const root = await createProject({
|
|
104
|
+
typecheck:
|
|
105
|
+
'node -e "console.log(\'src/notes.ts(12,5): error TS2322: Type string is not assignable to type number.\'); process.exit(2)"',
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const result = await createScriptVerifier(root)();
|
|
109
|
+
|
|
110
|
+
expect(result.passed).toBe(false);
|
|
111
|
+
expect(result.diagnostics).toEqual([
|
|
112
|
+
expect.objectContaining({
|
|
113
|
+
file: 'src/notes.ts',
|
|
114
|
+
line: 12,
|
|
115
|
+
severity: 'error',
|
|
116
|
+
}),
|
|
117
|
+
]);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
describe('createNodePrdDeps', () => {
|
|
122
|
+
it('allows only one worker to claim a story across service instances', async () => {
|
|
123
|
+
const root = await createProject({});
|
|
124
|
+
const first = createPrdService(createNodePrdDeps(root));
|
|
125
|
+
const second = createPrdService(createNodePrdDeps(root));
|
|
126
|
+
const { filename } = await first.create({
|
|
127
|
+
title: 'Claims',
|
|
128
|
+
overview: '',
|
|
129
|
+
problemStatement: '',
|
|
130
|
+
marketOpportunity: '',
|
|
131
|
+
targetUsers: [],
|
|
132
|
+
solutionDescription: '',
|
|
133
|
+
keyFeatures: [],
|
|
134
|
+
successMetrics: [],
|
|
135
|
+
});
|
|
136
|
+
const { story_id: storyId } = await first.addUserStory({
|
|
137
|
+
filename,
|
|
138
|
+
userType: 'member',
|
|
139
|
+
action: 'save a note',
|
|
140
|
+
benefit: 'keep a record',
|
|
141
|
+
acceptanceCriteria: ['The note is saved'],
|
|
142
|
+
priority: 'P2',
|
|
143
|
+
});
|
|
144
|
+
const claim = { filename, storyId, status: 'in_progress' as const };
|
|
145
|
+
|
|
146
|
+
const results = await Promise.allSettled([
|
|
147
|
+
first.updateStoryStatus({ ...claim, workerId: 'first' }),
|
|
148
|
+
second.updateStoryStatus({ ...claim, workerId: 'second' }),
|
|
149
|
+
]);
|
|
150
|
+
|
|
151
|
+
expect(results.map((result) => result.status).sort()).toEqual([
|
|
152
|
+
'fulfilled',
|
|
153
|
+
'rejected',
|
|
154
|
+
]);
|
|
155
|
+
const owner = results[0]?.status === 'fulfilled' ? 'first' : 'second';
|
|
156
|
+
const other = owner === 'first' ? 'second' : 'first';
|
|
157
|
+
expect((await first.getStory({ filename, storyId })).story.workerId).toBe(
|
|
158
|
+
owner,
|
|
159
|
+
);
|
|
160
|
+
await expect(
|
|
161
|
+
second.addStoryLog({
|
|
162
|
+
filename,
|
|
163
|
+
storyId,
|
|
164
|
+
workerId: other,
|
|
165
|
+
whatsNew: ['Saved'],
|
|
166
|
+
technicalNotes: [],
|
|
167
|
+
}),
|
|
168
|
+
).rejects.toThrow('claimed by another worker');
|
|
169
|
+
await first.updateStoryStatus({
|
|
170
|
+
...claim,
|
|
171
|
+
workerId: owner,
|
|
172
|
+
status: 'blocked',
|
|
173
|
+
});
|
|
174
|
+
await expect(
|
|
175
|
+
second.updateStoryStatus({ ...claim, workerId: other }),
|
|
176
|
+
).resolves.toMatchObject({ status: 'in_progress' });
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('uses an injected verifier instead of running scripts', async () => {
|
|
180
|
+
const root = await createProject({
|
|
181
|
+
typecheck: 'node -e "process.exit(1)"',
|
|
182
|
+
});
|
|
183
|
+
const verifyCompletion = async () => ({
|
|
184
|
+
passed: true,
|
|
185
|
+
checks: ['custom'],
|
|
186
|
+
errors: 0,
|
|
187
|
+
warnings: 0,
|
|
188
|
+
diagnostics: [],
|
|
189
|
+
messages: [],
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const deps = createNodePrdDeps(root, { verifyCompletion });
|
|
193
|
+
|
|
194
|
+
await expect(deps.verifyCompletion()).resolves.toMatchObject({
|
|
195
|
+
passed: true,
|
|
196
|
+
checks: ['custom'],
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|