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 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("reads a file pushed to since it was cleared as unread", () => {
28 expect(isFileViewed("c.ts", states, NO_OVERLAY)).toBe(false);
29 expect(isStaleViewedState(states?.get("c.ts"))).toBe(true);
30 expect(isStaleViewedState(states?.get("a.ts"))).toBe(false);
31 });
32
33 it("shows the press ahead of the host's answer", () => {
34 const overlay = new Map([
35 ["a.ts", false],
36 ["b.ts", true],
37 ["unloaded.ts", true],
38 ]);
39 expect(isFileViewed("b.ts", states, overlay)).toBe(true);
40 expect(isFileViewed("a.ts", states, overlay)).toBe(false);
41 expect(countViewedFiles(states, overlay)).toBe(2);
42 });
43});
44
45describe("settleFileViewedOverlay", () => {
46 it("drops a press the host has caught up on", () => {
47 const settled = settleFileViewedOverlay(
48 new Map([["a.ts", true]]),
49 states,
50 NOTHING_PENDING,
51 NOTHING_ANSWERED,
52 );
53 expect(settled.size).toBe(0);
54 });
55
56 it("keeps a press the host cannot have heard yet", () => {
57 const overlay = new Map([["a.ts", false]]);
58 const settled = settleFileViewedOverlay(overlay, states, new Set(["a.ts"]), NOTHING_ANSWERED);
59 expect(settled.get("a.ts")).toBe(false);
60 });
61
62 it("settles a file pushed to since it was cleared against un-ticking it", () => {
63 const settled = settleFileViewedOverlay(
64 new Map([["c.ts", false]]),
65 states,
66 NOTHING_PENDING,
67 NOTHING_ANSWERED,
68 );
69 expect(settled.size).toBe(0);
70 });
71
72 it("drops a tick once a read has answered for it, against what the reader pressed", () => {
73 const settled = settleFileViewedOverlay(
74 new Map([["c.ts", true]]),
75 states,
76 NOTHING_PENDING,
77 new Set(["c.ts"]),
78 );
79 expect(settled.size).toBe(0);
80 expect(isFileViewed("c.ts", states, settled)).toBe(false);
81 expect(isStaleViewedState(states?.get("c.ts"))).toBe(true);
82 });
83
84 it("holds a press made since the read that would otherwise answer for it", () => {
85 const overlay = new Map([["c.ts", true]]);
86 const settled = settleFileViewedOverlay(overlay, states, new Set(["c.ts"]), new Set(["c.ts"]));
87 expect(settled.get("c.ts")).toBe(true);
88 });
89});
90
91describe("revertFileViewedOverlay", () => {
92 const batch = [
93 { path: "a.ts", viewed: true },
94 { path: "b.ts", viewed: false },
95 ];
96 const both = new Set(["a.ts", "b.ts"]);
97
98 it("puts the checkbox back to the host's answer for everything the request answers for", () => {
99 const overlay = new Map([
100 ["a.ts", true],
101 ["b.ts", false],
102 ]);
103 expect(revertFileViewedOverlay(overlay, batch, both).size).toBe(0);
104 });
105
106 it("leaves a press the reader made after the request went out", () => {
107 const overlay = new Map([
108 ["a.ts", false],
109 ["b.ts", false],
110 ]);
111 const reverted = revertFileViewedOverlay(overlay, batch, new Set(["b.ts"]));
112 expect([...reverted]).toEqual([["a.ts", false]]);
113 });
114
115 it("leaves a path a later request took over, even pressed the same way", () => {
116 const overlay = new Map([["a.ts", true]]);
117 expect(revertFileViewedOverlay(overlay, batch, new Set(["b.ts"]))).toBe(overlay);
118 });
119});