-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathagent.spec.ts
More file actions
494 lines (423 loc) · 16.1 KB
/
agent.spec.ts
File metadata and controls
494 lines (423 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import path from 'node:path';
import * as prompts from '@voidzero-dev/vite-plus-prompts';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
detectExistingAgentTargetPaths,
detectExistingAgentTargetPath,
hasExistingAgentInstructions,
replaceMarkedAgentInstructionsSection,
resolveAgentTargetPaths,
selectAgentTargetPaths,
writeAgentInstructions,
} from '../agent.js';
import { pkgRoot } from '../path.js';
type MockNode =
| { kind: 'dir' }
| { kind: 'file'; content: string }
| { kind: 'symlink'; target: string };
class InMemoryFs {
private readonly nodes = new Map<string, MockNode>();
constructor() {
this.ensureDirectory(path.parse(process.cwd()).root);
}
existsSync(filePath: fs.PathLike): boolean {
return this.nodes.has(this.normalize(filePath));
}
lstatSync(filePath: fs.PathLike): fs.Stats {
const node = this.getNode(filePath);
return {
isSymbolicLink: () => node.kind === 'symlink',
} as fs.Stats;
}
async lstat(filePath: fs.PathLike): Promise<fs.Stats> {
return this.lstatSync(filePath);
}
async mkdir(dirPath: fs.PathLike, options: { recursive: true }): Promise<void> {
if (!options.recursive) {
throw new Error('Only recursive mkdir is supported in tests');
}
this.ensureDirectory(this.normalize(dirPath));
}
async readFile(filePath: fs.PathLike): Promise<string> {
const resolvedPath = this.resolvePath(filePath);
const node = this.nodes.get(resolvedPath);
if (!node || node.kind !== 'file') {
throw new Error(`ENOENT: no such file "${String(filePath)}"`);
}
return node.content;
}
async writeFile(filePath: fs.PathLike, content: string): Promise<void> {
const resolvedPath = this.resolvePathForWrite(filePath);
this.ensureDirectory(path.dirname(resolvedPath));
this.nodes.set(resolvedPath, { kind: 'file', content });
}
async appendFile(filePath: fs.PathLike, content: string): Promise<void> {
const resolvedPath = this.resolvePathForWrite(filePath);
this.ensureDirectory(path.dirname(resolvedPath));
const existing = this.nodes.get(resolvedPath);
if (!existing) {
this.nodes.set(resolvedPath, { kind: 'file', content });
return;
}
if (existing.kind !== 'file') {
throw new Error(`EISDIR: cannot append to non-file "${String(filePath)}"`);
}
existing.content += content;
}
async realpath(filePath: fs.PathLike): Promise<string> {
return this.resolvePath(filePath);
}
async symlink(target: string, filePath: fs.PathLike): Promise<void> {
const normalizedPath = this.normalize(filePath);
this.ensureDirectory(path.dirname(normalizedPath));
this.nodes.set(normalizedPath, { kind: 'symlink', target });
}
async readlink(filePath: fs.PathLike): Promise<string> {
const node = this.getNode(filePath);
if (node.kind !== 'symlink') {
throw new Error(`EINVAL: not a symlink "${String(filePath)}"`);
}
return node.target;
}
async unlink(filePath: fs.PathLike): Promise<void> {
const normalizedPath = this.normalize(filePath);
if (!this.nodes.has(normalizedPath)) {
throw new Error(`ENOENT: no such file "${String(filePath)}"`);
}
this.nodes.delete(normalizedPath);
}
readFileSync(filePath: fs.PathLike): string {
const resolvedPath = this.resolvePath(filePath);
const node = this.nodes.get(resolvedPath);
if (!node || node.kind !== 'file') {
throw new Error(`ENOENT: no such file "${String(filePath)}"`);
}
return node.content;
}
isSymlink(filePath: string): boolean {
return this.lstatSync(filePath).isSymbolicLink();
}
readlinkSync(filePath: string): string {
const node = this.getNode(filePath);
if (node.kind !== 'symlink') {
throw new Error(`EINVAL: not a symlink "${filePath}"`);
}
return node.target;
}
async readText(filePath: string): Promise<string> {
return this.readFile(filePath);
}
private normalize(filePath: fs.PathLike): string {
return path.resolve(String(filePath));
}
private getNode(filePath: fs.PathLike): MockNode {
const normalizedPath = this.normalize(filePath);
const node = this.nodes.get(normalizedPath);
if (!node) {
throw new Error(`ENOENT: no such file "${String(filePath)}"`);
}
return node;
}
private ensureDirectory(dirPath: string): void {
const normalizedPath = path.resolve(dirPath);
const root = path.parse(normalizedPath).root;
let current = root;
this.nodes.set(root, { kind: 'dir' });
const segments = path.relative(root, normalizedPath).split(path.sep).filter(Boolean);
for (const segment of segments) {
current = path.join(current, segment);
const node = this.nodes.get(current);
if (!node) {
this.nodes.set(current, { kind: 'dir' });
continue;
}
if (node.kind !== 'dir') {
throw new Error(`ENOTDIR: "${current}" is not a directory`);
}
}
}
private resolvePath(filePath: fs.PathLike): string {
let current = this.normalize(filePath);
const visited = new Set<string>();
while (true) {
const node = this.nodes.get(current);
if (!node) {
throw new Error(`ENOENT: no such file "${String(filePath)}"`);
}
if (node.kind !== 'symlink') {
return current;
}
if (visited.has(current)) {
throw new Error(`ELOOP: too many symlink levels "${String(filePath)}"`);
}
visited.add(current);
current = path.resolve(path.dirname(current), node.target);
}
}
private resolvePathForWrite(filePath: fs.PathLike): string {
const normalizedPath = this.normalize(filePath);
const node = this.nodes.get(normalizedPath);
if (node?.kind === 'symlink') {
return path.resolve(path.dirname(normalizedPath), node.target);
}
return normalizedPath;
}
}
const AGENT_TEMPLATE = ['<!--VITE PLUS START-->', 'template block', '<!--VITE PLUS END-->'].join(
'\n',
);
let mockFs: InMemoryFs;
let projectIndex = 0;
beforeEach(async () => {
vi.spyOn(prompts.log, 'message').mockImplementation(() => {});
mockFs = new InMemoryFs();
projectIndex = 0;
vi.spyOn(fs, 'existsSync').mockImplementation((filePath) => mockFs.existsSync(filePath));
vi.spyOn(fs, 'lstatSync').mockImplementation((filePath) => mockFs.lstatSync(filePath));
vi.spyOn(fs, 'readFileSync').mockImplementation((filePath) =>
mockFs.readFileSync(filePath as fs.PathLike),
);
vi.spyOn(fsPromises, 'appendFile').mockImplementation(async (filePath, data) =>
mockFs.appendFile(filePath as fs.PathLike, String(data)),
);
vi.spyOn(fsPromises, 'lstat').mockImplementation(async (filePath) => mockFs.lstat(filePath));
vi.spyOn(fsPromises, 'mkdir').mockImplementation(async (filePath, options) => {
await mockFs.mkdir(filePath, options as { recursive: true });
return undefined;
});
vi.spyOn(fsPromises, 'readFile').mockImplementation(async (filePath) =>
mockFs.readFile(filePath as fs.PathLike),
);
vi.spyOn(fsPromises, 'readlink').mockImplementation(async (filePath) =>
mockFs.readlink(filePath),
);
vi.spyOn(fsPromises, 'realpath').mockImplementation(async (filePath) =>
mockFs.realpath(filePath),
);
vi.spyOn(fsPromises, 'symlink').mockImplementation(async (target, filePath) => {
await mockFs.symlink(String(target), filePath);
});
vi.spyOn(fsPromises, 'unlink').mockImplementation(async (filePath) => {
await mockFs.unlink(filePath);
});
vi.spyOn(fsPromises, 'writeFile').mockImplementation(async (filePath, data) => {
await mockFs.writeFile(filePath as fs.PathLike, data as string);
});
await mockFs.writeFile(path.join(pkgRoot, 'AGENTS.md'), AGENT_TEMPLATE);
});
afterEach(() => {
vi.restoreAllMocks();
});
async function createProjectDir() {
const dir = path.join(pkgRoot, '__virtual__', `project-${projectIndex++}`);
await mockFs.mkdir(dir, { recursive: true });
return dir;
}
describe('resolveAgentTargetPaths', () => {
it('resolves legacy agent names and deduplicates target paths', () => {
expect(resolveAgentTargetPaths('claude,amp,opencode,chatgpt')).toEqual([
'CLAUDE.md',
'AGENTS.md',
]);
});
it('resolves file names directly', () => {
expect(
resolveAgentTargetPaths(['AGENTS.md', 'CLAUDE.md', '.github/copilot-instructions.md']),
).toEqual(['AGENTS.md', 'CLAUDE.md', '.github/copilot-instructions.md']);
});
it('resolves repeated --agent values and trims whitespace', () => {
expect(resolveAgentTargetPaths([' claude ', ' amp, opencode ', 'codex'])).toEqual([
'CLAUDE.md',
'AGENTS.md',
]);
});
it('falls back to AGENTS.md when no valid agents are provided', () => {
expect(resolveAgentTargetPaths()).toEqual(['AGENTS.md']);
expect(resolveAgentTargetPaths(' , , ')).toEqual(['AGENTS.md']);
});
});
describe('selectAgentTargetPaths', () => {
it('prompts with file-based targets and agent hints', async () => {
const multiselectSpy = vi.spyOn(prompts, 'multiselect').mockResolvedValue(['agents', 'claude']);
await expect(
selectAgentTargetPaths({
interactive: true,
onCancel: vi.fn(),
}),
).resolves.toEqual(['AGENTS.md', 'CLAUDE.md']);
expect(multiselectSpy).toHaveBeenCalledWith(
expect.objectContaining({
message: expect.stringContaining(
'Which coding agent instruction files should Vite+ create?',
),
initialValues: ['agents'],
options: expect.arrayContaining([
expect.objectContaining({
label: 'AGENTS.md',
value: 'agents',
hint: expect.stringContaining('Codex'),
}),
expect.objectContaining({
label: 'CLAUDE.md',
value: 'claude',
hint: 'Claude Code',
}),
]),
}),
);
});
});
describe('detectExistingAgentTargetPath', () => {
it('detects all existing regular agent files', async () => {
const dir = await createProjectDir();
await mockFs.writeFile(path.join(dir, 'AGENTS.md'), '# Agents');
await mockFs.writeFile(path.join(dir, 'CLAUDE.md'), '# Claude');
expect(detectExistingAgentTargetPaths(dir)).toEqual(['AGENTS.md', 'CLAUDE.md']);
});
it('detects existing regular agent files', async () => {
const dir = await createProjectDir();
await mockFs.writeFile(path.join(dir, 'CLAUDE.md'), '# Claude');
expect(detectExistingAgentTargetPath(dir)).toBe('CLAUDE.md');
});
it('ignores symlinked agent files', async () => {
const dir = await createProjectDir();
await mockFs.symlink('AGENTS.md', path.join(dir, 'CLAUDE.md'));
expect(detectExistingAgentTargetPath(dir)).toBeUndefined();
});
});
describe('replaceMarkedAgentInstructionsSection', () => {
it('replaces the marker block when markers are present in both files', () => {
const existing = [
'# Local instructions',
'<!--VITE PLUS START-->',
'old block',
'<!--VITE PLUS END-->',
'# Footer',
].join('\n');
const incoming = ['<!--VITE PLUS START-->', 'new block', '<!--VITE PLUS END-->'].join('\n');
expect(replaceMarkedAgentInstructionsSection(existing, incoming)).toBe(
[
'# Local instructions',
'<!--VITE PLUS START-->',
'new block',
'<!--VITE PLUS END-->',
'# Footer',
].join('\n'),
);
});
it('returns undefined when markers are missing in existing content', () => {
expect(
replaceMarkedAgentInstructionsSection(
'no markers here',
'<!--VITE PLUS START-->\nnew\n<!--VITE PLUS END-->',
),
).toBeUndefined();
});
});
describe('writeAgentInstructions symlink behavior', () => {
it('links non-standard agent files to AGENTS.md when AGENTS.md is selected', async () => {
const dir = await createProjectDir();
await writeAgentInstructions({
projectRoot: dir,
targetPaths: ['AGENTS.md', 'CLAUDE.md', 'GEMINI.md', '.github/copilot-instructions.md'],
interactive: false,
});
expect(mockFs.isSymlink(path.join(dir, 'AGENTS.md'))).toBe(false);
expect(mockFs.isSymlink(path.join(dir, 'CLAUDE.md'))).toBe(true);
expect(mockFs.readlinkSync(path.join(dir, 'CLAUDE.md'))).toBe('AGENTS.md');
expect(mockFs.isSymlink(path.join(dir, 'GEMINI.md'))).toBe(true);
expect(mockFs.readlinkSync(path.join(dir, 'GEMINI.md'))).toBe('AGENTS.md');
expect(mockFs.isSymlink(path.join(dir, '.github/copilot-instructions.md'))).toBe(true);
expect(mockFs.readlinkSync(path.join(dir, '.github/copilot-instructions.md'))).toBe(
path.join('..', 'AGENTS.md'),
);
});
it('falls back to copy when symlink throws EPERM (Windows without admin)', async () => {
const dir = await createProjectDir();
const symlinkSpy = vi.spyOn(fsPromises, 'symlink');
const copyFileSpy = vi.spyOn(fsPromises, 'copyFile').mockResolvedValue(undefined);
// Make symlink throw EPERM (Windows behavior without admin privileges)
symlinkSpy.mockRejectedValue(
Object.assign(new Error('EPERM: operation not permitted, symlink'), { code: 'EPERM' }),
);
await writeAgentInstructions({
projectRoot: dir,
targetPaths: ['AGENTS.md', 'CLAUDE.md', '.github/copilot-instructions.md'],
interactive: false,
});
// AGENTS.md should be written as a regular file (not symlinked)
expect(mockFs.existsSync(path.join(dir, 'AGENTS.md'))).toBe(true);
// Non-standard paths should fall back to copyFile since symlink failed
expect(copyFileSpy).toHaveBeenCalledWith(
path.join(dir, 'AGENTS.md'),
path.join(dir, 'CLAUDE.md'),
);
expect(copyFileSpy).toHaveBeenCalledWith(
path.join(dir, 'AGENTS.md'),
path.join(dir, '.github', 'copilot-instructions.md'),
);
});
it('does not replace existing non-symlink files with symlinks', async () => {
const dir = await createProjectDir();
const existingClaude = path.join(dir, 'CLAUDE.md');
await mockFs.writeFile(existingClaude, 'existing claude instructions');
await writeAgentInstructions({
projectRoot: dir,
targetPaths: ['AGENTS.md', 'CLAUDE.md'],
interactive: false,
});
expect(mockFs.isSymlink(existingClaude)).toBe(false);
expect(await mockFs.readText(existingClaude)).toBe('existing claude instructions');
expect(mockFs.existsSync(path.join(dir, 'AGENTS.md'))).toBe(true);
});
it('silently updates marker blocks without prompting in interactive mode', async () => {
const dir = await createProjectDir();
const targetPath = path.join(dir, 'AGENTS.md');
const existing = [
'# Local',
'<!--VITE PLUS START-->',
'old block',
'<!--VITE PLUS END-->',
].join('\n');
await mockFs.writeFile(targetPath, existing);
const selectSpy = vi.spyOn(prompts, 'select');
const successSpy = vi.spyOn(prompts.log, 'success');
await writeAgentInstructions({
projectRoot: dir,
targetPaths: ['AGENTS.md'],
interactive: true,
});
expect(selectSpy).not.toHaveBeenCalled();
expect(await mockFs.readText(targetPath)).toContain('template block');
expect(successSpy).not.toHaveBeenCalledWith('Updated agent instructions in AGENTS.md');
});
});
describe('hasExistingAgentInstructions', () => {
it('returns true when an agent file has start marker', async () => {
const dir = await createProjectDir();
await mockFs.writeFile(
path.join(dir, 'AGENTS.md'),
'<!--VITE PLUS START-->\ncontent\n<!--VITE PLUS END-->',
);
expect(hasExistingAgentInstructions(dir)).toBe(true);
});
it('returns true when CLAUDE.md has start marker', async () => {
const dir = await createProjectDir();
await mockFs.writeFile(
path.join(dir, 'CLAUDE.md'),
'<!--VITE PLUS START-->\ncontent\n<!--VITE PLUS END-->',
);
expect(hasExistingAgentInstructions(dir)).toBe(true);
});
it('returns false when files exist without markers', async () => {
const dir = await createProjectDir();
await mockFs.writeFile(path.join(dir, 'AGENTS.md'), '# No markers here');
expect(hasExistingAgentInstructions(dir)).toBe(false);
});
it('returns false when no files exist', async () => {
const dir = await createProjectDir();
expect(hasExistingAgentInstructions(dir)).toBe(false);
});
});