|
1 | | -import { describe, expect, test } from "bun:test"; |
| 1 | +import { describe, expect, jest, test } from "bun:test"; |
2 | 2 |
|
3 | 3 | import { watchGitBranch } from "./workspace-watch"; |
4 | 4 |
|
5 | | -function fakeClock(): { |
6 | | - readonly schedule: (tick: () => void, intervalMs: number) => () => void; |
7 | | - readonly tick: () => void; |
8 | | -} { |
9 | | - let ticker: (() => void) | null = null; |
10 | | - return { |
11 | | - schedule: (fn) => { |
12 | | - ticker = fn; |
13 | | - return () => { |
14 | | - ticker = null; |
15 | | - }; |
16 | | - }, |
17 | | - tick: () => ticker?.(), |
18 | | - }; |
| 5 | +/** The retired idle poll. An idle window of several of these must not spawn git. */ |
| 6 | +const FORMER_POLL_INTERVAL_MS = 5_000; |
| 7 | + |
| 8 | +async function flush(): Promise<void> { |
| 9 | + await Promise.resolve(); |
| 10 | + await Promise.resolve(); |
19 | 11 | } |
20 | 12 |
|
21 | 13 | describe("watchGitBranch", () => { |
22 | | - test("reports the branch on the first lookup, before any tick", async () => { |
| 14 | + test("reports the branch on the first lookup", async () => { |
23 | 15 | const seen: (string | null)[] = []; |
24 | | - const clock = fakeClock(); |
25 | | - const stop = watchGitBranch({ |
| 16 | + const watch = watchGitBranch({ |
26 | 17 | cwd: "/repo", |
27 | 18 | onBranch: (b) => seen.push(b), |
28 | 19 | fetchBranch: async () => "main", |
29 | | - schedule: clock.schedule, |
30 | 20 | }); |
31 | | - await Promise.resolve(); |
| 21 | + await flush(); |
32 | 22 | expect(seen).toEqual(["main"]); |
33 | | - stop(); |
| 23 | + watch.stop(); |
34 | 24 | }); |
35 | 25 |
|
36 | | - test("skips a tick while the previous lookup is still outstanding", async () => { |
| 26 | + test("one refresh during an outstanding lookup runs a single follow-up and reports the later branch", async () => { |
37 | 27 | let calls = 0; |
38 | 28 | const pending: ((branch: string | null) => void)[] = []; |
39 | | - const clock = fakeClock(); |
40 | | - const stop = watchGitBranch({ |
| 29 | + const seen: (string | null)[] = []; |
| 30 | + const watch = watchGitBranch({ |
41 | 31 | cwd: "/repo", |
42 | | - onBranch: () => undefined, |
| 32 | + onBranch: (b) => seen.push(b), |
43 | 33 | fetchBranch: () => { |
44 | 34 | calls += 1; |
45 | 35 | return new Promise((resolve) => { |
46 | 36 | pending.push(resolve); |
47 | 37 | }); |
48 | 38 | }, |
49 | | - schedule: clock.schedule, |
50 | 39 | }); |
51 | | - clock.tick(); |
52 | | - clock.tick(); |
| 40 | + watch.refresh(); |
| 41 | + watch.refresh(); |
53 | 42 | expect(calls).toBe(1); |
54 | 43 |
|
55 | 44 | pending[0]?.("main"); |
56 | | - await Promise.resolve(); |
57 | | - clock.tick(); |
| 45 | + await flush(); |
| 46 | + expect(calls).toBe(2); |
| 47 | + expect(seen).toEqual(["main"]); |
| 48 | + |
| 49 | + pending[1]?.("topic"); |
| 50 | + await flush(); |
| 51 | + expect(calls).toBe(2); |
| 52 | + expect(seen).toEqual(["main", "topic"]); |
| 53 | + watch.stop(); |
| 54 | + }); |
| 55 | + |
| 56 | + test("a throwing onBranch does not stick the guard, so a later refresh looks up again", async () => { |
| 57 | + let calls = 0; |
| 58 | + const pending: ((branch: string | null) => void)[] = []; |
| 59 | + const watch = watchGitBranch({ |
| 60 | + cwd: "/repo", |
| 61 | + onBranch: () => { |
| 62 | + throw new Error("paint failed"); |
| 63 | + }, |
| 64 | + fetchBranch: () => { |
| 65 | + calls += 1; |
| 66 | + return new Promise((resolve) => { |
| 67 | + pending.push(resolve); |
| 68 | + }); |
| 69 | + }, |
| 70 | + }); |
| 71 | + pending[0]?.("main"); |
| 72 | + await flush(); |
| 73 | + expect(calls).toBe(1); |
| 74 | + |
| 75 | + watch.refresh(); |
| 76 | + expect(calls).toBe(2); |
| 77 | + watch.stop(); |
| 78 | + }); |
| 79 | + |
| 80 | + test("a follow-up armed before onBranch throws still runs", async () => { |
| 81 | + let calls = 0; |
| 82 | + const pending: ((branch: string | null) => void)[] = []; |
| 83 | + const watch = watchGitBranch({ |
| 84 | + cwd: "/repo", |
| 85 | + onBranch: () => { |
| 86 | + throw new Error("paint failed"); |
| 87 | + }, |
| 88 | + fetchBranch: () => { |
| 89 | + calls += 1; |
| 90 | + return new Promise((resolve) => { |
| 91 | + pending.push(resolve); |
| 92 | + }); |
| 93 | + }, |
| 94 | + }); |
| 95 | + watch.refresh(); |
| 96 | + expect(calls).toBe(1); |
| 97 | + pending[0]?.("main"); |
| 98 | + await flush(); |
58 | 99 | expect(calls).toBe(2); |
59 | | - stop(); |
| 100 | + watch.stop(); |
| 101 | + }); |
| 102 | + |
| 103 | + test("a null result after a known branch is not reported", async () => { |
| 104 | + const seen: (string | null)[] = []; |
| 105 | + let branch: string | null = "main"; |
| 106 | + const watch = watchGitBranch({ |
| 107 | + cwd: "/repo", |
| 108 | + onBranch: (b) => seen.push(b), |
| 109 | + fetchBranch: async () => branch, |
| 110 | + }); |
| 111 | + await flush(); |
| 112 | + branch = null; |
| 113 | + watch.refresh(); |
| 114 | + await flush(); |
| 115 | + expect(seen).toEqual(["main"]); |
| 116 | + watch.stop(); |
60 | 117 | }); |
61 | 118 |
|
62 | 119 | test("a lookup that lands after stop is dropped", async () => { |
63 | 120 | const seen: (string | null)[] = []; |
64 | 121 | const pending: ((branch: string | null) => void)[] = []; |
65 | | - const clock = fakeClock(); |
66 | | - const stop = watchGitBranch({ |
| 122 | + const watch = watchGitBranch({ |
67 | 123 | cwd: "/repo", |
68 | 124 | onBranch: (b) => seen.push(b), |
69 | 125 | fetchBranch: () => |
70 | 126 | new Promise((resolve) => { |
71 | 127 | pending.push(resolve); |
72 | 128 | }), |
73 | | - schedule: clock.schedule, |
74 | 129 | }); |
75 | | - stop(); |
| 130 | + watch.stop(); |
76 | 131 | pending[0]?.("main"); |
77 | | - await Promise.resolve(); |
| 132 | + await flush(); |
78 | 133 | expect(seen).toEqual([]); |
79 | 134 | }); |
80 | 135 |
|
81 | 136 | test("a failed lookup neither reports nor wedges the guard", async () => { |
82 | 137 | let calls = 0; |
83 | 138 | const seen: (string | null)[] = []; |
84 | | - const clock = fakeClock(); |
85 | | - const stop = watchGitBranch({ |
| 139 | + const watch = watchGitBranch({ |
86 | 140 | cwd: "/repo", |
87 | 141 | onBranch: (b) => seen.push(b), |
88 | 142 | fetchBranch: async () => { |
89 | 143 | calls += 1; |
90 | 144 | throw new Error("git exploded"); |
91 | 145 | }, |
92 | | - schedule: clock.schedule, |
93 | 146 | }); |
94 | | - await Promise.resolve(); |
| 147 | + await flush(); |
95 | 148 | expect(seen).toEqual([]); |
96 | | - clock.tick(); |
| 149 | + watch.refresh(); |
| 150 | + await flush(); |
97 | 151 | expect(calls).toBe(2); |
98 | | - stop(); |
| 152 | + watch.stop(); |
| 153 | + }); |
| 154 | + |
| 155 | + test("an idle window of several former intervals spawns git zero times, and refresh still updates", async () => { |
| 156 | + jest.useFakeTimers(); |
| 157 | + try { |
| 158 | + let calls = 0; |
| 159 | + let branch = "main"; |
| 160 | + const seen: (string | null)[] = []; |
| 161 | + const watch = watchGitBranch({ |
| 162 | + cwd: "/repo", |
| 163 | + onBranch: (b) => seen.push(b), |
| 164 | + fetchBranch: async () => { |
| 165 | + calls += 1; |
| 166 | + return branch; |
| 167 | + }, |
| 168 | + }); |
| 169 | + await flush(); |
| 170 | + expect(seen).toEqual(["main"]); |
| 171 | + const afterStart = calls; |
| 172 | + |
| 173 | + jest.advanceTimersByTime(FORMER_POLL_INTERVAL_MS * 6); |
| 174 | + await flush(); |
| 175 | + expect(calls).toBe(afterStart); |
| 176 | + |
| 177 | + branch = "topic"; |
| 178 | + watch.refresh(); |
| 179 | + await flush(); |
| 180 | + expect(calls).toBe(afterStart + 1); |
| 181 | + expect(seen).toEqual(["main", "topic"]); |
| 182 | + watch.stop(); |
| 183 | + } finally { |
| 184 | + jest.useRealTimers(); |
| 185 | + } |
99 | 186 | }); |
100 | 187 | }); |
0 commit comments