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