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 { RegistryContext, useAtomValue } from "@effect/atom-react"; |
| 2 | import { EnvironmentId, type PeerLinkSummary } from "@t3tools/contracts"; |
| 3 | import * as DateTime from "effect/DateTime"; |
| 4 | import { AsyncResult, AtomRegistry } from "effect/reactivity"; |
| 5 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 6 | |
| 7 | import "~/index.css"; |
| 8 | import { peerLinkEnvironment } from "~/state/peerLinks"; |
| 9 | import { render } from "../../../test/render"; |
| 10 | import { LinkedEnvironmentsSettings } from "./LinkedEnvironmentsSettings"; |
| 11 | |
| 12 | const hostId = EnvironmentId.make("mac"); |
| 13 | const vmLink: PeerLinkSummary = { |
| 14 | environmentId: EnvironmentId.make("vm"), |
| 15 | label: "boat-task", |
| 16 | urls: ["http://boat-task.example.ts.net:3773"], |
| 17 | access: "full-access", |
| 18 | linkedAt: DateTime.makeUnsafe("2026-11-01T00:00:00.000Z"), |
| 19 | expiresAt: DateTime.makeUnsafe("2099-01-01T00:00:00.000Z"), |
| 20 | lastReachedAt: null, |
| 21 | lastError: null, |
| 22 | status: "reachable", |
| 23 | }; |
| 24 | |
| 25 | // What the host's `peerLinks.list` answers. The CLI and agents link machines without this client. |
| 26 | const server = vi.hoisted(() => ({ |
| 27 | links: [] as Array<PeerLinkSummary>, |
| 28 | started: 0, |
| 29 | interrupted: 0, |
| 30 | answer: Promise.resolve(), |
| 31 | })); |
| 32 | |
| 33 | function setup() { |
| 34 | server.links = []; |
| 35 | server.started = 0; |
| 36 | server.interrupted = 0; |
| 37 | server.answer = Promise.resolve(); |
| 38 | } |
| 39 | |
| 40 | vi.mock(import("~/state/peerLinks"), async (original) => { |
| 41 | const actual = await original(); |
| 42 | const { Atom: AtomModule } = await import("effect/reactivity"); |
| 43 | const EffectModule = await import("effect/Effect"); |
| 44 | // The real list query's options: stale-while-revalidate, revalidating only when it is built. |
| 45 | const list = AtomModule.make( |
| 46 | EffectModule.gen(function* () { |
| 47 | server.started += 1; |
| 48 | yield* EffectModule.promise(() => server.answer); |
| 49 | return { links: [...server.links] }; |
| 50 | }).pipe( |
| 51 | EffectModule.onInterrupt(() => |
| 52 | EffectModule.sync(() => { |
| 53 | server.interrupted += 1; |
| 54 | }), |
| 55 | ), |
| 56 | ), |
| 57 | ).pipe(AtomModule.swr({ staleTime: 10_000, revalidateOnMount: true })); |
| 58 | const denied = AtomModule.make(false); |
| 59 | return { |
| 60 | ...actual, |
| 61 | peerLinkEnvironment: { |
| 62 | ...actual.peerLinkEnvironment, |
| 63 | list: (() => list) as never, |
| 64 | link: { ...actual.peerLinkEnvironment.link, permissionAtom: () => denied }, |
| 65 | unlink: { ...actual.peerLinkEnvironment.unlink, permissionAtom: () => denied }, |
| 66 | }, |
| 67 | }; |
| 68 | }); |
| 69 | |
| 70 | /** Stands in for the chat composer, which reads the same list while a thread is open. */ |
| 71 | function ComposerReader() { |
| 72 | const result = useAtomValue(peerLinkEnvironment.list({ environmentId: hostId, input: {} })); |
| 73 | return AsyncResult.isSuccess(result) ? <p>Composer loaded</p> : null; |
| 74 | } |
| 75 | |
| 76 | describe("LinkedEnvironmentsSettings", () => { |
| 77 | it("shows a link made elsewhere while the composer keeps the list loaded", async () => { |
| 78 | setup(); |
| 79 | const registry = AtomRegistry.make(); |
| 80 | using _registry = { [Symbol.dispose]: () => registry.dispose() }; |
| 81 | await using composer = await render( |
| 82 | <RegistryContext.Provider value={registry}> |
| 83 | <ComposerReader /> |
| 84 | </RegistryContext.Provider>, |
| 85 | ); |
| 86 | await expect.element(composer.getByText("Composer loaded")).toBeVisible(); |
| 87 | |
| 88 | server.links = [vmLink]; |
| 89 | await using screen = await render( |
| 90 | <RegistryContext.Provider value={registry}> |
| 91 | <LinkedEnvironmentsSettings |
| 92 | environmentId={hostId} |
| 93 | environmentLabel={null} |
| 94 | isSearchTarget={false} |
| 95 | /> |
| 96 | </RegistryContext.Provider>, |
| 97 | ); |
| 98 | |
| 99 | await expect.element(screen.getByRole("heading", { name: "boat-task" })).toBeVisible(); |
| 100 | }); |
| 101 | |
| 102 | it("leaves a list that is still loading to answer", async () => { |
| 103 | setup(); |
| 104 | // useLiveRefresh remembers each view's last read across mounts, so this view gets its own host. |
| 105 | const loadingHostId = EnvironmentId.make("mac-loading"); |
| 106 | let answer = () => {}; |
| 107 | server.answer = new Promise((resolve) => { |
| 108 | answer = resolve; |
| 109 | }); |
| 110 | server.links = [vmLink]; |
| 111 | const registry = AtomRegistry.make(); |
| 112 | using _registry = { [Symbol.dispose]: () => registry.dispose() }; |
| 113 | await using screen = await render( |
| 114 | <RegistryContext.Provider value={registry}> |
| 115 | <LinkedEnvironmentsSettings |
| 116 | environmentId={loadingHostId} |
| 117 | environmentLabel={null} |
| 118 | isSearchTarget={false} |
| 119 | /> |
| 120 | </RegistryContext.Provider>, |
| 121 | ); |
| 122 | await expect.element(screen.getByRole("status")).toBeVisible(); |
| 123 | |
| 124 | answer(); |
| 125 | await expect.element(screen.getByRole("heading", { name: "boat-task" })).toBeVisible(); |
| 126 | expect(server).toMatchObject({ started: 1, interrupted: 0 }); |
| 127 | }); |
| 128 | }); |