The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { isChunkLoadError, reloadOnceForChunkLoadError } from "./chunkReloadGuard"; |
| 4 | |
| 5 | function createStorageStub(): Storage { |
| 6 | const store = new Map<string, string>(); |
| 7 | return { |
| 8 | getItem: (key) => store.get(key) ?? null, |
| 9 | setItem: (key, value) => { |
| 10 | store.set(key, value); |
| 11 | }, |
| 12 | removeItem: (key) => { |
| 13 | store.delete(key); |
| 14 | }, |
| 15 | clear: () => store.clear(), |
| 16 | key: (index) => [...store.keys()][index] ?? null, |
| 17 | get length() { |
| 18 | return store.size; |
| 19 | }, |
| 20 | }; |
| 21 | } |
| 22 | |
| 23 | describe("reloadOnceForChunkLoadError", () => { |
| 24 | it("reloads on the first failure and lets the second one surface", () => { |
| 25 | const storage = createStorageStub(); |
| 26 | const reload = vi.fn(); |
| 27 | |
| 28 | expect(reloadOnceForChunkLoadError(() => storage, reload, "1.0.0")).toBe(true); |
| 29 | expect(reload).toHaveBeenCalledTimes(1); |
| 30 | |
| 31 | expect(reloadOnceForChunkLoadError(() => storage, reload, "1.0.0")).toBe(false); |
| 32 | expect(reload).toHaveBeenCalledTimes(1); |
| 33 | }); |
| 34 | |
| 35 | it("reloads again once a deploy changed the build", () => { |
| 36 | const storage = createStorageStub(); |
| 37 | const reload = vi.fn(); |
| 38 | |
| 39 | reloadOnceForChunkLoadError(() => storage, reload, "1.0.0"); |
| 40 | |
| 41 | expect(reloadOnceForChunkLoadError(() => storage, reload, "1.0.1")).toBe(true); |
| 42 | expect(reload).toHaveBeenCalledTimes(2); |
| 43 | }); |
| 44 | |
| 45 | it("never reloads when storage is blocked, so a persistent failure cannot loop", () => { |
| 46 | const reload = vi.fn(); |
| 47 | const blocked = () => { |
| 48 | throw new DOMException("blocked", "SecurityError"); |
| 49 | }; |
| 50 | |
| 51 | expect(reloadOnceForChunkLoadError(blocked, reload)).toBe(false); |
| 52 | expect(reload).not.toHaveBeenCalled(); |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | describe("isChunkLoadError", () => { |
| 57 | it.each([ |
| 58 | ["Failed to fetch dynamically imported module: t3code://app/assets/a.js", true], |
| 59 | ["error loading dynamically imported module: https://app.t3.codes/assets/a.js", true], |
| 60 | ["Importing a module script failed.", true], |
| 61 | ["Cannot read properties of undefined", false], |
| 62 | ])("%s", (message, expected) => { |
| 63 | expect(isChunkLoadError(new TypeError(message))).toBe(expected); |
| 64 | }); |
| 65 | }); |