-
Notifications
You must be signed in to change notification settings - Fork 35
/
vitest.mocks.ts
405 lines (330 loc) · 8.98 KB
/
vitest.mocks.ts
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* Copyright (c) 2024 The Diffusion Studio Authors
*
* This Source Code Form is subject to the terms of the Mozilla
* Public License, v. 2.0 that can be found in the LICENSE file.
*/
import { vi } from 'vitest';
export class FontFaceMock {
id: string;
uri: string;
weight = '';
constructor(id: string, uri: string) {
this.id = id;
this.uri = uri;
}
async load() {
return this;
}
}
export class FileMock {
name: string;
data: Array<any>;
type: string;
constructor(data: Array<any>, fileName: string, options?: { type: string }) {
this.name = fileName;
this.data = data;
this.type = options?.type ?? 'video/mp4';
}
async arrayBuffer() {
return new ArrayBuffer(0);
}
stream() {
return {
pipeTo: async (writable: {
write: () => Promise<void>;
close: () => void;
file: (file: FileMock) => void;
}) => writable.file(this),
};
}
}
export class URLMock {
href: string;
constructor(name: string, path: string) {
this.href = path + '/' + name;
}
static createObjectURL(_: Blob | File) {
return 'blob:chrome://new-tab-page/3dc0f2b7-7773-4cd4-a397-2e43b1bba7cd';
}
}
export const defaultFetchMockReturnValue = {
ok: true,
json: () => new Promise((resolve) => resolve({})),
arrayBuffer: () => new Promise((resolve) => resolve(new ArrayBuffer(0))),
blob: () => new Promise((resolve) => resolve(new Blob())),
};
export function setFetchMockReturnValue(response: Partial<Response>) {
const fetchMock = vi.fn().mockResolvedValue(response);
Object.assign(globalThis, { fetch: fetchMock });
return () => {
const fetchMock = vi.fn().mockResolvedValue(defaultFetchMockReturnValue);
Object.assign(globalThis, { fetch: fetchMock });
};
}
export function queryLocalFonts() {
return [
{
family: 'Al Bayan',
fullName: 'Al Bayan Plain',
postscriptName: 'AlBayan',
style: 'Plain',
},
{
family: 'Al Bayan',
fullName: 'Al Bayan Bold',
postscriptName: 'AlBayan-Bold',
style: 'Bold',
},
{
family: 'Al Nile',
fullName: 'Al Nile',
postscriptName: 'AlNile',
style: 'Regular',
},
{
family: 'Al Nile',
fullName: 'Al Nile Bold',
postscriptName: 'AlNile-Bold',
style: 'Bold',
},
{
family: 'Al Tarikh',
fullName: 'Al Tarikh Regular',
postscriptName: 'AlTarikh',
style: 'Regular',
},
{
family: 'American Typewriter',
fullName: 'American Typewriter',
postscriptName: 'AmericanTypewriter',
style: 'Regular',
},
{
family: 'American Typewriter',
fullName: 'American Typewriter Bold',
postscriptName: 'AmericanTypewriter-Bold',
style: 'Bold',
},
];
}
export class AudioEncoderMock {
init: AudioEncoderInit;
config?: AudioEncoderConfig;
data: AudioData[] = [];
public constructor(init: AudioEncoderInit) {
this.init = init;
}
public configure(config: AudioEncoderConfig): void {
this.config = config;
}
public encode(data: AudioData): void {
this.data.push(data);
}
public async flush(): Promise<void> {
return;
}
public static async isConfigSupported(config: VideoEncoderConfig): Promise<VideoEncoderSupport> {
return {
supported: true,
config
};
}
}
export class AudioDataMock {
init: AudioDataInit;
constructor(init: AudioDataInit) {
this.init = init;
}
}
export class VideoEncoderMock {
init: VideoEncoderInit;
config?: VideoEncoderConfig;
data: { frame: VideoFrame; options?: VideoEncoderEncodeOptions }[] = [];
ondequeue?(): void;
public constructor(init: VideoEncoderInit) {
this.init = init;
}
public configure(config: VideoEncoderConfig): void {
this.config = config;
}
public encode(frame: VideoFrame, options?: VideoEncoderEncodeOptions): void {
this.data.push({ frame, options });
this.ondequeue?.();
}
public static async isConfigSupported(_: VideoEncoderConfig): Promise<VideoEncoderSupport> {
return {
supported: true,
};
}
public async flush(): Promise<void> {
return;
}
}
export class VideoFrameMock {
image: CanvasImageSource;
init?: VideoFrameInit;
constructor(image: CanvasImageSource, init?: VideoFrameInit) {
this.image = image;
this.init = init;
}
public close() {
return;
}
}
export class FileSystemWritableFileStreamMock {
data: any;
fileName: string;
constructor(data: any, fileName: string) {
this.data = data;
this.fileName = fileName;
}
async write(data: FileSystemWriteChunkType) {
this.data[this.fileName] = data;
}
async close() { }
// TODO: Why is this required?
public file(file: File) {
Object.assign(file, { name: this.fileName });
this.data[this.fileName] = file;
}
}
export class FileSystemFileHandleMock {
data: any;
fileName: string;
constructor(data: any, fileName: string) {
this.data = data;
this.fileName = fileName;
}
async getFile(): Promise<File> {
return this.data[this.fileName];
}
async remove(): Promise<void> {
delete this.data[this.fileName];
}
async createWritable(): Promise<FileSystemWritableFileStreamMock> {
return new FileSystemWritableFileStreamMock(this.data, this.fileName);
}
}
export class FileSystemDirectoryHandleMock {
data: any = {};
directory?: string;
async getDirectoryHandle(name: string, options?: { create: boolean }) {
if (!(name in this.data) && !options?.create) {
throw new Error('Directory does not exist');
}
if (!(name in this.data)) {
this.data[name] = {};
}
this.directory = name;
return this;
}
async getFileHandle(name: string, options?: { create: boolean }) {
if (!this.directory) {
throw new Error('Must get directory handle first');
}
if (!(name in this.data[this.directory]) && !options?.create) {
throw new Error('File does not exist');
}
if (!(name in this.data[this.directory])) {
this.data[this.directory][name] = new File([], '');
}
return new FileSystemFileHandleMock(this.data[this.directory], name);
}
async remove(_?: { recursive: true }) {
if (!this.directory) {
throw new Error('Must get directory handle first');
}
delete this.data[this.directory];
}
async *keys() {
if (!this.directory) {
throw new Error('Must get directory handle first');
}
for (const key of Object.keys(this.data[this.directory])) {
yield key;
}
}
}
export const opfs = new FileSystemDirectoryHandleMock();
export class AudioBufferMock {
sampleRate: number;
length: number;
duration: number;
numberOfChannels: number;
channelData: Float32Array[];
constructor({ sampleRate, length, numberOfChannels }: { sampleRate: number; length: number; numberOfChannels: number }) {
this.sampleRate = sampleRate;
this.length = length;
this.duration = length / sampleRate;
this.numberOfChannels = numberOfChannels;
this.channelData = Array(numberOfChannels)
.fill(null)
.map(() => new Float32Array(length));
}
getChannelData(channel: number): Float32Array {
if (channel >= this.numberOfChannels || channel < 0) {
throw new Error("Channel index out of range");
}
return this.channelData[channel];
}
copyToChannel(source: Float32Array, channel: number, startInChannel = 0): void {
const channelData = this.getChannelData(channel);
channelData.set(source, startInChannel);
}
copyFromChannel(destination: Float32Array, channel: number, startInChannel = 0): void {
const channelData = this.getChannelData(channel);
destination.set(channelData.subarray(startInChannel, startInChannel + destination.length));
}
}
export class OfflineAudioContextMock {
sampleRate: number;
length: number;
numberOfChannels: number;
constructor({ sampleRate, length, numberOfChannels }: OfflineAudioContextOptions) {
this.sampleRate = sampleRate;
this.length = length;
this.numberOfChannels = numberOfChannels ?? 2;
}
createBuffer(numberOfChannels: number, length: number, sampleRate: number): AudioBuffer {
return new AudioBufferMock({ numberOfChannels, length, sampleRate }) as any as AudioBuffer;
}
}
export class MockFileSystemFileHandleMock {
kind: 'file' = 'file';
name: string;
constructor(name: string) {
this.name = name;
}
async getFile(): Promise<File> {
const file = new File(['mock content'], this.name, { type: 'text/plain' });
return Promise.resolve(file);
}
async isSameEntry(other: FileSystemHandle): Promise<boolean> {
return this as any == other;
}
async createWritable(): Promise<FileSystemWritableFileStream> {
const stream = new MockFileSystemWritableFileStreamMock();
return Promise.resolve(stream) as any;
}
// Other methods can be added if needed
}
export class MockFileSystemWritableFileStreamMock {
async write(data: BufferSource | Blob | string | ArrayBufferView): Promise<void> {
// Mock writing to the file
console.log('Writing data:', data);
return Promise.resolve();
}
async seek(position: number): Promise<void> {
console.log('Seeking to position:', position);
return Promise.resolve();
}
async truncate(size: number): Promise<void> {
console.log('Truncating to size:', size);
return Promise.resolve();
}
async close(): Promise<void> {
console.log('Closing the file stream');
return Promise.resolve();
}
}