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

pullRequestFilesViewed.logic.test.ts

129 lines4.2 KB

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