Elffuss BraidHub

kikocis/continue public

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