The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { DesktopWslState } from "@t3tools/contracts"; |
| 2 | import { AsyncResult, AtomRegistry } from "effect/reactivity"; |
| 3 | import { expect, it, vi } from "vite-plus/test"; |
| 4 | |
| 5 | import { desktopWslAccessStateAtom, desktopWslStateAtom } from "./desktopWslState"; |
| 6 | |
| 7 | const ubuntuOnly: DesktopWslState = { |
| 8 | enabled: false, |
| 9 | distro: null, |
| 10 | available: true, |
| 11 | wslOnly: false, |
| 12 | distros: [{ name: "Ubuntu", isDefault: true, version: 2 }], |
| 13 | preflightError: null, |
| 14 | }; |
| 15 | const withDebian: DesktopWslState = { |
| 16 | ...ubuntuOnly, |
| 17 | distros: [...ubuntuOnly.distros, { name: "Debian", isDefault: false, version: 2 }], |
| 18 | }; |
| 19 | |
| 20 | it("refetches on every Access visit and reuses a fresh result for Search", async () => { |
| 21 | const pending = Promise.withResolvers<DesktopWslState>(); |
| 22 | const getWslState = vi |
| 23 | .fn<() => Promise<DesktopWslState>>() |
| 24 | .mockResolvedValueOnce(ubuntuOnly) |
| 25 | .mockReturnValueOnce(pending.promise); |
| 26 | vi.stubGlobal("window", { desktopBridge: { getWslState } }); |
| 27 | using _now = vi.spyOn(Date, "now").mockReturnValue(1_000_000); |
| 28 | const tasks: Array<() => void> = []; |
| 29 | const registry = AtomRegistry.make({ |
| 30 | scheduleTask: (task) => { |
| 31 | tasks.push(task); |
| 32 | return () => {}; |
| 33 | }, |
| 34 | }); |
| 35 | const runTasks = () => { |
| 36 | for (const task of tasks.splice(0)) task(); |
| 37 | }; |
| 38 | const settled = (atom: typeof desktopWslStateAtom) => |
| 39 | new Promise<DesktopWslState>((resolve) => { |
| 40 | const unsubscribe = registry.subscribe( |
| 41 | atom, |
| 42 | (result) => { |
| 43 | if (result._tag === "Success" && !result.waiting) { |
| 44 | unsubscribe(); |
| 45 | resolve(result.value); |
| 46 | } |
| 47 | }, |
| 48 | { immediate: true }, |
| 49 | ); |
| 50 | }); |
| 51 | try { |
| 52 | const closeSearch = registry.mount(desktopWslStateAtom); |
| 53 | expect(await settled(desktopWslStateAtom)).toEqual(ubuntuOnly); |
| 54 | closeSearch(); |
| 55 | runTasks(); |
| 56 | |
| 57 | registry.mount(desktopWslStateAtom)(); |
| 58 | runTasks(); |
| 59 | expect(getWslState).toHaveBeenCalledTimes(1); |
| 60 | |
| 61 | const closeAccess = registry.mount(desktopWslAccessStateAtom); |
| 62 | runTasks(); |
| 63 | expect(getWslState).toHaveBeenCalledTimes(2); |
| 64 | expect(AsyncResult.getOrThrow(registry.get(desktopWslAccessStateAtom))).toEqual(ubuntuOnly); |
| 65 | pending.resolve(withDebian); |
| 66 | expect(await settled(desktopWslAccessStateAtom)).toEqual(withDebian); |
| 67 | closeAccess(); |
| 68 | } finally { |
| 69 | registry.dispose(); |
| 70 | vi.unstubAllGlobals(); |
| 71 | } |
| 72 | }); |