()
| 3 | |
| 4 | // Mock OPFS 文件系统 |
| 5 | function createMockOPFS() { |
| 6 | function createMockWritable() { |
| 7 | let data: any = null; |
| 8 | return { |
| 9 | write: vi.fn(async (content: any) => { |
| 10 | data = content; |
| 11 | }), |
| 12 | close: vi.fn(async () => {}), |
| 13 | getData: () => data, |
| 14 | }; |
| 15 | } |
| 16 | |
| 17 | function createMockFileHandle(name: string, dir: Map<string, any>) { |
| 18 | return { |
| 19 | kind: "file" as const, |
| 20 | getFile: vi.fn(async () => { |
| 21 | const content = dir.get(name); |
| 22 | if (content instanceof Blob) return content; |
| 23 | if (content instanceof ArrayBuffer) return new Blob([content]); |
| 24 | if (content instanceof Uint8Array) return new Blob([content.buffer as ArrayBuffer]); |
| 25 | if (typeof content === "string") return new Blob([content], { type: "application/octet-stream" }); |
| 26 | return new Blob([""], { type: "application/octet-stream" }); |
| 27 | }), |
| 28 | createWritable: vi.fn(async () => { |
| 29 | const writable = createMockWritable(); |
| 30 | const origClose = writable.close; |
| 31 | writable.close = vi.fn(async () => { |
| 32 | const written = writable.getData(); |
| 33 | dir.set(name, written); |
| 34 | await origClose(); |
| 35 | }); |
| 36 | return writable; |
| 37 | }), |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | function createMockDirHandle(store: Map<string, any>): any { |
| 42 | return { |
| 43 | kind: "directory" as const, |
| 44 | getDirectoryHandle: vi.fn(async (name: string, opts?: { create?: boolean }) => { |
| 45 | if (!store.has("__dir__" + name)) { |
| 46 | if (opts?.create) { |
| 47 | store.set("__dir__" + name, new Map()); |
| 48 | } else { |
| 49 | throw new Error("Not found"); |
| 50 | } |
| 51 | } |
| 52 | return createMockDirHandle(store.get("__dir__" + name)); |
| 53 | }), |
| 54 | getFileHandle: vi.fn(async (name: string, opts?: { create?: boolean }) => { |
| 55 | if (!store.has(name) && !opts?.create) { |
| 56 | throw new Error("Not found"); |
| 57 | } |
| 58 | if (!store.has(name)) { |
| 59 | store.set(name, ""); |
| 60 | } |
| 61 | return createMockFileHandle(name, store); |
| 62 | }), |
no test coverage detected