Skip to content

Commit 12d1e4c

Browse files
only21milcodexmaria-rcks
authored
fix(web): keep truncated Markdown task previews read-only (pingdotgg#22339)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 9588f0c commit 12d1e4c

2 files changed

Lines changed: 163 additions & 1 deletion

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
import { EnvironmentId, ThreadId, type ProjectReadFileResult } from "@t3tools/contracts";
2+
import { AsyncResult } from "effect/reactivity";
3+
import { describe, expect, it, vi } from "vite-plus/test";
4+
import { act, fakeTimers, render } from "../../../test/render";
5+
6+
const state = vi.hoisted(() => ({
7+
file: null as ProjectReadFileResult | null,
8+
optimistic: null as ProjectReadFileResult | null,
9+
writeFile: vi.fn(),
10+
}));
11+
12+
vi.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+
}));
17+
vi.mock(import("~/state/session"), async (original) => ({
18+
...(await original()),
19+
readEnvironmentScope: () => true,
20+
useEnvironmentScope: () => true,
21+
}));
22+
vi.mock(import("~/state/filesystem"), async (original) => ({
23+
...(await original()),
24+
useFilesystemReadAccess: () => ({ canReadFiles: true, isPending: false, error: null }),
25+
}));
26+
vi.mock(import("~/state/use-atom-command"), async (original) => ({
27+
...(await original()),
28+
useAtomCommand: (() => state.writeFile) as never,
29+
}));
30+
vi.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+
});
39+
vi.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+
}));
44+
vi.mock(import("~/hooks/useTheme"), async (original) => ({
45+
...(await original()),
46+
useTheme: () => ({ resolvedTheme: "dark" }) as never,
47+
}));
48+
vi.mock(import("~/hooks/useWorkspaceMutationRefresh"), () => ({
49+
useWorkspaceMutationRefresh: vi.fn(),
50+
}));
51+
vi.mock(import("./FileBreadcrumbs"), () => ({ FileBreadcrumbs: () => <></> }));
52+
vi.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+
76+
import FilePreviewPanel from "./FilePreviewPanel";
77+
78+
const environmentId = EnvironmentId.make("markdown-preview-test");
79+
const threadRef = { environmentId, threadId: ThreadId.make("thread") };
80+
function 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+
};
96+
}
97+
98+
function 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+
);
126+
}
127+
128+
describe("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+
});

‎apps/web/src/components/files/FilePreviewPanel.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ export default function FilePreviewPanel({
14861486
threadRef={threadRef}
14871487
contents={file.data.contents}
14881488
workspaceMutationId={workspaceMutationId}
1489-
readOnly={isHostFile || !canWriteFiles}
1489+
readOnly={file.data.truncated || isHostFile || !canWriteFiles}
14901490
onPendingChange={onPendingChange}
14911491
/>
14921492
) : tableDelimiter && renderTable ? (

0 commit comments

Comments
 (0)