Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/chatSessions/claude/node/test/mockClaudeCodeSdkService.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type { ForkSessionOptions, ForkSessionResult, GetSubagentMessagesOptions, ListSubagentsOptions, Options, Query, SDKAssistantMessage, SDKResultMessage, SDKSessionInfo, SDKUserMessage, SessionMessage } from '@anthropic-ai/claude-agent-sdk';
7
import type { IClaudeCodeSdkService } from '../claudeCodeSdkService';
8
9
/**
10
* Mock implementation of IClaudeCodeService for testing
11
*/
12
export class MockClaudeCodeSdkService implements IClaudeCodeSdkService {
13
readonly _serviceBrand: undefined;
14
public queryCallCount = 0;
15
public setModelCallCount = 0;
16
public lastSetModel: string | undefined;
17
public setPermissionModeCallCount = 0;
18
public lastSetPermissionMode: string | undefined;
19
public lastQueryOptions: Options | undefined;
20
public readonly receivedMessages: SDKUserMessage[] = [];
21
22
public mockSessions: SDKSessionInfo[] = [];
23
public mockSessionMessages: SessionMessage[] = [];
24
public mockSubagentIds: string[] = [];
25
public mockSubagentMessages: Map<string, SessionMessage[]> = new Map();
26
27
public async query(options: {
28
prompt: AsyncIterable<SDKUserMessage>;
29
options: Options;
30
}): Promise<Query> {
31
this.queryCallCount++;
32
this.lastQueryOptions = options.options;
33
return this.createMockQuery(options.prompt);
34
}
35
36
public async listSessions(dir?: string): Promise<SDKSessionInfo[]> {
37
return this.mockSessions;
38
}
39
40
public async getSessionInfo(sessionId: string, dir: string): Promise<SDKSessionInfo | undefined> {
41
return this.mockSessions.find(s => s.sessionId === sessionId);
42
}
43
44
public async getSessionMessages(sessionId: string, dir: string): Promise<SessionMessage[]> {
45
return this.mockSessionMessages;
46
}
47
48
public lastRenameSessionId: string | undefined;
49
public lastRenameTitle: string | undefined;
50
51
public async renameSession(sessionId: string, title: string): Promise<void> {
52
this.lastRenameSessionId = sessionId;
53
this.lastRenameTitle = title;
54
}
55
56
public lastForkSessionId: string | undefined;
57
public lastForkOptions: ForkSessionOptions | undefined;
58
59
public async forkSession(sessionId: string, options?: ForkSessionOptions): Promise<ForkSessionResult> {
60
this.lastForkSessionId = sessionId;
61
this.lastForkOptions = options;
62
return { sessionId: 'forked-session-id' } as ForkSessionResult;
63
}
64
65
public async listSubagents(sessionId: string, options?: ListSubagentsOptions): Promise<string[]> {
66
return this.mockSubagentIds;
67
}
68
69
public async getSubagentMessages(sessionId: string, agentId: string, options?: GetSubagentMessagesOptions): Promise<SessionMessage[]> {
70
return this.mockSubagentMessages.get(agentId) ?? [];
71
}
72
73
private createMockQuery(prompt: AsyncIterable<SDKUserMessage>): Query {
74
const generator = this.createMockGenerator(prompt);
75
return {
76
[Symbol.asyncIterator]: () => generator,
77
setModel: async (modelId: string) => {
78
this.setModelCallCount++;
79
this.lastSetModel = modelId;
80
},
81
setPermissionMode: async (mode: string) => {
82
this.setPermissionModeCallCount++;
83
this.lastSetPermissionMode = mode;
84
},
85
abort: () => { /* no-op for mock */ },
86
} as unknown as Query;
87
}
88
89
private async* createMockGenerator(prompt: AsyncIterable<SDKUserMessage>): AsyncGenerator<SDKAssistantMessage | SDKResultMessage, void, unknown> {
90
// For every user message yielded, emit an assistant text and then a result
91
for await (const msg of prompt) {
92
this.receivedMessages.push(msg);
93
yield {
94
type: 'assistant',
95
session_id: 'sess-1',
96
message: {
97
role: 'assistant',
98
content: [
99
{ type: 'text', text: 'Hello from mock!' }
100
]
101
}
102
} as SDKAssistantMessage;
103
yield {
104
type: 'result',
105
subtype: 'error_max_turns',
106
uuid: 'mock-uuid',
107
session_id: 'sess-1',
108
duration_ms: 0,
109
duration_api_ms: 0,
110
is_error: false,
111
num_turns: 0,
112
total_cost_usd: 0,
113
usage: { input_tokens: 0, output_tokens: 0 },
114
permission_denials: []
115
} as unknown as SDKResultMessage;
116
}
117
}
118
}
119
120