Elffuss BraidHub

kikocis/continue public

main · 3.2 KiBRawPermalink
1import { vi } from "vitest";
2
3export interface UITestContext {
4 mockUseService: ReturnType<typeof vi.fn>;
5 mockUseServices: ReturnType<typeof vi.fn>;
6 mockUseChat: ReturnType<typeof vi.fn>;
7 mockServiceContainer: any;
8 cleanup: () => void;
9}
10
11// Export a mock LLM API for testing
12export const mockLlmApi = {
13 chatCompletionNonStream: vi.fn(),
14 chatCompletionStream: vi.fn(),
15 completionNonStream: vi.fn(),
16 completionStream: vi.fn(),
17 embed: vi.fn(),
18 rerank: vi.fn(),
19 fimStream: vi.fn(),
20 listModels: vi.fn(),
21 completions: vi.fn(),
22 stream: vi.fn(),
23 abort: vi.fn(),
24} as any;
25
26/**
27 * Creates a test context with common UI mocks for TUI components
28 */
29export function createUITestContext(
30 options: {
31 serviceValue?: any;
32 serviceState?: "idle" | "loading" | "ready" | "error";
33 services?: Record<string, any>;
34 allServicesReady?: boolean;
35 chatMessages?: any[];
36 isWaitingForResponse?: boolean;
37 } = {},
38): UITestContext {
39 const {
40 serviceValue = null,
41 serviceState = "idle",
42 services: customServices = {},
43 allServicesReady = false,
44 chatMessages = [],
45 isWaitingForResponse = false,
46 } = options;
47
48 // Default services with FileIndexService mock
49 const defaultServices = {
50 fileIndex: {
51 files: [
52 { path: "README.md", displayName: "README.md" },
53 { path: "src/index.ts", displayName: "src/index.ts" },
54 { path: "package.json", displayName: "package.json" },
55 ],
56 isIndexing: false,
57 error: null,
58 },
59 ...customServices,
60 };
61
62 const services = { ...defaultServices, ...customServices };
63
64 // Mock useService hook
65 const mockUseService = vi.fn().mockReturnValue({
66 value: serviceValue,
67 state: serviceState,
68 error: null,
69 reload: vi.fn(),
70 });
71
72 // Mock useServices hook
73 const mockUseServices = vi.fn().mockReturnValue({
74 services,
75 loading: !allServicesReady,
76 error: null,
77 allReady: allServicesReady,
78 });
79
80 // Mock useChat hook
81 const mockUseChat = vi.fn().mockReturnValue({
82 messages: chatMessages,
83 setMessages: vi.fn(),
84 chatHistory: [],
85 setChatHistory: vi.fn(),
86 isWaitingForResponse,
87 responseStartTime: null,
88 inputMode: true,
89 attachedFiles: [],
90 handleUserMessage: vi.fn().mockResolvedValue(undefined),
91 handleInterrupt: vi.fn(),
92 handleFileAttached: vi.fn(),
93 resetChatHistory: vi.fn(),
94 });
95
96 // Mock service container
97 const mockServiceContainer = {
98 getSync: vi.fn((serviceName: string) => ({
99 state: "error",
100 value: null,
101 error: new Error(`Service '${serviceName}' not registered`),
102 })),
103 isReady: vi.fn(() => allServicesReady),
104 on: vi.fn(),
105 off: vi.fn(),
106 load: vi.fn(() => Promise.resolve()),
107 reload: vi.fn(() => Promise.resolve()),
108 emit: vi.fn(),
109 setMaxListeners: vi.fn(),
110 };
111
112 const cleanup = () => {
113 vi.clearAllMocks();
114 };
115
116 return {
117 mockUseService,
118 mockUseServices,
119 mockUseChat,
120 mockServiceContainer,
121 cleanup,
122 };
123}
124
125/**
126 * Creates a minimal test context for unit tests that don't need UI mocks
127 */
128export function createMinimalTestContext(): { cleanup: () => void } {
129 const cleanup = () => {
130 vi.clearAllMocks();
131 };
132
133 return { cleanup };
134}