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 { DesktopUpdateState } from "@t3tools/contracts"; |
| 2 | import * as AsyncResult from "effect/reactivity/AsyncResult"; |
| 3 | import { AtomRegistry } from "effect/reactivity"; |
| 4 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 5 | |
| 6 | import { createDesktopUpdateStateAtom } from "./desktopUpdate"; |
| 7 | |
| 8 | const baseState: DesktopUpdateState = { |
| 9 | enabled: true, |
| 10 | status: "idle", |
| 11 | channel: "latest", |
| 12 | currentVersion: "1.0.0", |
| 13 | hostArch: "x64", |
| 14 | appArch: "x64", |
| 15 | runningUnderArm64Translation: false, |
| 16 | availableVersion: null, |
| 17 | downloadedVersion: null, |
| 18 | releaseNotes: [], |
| 19 | omittedReleaseCount: 0, |
| 20 | downloadPercent: null, |
| 21 | checkedAt: null, |
| 22 | message: null, |
| 23 | errorContext: null, |
| 24 | canRetry: false, |
| 25 | }; |
| 26 | |
| 27 | afterEach(() => { |
| 28 | vi.restoreAllMocks(); |
| 29 | }); |
| 30 | |
| 31 | describe("desktopUpdateStateAtom", () => { |
| 32 | it("does not let a slower initial read overwrite a newer update event", async () => { |
| 33 | let resolveInitial: ((state: DesktopUpdateState) => void) | undefined; |
| 34 | let listener: ((state: DesktopUpdateState) => void) | undefined; |
| 35 | const atom = createDesktopUpdateStateAtom(() => ({ |
| 36 | getUpdateState: () => |
| 37 | new Promise<DesktopUpdateState>((resolve) => { |
| 38 | resolveInitial = resolve; |
| 39 | }), |
| 40 | onUpdateState: (nextListener) => { |
| 41 | listener = nextListener; |
| 42 | return () => undefined; |
| 43 | }, |
| 44 | })); |
| 45 | const registry = AtomRegistry.make(); |
| 46 | registry.mount(atom); |
| 47 | |
| 48 | await vi.waitFor(() => expect(listener).toBeDefined()); |
| 49 | const newerState: DesktopUpdateState = { ...baseState, status: "checking" }; |
| 50 | listener?.(newerState); |
| 51 | resolveInitial?.(baseState); |
| 52 | |
| 53 | await vi.waitFor(() => { |
| 54 | expect(AsyncResult.getOrElse(registry.get(atom), () => null)).toEqual(newerState); |
| 55 | }); |
| 56 | registry.dispose(); |
| 57 | }); |
| 58 | }); |