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 { EnvironmentId, type DeviceDetail, type DeviceSummary } from "@t3tools/contracts"; |
| 2 | import type { DeviceHubAccess } from "@t3tools/client-runtime/device/hub-access"; |
| 3 | import type { AtomCommandConcurrency } from "@t3tools/client-runtime/state/runtime"; |
| 4 | import { RegistryContext } from "@effect/atom-react"; |
| 5 | import * as Cause from "effect/Cause"; |
| 6 | import type { Effect } from "effect/Effect"; |
| 7 | import { AtomRegistry } from "effect/reactivity"; |
| 8 | import { useEffect } from "react"; |
| 9 | import { expect, it, vi } from "vite-plus/test"; |
| 10 | |
| 11 | import { act, render } from "../../../test/render"; |
| 12 | |
| 13 | type Result = |
| 14 | | { _tag: "Success"; value: DeviceDetail } |
| 15 | | { _tag: "Failure"; cause: Cause.Cause<Error> }; |
| 16 | const { read, action, subscribe } = vi.hoisted(() => ({ |
| 17 | read: vi.fn<() => Promise<Result>>(), |
| 18 | action: vi.fn<() => Promise<Result>>(), |
| 19 | subscribe: |
| 20 | vi.fn<(_target: unknown, onChange: (app: { id: string } | null) => void) => () => void>(), |
| 21 | })); |
| 22 | // Stubs only the transport; the device atoms' real scheduler and lanes decide ordering. |
| 23 | vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => { |
| 24 | const runtime = await original(); |
| 25 | const Effects = await import("effect/Effect"); |
| 26 | const createEnvironmentRpcCommand = ( |
| 27 | _runtime: unknown, |
| 28 | options: { |
| 29 | readonly label: string; |
| 30 | readonly scheduler?: { |
| 31 | readonly schedule: ( |
| 32 | registry: AtomRegistry.AtomRegistry, |
| 33 | concurrency: AtomCommandConcurrency<unknown>, |
| 34 | input: unknown, |
| 35 | effect: Effect<DeviceDetail, Error>, |
| 36 | ) => Effect<DeviceDetail, Error>; |
| 37 | }; |
| 38 | readonly concurrency?: AtomCommandConcurrency<unknown>; |
| 39 | }, |
| 40 | ) => { |
| 41 | const send = options.label === "environment-data:device:detail" ? read : action; |
| 42 | const sent = Effects.promise(() => send()).pipe( |
| 43 | Effects.flatMap((result) => |
| 44 | result._tag === "Success" ? Effects.succeed(result.value) : Effects.failCause(result.cause), |
| 45 | ), |
| 46 | ); |
| 47 | return { |
| 48 | label: options.label, |
| 49 | run: (registry: AtomRegistry.AtomRegistry, input: unknown) => |
| 50 | options.scheduler && options.concurrency |
| 51 | ? options.scheduler.schedule(registry, options.concurrency, input, sent) |
| 52 | : sent, |
| 53 | }; |
| 54 | }; |
| 55 | return { ...runtime, createEnvironmentRpcCommand: createEnvironmentRpcCommand as never }; |
| 56 | }); |
| 57 | vi.mock(import("./deviceHubApi"), async (original) => ({ |
| 58 | ...(await original()), |
| 59 | subscribeDeviceForeground: subscribe as never, |
| 60 | })); |
| 61 | |
| 62 | import { deviceDetailReadAtom } from "~/state/device"; |
| 63 | import { useDeviceControls, type DeviceControls } from "./useDeviceControls"; |
| 64 | |
| 65 | const offline = Cause.fail(new Error("Device offline")); |
| 66 | const device: DeviceSummary = { |
| 67 | hostId: "remote", |
| 68 | id: "phone", |
| 69 | name: "Phone", |
| 70 | platform: "ios", |
| 71 | version: "iOS", |
| 72 | booted: true, |
| 73 | physical: false, |
| 74 | }; |
| 75 | const snapshot = (appearance: "light" | "dark"): Result => ({ |
| 76 | _tag: "Success", |
| 77 | value: { |
| 78 | hostId: "remote", |
| 79 | deviceId: "phone", |
| 80 | settings: { appearance }, |
| 81 | foregroundApp: null, |
| 82 | readAt: "2026-09-20T00:00:00Z", |
| 83 | }, |
| 84 | }); |
| 85 | function deferred() { |
| 86 | let resolve!: (value: Result) => void; |
| 87 | const promise = new Promise<Result>((done) => { |
| 88 | resolve = done; |
| 89 | }); |
| 90 | return { promise, resolve }; |
| 91 | } |
| 92 | let controls: DeviceControls; |
| 93 | const environmentId = EnvironmentId.make("environment"); |
| 94 | function Probe({ visible, access = null }: { visible: boolean; access?: DeviceHubAccess | null }) { |
| 95 | const next = useDeviceControls({ environmentId, device, access, visible }); |
| 96 | useEffect(() => { |
| 97 | controls = next; |
| 98 | }, [next]); |
| 99 | return null; |
| 100 | } |
| 101 | function setup() { |
| 102 | read.mockReset(); |
| 103 | action.mockReset(); |
| 104 | subscribe.mockReset(); |
| 105 | subscribe.mockReturnValue(() => {}); |
| 106 | read.mockResolvedValue(snapshot("light")); |
| 107 | // No idle TTL, as in the app: hiding the panel ends its read. |
| 108 | const registry = AtomRegistry.make(); |
| 109 | const view = (visible: boolean) => ( |
| 110 | <RegistryContext.Provider value={registry}> |
| 111 | <Probe visible={visible} /> |
| 112 | </RegistryContext.Provider> |
| 113 | ); |
| 114 | const readAtom = deviceDetailReadAtom({ environmentId, hostId: "remote", deviceId: "phone" }); |
| 115 | const hide = async (screen: { rerender: (element: React.ReactElement) => Promise<unknown> }) => { |
| 116 | await screen.rerender(view(false)); |
| 117 | await vi.waitFor(() => expect(registry.getNodes().has(readAtom)).toBe(false)); |
| 118 | }; |
| 119 | return { view, hide }; |
| 120 | } |
| 121 | |
| 122 | it("serializes rapid actions and retains confirmed settings while pending or after failure", async () => { |
| 123 | const { view } = setup(); |
| 124 | await using _screen = await render(view(true)); |
| 125 | await vi.waitFor(() => expect(controls.detail?.settings.appearance).toBe("light")); |
| 126 | const response = deferred(); |
| 127 | action.mockReturnValueOnce(response.promise); |
| 128 | let pending!: Promise<boolean>; |
| 129 | await act(async () => { |
| 130 | pending = controls.act({ type: "setAppearance", value: "dark" }); |
| 131 | expect(await controls.act({ type: "setAppearance", value: "light" })).toBe(false); |
| 132 | }); |
| 133 | expect(action).toHaveBeenCalledOnce(); |
| 134 | expect(controls.disabled).toBe(true); |
| 135 | expect(controls.detail?.settings.appearance).toBe("light"); |
| 136 | await act(async () => { |
| 137 | response.resolve(snapshot("dark")); |
| 138 | expect(await pending).toBe(true); |
| 139 | }); |
| 140 | expect(controls.detail?.settings.appearance).toBe("dark"); |
| 141 | action.mockResolvedValueOnce({ _tag: "Failure", cause: offline }); |
| 142 | await act(async () => { |
| 143 | expect(await controls.act({ type: "setAppearance", value: "light" })).toBe(false); |
| 144 | }); |
| 145 | expect(controls.detail?.settings.appearance).toBe("dark"); |
| 146 | expect(controls.error).toBe("Device offline"); |
| 147 | expect(controls.disabled).toBe(false); |
| 148 | }); |
| 149 | |
| 150 | it("confirms settings again after a command sent before the panel reopened", async () => { |
| 151 | const { view, hide } = setup(); |
| 152 | await using screen = await render(view(true)); |
| 153 | await vi.waitFor(() => expect(controls.detail).not.toBeNull()); |
| 154 | const response = deferred(); |
| 155 | action.mockReturnValueOnce(response.promise); |
| 156 | let pending!: Promise<boolean>; |
| 157 | await act(async () => { |
| 158 | pending = controls.act({ type: "setAppearance", value: "dark" }); |
| 159 | }); |
| 160 | await hide(screen); |
| 161 | read.mockResolvedValueOnce({ _tag: "Failure", cause: offline }); |
| 162 | await screen.rerender(view(true)); |
| 163 | await act(async () => { |
| 164 | response.resolve(snapshot("dark")); |
| 165 | await pending; |
| 166 | }); |
| 167 | await vi.waitFor(() => expect(controls.error).toBe("Device offline")); |
| 168 | expect(controls.detail).toBeNull(); |
| 169 | expect(controls.disabled).toBe(true); |
| 170 | await act(async () => controls.act({ type: "setAppearance", value: "light" })); |
| 171 | expect(action).toHaveBeenCalledOnce(); |
| 172 | await hide(screen); |
| 173 | await screen.rerender(view(true)); |
| 174 | await vi.waitFor(() => expect(controls.disabled).toBe(false)); |
| 175 | expect(controls.error).toBeNull(); |
| 176 | }); |
| 177 | |
| 178 | it("does not let a read sent before an action overwrite the action's answer", async () => { |
| 179 | const { view, hide } = setup(); |
| 180 | await using screen = await render(view(true)); |
| 181 | await vi.waitFor(() => expect(controls.detail).not.toBeNull()); |
| 182 | await hide(screen); |
| 183 | const refresh = deferred(); |
| 184 | read.mockReturnValueOnce(refresh.promise); |
| 185 | await screen.rerender(view(true)); |
| 186 | await vi.waitFor(() => expect(read).toHaveBeenCalledTimes(2)); |
| 187 | action.mockResolvedValueOnce(snapshot("dark")); |
| 188 | let pending!: Promise<boolean>; |
| 189 | await act(async () => { |
| 190 | pending = controls.act({ type: "setAppearance", value: "dark" }); |
| 191 | }); |
| 192 | await act(async () => { |
| 193 | refresh.resolve(snapshot("light")); |
| 194 | await pending; |
| 195 | }); |
| 196 | expect(controls.detail?.settings.appearance).toBe("dark"); |
| 197 | }); |