-
-
Notifications
You must be signed in to change notification settings - Fork 314
Expand file tree
/
Copy pathacp-client.spec.ts
More file actions
205 lines (165 loc) · 6.9 KB
/
Copy pathacp-client.spec.ts
File metadata and controls
205 lines (165 loc) · 6.9 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
import test from 'ava';
import * as vscode from 'vscode';
import { NanocoderAcpClient } from './acp-client';
import { AcpStateManager } from './acp-state';
test('NanocoderAcpClient - permission flow', async (t) => {
const outputChannel = { appendLine: () => {} } as any;
const stateManager = new AcpStateManager();
const client = new NanocoderAcpClient(outputChannel, stateManager);
let requestedToolCallId = '';
client.onPermissionRequested = (toolCallId) => {
requestedToolCallId = toolCallId;
};
// Mock incoming permission request from ACP
const mockToolCall = { toolCallId: 'call_123', name: 'test_tool', arguments: {} };
// Start the async request, it should pend
const requestPromise = client.handlePermissionRequest({ toolCall: mockToolCall });
t.is(requestedToolCallId, 'call_123', 'Should emit onPermissionRequested');
t.true(client.hasPendingPermissions(), 'Should have pending permissions');
// Resolve the permission
client.resolvePermission('call_123', true);
const result = await requestPromise;
t.is((result as any).outcome.optionId, 'allow');
t.false(client.hasPendingPermissions(), 'Pending permissions should be cleared');
});
test('NanocoderAcpClient - forwards background title notifications', async (t) => {
const client = makeClient({});
let notified = false;
client.onSessionTitleChanged = () => {
notified = true;
};
await client.handleExtNotification('_nanocoder/sessionTitleChanged', {
sessionId: 'session-1',
title: 'Updated title',
});
t.true(notified);
});
function makeClient(connection: unknown) {
const outputChannel = { appendLine: () => {} } as any;
const client = new NanocoderAcpClient(outputChannel, new AcpStateManager());
client.setConnection(connection as any);
// setConnection drops the session id; restore one or the guards short-circuit.
(client as any)._sessionId = 'session-1';
return client;
}
test('NanocoderAcpClient - cancel resolves and clears pending permissions', async (t) => {
const client = makeClient({ cancel: async () => {} });
let cancelledIds: string[] = [];
client.onPermissionsCancelled = (ids) => {
cancelledIds = ids;
};
const requestPromise = client.handlePermissionRequest({
toolCall: { toolCallId: 'call_123', name: 'write_file', arguments: {} },
});
t.true(client.hasPendingPermissions());
await client.cancel();
const result = await requestPromise;
t.is((result as any).outcome.outcome, 'cancelled', 'Agent expects a cancelled outcome');
t.false(
client.hasPendingPermissions(),
'A stale resolver would block every later message with "Please approve or deny the pending tool"',
);
t.deepEqual(cancelledIds, ['call_123'], 'UI needs the ids to dismiss their cards');
});
test('NanocoderAcpClient - newChat clears pending permissions', async (t) => {
const client = makeClient({ cancel: async () => {} });
const requestPromise = client.handlePermissionRequest({
toolCall: { toolCallId: 'call_456', name: 'bash', arguments: {} },
});
t.true(client.hasPendingPermissions());
client.newChat();
const result = await requestPromise;
t.is((result as any).outcome.outcome, 'cancelled');
t.false(client.hasPendingPermissions(), 'Abandoning the chat abandons its approval prompts');
});
test('NanocoderAcpClient - a cancelled prompt does not raise an error toast', async (t) => {
let rejectPrompt: (error: Error) => void = () => {};
const client = makeClient({
prompt: () => new Promise((_resolve, reject) => { rejectPrompt = reject; }),
cancel: async () => {},
});
const originalShowErrorMessage = vscode.window.showErrorMessage;
let shownError: string | undefined;
(vscode.window as any).showErrorMessage = (message: string) => {
shownError = message;
};
t.teardown(() => {
(vscode.window as any).showErrorMessage = originalShowErrorMessage;
});
const promptPromise = client.prompt('hello');
// Cancel mid-flight, then the agent tears the stream down and prompt rejects.
await client.cancel();
rejectPrompt(new Error('Operation was cancelled'));
const cancelledResult = await promptPromise;
t.is(shownError, undefined, 'Cancelling is not a failure the user needs to be told about');
t.is(cancelledResult?.stopReason, 'cancelled', 'The webview needs the cancelled terminal state');
// The flag must not leak into the next turn, or a real failure goes unreported.
rejectPrompt = () => {};
const secondPrompt = client.prompt('hello again');
rejectPrompt(new Error('RequestError: Internal error (500)'));
await secondPrompt;
t.regex(shownError ?? '', /RequestError/, 'A genuine failure must still surface');
});
test('NanocoderAcpClient - listTimeline returns entries from extMethod', async (t) => {
const client = makeClient({
extMethod: async (method: string, params: Record<string, unknown>) => {
t.is(method, 'timeline/list');
t.is(params.sessionId, 'session-1');
return {
entries: [
{
id: 'cp-1',
seq: 1,
toolCallId: 'call-1',
toolName: 'write_file',
title: 'write_file: a.ts',
timestamp: '2026-01-01T00:00:00.000Z',
filesChanged: ['a.ts'],
},
],
};
},
});
const entries = await client.listTimeline();
t.is(entries.length, 1);
t.is(entries[0].id, 'cp-1');
});
test('NanocoderAcpClient - revertTimeline calls timeline/revert', async (t) => {
let called: {method?: string; params?: Record<string, unknown>} = {};
const client = makeClient({
extMethod: async (method: string, params: Record<string, unknown>) => {
called = {method, params};
return {revertedTo: {id: 'cp-1'}, filesRestored: ['a.ts']};
},
});
await client.revertTimeline('cp-1');
t.is(called.method, 'timeline/revert');
t.deepEqual(called.params, {sessionId: 'session-1', checkpointId: 'cp-1'});
});
test('NanocoderAcpClient - revertTimeline throws when there is no session', async (t) => {
const outputChannel = {appendLine: () => {}} as any;
const client = new NanocoderAcpClient(outputChannel, new AcpStateManager());
// Returning quietly here would let the caller clear the chat view for a
// revert that never happened.
await t.throwsAsync(client.revertTimeline('cp-1'), {message: /Not connected/});
});
test('NanocoderAcpClient - revertTimeline rethrows a refused revert', async (t) => {
const client = makeClient({
extMethod: async () => {
throw new Error('Cannot revert the timeline while a prompt is in progress');
},
});
await t.throwsAsync(client.revertTimeline('cp-1'), {message: /in progress/});
});
test('NanocoderAcpClient - reconnecting clears permissions left by the dead process', async (t) => {
const outputChannel = {appendLine: () => {}} as any;
const stateManager = new AcpStateManager();
const client = new NanocoderAcpClient(outputChannel, stateManager);
const requestPromise = client.handlePermissionRequest({
toolCall: {toolCallId: 'call_456', name: 'test_tool', arguments: {}},
});
client.setConnection({} as any);
const result = await requestPromise;
t.is((result as any).outcome.outcome, 'cancelled');
t.false(client.hasPendingPermissions());
});