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

LinkedThreadSheet.test.tsx

134 lines5.4 KB

1import { expect, it } from "@effect/vitest";
2import {
3 followLinkedThread,
4 type LinkedThreadView as LinkedThreadState,
5} from "@t3tools/client-runtime/linked-thread";
6import { EnvironmentId, type OrchestrationV2ThreadHistoryPage } from "@t3tools/contracts";
7import * as Effect from "effect/Effect";
8import * as Queue from "effect/Queue";
9import * as Stream from "effect/Stream";
10import { act, type ReactNode } from "react";
11import { createRoot } from "react-dom/client";
12import { vi } from "vite-plus/test";
13​
14import { v2Projection } from "../../../../../packages/client-runtime/src/state/orchestrationV2TestFixtures";
15import { useLinkedThreadView } from "../../state/linkedThread";
16import { LinkedThreadView } from "./LinkedThreadSheet";
17​
18vi.mock("@react-navigation/native", () => ({
19 StackActions: {},
20 useIsFocused: () => true,
21 useNavigation: () => ({}),
22}));
23vi.mock("react-native", () => {
24 const Container = ({ children }: { children: ReactNode }) => <div>{children}</div>;
25 return {
26 AppState: { currentState: "active", addEventListener: () => ({ remove() {} }) },
27 Platform: { OS: "android" },
28 Pressable: Container,
29 ScrollView: Container,
30 Text: Container,
31 View: Container,
32 };
33});
34vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) }));
35vi.mock("../../components/ui/AppActivityIndicator", () => ({ AppActivityIndicator: () => null }));
36vi.mock("../../components/ui/Button", () => ({
37 Button: (props: { label: string; disabled?: boolean; onPress: () => void }) => (
38 <button type="button" disabled={props.disabled} onClick={props.onPress}>
39 {props.label}
40 </button>
41 ),
42}));
43vi.mock("../../components/ui/ScreenContent", () => ({
44 ScreenContent: ({ children }: { children: ReactNode }) => children,
45}));
46vi.mock("../../native/SelectableMarkdownText", () => ({ SelectableMarkdownText: () => null }));
47vi.mock("../../state/environments", () => ({ useEnvironments: () => ({ environments: [] }) }));
48vi.mock("../../state/linkedThread", () => ({
49 useConnectLinkedEnvironment: () => ({ state: { phase: "idle" } }),
50 useLinkedThreadView: vi.fn(),
51}));
52vi.mock("../../state/peerLinks", () => ({ peerLinkEnvironment: {} }));
53vi.mock("../../state/query", () => ({ useEnvironmentQuery: () => ({}) }));
54vi.mock("../files/FileMarkdownPreview", () => ({ useMarkdownPreviewStyles: () => ({}) }));
55vi.mock("./ReadOnlyComposerBar", () => ({
56 ReadOnlyComposerBar: ({ children }: { children: ReactNode }) => children,
57}));
58vi.mock("./thread-inspector-header", () => ({ ThreadInspectorHeader: () => null }));
59vi.mock("./useOpenLinkedThread", () => ({ useOpenLinkedThread: () => undefined }));
60​
61it.effect(
62 "shows a page error after Load earlier fails on an already disconnected linked chat",
63 () =>
64 Effect.gen(function* () {
65 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
66 const container = document.createElement("div");
67 const root = createRoot(container);
68 const target = {
69 viaEnvironmentId: EnvironmentId.make("via"),
70 environmentId: EnvironmentId.make("linked"),
71 threadId: v2Projection.thread.id,
72 label: "Linked test environment",
73 title: null,
74 };
75 const warning = "Linked test environment is not answering this environment";
76 const pageError = "Could not load earlier turns. Try again.";
77 const loadEarlier = () =>
78 [...container.querySelectorAll("button")].find(
79 (button) => button.textContent === "Load earlier",
80 )!;
81​
82 try {
83 const unavailable = { _tag: "PeerLinkThreadReadError", reason: "unreachable" };
84 const pages =
85 yield* Queue.unbounded<
86 Effect.Effect<OrchestrationV2ThreadHistoryPage, typeof unavailable>
87 >();
88 const views = yield* Queue.unbounded<LinkedThreadState>();
89 yield* followLinkedThread({
90 items: Stream.make({
91 kind: "snapshot",
92 snapshotSequence: 1,
93 projection: v2Projection,
94 historyCursor: "before-recent",
95 hasMoreHistory: true,
96 } as const).pipe(Stream.concat(Stream.fail(unavailable))),
97 historyPage: () => Queue.take(pages).pipe(Effect.flatten),
98 }).pipe(
99 Stream.runForEach((view) => Queue.offer(views, view)),
100 Effect.forkScoped,
101 );
102​
103 const renderNext = Effect.gen(function* () {
104 const view = yield* Queue.take(views);
105 vi.mocked(useLinkedThreadView).mockReturnValue({
106 view,
107 error: view.error ?? null,
108 retry() {},
109 });
110 act(() => root.render(<LinkedThreadView target={target} />));
111 });
112​
113 yield* renderNext; // Initial snapshot.
114 yield* renderNext; // Connection warning.
115 expect(container.textContent).toContain(warning);
116 expect(container.textContent).not.toContain(pageError);
117​
118 act(() => loadEarlier().click());
119 yield* renderNext;
120 yield* Queue.offer(pages, Effect.fail(unavailable));
121 yield* renderNext;
122 expect(loadEarlier().parentElement?.textContent).toContain(pageError);
123 expect(container.textContent).toContain(warning);
124​
125 act(() => loadEarlier().click());
126 yield* renderNext;
127 expect(container.textContent).not.toContain(pageError);
128 expect(loadEarlier().disabled).toBe(true);
129 } finally {
130 act(() => root.unmount());
131 vi.unstubAllGlobals();
132 }
133 }).pipe(Effect.scoped),
134);