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

pullRequestDiffDebug.test.ts

89 lines3.7 KB

1import { expect, it, vi } from "vite-plus/test";
2​
3vi.mock("react-native", () => ({ View: () => null }));
4vi.mock("expo", () => ({ requireNativeView: vi.fn() }));
5vi.mock("../../components/ui/EmptyState", () => ({ EmptyState: () => null }));
6vi.mock("../../components/ui/AppActivityIndicator", () => ({ AppActivityIndicator: () => null }));
7​
8import {
9 handlePullRequestDiffDebugEvent,
10 pullRequestDiffInitialPosition,
11} from "./pullRequestDiffDebug";
12import { createReviewDiffHighlightScheduler } from "../review/reviewDiffHighlightScheduler";
13import type { NativeReviewDiffViewProps } from "../diffs/nativeReviewDiffSurface";
14​
15it("enters at a file header without changing guide focus positioning", () => {
16 expect(pullRequestDiffInitialPosition("last-file", -1)).toEqual({
17 initialFileId: "last-file",
18 initialRowIndex: -1,
19 });
20 expect(pullRequestDiffInitialPosition("last-file", 904)).toEqual({
21 initialFileId: "",
22 initialRowIndex: 901,
23 });
24 expect(pullRequestDiffInitialPosition(undefined, -1)).toEqual({
25 initialFileId: "",
26 initialRowIndex: -1,
27 });
28});
29​
30it.each([true, false])(
31 "starts warm highlighting at the declared file without a JS jump (iOS: %s)",
32 async (isIos) => {
33 vi.useFakeTimers();
34 // A warm engine can issue its source RPC immediately when the scheduler flushes.
35 const sourceRequest = vi.fn();
36 const scheduler = createReviewDiffHighlightScheduler(sourceRequest);
37 const scrollToFile = vi.fn(async () => {});
38 const loadMore = vi.fn();
39 const onDebug: NonNullable<NativeReviewDiffViewProps["onDebug"]> = ({ nativeEvent }) => {
40 if (nativeEvent.message === "visible-range")
41 scheduler.update({
42 firstRowIndex: nativeEvent.firstRowIndex as number,
43 lastRowIndex: nativeEvent.lastRowIndex as number,
44 });
45 };
46 let lastShownRowIndex = 999;
47 const emit = (nativeEvent: Record<string, unknown>) =>
48 handlePullRequestDiffDebugEvent({ nativeEvent } as Parameters<typeof onDebug>[0], {
49 rowCount: 1000,
50 lastShownRowIndex,
51 targetFileId: "last-file",
52 focusIndex: -1,
53 isIos,
54 nativeView: { current: { scrollToFile, scrollToTop: vi.fn(async () => {}) } },
55 jumpStarted: () => true,
56 setJumpStarted: () => {},
57 onDebug,
58 loadMore,
59 });
60 try {
61 const position = pullRequestDiffInitialPosition("last-file", -1);
62 emit({ message: "rows-decoded", rows: 1000 });
63 // Native applies the declared file before publishing its viewport. A later
64 // imperative bridge command cannot retract an RPC started from a top range.
65 const first = position.initialFileId === "last-file" ? 900 : 0;
66 emit({
67 message: "visible-range",
68 firstRowIndex: first,
69 lastRowIndex: first + 40,
70 totalRows: 1000,
71 });
72 await vi.runOnlyPendingTimersAsync();
73 expect(sourceRequest.mock.calls).toEqual([[{ firstRowIndex: 900, lastRowIndex: 940 }]]);
74 expect(scrollToFile).not.toHaveBeenCalled();
75 // Near-end pagination and stale-payload filtering still use native ranges.
76 emit({ message: "visible-range", firstRowIndex: 950, lastRowIndex: 990, totalRows: 999 });
77 expect(loadMore).not.toHaveBeenCalled();
78 emit({ message: "visible-range", firstRowIndex: 950, lastRowIndex: 990, totalRows: 1000 });
79 expect(loadMore).toHaveBeenCalledOnce();
80 // A folded final file ends at its header, short of the full payload's tail.
81 lastShownRowIndex = 920;
82 emit({ message: "visible-range", firstRowIndex: 880, lastRowIndex: 920, totalRows: 1000 });
83 expect(loadMore).toHaveBeenCalledTimes(2);
84 } finally {
85 scheduler.cancel();
86 vi.useRealTimers();
87 }
88 },
89);