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