Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ChildThreadApprovals.browser.test.tsx

52 lines2.0 KB

1import type { ChildThreadApproval } from "@t3tools/client-runtime/state/thread-requests";
2import { EnvironmentId, RuntimeRequestId, ThreadId } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import { AsyncResult, Atom } from "effect/reactivity";
5import { expect, it, vi } from "vite-plus/test";
6​
7import { render } from "../../../test/render";
8​
9const state = vi.hoisted(() => ({ respond: vi.fn(), approvalsAtom: null as unknown }));
10​
11vi.mock("../../state/session", () => ({ useEnvironmentScope: () => true }));
12vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => state.respond }));
13vi.mock("../../state/threads", () => ({
14 childThreadApprovalsAtom: () => state.approvalsAtom,
15 threadEnvironment: { respondToApproval: {} },
16}));
17​
18import { ChildThreadApprovals } from "./ChildThreadApprovals";
19​
20function setup() {
21 state.respond.mockReset();
22 state.respond
23 .mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("Child environment offline"))))
24 .mockResolvedValueOnce(AsyncResult.success(undefined));
25 const thread = {
26 environmentId: EnvironmentId.make("child-env"),
27 threadId: ThreadId.make("child"),
28 };
29 state.approvalsAtom = Atom.make<ReadonlyArray<ChildThreadApproval>>([
30 {
31 thread,
32 title: "Compile task",
33 approval: {
34 requestId: RuntimeRequestId.make("approval"),
35 requestKind: "command",
36 detail: "Run compiler",
37 createdAt: "2026-11-01T00:00:00.000Z",
38 responseCapability: "live",
39 },
40 },
41 ]);
42 return thread;
44​
45it("shows a failed decision and clears the error when the user retries", async () => {
46 const thread = setup();
47 await using screen = await render(<ChildThreadApprovals threadRef={thread} />);
48 await screen.getByRole("button", { name: "Approve", exact: true }).click();
49 await expect.element(screen.getByRole("alert")).toHaveTextContent("Child environment offline");
50 await screen.getByRole("button", { name: "Approve", exact: true }).click();
51 await expect.element(screen.getByRole("alert")).not.toBeInTheDocument();
52});