Open History to see the last commit
1import {
2 EnvironmentId,
3 ProjectId,
4 type PullRequestFilesViewedResult,
5 type PullRequestRef,
6} from "@t3tools/contracts";
7import { AsyncResult } from "effect/reactivity";
8import { act, StrictMode } from "react";
9import { create, type ReactTestRenderer } from "react-test-renderer";
10import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
11
12const { host, setFilesViewed, toastAdd } = vi.hoisted(() => ({
13 host: { data: null as unknown, refresh: vi.fn() },
14 setFilesViewed: vi.fn(),
15 toastAdd: vi.fn(),
16}));
17
18vi.mock("~/state/pullRequests", () => ({
19 pullRequestEnvironment: { filesViewed: () => null, setFilesViewed: {} },
20}));
21vi.mock("~/state/query", () => ({
22 useEnvironmentQuery: () => ({
23 data: host.data,
24 error: null,
25 isPending: false,
26 isSuccess: true,
27 refresh: host.refresh,
28 }),
29}));
30vi.mock("~/state/use-atom-command", () => ({ useAtomCommand: () => setFilesViewed }));
31vi.mock("../ui/toast", () => ({ toastManager: { add: toastAdd } }));
32
33import {
34 usePullRequestFilesViewed,
35 type PullRequestFilesViewedView,
36} from "./usePullRequestFilesViewed";
37
38const environmentId = EnvironmentId.make("pr-files-viewed-audit");
39const reference: PullRequestRef = {
40 projectId: ProjectId.make("project-a"),
41 repository: "acme/web",
42 number: 42,
43};
44
45function answer(state: "unviewed" | "viewed" | "dismissed"): PullRequestFilesViewedResult {
46 return { files: [{ path: "a.ts", state }], truncated: false };
47}
48
49let renderer: ReactTestRenderer | null = null;
50
51function Probe(_props: { readonly view: PullRequestFilesViewedView }) {
52 return null;
53}
54
55function Surface() {
56 const view = usePullRequestFilesViewed({ environmentId, reference, enabled: true });
57 return <Probe view={view} />;
58}
59
60function view(): PullRequestFilesViewedView {
61 return renderer!.root.findByType(Probe).props.view;
62}
63
64async function reads(state: "unviewed" | "viewed" | "dismissed") {
65 host.data = answer(state);
66 await act(async () =>
67 renderer!.update(
68 <StrictMode>
69 <Surface />
70 </StrictMode>,
71 ),
72 );
73}
74
75beforeEach(async () => {
76 vi.useFakeTimers();
77 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
78 host.data = answer("unviewed");
79 host.refresh.mockReset();
80 toastAdd.mockReset();
81 setFilesViewed.mockReset().mockResolvedValue(AsyncResult.success(undefined));
82 act(() => {
83 renderer = create(
84 <StrictMode>
85 <Surface />
86 </StrictMode>,
87 );
88 });
89});
90
91afterEach(async () => {
92 await act(async () => renderer?.unmount());
93 renderer = null;
94 vi.useRealTimers();
95 vi.unstubAllGlobals();
96});
97
98describe("a mark whose file was pushed to before the read that followed it", () => {
99 it("gives way to the host and shows the file as changed", async () => {
100 view().setViewed("a.ts", true);
101 await act(async () => vi.advanceTimersByTimeAsync(500));
102 expect(setFilesViewed).toHaveBeenCalledExactlyOnceWith({
103 environmentId,
104 input: { ...reference, files: [{ path: "a.ts", viewed: true }] },
105 });
106 expect(host.refresh).toHaveBeenCalled();
107
108 await reads("dismissed");
109
110 expect(view().isViewed("a.ts")).toBe(false);
111 expect(view().isStale("a.ts")).toBe(true);
112 });
113
114 it("stays given way to on every later read, having nothing left to recover", async () => {
115 view().setViewed("a.ts", true);
116 await act(async () => vi.advanceTimersByTimeAsync(500));
117 await reads("dismissed");
118
119 view().refresh();
120 await reads("dismissed");
121
122 expect(view().isViewed("a.ts")).toBe(false);
123 expect(view().isStale("a.ts")).toBe(true);
124 });
125});
126
127describe("a mark the host has not answered for yet", () => {
128 it("holds the press while the write is still out", async () => {
129 let land = (_result: unknown) => {};
130 setFilesViewed.mockReturnValueOnce(new Promise((resolve) => (land = resolve)));
131
132 view().setViewed("a.ts", true);
133 await act(async () => vi.advanceTimersByTimeAsync(500));
134
135 await reads("unviewed");
136 expect(view().isViewed("a.ts")).toBe(true);
137
138 await act(async () => land(AsyncResult.success(undefined)));
139 expect(view().isViewed("a.ts")).toBe(true);
140 });
141
142 it("holds a press made since the read that would otherwise answer for it", async () => {
143 view().setViewed("a.ts", true);
144 await act(async () => vi.advanceTimersByTimeAsync(500));
145
146 view().setViewed("a.ts", true);
147 await reads("dismissed");
148
149 expect(view().isViewed("a.ts")).toBe(true);
150 expect(view().isStale("a.ts")).toBe(false);
151 });
152});