| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | countViewedFiles, |
| 5 | isFileViewed, |
| 6 | isStaleViewedState, |
| 7 | revertFileViewedOverlay, |
| 8 | settleFileViewedOverlay, |
| 9 | toFileViewedBatch, |
| 10 | toFileViewedStates, |
| 11 | type FileViewedOverlay, |
| 12 | } from "./pullRequestFilesViewed.logic"; |
| 13 | |
| 14 | const NO_OVERLAY: FileViewedOverlay = new Map(); |
| 15 | const NOTHING_PENDING: ReadonlySet<string> = new Set(); |
| 16 | const NOTHING_ANSWERED: ReadonlySet<string> = new Set(); |
| 17 | |
| 18 | const 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 | |
| 27 | describe("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 | |
| 50 | describe("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 | // A file the host knows about but the diff has not paged in yet is not counted. |
| 55 | expect(countViewedFiles(["b.ts"], states, NO_OVERLAY)).toBe(0); |
| 56 | }); |
| 57 | }); |
| 58 | |
| 59 | describe("settleFileViewedOverlay", () => { |
| 60 | it("drops a press the host has caught up on", () => { |
| 61 | const settled = settleFileViewedOverlay( |
| 62 | new Map([["a.ts", true]]), |
| 63 | states, |
| 64 | NOTHING_PENDING, |
| 65 | NOTHING_ANSWERED, |
| 66 | ); |
| 67 | expect(settled.size).toBe(0); |
| 68 | }); |
| 69 | |
| 70 | it("keeps a press the host still disagrees with", () => { |
| 71 | const overlay = new Map([["b.ts", true]]); |
| 72 | expect(settleFileViewedOverlay(overlay, states, NOTHING_PENDING, NOTHING_ANSWERED)).toBe( |
| 73 | overlay, |
| 74 | ); |
| 75 | }); |
| 76 | |
| 77 | it("keeps a press the host cannot have heard yet", () => { |
| 78 | // An answer already on its way when the file was un-ticked would otherwise put the tick back. |
| 79 | const overlay = new Map([["a.ts", false]]); |
| 80 | const settled = settleFileViewedOverlay(overlay, states, new Set(["a.ts"]), NOTHING_ANSWERED); |
| 81 | expect(settled.get("a.ts")).toBe(false); |
| 82 | }); |
| 83 | |
| 84 | it("settles a file pushed to since it was cleared against un-ticking it", () => { |
| 85 | const settled = settleFileViewedOverlay( |
| 86 | new Map([["c.ts", false]]), |
| 87 | states, |
| 88 | NOTHING_PENDING, |
| 89 | NOTHING_ANSWERED, |
| 90 | ); |
| 91 | expect(settled.size).toBe(0); |
| 92 | }); |
| 93 | |
| 94 | it("drops a tick once a read has answered for it, against what the reader pressed", () => { |
| 95 | // The tick landed, the file was pushed to before the read that followed it came back, and the |
| 96 | // host answers `dismissed`. Holding the tick would hide that push for as long as the tab |
| 97 | // stayed open, and no refresh would recover it: every later answer says `dismissed` too. |
| 98 | const settled = settleFileViewedOverlay( |
| 99 | new Map([["c.ts", true]]), |
| 100 | states, |
| 101 | NOTHING_PENDING, |
| 102 | new Set(["c.ts"]), |
| 103 | ); |
| 104 | expect(settled.size).toBe(0); |
| 105 | expect(isFileViewed("c.ts", states, settled)).toBe(false); |
| 106 | expect(isStaleViewedState(states?.get("c.ts"))).toBe(true); |
| 107 | }); |
| 108 | |
| 109 | it("holds a press made since the read that would otherwise answer for it", () => { |
| 110 | const overlay = new Map([["c.ts", true]]); |
| 111 | const settled = settleFileViewedOverlay(overlay, states, new Set(["c.ts"]), new Set(["c.ts"])); |
| 112 | expect(settled.get("c.ts")).toBe(true); |
| 113 | }); |
| 114 | |
| 115 | it("holds everything until the host has answered at all", () => { |
| 116 | const overlay = new Map([["a.ts", true]]); |
| 117 | expect(settleFileViewedOverlay(overlay, null, NOTHING_PENDING, NOTHING_ANSWERED)).toBe(overlay); |
| 118 | }); |
| 119 | }); |
| 120 | |
| 121 | describe("toFileViewedBatch", () => { |
| 122 | it("carries both directions in one batch", () => { |
| 123 | expect( |
| 124 | toFileViewedBatch( |
| 125 | new Map([ |
| 126 | ["a.ts", false], |
| 127 | ["b.ts", true], |
| 128 | ]), |
| 129 | ), |
| 130 | ).toEqual([ |
| 131 | { path: "a.ts", viewed: false }, |
| 132 | { path: "b.ts", viewed: true }, |
| 133 | ]); |
| 134 | }); |
| 135 | }); |
| 136 | |
| 137 | describe("revertFileViewedOverlay", () => { |
| 138 | const batch = [ |
| 139 | { path: "a.ts", viewed: true }, |
| 140 | { path: "b.ts", viewed: false }, |
| 141 | ]; |
| 142 | const both = new Set(["a.ts", "b.ts"]); |
| 143 | |
| 144 | it("puts the checkbox back to the host's answer for everything the request answers for", () => { |
| 145 | const overlay = new Map([ |
| 146 | ["a.ts", true], |
| 147 | ["b.ts", false], |
| 148 | ]); |
| 149 | expect(revertFileViewedOverlay(overlay, batch, both).size).toBe(0); |
| 150 | }); |
| 151 | |
| 152 | it("leaves a press the reader made after the request went out", () => { |
| 153 | // The second press is waiting on a flush of its own, so the first one failing says nothing |
| 154 | // about it. |
| 155 | const overlay = new Map([ |
| 156 | ["a.ts", false], |
| 157 | ["b.ts", false], |
| 158 | ]); |
| 159 | const reverted = revertFileViewedOverlay(overlay, batch, new Set(["b.ts"])); |
| 160 | expect([...reverted]).toEqual([["a.ts", false]]); |
| 161 | }); |
| 162 | |
| 163 | it("leaves a path a later request took over, even pressed the same way", () => { |
| 164 | // Both requests carry `a.ts` as viewed, so the value cannot tell them apart. The later one |
| 165 | // owns the path now and is the one that answers for it. |
| 166 | const overlay = new Map([["a.ts", true]]); |
| 167 | expect(revertFileViewedOverlay(overlay, batch, new Set(["b.ts"]))).toBe(overlay); |
| 168 | }); |
| 169 | |
| 170 | it("leaves a path the request never carried", () => { |
| 171 | const overlay = new Map([["c.ts", true]]); |
| 172 | expect(revertFileViewedOverlay(overlay, batch, both)).toBe(overlay); |
| 173 | }); |
| 174 | }); |