The best way to control Claude Code, Codex and more from any computer or phone
test: pull request Code tab keeps a hidden test file open through collapse all (#39213)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Wout Stiens committed
Nov 19, 2026 at 18:57 UTC
803f21f6a5211dd83ca26e02f7382a2396338bee
7 files changed
+372
-62
apps/web/src/components/pullRequest/PullRequestCodeTab.browser.test.tsx
+136
-18
@@ -8,16 +8,16 @@ import {
8
type PullRequestDiffResult,
9
} from "@t3tools/contracts";
10
import { expect, it, vi } from "vite-plus/test";
11
-import { userEvent } from "vite-plus/test/browser";
11
+import { page, userEvent } from "vite-plus/test/browser";
12
13
import "~/index.css";
14
import { __setClientSettingsForTests } from "../../hooks/useSettings";
15
import { setAppearanceMode } from "../../hooks/useTheme";
16
import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings";
17
18
-const { diffAtom, pagedDiffAtom } = await vi.hoisted(async () => {
18
+const { diffAtom, testFileDiffAtom, pagedDiffAtom } = await vi.hoisted(async () => {
19
const { AsyncResult, Atom } = await import("effect/reactivity");
20
- const filePatch = (path: string) =>
20
+ const filePatch = (path: string, addition = "after()") =>
21
[
22
`diff --git a/${path} b/${path}`,
23
"index 1111111..2222222 100644",
@@ -25,7 +25,7 @@ const { diffAtom, pagedDiffAtom } = await vi.hoisted(async () => {
25
`+++ b/${path}`,
26
"@@ -1,6 +1,6 @@",
27
"-before()",
28
- "+after()",
28
+ `+${addition}`,
29
" one()",
30
" two()",
31
" three()",
@@ -38,28 +38,47 @@ const { diffAtom, pagedDiffAtom } = await vi.hoisted(async () => {
38
truncated: false,
39
nextCursor: null,
40
};
41
- const pages: Record<string, PullRequestDiffResult> = {
42
- first: {
43
- patch: Array.from({ length: 40 }, (_, index) =>
44
- filePatch(index === 1 ? "early-1.test.ts" : `early-${index}.ts`),
45
- ).join(""),
46
- truncated: true,
47
- nextCursor: "page-2",
48
- },
49
- "page-2": { patch: filePatch("late.test.ts"), truncated: false, nextCursor: null },
50
- };
41
+ const pagedDiffAtom = Atom.family((shortPages: boolean) => {
42
+ const pages: Record<string, PullRequestDiffResult> = {
43
+ first: {
44
+ patch: Array.from({ length: shortPages ? 2 : 40 }, (_, index) =>
45
+ filePatch(index === 1 ? "early-1.test.ts" : `early-${index}.ts`),
46
+ ).join(""),
47
+ truncated: true,
48
+ nextCursor: "page-2",
49
+ },
50
+ "page-2": {
51
+ patch: (shortPages ? filePatch("late.ts") : "") + filePatch("late.test.ts"),
52
+ truncated: false,
53
+ nextCursor: null,
54
+ },
55
+ };
56
+ return Atom.family((cursor: string) => Atom.make(AsyncResult.success(pages[cursor]!)));
57
+ });
58
return {
59
// An environment query whose refetch returns equal content hands back the previous value
60
// itself, so a refresh publishes a new result around the very same page.
61
diffAtom: Atom.make(() => AsyncResult.success(firstPage)),
55
- pagedDiffAtom: Atom.family((cursor: string) =>
56
- Atom.make(() => AsyncResult.success(pages[cursor]!)),
62
+ testFileDiffAtom: Atom.make(() =>
63
+ AsyncResult.success({
64
+ patch:
65
+ filePatch("early-0.ts") +
66
+ filePatch("early-1.test.ts", "testOnly()") +
67
+ filePatch("early-2.generated.ts") +
68
+ (mocks.refreshed ? filePatch("early-3.ts") : ""),
69
+ truncated: false,
70
+ nextCursor: null,
71
+ }),
72
),
73
+ pagedDiffAtom,
74
};
75
});
76
77
const mocks = vi.hoisted(() => ({
78
paged: false,
79
+ shortPages: false,
80
+ withTestFile: false,
81
+ refreshed: false,
82
diffCommits: [] as Array<string | undefined>,
83
viewedPaths: null as ReadonlySet<string> | null,
84
viewedListeners: new Set<() => void>(),
@@ -108,13 +127,14 @@ vi.mock(import("~/state/pullRequests"), async (original) => {
127
...actual.pullRequestEnvironment,
128
diff: (({ input }: { input: { cursor?: string; commit?: string } }) => {
129
mocks.diffCommits.push(input.commit);
111
- return mocks.paged ? pagedDiffAtom(input.cursor ?? "first") : diffAtom;
130
+ if (mocks.paged) return pagedDiffAtom(mocks.shortPages)(input.cursor ?? "first");
131
+ return mocks.withTestFile ? testFileDiffAtom : diffAtom;
132
}) as never,
133
},
134
};
135
});
136
117
-import { render } from "../../../test/render";
137
+import { render, stubGlobal } from "../../../test/render";
138
import PullRequestCodeTab from "./PullRequestCodeTab";
139
140
const detail: PullRequestDetailView = {
@@ -173,6 +193,9 @@ function setup({ paged }: { paged: boolean }) {
193
window.localStorage.removeItem("t3code.diffHideTestFiles");
194
__setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
195
mocks.paged = paged;
196
+ mocks.shortPages = false;
197
+ mocks.withTestFile = false;
198
+ mocks.refreshed = false;
199
mocks.diffCommits = [];
200
mocks.viewedPaths = null;
201
mocks.viewedListeners.clear();
@@ -341,6 +364,101 @@ it("pages forward to a file asked for that is not loaded yet, then scrolls to it
364
await expectFileHeaderShown(screen, container, "late.test.ts");
365
});
366
367
+it("keeps a linked test file expanded when a refresh changes the rest of the diff", async () => {
368
+ setup({ paged: false });
369
+ mocks.withTestFile = true;
370
+ __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: true });
371
+ using _preferences = {
372
+ [Symbol.dispose]() {
373
+ window.localStorage.removeItem("t3code.diffHideTestFiles");
374
+ __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
375
+ },
376
+ };
377
+ const container = document.createElement("div");
378
+ container.className = "flex h-[700px] w-[900px]";
379
+ await using screen = await render(tab(0), { container });
380
+ await screen.getByRole("button", { name: "Hide test files" }).click();
381
+
382
+ const path = "early-1.test.ts";
383
+ const url = `${detail.url}/files#diff-${bytesToHex(sha256(new TextEncoder().encode(path)))}`;
384
+ const settled = vi.fn();
385
+ await screen.rerender(tab(0, null, url, settled));
386
+ await vi.waitFor(() => expect(settled).toHaveBeenCalledWith(url));
387
+ await screen.rerender(tab(0));
388
+ const testLine = screen.getByText("testOnly()", { exact: true });
389
+ await expect.element(testLine).toBeVisible();
390
+
391
+ mocks.refreshed = true;
392
+ await screen.rerender(tab(1));
393
+ await expect.element(screen.getByText("4 files", { exact: true })).toBeVisible();
394
+ await expect.element(testLine).toBeVisible();
395
+});
396
+
397
+// Two short pages of a source and a test file each; the next page loads only through the file tree.
398
+async function renderShortPages(stack: AsyncDisposableStack) {
399
+ setup({ paged: true });
400
+ mocks.shortPages = true;
401
+ __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, diffFilesCollapsed: false });
402
+ stack.defer(() => {
403
+ window.localStorage.removeItem("t3code.diffHideTestFiles");
404
+ window.localStorage.removeItem("t3code.pullRequestFileTreeOpen");
405
+ __setClientSettingsForTests(DEFAULT_CLIENT_SETTINGS);
406
+ });
407
+ stack.use(
408
+ stubGlobal(
409
+ "IntersectionObserver",
410
+ class extends IntersectionObserver {
411
+ constructor(callback: IntersectionObserverCallback, options?: IntersectionObserverInit) {
412
+ super(options?.rootMargin === "240px" ? () => {} : callback, options);
413
+ }
414
+ },
415
+ ),
416
+ );
417
+ const container = document.createElement("div");
418
+ container.className = "flex h-[1000px] w-[900px]";
419
+ const screen = stack.use(await render(tab(0), { container }));
420
+ const loadNextPage = async () => {
421
+ await screen.getByRole("button", { name: "Show file tree", exact: true }).click();
422
+ const loadMore = screen.getByRole("button", { name: "Load more files", exact: true });
423
+ await loadMore.click();
424
+ await expect.element(loadMore).not.toBeInTheDocument();
425
+ await screen.getByRole("button", { name: "Hide file tree", exact: true }).click();
426
+ };
427
+ // The later page sits at the bottom of the list, which grows as expanded files render.
428
+ const diffToggle = async (path: string) => {
429
+ const scroller = container.querySelector(".diff-render-surface")!;
430
+ const header = await vi.waitFor(() => {
431
+ scroller.scrollTop = scroller.scrollHeight;
432
+ return screen.getByText(path, { exact: true }).element();
433
+ });
434
+ const { host } = header.getRootNode() as ShadowRoot;
435
+ return page.elementLocator(host).getByRole("button", { name: /^(Expand|Collapse) diff$/ });
436
+ };
437
+ const filter = screen.getByRole("button", { name: "Hide test files" });
438
+ return { screen, loadNextPage, diffToggle, filter };
439
+}
440
+
441
+it("keeps a later test file folded by hand through a refresh and bulk toggles while hidden", async () => {
442
+ await using stack = new AsyncDisposableStack();
443
+ const { screen, loadNextPage, diffToggle, filter } = await renderShortPages(stack);
444
+ await loadNextPage();
445
+ const lateTest = await diffToggle("late.test.ts");
446
+ await lateTest.click();
447
+ await expect.element(lateTest).toHaveAttribute("aria-expanded", "false");
448
+ await filter.click();
449
+
450
+ // A refresh drops the loaded later page, so the bulk toggles below run without it.
451
+ await screen.rerender(tab(1));
452
+ await expect.element(screen.getByText("1 test file hidden", { exact: true })).toBeVisible();
453
+ await screen.getByRole("button", { name: "Collapse all files", exact: true }).click();
454
+ await screen.getByRole("button", { name: "Expand all files", exact: true }).click();
455
+ await loadNextPage();
456
+ await filter.click();
457
+
458
+ await expect.element(screen.getByText("4 files", { exact: true })).toBeVisible();
459
+ await expect.element(await diffToggle("late.test.ts")).toHaveAttribute("aria-expanded", "false");
460
+});
461
+
462
it("never requests the diff of a selected commit the pull request no longer has", async () => {
463
setup({ paged: false });
464
const container = document.createElement("div");
apps/web/src/components/pullRequest/PullRequestCodeTab.tsx
+72
-42
@@ -1,7 +1,9 @@
1
import {
2
+ EMPTY_PULL_REQUEST_DIFF_FOLDS,
3
+ foldAllPullRequestDiffs,
4
isFileDiffCollapsed,
5
withFileDiffFold,
4
- type DiffFoldOverride,
6
+ withPullRequestDiffFold,
7
type FileDiffFoldChoices,
8
} from "@t3tools/client-runtime/pull-request-diff-fold";
9
import {
@@ -82,6 +84,7 @@ import { isTestChangedFile } from "@t3tools/shared/changedFileKind";
84
import { isWorkspaceImagePreviewPath } from "@t3tools/shared/mediaPreview";
85
import { orderDiffFiles } from "./pullRequestFileOrder.logic";
86
import {
87
+ buildFileDiffIdentityKey,
88
buildFileDiffRenderKey,
89
fnv1a32,
90
getDiffLineStat,
@@ -388,6 +391,13 @@ export interface PullRequestAgentSelectionInput {
391
readonly request: string;
392
}
393
394
+// A test's fold belongs to the file even when filtering or a refresh replaces its viewer item.
395
+function buildFileDiffFoldKey(fileDiff: FileDiffMetadata): string {
396
+ return isTestChangedFile(resolveFileDiffPath(fileDiff))
397
+ ? buildFileDiffIdentityKey(fileDiff)
398
+ : buildFileDiffRenderKey(fileDiff);
399
+}
400
+
401
function isRemovedLine(fileDiff: FileDiffMetadata, oldLine: number): boolean {
402
return fileDiff.hunks.some((hunk) => {
403
let next = hunk.deletionStart;
@@ -451,11 +461,19 @@ function PullRequestCodeTab({
461
diffLayout: settings.diffLayout,
462
wordWrap: settings.wordWrap,
463
}));
454
- const [foldChoices, setFoldChoices] = useState<FileDiffFoldChoices>(() => new Map());
464
+ const [folds, setFolds] = useState(EMPTY_PULL_REQUEST_DIFF_FOLDS);
465
+ const foldGroup = (path: string) => {
466
+ const group = isTestChangedFile(path) ? folds.test : folds.source;
467
+ return group.override === null && settings.diffFilesCollapsed
468
+ ? { ...group, override: "folded" as const }
469
+ : group;
470
+ };
471
+ const setFoldChoices = (
472
+ path: string,
473
+ update: (choices: FileDiffFoldChoices) => FileDiffFoldChoices,
474
+ ) => setFolds((current) => withPullRequestDiffFold(current, isTestChangedFile(path), update));
475
const [lastViewedFileKey, setLastViewedFileKey] = useState<string | null>(null);
476
const [visibleCommitCount, setVisibleCommitCount] = useState(COMMIT_PAGE_SIZE);
457
- const [foldOverride, setFoldOverride] = useState<DiffFoldOverride>(null);
458
- const effectiveFoldOverride = foldOverride ?? (settings.diffFilesCollapsed ? "folded" : null);
477
const diffLayout = settings.diffLayout;
478
const [wordWrapOverride, setWordWrapOverride] = useState<boolean | null>(null);
479
const wordWrap = wordWrapOverride ?? settings.wordWrap;
@@ -496,8 +514,7 @@ function PullRequestCodeTab({
514
setLastViewedFileKey(null);
515
setDraft(null);
516
setSelectedLines(null);
499
- setFoldChoices(new Map());
500
- setFoldOverride(null);
517
+ setFolds(EMPTY_PULL_REQUEST_DIFF_FOLDS);
518
setVisibleCommitCount(COMMIT_PAGE_SIZE);
519
setOrphansOpen(false);
520
setSliceState({ key: scopeKey, cursor: null, slices: NO_SLICES });
@@ -707,10 +724,9 @@ function PullRequestCodeTab({
724
if (hasCutLines(data.patch)) cutFiles.add(file);
725
} else withheldStatuses.set(path, "empty");
726
});
710
- // Fold choices follow render keys, so a loaded or refetched patch would otherwise fold again
711
- // when marked Viewed: it opens on first load and keeps its earlier key's choice after.
727
+ // A withheld patch opens on first load and keeps its choice when its fold key changes.
728
const rekeyed = [...withheldFiles].flatMap(([path, file]) => {
713
- const key = buildFileDiffRenderKey(file);
729
+ const key = buildFileDiffFoldKey(file);
730
return withheldLoads.shownKeys.get(path) === key ? [] : [[path, key] as const];
731
});
732
if (withheldLoads.scope === scopeKey && rekeyed.length > 0) {
@@ -718,13 +734,13 @@ function PullRequestCodeTab({
734
...withheldLoads,
735
shownKeys: new Map([...withheldLoads.shownKeys, ...rekeyed]),
736
});
721
- setFoldChoices((current) =>
722
- rekeyed.reduce((next, [path, key]) => {
723
- const shown = withheldLoads.shownKeys.get(path);
724
- const collapsed = shown === undefined ? false : current.get(shown);
725
- return collapsed === undefined ? next : withFileDiffFold(next, key, collapsed);
726
- }, current),
727
- );
737
+ for (const [path, key] of rekeyed) {
738
+ const shown = withheldLoads.shownKeys.get(path);
739
+ setFoldChoices(path, (current) => {
740
+ const collapsed = shown === undefined ? (current.get(key) ?? false) : current.get(shown);
741
+ return collapsed === undefined ? current : withFileDiffFold(current, key, collapsed);
742
+ });
743
+ }
744
}
745
const loadedFiles =
746
withheldFiles.size === 0
@@ -878,6 +894,7 @@ function PullRequestCodeTab({
894
}
895
return {
896
fileKey,
897
+ foldKey: buildFileDiffFoldKey(fileDiff),
898
path,
899
fileDiff,
900
annotations,
@@ -947,11 +964,12 @@ function PullRequestCodeTab({
964
const foldsAsViewed = (path: string) =>
965
filesViewedEnabled && isFileViewed(path) && !isFileViewedStale(path);
966
const items: CodeViewDiffItem<ReviewAnnotationGroup>[] = annotatedFiles.map(
950
- ({ fileKey, path, fileDiff, annotations, annotationsVersion }) => {
967
+ ({ fileKey, foldKey, path, fileDiff, annotations, annotationsVersion }) => {
968
+ const group = foldGroup(path);
969
const collapsed = isFileDiffCollapsed(
952
- fileKey,
953
- effectiveFoldOverride,
954
- foldChoices,
970
+ foldKey,
971
+ group.override,
972
+ group.choices,
973
foldsAsViewed(path),
974
);
975
const viewedMark = filesViewedEnabled
@@ -995,14 +1013,13 @@ function PullRequestCodeTab({
1013
};
1014
1015
const toggleFile = (fileKey: string) => {
998
- const path = annotatedFiles.find((file) => file.fileKey === fileKey)?.path;
999
- const viewed = path !== undefined && foldsAsViewed(path);
1000
- setFoldChoices((current) =>
1001
- withFileDiffFold(
1002
- current,
1003
- fileKey,
1004
- !isFileDiffCollapsed(fileKey, effectiveFoldOverride, current, viewed),
1005
- ),
1016
+ const file = annotatedFiles.find((file) => file.fileKey === fileKey);
1017
+ if (file === undefined) return;
1018
+ const { path, foldKey } = file;
1019
+ const { override } = foldGroup(path);
1020
+ const viewed = foldsAsViewed(path);
1021
+ setFoldChoices(path, (current) =>
1022
+ withFileDiffFold(current, foldKey, !isFileDiffCollapsed(foldKey, override, current, viewed)),
1023
);
1024
};
1025
@@ -1017,13 +1034,10 @@ function PullRequestCodeTab({
1034
// A commit's diff names that commit's version of the file, not the head's.
1035
setViewed(file.path, viewed, commit === null ? file.fileDiff.newObjectId : undefined);
1036
}
1020
- setFoldChoices((current) =>
1021
- range.reduce(
1022
- (next, file) =>
1023
- withFileDiffFold(next, file.fileKey, effectiveFoldOverride === null ? undefined : viewed),
1024
- current,
1025
- ),
1026
- );
1037
+ for (const file of range) {
1038
+ const collapsed = foldGroup(file.path).override === null ? undefined : viewed;
1039
+ setFoldChoices(file.path, (current) => withFileDiffFold(current, file.foldKey, collapsed));
1040
+ }
1041
};
1042
const fileViewedToggle = {
1043
enabled: filesViewedEnabled,
@@ -1036,14 +1050,24 @@ function PullRequestCodeTab({
1050
const item = items.find((candidate) => resolveFileDiffPath(candidate.fileDiff) === path);
1051
if (item === undefined) return;
1052
// Record the unfold even when already open, so a Viewed mark that loads later cannot fold it.
1039
- setFoldChoices((current) => withFileDiffFold(current, item.id, false));
1053
+ setFoldChoices(path, (current) =>
1054
+ withFileDiffFold(current, buildFileDiffFoldKey(item.fileDiff), false),
1055
+ );
1056
if (range) setSelectedLines({ id: item.id, range });
1057
codeViewReveal.request(item.id, range);
1058
};
1059
1060
const toggleAllFiles = () => {
1045
- setFoldOverride(areAllDiffFilesCollapsed(fileKeys, collapsedFileKeys) ? "expanded" : "folded");
1046
- setFoldChoices(new Map());
1061
+ const shownTestKeys = annotatedFiles.flatMap((file) =>
1062
+ isTestChangedFile(file.path) ? [file.foldKey] : [],
1063
+ );
1064
+ setFolds((current) =>
1065
+ foldAllPullRequestDiffs(
1066
+ current,
1067
+ allFilesCollapsed ? "expanded" : "folded",
1068
+ hideTestFiles ? { shownKeys: shownTestKeys } : null,
1069
+ ),
1070
+ );
1071
};
1072
1073
const orderedCommits = // GitLab preserves UTC offsets, so compare instants.
@@ -1203,10 +1227,16 @@ function PullRequestCodeTab({
1227
onItemCollapsedChange: (
1228
_item: CodeViewItem<ReviewAnnotationGroup>,
1229
nextItem: CodeViewItem<ReviewAnnotationGroup>,
1206
- ) =>
1207
- setFoldChoices((current) =>
1208
- withFileDiffFold(current, nextItem.id, nextItem.collapsed === true),
1209
- ),
1230
+ ) => {
1231
+ if (nextItem.type !== "diff") return;
1232
+ setFoldChoices(resolveFileDiffPath(nextItem.fileDiff), (current) =>
1233
+ withFileDiffFold(
1234
+ current,
1235
+ buildFileDiffFoldKey(nextItem.fileDiff),
1236
+ nextItem.collapsed === true,
1237
+ ),
1238
+ );
1239
+ },
1240
};
1241
1242
const runThreadCommand = async (
apps/web/src/lib/diffCollapse.test.ts
new
+15
@@ -0,0 +1,15 @@
1
+import { describe, expect, it } from "vite-plus/test";
2
+
3
+import { toggleAllDiffFiles } from "./diffCollapse";
4
+
5
+describe("toggleAllDiffFiles", () => {
6
+ it.each([
7
+ { action: "expanding", collapsed: ["a", "b", "hidden"], expected: ["hidden"] },
8
+ { action: "collapsing", collapsed: ["a", "hidden"], expected: ["a", "b", "hidden"] },
9
+ ])(
10
+ "keeps a folded file outside the given ones folded when $action",
11
+ ({ collapsed, expected }) => {
12
+ expect([...toggleAllDiffFiles(["a", "b"], new Set(collapsed))].toSorted()).toEqual(expected);
13
+ },
14
+ );
15
+});
apps/web/src/lib/diffCollapse.ts
+8
-1
@@ -5,9 +5,16 @@ export function areAllDiffFilesCollapsed(
5
return fileKeys.length > 0 && fileKeys.every((fileKey) => collapsedFileKeys.has(fileKey));
6
}
7
8
+/** Expands or collapses all of `fileKeys`; every other file, such as a hidden one, keeps its fold. */
9
export function toggleAllDiffFiles(
10
fileKeys: ReadonlyArray<string>,
11
collapsedFileKeys: ReadonlySet<string>,
12
): ReadonlySet<string> {
12
- return areAllDiffFilesCollapsed(fileKeys, collapsedFileKeys) ? new Set() : new Set(fileKeys);
13
+ const collapse = !areAllDiffFilesCollapsed(fileKeys, collapsedFileKeys);
14
+ const next = new Set(collapsedFileKeys);
15
+ for (const fileKey of fileKeys) {
16
+ if (collapse) next.add(fileKey);
17
+ else next.delete(fileKey);
18
+ }
19
+ return next;
20
}
packages/client-runtime/src/pullRequestDiffFold.test.ts
+39
-1
@@ -1,6 +1,13 @@
1
import { describe, expect, it } from "vite-plus/test";
2
3
-import { isFileDiffCollapsed, withFileDiffFold } from "./pullRequestDiffFold.ts";
3
+import {
4
+ EMPTY_PULL_REQUEST_DIFF_FOLDS,
5
+ foldAllPullRequestDiffs,
6
+ isFileDiffCollapsed,
7
+ withFileDiffFold,
8
+ withPullRequestDiffFold,
9
+ type DiffFoldGroup,
10
+} from "./pullRequestDiffFold.ts";
11
12
describe("isFileDiffCollapsed", () => {
13
it("collapses saved viewed files on reopening and when a later read marks them viewed", () => {
@@ -39,3 +46,34 @@ describe("withFileDiffFold", () => {
46
expect(withFileDiffFold(folded, "a.ts", undefined).has("a.ts")).toBe(false);
47
});
48
});
49
+
50
+describe("foldAllPullRequestDiffs", () => {
51
+ it.each([
52
+ { override: "expanded", testsHidden: false },
53
+ { override: "expanded", testsHidden: true },
54
+ { override: "folded", testsHidden: false },
55
+ { override: "folded", testsHidden: true },
56
+ ] as const)("$override with tests hidden: $testsHidden", ({ override, testsHidden }) => {
57
+ const bulk = override === "folded";
58
+ let folds = withPullRequestDiffFold(EMPTY_PULL_REQUEST_DIFF_FOLDS, false, (choices) =>
59
+ withFileDiffFold(choices, "source", !bulk),
60
+ );
61
+ folds = withPullRequestDiffFold(folds, true, (choices) =>
62
+ withFileDiffFold(withFileDiffFold(choices, "hidden-test", !bulk), "revealed-test", !bulk),
63
+ );
64
+ const next = foldAllPullRequestDiffs(
65
+ folds,
66
+ override,
67
+ testsHidden ? { shownKeys: ["revealed-test"] } : null,
68
+ );
69
+ const collapsed = (group: DiffFoldGroup, key: string, viewed = false) =>
70
+ isFileDiffCollapsed(key, group.override, group.choices, viewed);
71
+
72
+ expect(collapsed(next.source, "source")).toBe(bulk);
73
+ expect(collapsed(next.source, "later-source", !bulk)).toBe(bulk);
74
+ expect(collapsed(next.test, "revealed-test")).toBe(bulk);
75
+ // A hidden test keeps its own choice, or its live Viewed mark when it has none.
76
+ expect(collapsed(next.test, "hidden-test")).toBe(testsHidden ? !bulk : bulk);
77
+ expect(collapsed(next.test, "later-test", !bulk)).toBe(testsHidden ? !bulk : bulk);
78
+ });
79
+});
packages/client-runtime/src/pullRequestDiffFold.ts
+52
@@ -23,3 +23,55 @@ export function withFileDiffFold(
23
else next.set(fileKey, collapsed);
24
return next;
25
}
26
+
27
+export interface DiffFoldGroup {
28
+ readonly override: DiffFoldOverride;
29
+ readonly choices: FileDiffFoldChoices;
30
+}
31
+
32
+/** Test files keep their own group, so a bulk fold while they are hidden leaves them as they were. */
33
+export interface PullRequestDiffFolds {
34
+ readonly source: DiffFoldGroup;
35
+ readonly test: DiffFoldGroup;
36
+}
37
+
38
+const EMPTY_GROUP: DiffFoldGroup = { override: null, choices: new Map() };
39
+
40
+export const EMPTY_PULL_REQUEST_DIFF_FOLDS: PullRequestDiffFolds = {
41
+ source: EMPTY_GROUP,
42
+ test: EMPTY_GROUP,
43
+};
44
+
45
+export function withPullRequestDiffFold(
46
+ folds: PullRequestDiffFolds,
47
+ test: boolean,
48
+ update: (choices: FileDiffFoldChoices) => FileDiffFoldChoices,
49
+): PullRequestDiffFolds {
50
+ const group = test ? folds.test : folds.source;
51
+ const choices = update(group.choices);
52
+ if (choices === group.choices) return folds;
53
+ return { ...folds, [test ? "test" : "source"]: { ...group, choices } };
54
+}
55
+
56
+/**
57
+ * Expand or fold every file, on later pages too. While test files are hidden, only the tests
58
+ * still shown (a revealed one) follow; the rest keep their choices and fallback.
59
+ */
60
+export function foldAllPullRequestDiffs(
61
+ folds: PullRequestDiffFolds,
62
+ override: NonNullable<DiffFoldOverride>,
63
+ hiddenTests: { readonly shownKeys: ReadonlyArray<string> } | null,
64
+): PullRequestDiffFolds {
65
+ const all: DiffFoldGroup = { override, choices: new Map() };
66
+ if (hiddenTests === null) return { source: all, test: all };
67
+ return {
68
+ source: all,
69
+ test: {
70
+ ...folds.test,
71
+ choices: hiddenTests.shownKeys.reduce(
72
+ (choices, key) => withFileDiffFold(choices, key, override === "folded"),
73
+ folds.test.choices,
74
+ ),
75
+ },
76
+ };
77
+}
scripts/scenarios/expanding-visible-diffs-keeps-hidden-tests-folded.e2e.ts
new
+50
@@ -0,0 +1,50 @@
1
+import { expect } from "e2e";
2
+import * as Effect from "effect/Effect";
3
+import * as FileSystem from "effect/FileSystem";
4
+
5
+import { runPromise, test, WEB } from "./t3.ts";
6
+
7
+test(
8
+ "Expanding visible diffs keeps a hidden test file folded",
9
+ WEB,
10
+ async ({ t3, app, screen }) => {
11
+ const project = await t3.project("demo", {
12
+ "notes.txt": "Old implementation notes\n",
13
+ "notes.test.ts": "const expected = 1;\n",
14
+ });
15
+ await runPromise(
16
+ Effect.gen(function* () {
17
+ const fs = yield* FileSystem.FileSystem;
18
+ yield* fs.writeFileString(`${project}/notes.txt`, "New implementation notes\n");
19
+ yield* fs.writeFileString(`${project}/notes.test.ts`, "const expected = 2;\n");
20
+ }),
21
+ );
22
+ const thread = await t3.thread({ project, title: "Diff folds", messages: ["first"] });
23
+
24
+ await t3.open(thread);
25
+ await screen.getByText("first fixture turn complete", { exact: false }).waitFor();
26
+ await screen.getByRole("button", "Toggle right panel").tap();
27
+ await screen.getByText("Diff").tap();
28
+
29
+ const fileToggle = (path: string, expanded: boolean) =>
30
+ screen.getByRole("button", `${expanded ? "Collapse" : "Expand"} ${path}`, { exact: true });
31
+ for (const path of ["notes.test.ts", "notes.txt"]) {
32
+ // Open each file once to load its patch before testing the saved folds.
33
+ await fileToggle(path, false).tap();
34
+ await fileToggle(path, true).tap();
35
+ await fileToggle(path, false).waitFor();
36
+ }
37
+
38
+ const filter = screen.getByRole("button", "Hide test files", { exact: true });
39
+ await filter.tap();
40
+ await fileToggle("notes.test.ts", false).waitFor({ state: "hidden" });
41
+ await screen.getByRole("button", "Expand all files", { exact: true }).tap();
42
+ await fileToggle("notes.txt", true).waitFor();
43
+ await filter.tap();
44
+
45
+ const testFile = screen.getByRole("button", /^(Collapse|Expand) notes\.test\.ts$/);
46
+ await testFile.waitFor();
47
+ await app.screenshot("test-file-shown-again");
48
+ await expect(testFile).toHaveAttribute("aria-expanded", "false");
49
+ },
50
+);