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 { isClientHostEnvironment } from "./clientHostEnvironment"; |
| 2 | import { |
| 3 | PrimaryConnectionTarget, |
| 4 | SshConnectionTarget, |
| 5 | RelayConnectionTarget, |
| 6 | } from "@t3tools/client-runtime/connection"; |
| 7 | import { EnvironmentId } from "@t3tools/contracts"; |
| 8 | import { Atom, AtomRegistry } from "effect/reactivity"; |
| 9 | import { describe, expect, it } from "vite-plus/test"; |
| 10 | |
| 11 | import { createLocalEnvironmentIdAtom } from "./primaryEnvironment"; |
| 12 | |
| 13 | const PRIMARY = EnvironmentId.make("primary"); |
| 14 | const SERVICE = EnvironmentId.make("service"); |
| 15 | const REMOTE = EnvironmentId.make("remote"); |
| 16 | |
| 17 | function localEnvironmentId(input: { |
| 18 | primary: EnvironmentId | null; |
| 19 | host: string | null; |
| 20 | catalog: ReadonlyArray<EnvironmentId>; |
| 21 | }) { |
| 22 | const atom = createLocalEnvironmentIdAtom({ |
| 23 | primaryEnvironmentIdAtom: Atom.make(input.primary), |
| 24 | hostEnvironmentIdAtom: Atom.make(input.host), |
| 25 | catalogValueAtom: Atom.make({ entries: new Map(input.catalog.map((id) => [id, null])) }), |
| 26 | }); |
| 27 | return AtomRegistry.make().get(atom); |
| 28 | } |
| 29 | |
| 30 | describe("localEnvironmentIdAtom", () => { |
| 31 | it("prefers the primary environment over this machine's background service", () => { |
| 32 | expect( |
| 33 | localEnvironmentId({ primary: PRIMARY, host: "service", catalog: [PRIMARY, SERVICE] }), |
| 34 | ).toBe(PRIMARY); |
| 35 | }); |
| 36 | |
| 37 | it("uses the connected background service sharing the app's T3 home when there is no primary", () => { |
| 38 | expect(localEnvironmentId({ primary: null, host: "service", catalog: [REMOTE, SERVICE] })).toBe( |
| 39 | SERVICE, |
| 40 | ); |
| 41 | }); |
| 42 | |
| 43 | it("uses no environment when that service is not among the connections", () => { |
| 44 | expect(localEnvironmentId({ primary: null, host: "service", catalog: [REMOTE] })).toBeNull(); |
| 45 | expect(localEnvironmentId({ primary: null, host: null, catalog: [REMOTE] })).toBeNull(); |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | it("recognizes the desktop host service and direct browser loopback, excluding remote primaries", () => { |
| 50 | const target = (httpBaseUrl: string) => |
| 51 | new PrimaryConnectionTarget({ |
| 52 | environmentId: PRIMARY, |
| 53 | label: "Local", |
| 54 | httpBaseUrl, |
| 55 | wsBaseUrl: "ws://localhost/ws", |
| 56 | }); |
| 57 | expect(isClientHostEnvironment(SERVICE, "service", null, true)).toBe(true); |
| 58 | expect(isClientHostEnvironment(REMOTE, "service", target("https://remote.example"), false)).toBe( |
| 59 | false, |
| 60 | ); |
| 61 | expect(isClientHostEnvironment(PRIMARY, null, target("http://localhost:3000"), false)).toBe(true); |
| 62 | expect(isClientHostEnvironment(PRIMARY, null, target("http://127.0.0.1:3000"), false)).toBe(true); |
| 63 | expect(isClientHostEnvironment(PRIMARY, null, target("https://remote.example"), true)).toBe(true); |
| 64 | expect(isClientHostEnvironment(REMOTE, null, null, true)).toBe(false); |
| 65 | for (const target of [ |
| 66 | new SshConnectionTarget({ environmentId: REMOTE, label: "SSH", connectionId: "ssh-1" }), |
| 67 | new RelayConnectionTarget({ environmentId: REMOTE, label: "Relay" }), |
| 68 | ]) { |
| 69 | expect(isClientHostEnvironment(REMOTE, "service", target, true)).toBe(false); |
| 70 | } |
| 71 | }); |