-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Expand file tree
/
Copy pathopeners.test.ts
More file actions
360 lines (308 loc) · 11 KB
/
Copy pathopeners.test.ts
File metadata and controls
360 lines (308 loc) · 11 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
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import {
BUILTIN_OPENERS,
buildLaunchCommand,
findOpener,
isOpenerCommandAvailable,
listOpenerChoices,
mergeOpenerTable,
} from '../../src/core/openers.js';
const CONFIG_PATH = '/home/dev/.config/openspec/config.json';
describe('openers core', () => {
describe('built-in table', () => {
it('carries the locked v1 rows', () => {
expect(BUILTIN_OPENERS.map((opener) => [opener.id, opener.style])).toEqual([
['code', 'workspace-file'],
['cursor', 'workspace-file'],
['claude', 'attach-dirs'],
['codex', 'attach-dirs'],
]);
expect(findOpener([...BUILTIN_OPENERS], 'codex')?.args).toEqual([
'--sandbox',
'workspace-write',
]);
expect(findOpener([...BUILTIN_OPENERS], 'claude')?.attachFlag).toBe(
'--add-dir'
);
});
});
describe('config merge', () => {
it('returns built-ins for an absent openers key', () => {
expect(mergeOpenerTable(undefined, CONFIG_PATH)).toEqual([
...BUILTIN_OPENERS,
]);
expect(mergeOpenerTable(null, CONFIG_PATH)).toEqual([...BUILTIN_OPENERS]);
});
it('adds a new workspace-file tool with defaults from its id', () => {
const table = mergeOpenerTable(
{ zed: { style: 'workspace-file' } },
CONFIG_PATH
);
const zed = findOpener(table, 'zed');
expect(zed).toEqual({
id: 'zed',
label: 'zed',
style: 'workspace-file',
command: 'zed',
args: [],
attachFlag: '--add-dir',
});
});
it('overrides only the fields a built-in row sets', () => {
const table = mergeOpenerTable(
{ claude: { attach_flag: '--dir' } },
CONFIG_PATH
);
const claude = findOpener(table, 'claude');
expect(claude?.attachFlag).toBe('--dir');
expect(claude?.label).toBe('Claude Code');
expect(claude?.command).toBe('claude');
expect(claude?.style).toBe('attach-dirs');
});
it.each([{ args: [] }, { args: ['--model', 'custom model'] }])(
'replaces built-in args with $args without changing omitted fields',
({ args }) => {
const builtin = findOpener([...BUILTIN_OPENERS], 'codex')!;
const table = mergeOpenerTable({ codex: { args } }, CONFIG_PATH);
expect(findOpener(table, 'codex')).toEqual({ ...builtin, args });
expect(builtin.args).toEqual(['--sandbox', 'workspace-write']);
}
);
it('rejects an unknown style naming the two valid styles', () => {
try {
mergeOpenerTable({ vim: { style: 'tabs' } }, CONFIG_PATH);
expect.unreachable('expected invalid_opener_config');
} catch (error) {
const diagnostic = (
error as { diagnostic: { code: string; fix?: string } }
).diagnostic;
expect(diagnostic.code).toBe('invalid_opener_config');
expect(diagnostic.fix).toContain("'workspace-file' or 'attach-dirs'");
expect(diagnostic.fix).toContain(CONFIG_PATH);
}
});
it('rejects a new tool that omits style', () => {
expect(() =>
mergeOpenerTable({ zed: { command: 'zed' } }, CONFIG_PATH)
).toThrowError(/'zed' adds a new tool and must set style/);
});
it('rejects malformed rows instead of ignoring them', () => {
expect(() => mergeOpenerTable('zed', CONFIG_PATH)).toThrowError(
/Invalid openers config/
);
expect(() =>
mergeOpenerTable({ zed: { style: 'workspace-file', extra: 1 } }, CONFIG_PATH)
).toThrowError(/Invalid openers config/);
});
});
describe('availability scan', () => {
let tempDir: string;
beforeEach(() => {
// listOpenerChoices hides CLI-agent (attach-dirs) tools by default;
// this suite asserts the full table, so enable them.
process.env.OPENSPEC_ENABLE_CLI_AGENT_OPENERS = '1';
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'openspec-openers-'));
});
afterEach(() => {
delete process.env.OPENSPEC_ENABLE_CLI_AGENT_OPENERS;
fs.rmSync(tempDir, { recursive: true, force: true });
});
function makeExecutable(name: string): string {
const filePath = path.join(tempDir, name);
fs.writeFileSync(filePath, '#!/bin/sh\nexit 0\n');
fs.chmodSync(filePath, 0o755);
return filePath;
}
// posix-only: these exercise the real execute bit and the ':'-delimited
// PATH against a real temp dir. On win32 chmod is a no-op and the temp
// path's drive-letter colon shatters posix PATH splitting; win32
// availability is covered by the injected-seam cases below.
const itPosix = it.skipIf(process.platform === 'win32');
itPosix('finds an executable on the posix PATH', () => {
makeExecutable('faketool');
expect(
isOpenerCommandAvailable('faketool', {
env: { PATH: tempDir },
platform: 'linux',
})
).toBe(true);
expect(
isOpenerCommandAvailable('missing', {
env: { PATH: tempDir },
platform: 'linux',
})
).toBe(false);
});
itPosix('honors the case-insensitive Path key', () => {
makeExecutable('faketool');
expect(
isOpenerCommandAvailable('faketool', {
env: { Path: tempDir },
platform: 'linux',
})
).toBe(true);
});
itPosix('requires the execute bit on posix', () => {
const filePath = path.join(tempDir, 'notexec');
fs.writeFileSync(filePath, 'data');
fs.chmodSync(filePath, 0o644);
expect(
isOpenerCommandAvailable('notexec', {
env: { PATH: tempDir },
platform: 'linux',
})
).toBe(false);
});
it('stats separator-bearing commands directly', () => {
const filePath = makeExecutable('direct');
expect(
isOpenerCommandAvailable(filePath, {
env: { PATH: '' },
platform: 'linux',
})
).toBe(true);
});
it('walks the win32 PATHEXT matrix through the injected stat seam', () => {
const seen: string[] = [];
const available = isOpenerCommandAvailable('tool', {
env: { Path: 'C:\\bin;D:\\apps' },
platform: 'win32',
isExecutableFile: (candidate) => {
seen.push(candidate);
return candidate === 'D:\\apps\\tool.CMD';
},
});
expect(available).toBe(true);
expect(seen).toContain('C:\\bin\\tool.COM');
expect(seen).toContain('C:\\bin\\tool.EXE');
expect(seen).toContain('D:\\apps\\tool.CMD');
});
it('honors a custom PATHEXT', () => {
const seen: string[] = [];
isOpenerCommandAvailable('tool', {
env: { PATH: 'C:\\bin', PATHEXT: '.WSF;.LNK' },
platform: 'win32',
isExecutableFile: (candidate) => {
seen.push(candidate);
return false;
},
});
expect(seen).toEqual(['C:\\bin\\tool.WSF', 'C:\\bin\\tool.LNK']);
});
it('matches a command already carrying a known extension as-is, never doubled', () => {
const seen: string[] = [];
const available = isOpenerCommandAvailable('tool.cmd', {
env: { PATH: 'C:\\bin' },
platform: 'win32',
isExecutableFile: (candidate) => {
seen.push(candidate);
return candidate === 'C:\\bin\\tool.cmd';
},
});
expect(available).toBe(true);
// Exactly the bare candidate - no tool.cmd.COM/.EXE doubling
// (the scan must agree with spawn-time resolution).
expect(seen).toEqual(['C:\\bin\\tool.cmd']);
const negative: string[] = [];
isOpenerCommandAvailable('tool.cmd', {
env: { PATH: 'C:\\bin' },
platform: 'win32',
isExecutableFile: (candidate) => {
negative.push(candidate);
return false;
},
});
expect(negative).toEqual(['C:\\bin\\tool.cmd']);
});
itPosix('sorts choices available-first preserving table order', () => {
makeExecutable('claude');
makeExecutable('codex');
const choices = listOpenerChoices([...BUILTIN_OPENERS], {
env: { PATH: tempDir },
platform: 'linux',
});
expect(
choices.map((choice) => [choice.opener.id, choice.available])
).toEqual([
['claude', true],
['codex', true],
['code', false],
['cursor', false],
]);
expect(choices[2].note).toBe('(code not found on PATH)');
});
});
describe('launch command builder', () => {
const members = [
{ name: 'team-context', path: '/abs/team-context' },
{ name: 'web-app', path: '/abs/web-app' },
{ name: 'api', path: '/abs/api' },
];
const codeWorkspacePath = '/data/worksets/platform.code-workspace';
it('workspace-file style passes pre-args plus the file path only', () => {
const code = findOpener([...BUILTIN_OPENERS], 'code')!;
const command = buildLaunchCommand(code, { members, codeWorkspacePath });
expect(command).toEqual({
executable: 'code',
args: [codeWorkspacePath],
cwd: '/abs/team-context',
label: 'VS Code',
style: 'workspace-file',
});
});
it('attach-dirs style attaches every member, the primary included', () => {
const claude = findOpener([...BUILTIN_OPENERS], 'claude')!;
const command = buildLaunchCommand(claude, { members, codeWorkspacePath });
expect(command.args).toEqual([
'--add-dir',
'/abs/team-context',
'--add-dir',
'/abs/web-app',
'--add-dir',
'/abs/api',
]);
expect(command.cwd).toBe('/abs/team-context');
});
it('codex carries its sandbox pre-args before the attach pairs', () => {
const codex = findOpener([...BUILTIN_OPENERS], 'codex')!;
const command = buildLaunchCommand(codex, {
members: [members[0]],
codeWorkspacePath,
});
expect(command.args).toEqual([
'--sandbox',
'workspace-write',
'--add-dir',
'/abs/team-context',
]);
});
it('never emits a positional argument for attach-dirs tools', () => {
const claude = findOpener([...BUILTIN_OPENERS], 'claude')!;
const command = buildLaunchCommand(claude, { members, codeWorkspacePath });
// Every argv entry is either a flag or the value following one.
for (let index = 0; index < command.args.length; index += 2) {
expect(command.args[index]).toBe('--add-dir');
}
expect(command.args.length % 2).toBe(0);
});
it('a configured attach_flag rename flows into the argv', () => {
const table = mergeOpenerTable(
{ claude: { attach_flag: '--dir' } },
CONFIG_PATH
);
const command = buildLaunchCommand(findOpener(table, 'claude')!, {
members: [members[0], members[1]],
codeWorkspacePath,
});
expect(command.args).toEqual([
'--dir',
'/abs/team-context',
'--dir',
'/abs/web-app',
]);
});
});
});