Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 countViewedFiles,
5 isFileViewed,
6 isStaleViewedState,
7 revertFileViewedOverlay,
8 settleFileViewedOverlay,
9 toFileViewedBatch,
10 toFileViewedStates,
11 type FileViewedOverlay,
12} from "./pullRequestFilesViewed.logic";
13
14const NO_OVERLAY: FileViewedOverlay = new Map();
15const NOTHING_PENDING: ReadonlySet<string> = new Set();
16const NOTHING_ANSWERED: ReadonlySet<string> = new Set();
17
18const states = toFileViewedStates({
19 files: [
20 { path: "a.ts", state: "viewed" },
21 { path: "b.ts", state: "unviewed" },
22 { path: "c.ts", state: "dismissed" },
23 ],
24 truncated: false,
25});
26
27describe("isFileViewed", () => {
28 it("follows the host for a file the reader has not pressed", () => {
29 expect(isFileViewed("a.ts", states, NO_OVERLAY)).toBe(true);
30 expect(isFileViewed("b.ts", states, NO_OVERLAY)).toBe(false);
31 });
32
33 it("reads a file pushed to since it was cleared as unread", () => {
34 expect(isFileViewed("c.ts", states, NO_OVERLAY)).toBe(false);
35 expect(isStaleViewedState(states?.get("c.ts"))).toBe(true);
36 expect(isStaleViewedState(states?.get("a.ts"))).toBe(false);
37 });
38
39 it("shows the press ahead of the host's answer", () => {
40 expect(isFileViewed("b.ts", states, new Map([["b.ts", true]]))).toBe(true);
41 expect(isFileViewed("a.ts", states, new Map([["a.ts", false]]))).toBe(false);
42 });
43
44 it("answers a file the host has said nothing about, before its answer arrives", () => {
45 expect(isFileViewed("z.ts", null, NO_OVERLAY)).toBe(false);
46 expect(isFileViewed("z.ts", null, new Map([["z.ts", true]]))).toBe(true);
47 });
48});
49
50describe("countViewedFiles", () => {
51 it("counts only the files on screen, presses included", () => {
52 expect(countViewedFiles(["a.ts", "b.ts", "c.ts"], states, NO_OVERLAY)).toBe(1);
53 expect(countViewedFiles(["a.ts", "b.ts", "c.ts"], states, new Map([["b.ts", true]]))).toBe(2);
54 expect(countViewedFiles(["b.ts"], states, NO_OVERLAY)).toBe(0);
55 });
56});
57
58describe("settleFileViewedOverlay", () => {
59 it("drops a press the host has caught up on", () => {
60 const settled = settleFileViewedOverlay(
61 new Map([["a.ts", true]]),
62 states,
63 NOTHING_PENDING,
64 NOTHING_ANSWERED,
65 );
66 expect(settled.size).toBe(0);
67 });
68
69 it("keeps a press the host still disagrees with", () => {
70 const overlay = new Map([["b.ts", true]]);
71 expect(settleFileViewedOverlay(overlay, states, NOTHING_PENDING, NOTHING_ANSWERED)).toBe(
72 overlay,
73 );
74 });
75
76 it("keeps a press the host cannot have heard yet", () => {
77 const overlay = new Map([["a.ts", false]]);
78 const settled = settleFileViewedOverlay(overlay, states, new Set(["a.ts"]), NOTHING_ANSWERED);
79 expect(settled.get("a.ts")).toBe(false);
80 });
81
82 it("settles a file pushed to since it was cleared against un-ticking it", () => {
83 const settled = settleFileViewedOverlay(
84 new Map([["c.ts", false]]),
85 states,
86 NOTHING_PENDING,
87 NOTHING_ANSWERED,
88 );
89 expect(settled.size).toBe(0);
90 });
91
92 it("drops a tick once a read has answered for it, against what the reader pressed", () => {
93 const settled = settleFileViewedOverlay(
94 new Map([["c.ts", true]]),
95 states,
96 NOTHING_PENDING,
97 new Set(["c.ts"]),
98 );
99 expect(settled.size).toBe(0);
100 expect(isFileViewed("c.ts", states, settled)).toBe(false);
101 expect(isStaleViewedState(states?.get("c.ts"))).toBe(true);
102 });
103
104 it("holds a press made since the read that would otherwise answer for it", () => {
105 const overlay = new Map([["c.ts", true]]);
106 const settled = settleFileViewedOverlay(overlay, states, new Set(["c.ts"]), new Set(["c.ts"]));
107 expect(settled.get("c.ts")).toBe(true);
108 });
109
110 it("holds everything until the host has answered at all", () => {
111 const overlay = new Map([["a.ts", true]]);
112 expect(settleFileViewedOverlay(overlay, null, NOTHING_PENDING, NOTHING_ANSWERED)).toBe(overlay);
113 });
114});
115
116describe("toFileViewedBatch", () => {
117 it("carries both directions in one batch", () => {
118 expect(
119 toFileViewedBatch(
120 new Map([
121 ["a.ts", false],
122 ["b.ts", true],
123 ]),
124 ),
125 ).toEqual([
126 { path: "a.ts", viewed: false },
127 { path: "b.ts", viewed: true },
128 ]);
129 });
130});
131
132describe("revertFileViewedOverlay", () => {
133 const batch = [
134 { path: "a.ts", viewed: true },
135 { path: "b.ts", viewed: false },
136 ];
137 const both = new Set(["a.ts", "b.ts"]);
138
139 it("puts the checkbox back to the host's answer for everything the request answers for", () => {
140 const overlay = new Map([
141 ["a.ts", true],
142 ["b.ts", false],
143 ]);
144 expect(revertFileViewedOverlay(overlay, batch, both).size).toBe(0);
145 });
146
147 it("leaves a press the reader made after the request went out", () => {
148 const overlay = new Map([
149 ["a.ts", false],
150 ["b.ts", false],
151 ]);
152 const reverted = revertFileViewedOverlay(overlay, batch, new Set(["b.ts"]));
153 expect([...reverted]).toEqual([["a.ts", false]]);
154 });
155
156 it("leaves a path a later request took over, even pressed the same way", () => {
157 const overlay = new Map([["a.ts", true]]);
158 expect(revertFileViewedOverlay(overlay, batch, new Set(["b.ts"]))).toBe(overlay);
159 });
160
161 it("leaves a path the request never carried", () => {
162 const overlay = new Map([["c.ts", true]]);
163 expect(revertFileViewedOverlay(overlay, batch, both)).toBe(overlay);
164 });
165});