The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 108 lines 3.49 KB
Raw
1 import { RegistryContext } from "@effect/atom-react";
2 import { EnvironmentId } from "@t3tools/contracts";
3 import {
4 EnvironmentRegistry,
5 PrimaryConnectionTarget,
6 type ConnectionCatalogEntry,
7 } from "@t3tools/client-runtime/connection";
8 import * as Effect from "effect/Effect";
9 import * as Option from "effect/Option";
10 import * as SubscriptionRef from "effect/SubscriptionRef";
11 import { AtomRegistry } from "effect/reactivity";
12 import { act, useLayoutEffect } from "react";
13 import { createRoot } from "react-dom/client";
14 import { describe, expect, it } from "@effect/vitest";
15 import { vi } from "vite-plus/test";
16
17 const fixture = vi.hoisted(() => ({
18 environment: undefined as
19 | Pick<
20 EnvironmentRegistry.EnvironmentRegistry["Service"],
21 "entries" | "retryNow" | "setEnabled"
22 >
23 | undefined,
24 }));
25
26 vi.mock("../../connection/runtime", async () => {
27 const { Atom } = await import("effect/reactivity");
28 const Layer = await import("effect/Layer");
29 const { EnvironmentRegistry } = await import("@t3tools/client-runtime/connection");
30 return {
31 connectionAtomRuntime: Atom.runtime(
32 Layer.suspend(() =>
33 Layer.succeed(
34 EnvironmentRegistry.EnvironmentRegistry,
35 fixture.environment as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"],
36 ),
37 ),
38 ),
39 };
40 });
41
42 import { useRetryEnvironment } from "./useRetryEnvironment";
43
44 const environmentId = EnvironmentId.make("retry-environment");
45
46 describe("useRetryEnvironment", () => {
47 it.effect.each([
48 { enabled: false, retries: 0 },
49 { enabled: true, retries: 1 },
50 ])("Retry now with the environment enabled: $enabled", ({ enabled, retries }) =>
51 Effect.gen(function* () {
52 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
53 const target = new PrimaryConnectionTarget({
54 environmentId,
55 label: "Retry",
56 httpBaseUrl: "https://retry.test",
57 wsBaseUrl: "wss://retry.test",
58 });
59 const entries = yield* SubscriptionRef.make<
60 ReadonlyMap<EnvironmentId, ConnectionCatalogEntry>
61 >(new Map([[environmentId, { target, profile: Option.none(), enabled }]]));
62 let retryCount = 0;
63 fixture.environment = {
64 entries,
65 retryNow: () =>
66 Effect.sync(() => {
67 retryCount += 1;
68 }),
69 setEnabled: (id, next) =>
70 SubscriptionRef.update(entries, (current) =>
71 new Map(current).set(id, { ...current.get(id)!, enabled: next }),
72 ),
73 };
74
75 const registry = AtomRegistry.make();
76 const root = createRoot(document.createElement("div"));
77 using _mounted = {
78 [Symbol.dispose]() {
79 act(() => root.unmount());
80 registry.dispose();
81 },
82 };
83 let retryEnvironment!: ReturnType<typeof useRetryEnvironment>;
84 function Probe() {
85 const value = useRetryEnvironment();
86 useLayoutEffect(() => {
87 retryEnvironment = value;
88 });
89 return null;
90 }
91 yield* Effect.promise(async () => {
92 await act(async () =>
93 root.render(
94 <RegistryContext value={registry}>
95 <Probe />
96 </RegistryContext>,
97 ),
98 );
99 await act(async () => {
100 await retryEnvironment(environmentId);
101 });
102 });
103
104 const entry = (yield* SubscriptionRef.get(entries)).get(environmentId);
105 expect({ enabled: entry?.enabled, retries: retryCount }).toEqual({ enabled: true, retries });
106 }),
107 );
108 });