-
-
Notifications
You must be signed in to change notification settings - Fork 636
Expand file tree
/
Copy pathjest.setup.ts
More file actions
206 lines (187 loc) · 5.28 KB
/
Copy pathjest.setup.ts
File metadata and controls
206 lines (187 loc) · 5.28 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
206
import '@testing-library/jest-dom';
import { TextEncoder, TextDecoder } from 'util';
// Suppress console.log during tests
const originalConsoleLog = console.log;
beforeAll(() => {
console.log = jest.fn();
});
afterAll(() => {
console.log = originalConsoleLog;
});
if (typeof global.TextEncoder === 'undefined') {
global.TextEncoder = TextEncoder;
}
if (typeof global.TextDecoder === 'undefined') {
global.TextDecoder = TextDecoder as unknown as typeof global.TextDecoder;
}
class ResizeObserver {
observe() {}
unobserve() {}
disconnect() {}
}
(global as any).ResizeObserver = ResizeObserver;
// --- Tauri Mocks ---
// Mock matchMedia
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // deprecated
removeListener: jest.fn(), // deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
// Mock Tauri Internals
(window as any).__TAURI_INTERNALS__ = {
invoke: jest.fn().mockResolvedValue(null),
transformCallback: jest.fn(),
metadata: {},
};
// Mock the module imports
jest.mock('@tauri-apps/api/core', () => ({
invoke: jest.fn().mockImplementation((cmd: string) => {
switch (cmd) {
case 'is_autostart_enabled':
return Promise.resolve(false);
case 'enable_autostart':
case 'disable_autostart':
return Promise.resolve(undefined);
default:
return Promise.resolve(null);
}
}),
}));
jest.mock('@tauri-apps/api/app', () => ({
getVersion: jest.fn().mockResolvedValue('1.0.0'),
getName: jest.fn().mockResolvedValue('PictoPy'),
getTauriVersion: jest.fn().mockResolvedValue('2.0.0'),
}));
jest.mock('@tauri-apps/plugin-updater', () => ({
check: jest.fn().mockResolvedValue(null),
}));
jest.mock('@tauri-apps/plugin-dialog', () => ({
save: jest.fn().mockResolvedValue(null),
open: jest.fn().mockResolvedValue(null),
ask: jest.fn().mockResolvedValue(false),
}));
jest.mock('@tauri-apps/plugin-fs', () => ({
readDir: jest.fn().mockResolvedValue([]),
createDir: jest.fn().mockResolvedValue(undefined),
}));
jest.mock('@tauri-apps/plugin-shell', () => ({
open: jest.fn().mockResolvedValue(undefined),
}));
jest.mock('@tauri-apps/plugin-store', () => ({
Store: jest.fn().mockImplementation(() => ({
get: jest.fn().mockResolvedValue(null),
set: jest.fn().mockResolvedValue(undefined),
save: jest.fn().mockResolvedValue(undefined),
load: jest.fn().mockResolvedValue(undefined),
delete: jest.fn().mockResolvedValue(true),
has: jest.fn().mockResolvedValue(false),
clear: jest.fn().mockResolvedValue(undefined),
keys: jest.fn().mockResolvedValue([]),
values: jest.fn().mockResolvedValue([]),
entries: jest.fn().mockResolvedValue([]),
length: jest.fn().mockResolvedValue(0),
onKeyChange: jest.fn().mockResolvedValue(() => {}), // Returns unlisten function
onChange: jest.fn().mockResolvedValue(() => {}), // Returns unlisten function
})),
}));
// Mock Axios
jest.mock('axios', () => {
const mockAxiosInstance = {
get: jest.fn().mockResolvedValue({ data: [] }),
post: jest.fn().mockResolvedValue({ data: {} }),
put: jest.fn().mockResolvedValue({ data: {} }),
patch: jest.fn().mockResolvedValue({ data: {} }),
delete: jest.fn().mockResolvedValue({ data: {} }),
interceptors: {
request: { use: jest.fn(), eject: jest.fn() },
response: { use: jest.fn(), eject: jest.fn() },
},
};
return {
default: mockAxiosInstance,
create: jest.fn(() => mockAxiosInstance),
...mockAxiosInstance,
};
});
// Mock Global Fetch
const mockModelStatus = {
success: true,
data: {
object_detection_nano: {
feature: 'object_detection',
tier: 'nano',
installed: true,
},
face_detection_nano: {
feature: 'face_detection',
tier: 'nano',
installed: true,
},
object_detection_small: {
feature: 'object_detection',
tier: 'small',
installed: true,
},
face_detection_small: {
feature: 'face_detection',
tier: 'small',
installed: true,
},
object_detection_medium: {
feature: 'object_detection',
tier: 'medium',
installed: true,
},
face_detection_medium: {
feature: 'face_detection',
tier: 'medium',
installed: true,
},
},
};
const mockHardwareInfo = {
success: true,
data: {
ram_gb: 16,
gpu_detected: true,
gpu_names: ['NVIDIA GeForce RTX 4070'],
available_providers: ['CUDAExecutionProvider', 'CPUExecutionProvider'],
recommended_tier: 'small',
},
};
global.fetch = jest.fn().mockImplementation((url: string) => {
if (url.includes('/models/status')) {
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve(mockModelStatus),
});
}
if (url.includes('/models/hardware')) {
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve(mockHardwareInfo),
});
}
if (url.includes('/models/setup')) {
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve({ success: true }),
});
}
return Promise.resolve({
ok: true,
status: 200,
json: () => Promise.resolve({}),
});
});