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

FilePreviewPanel.browser.test.tsx

162 lines5.6 KB

1import { EnvironmentId, ThreadId, type ProjectReadFileResult } from "@t3tools/contracts";
2import { AsyncResult } from "effect/reactivity";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { act, fakeTimers, render } from "../../../test/render";
5​
6const state = vi.hoisted(() => ({
7 file: null as ProjectReadFileResult | null,
8 optimistic: null as ProjectReadFileResult | null,
9 writeFile: vi.fn(),
10}));
11​
12vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => ({
13 ...(await original()),
14 runAtomCommand: ((_registry: unknown, _command: unknown, request: unknown) =>
15 state.writeFile(request)) as never,
16}));
17vi.mock(import("~/state/session"), async (original) => ({
18 ...(await original()),
19 readEnvironmentScope: () => true,
20 useEnvironmentScope: () => true,
21}));
22vi.mock(import("~/state/filesystem"), async (original) => ({
23 ...(await original()),
24 useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }),
25}));
26vi.mock(import("~/state/use-atom-command"), async (original) => ({
27 ...(await original()),
28 useAtomCommand: (() => state.writeFile) as never,
29}));
30vi.mock(import("~/state/server"), async (original) => {
31 const actual = await original();
32 const { Atom } = await import("effect/reactivity");
33 const noConfig = Atom.make(null);
34 return {
35 ...actual,
36 serverEnvironment: { ...actual.serverEnvironment, configValueAtom: (() => noConfig) as never },
37 };
38});
39vi.mock(import("~/remoteOpen"), async (original) => ({
40 ...(await original()),
41 useRemoteOpenState: () => ({ mode: "local-exec" }) as never,
42 useRemoteOpenResolution: () => ({ state: { mode: "local-exec" }, isResolved: true }) as never,
43}));
44vi.mock(import("~/hooks/useTheme"), async (original) => ({
45 ...(await original()),
46 useTheme: () => ({ resolvedTheme: "dark" }) as never,
47}));
48vi.mock(import("~/hooks/useWorkspaceMutationRefresh"), () => ({
49 useWorkspaceMutationRefresh: vi.fn(),
50}));
51vi.mock(import("./FileBreadcrumbs"), () => ({ FileBreadcrumbs: () => <></> }));
52vi.mock(import("./projectFilesQueryState"), async (original) => ({
53 ...(await original()),
54 useProjectFileQuery: () => ({
55 data: state.file,
56 error: null,
57 isPending: false,
58 readError: null,
59 isNotFile: false,
60 isFile: true,
61 refresh: vi.fn(),
62 }),
63 getOptimisticProjectFileQueryData: () => state.optimistic,
64 getUnsavedProjectFileQueryData: () => null,
65 setProjectFileQueryData: (
66 _environmentId: EnvironmentId,
67 _cwd: string,
68 relativePath: string,
69 contents: string,
70 ) => {
71 state.optimistic = { relativePath, contents, truncated: false, byteLength: contents.length };
72 },
73 confirmProjectFileQueryData: () => true,
74}));
75​
76import FilePreviewPanel from "./FilePreviewPanel";
77​
78const environmentId = EnvironmentId.make("markdown-preview-test");
79const threadRef = { environmentId, threadId: ThreadId.make("thread") };
80function setup() {
81 const keys = ["t3code.fileExplorerOpen", "t3code.renderMarkdown"];
82 const previous = keys.map((key) => localStorage.getItem(key));
83 localStorage.setItem(keys[0]!, "false");
84 localStorage.setItem(keys[1]!, "true");
85 state.optimistic = null;
86 state.writeFile.mockReset().mockResolvedValue(AsyncResult.success(undefined));
87 return {
88 [Symbol.dispose]() {
89 for (const [index, key] of keys.entries()) {
90 const value = previous[index];
91 if (value == null) localStorage.removeItem(key);
92 else localStorage.setItem(key, value);
93 }
94 },
95 };
97​
98function markdownPreview(truncated: boolean) {
99 state.file = {
100 relativePath: "tasks.md",
101 contents: "- [ ] Keep the rest of this file\n",
102 byteLength: truncated ? 1_200_000 : 32,
103 truncated,
104 };
105 return (
106 <FilePreviewPanel
107 environmentId={environmentId}
108 cwd="/workspace"
109 projectName="Project"
110 relativePath="tasks.md"
111 threadRef={threadRef}
112 composerDraftTarget={threadRef}
113 keybindings={[]}
114 availableEditors={[]}
115 openInShortcutEnabled={false}
116 revealLine={null}
117 revealRequestId={0}
118 onOpenFile={vi.fn()}
119 onNavigateFolder={vi.fn()}
120 folderTrail={undefined}
121 onPendingChange={vi.fn()}
122 selectedFilePending={false}
123 workspaceMutationId={null}
124 />
125 );
127​
128describe("rendered Markdown task saves", () => {
129 it("keeps a truncated read read-only without queuing a replacement", async () => {
130 using _settings = setup();
131 await using screen = await render(markdownPreview(true));
132 using _timers = fakeTimers();
133 const checkbox = screen.container.querySelector<HTMLInputElement>('input[type="checkbox"]')!;
134 expect(checkbox).not.toBeNull();
135 expect(checkbox.disabled).toBe(true);
136 expect(checkbox.readOnly).toBe(true);
137 await act(async () => checkbox.click());
138 await vi.advanceTimersByTimeAsync(500);
139 expect(checkbox.checked).toBe(false);
140 expect(state.optimistic).toBeNull();
141 expect(state.writeFile).not.toHaveBeenCalled();
142 });
143​
144 it("still toggles and saves tasks from a complete read", async () => {
145 using _settings = setup();
146 await using screen = await render(markdownPreview(false));
147 using _timers = fakeTimers();
148 const checkbox = screen.container.querySelector<HTMLInputElement>('input[type="checkbox"]')!;
149 expect(checkbox).not.toBeNull();
150 expect(checkbox.disabled).toBe(false);
151 await act(async () => checkbox.click());
152 await vi.advanceTimersByTimeAsync(500);
153 expect(state.writeFile).toHaveBeenCalledExactlyOnceWith({
154 environmentId,
155 input: {
156 cwd: "/workspace",
157 relativePath: "tasks.md",
158 contents: "- [x] Keep the rest of this file\n",
159 },
160 });
161 });
162});