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

pullRequestFilesViewed.logic.test.ts

156 lines4.9 KB

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