-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathroutes.test.ts
More file actions
145 lines (125 loc) · 4.25 KB
/
Copy pathroutes.test.ts
File metadata and controls
145 lines (125 loc) · 4.25 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
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import express from 'express';
import { AgentStore } from '../src/store/AgentStore.js';
import { templateRoutes } from '../src/routes/templates.js';
import { directoryRoutes } from '../src/routes/directories.js';
// Simple test helper
async function request(app: express.Express, method: string, url: string, body?: unknown) {
// Use a simple approach - create a test server
const { createServer } = await import('http');
return new Promise<{ status: number; body: unknown }>((resolve, reject) => {
const server = createServer(app);
server.listen(0, () => {
const addr = server.address();
if (!addr || typeof addr === 'string') {
server.close();
reject(new Error('Bad address'));
return;
}
const options: RequestInit = {
method,
headers: { 'Content-Type': 'application/json' },
};
if (body) options.body = JSON.stringify(body);
fetch(`http://127.0.0.1:${addr.port}${url}`, options)
.then(async (res) => {
const json = await res.json().catch(() => null);
server.close();
resolve({ status: res.status, body: json });
})
.catch((err) => {
server.close();
reject(err);
});
});
});
}
describe('Template routes', () => {
let tmpDir: string;
let app: express.Express;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'routes-test-'));
const store = new AgentStore(tmpDir);
app = express();
app.use(express.json());
app.use('/api/templates', templateRoutes(store));
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('creates and lists templates', async () => {
// AgentStore seeds a default template on init, so capture the baseline count
const baselineRes = await request(app, 'GET', '/api/templates');
const baselineCount = (baselineRes.body as unknown[]).length;
const createRes = await request(app, 'POST', '/api/templates', {
name: 'Test',
content: '# Test',
});
expect(createRes.status).toBe(201);
const listRes = await request(app, 'GET', '/api/templates');
expect(listRes.status).toBe(200);
expect(Array.isArray(listRes.body)).toBe(true);
expect((listRes.body as unknown[]).length).toBe(baselineCount + 1);
});
it('rejects template without name', async () => {
const res = await request(app, 'POST', '/api/templates', { content: 'x' });
expect(res.status).toBe(400);
});
});
describe('Directory routes', () => {
let tmpDir: string;
let app: express.Express;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dirroutes-test-'));
fs.mkdirSync(path.join(tmpDir, 'testdir'));
fs.writeFileSync(path.join(tmpDir, 'CLAUDE.md'), '# Claude');
app = express();
app.use(express.json());
app.use('/api/directories', directoryRoutes());
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('lists directory contents', async () => {
const res = await request(
app,
'GET',
`/api/directories?path=${encodeURIComponent(tmpDir)}`,
);
expect(res.status).toBe(200);
const body = res.body as { entries: unknown[] };
expect(body.entries).toBeDefined();
});
it('returns provider-specific instruction files when present', async () => {
fs.writeFileSync(path.join(tmpDir, 'AGENTS.md'), '# Codex');
const res = await request(
app,
'GET',
`/api/directories/claude-md?path=${encodeURIComponent(tmpDir)}&provider=codex`,
);
expect(res.status).toBe(200);
expect(res.body).toEqual({
exists: true,
content: '# Codex',
fileName: 'AGENTS.md',
matchedProvider: 'codex',
});
});
it('falls back to compatible instruction files across providers', async () => {
const res = await request(
app,
'GET',
`/api/directories/claude-md?path=${encodeURIComponent(tmpDir)}&provider=codex`,
);
expect(res.status).toBe(200);
expect(res.body).toEqual({
exists: true,
content: '# Claude',
fileName: 'CLAUDE.md',
matchedProvider: 'claude',
});
});
});