| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Client} from 'fb-watchman'; |
| b69ab31 | | | 9 | import type {RepoInfo} from 'isl/src/types'; |
| b69ab31 | | | 10 | import type {EdenFSNotifications} from '../edenFsNotifications'; |
| b69ab31 | | | 11 | import type {ServerPlatform} from '../serverPlatform'; |
| b69ab31 | | | 12 | import type {Watchman} from '../watchman'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | import fs from 'node:fs'; |
| b69ab31 | | | 15 | import {TypedEventEmitter} from 'shared/TypedEventEmitter'; |
| b69ab31 | | | 16 | import {mockLogger} from 'shared/testUtils'; |
| b69ab31 | | | 17 | import {Internal} from '../Internal'; |
| b69ab31 | | | 18 | import {PageFocusTracker} from '../PageFocusTracker'; |
| b69ab31 | | | 19 | import {WatchForChanges} from '../WatchForChanges'; |
| b69ab31 | | | 20 | import {makeServerSideTracker} from '../analytics/serverSideTracker'; |
| b69ab31 | | | 21 | import {ONE_MINUTE_MS} from '../constants'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | const mockTracker = makeServerSideTracker( |
| b69ab31 | | | 24 | mockLogger, |
| b69ab31 | | | 25 | {platformName: 'test'} as ServerPlatform, |
| b69ab31 | | | 26 | '0.1', |
| b69ab31 | | | 27 | jest.fn(), |
| b69ab31 | | | 28 | ); |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | jest.mock('fb-watchman', () => { |
| b69ab31 | | | 31 | // make a fake watchman object which returns () => undefined for every property |
| b69ab31 | | | 32 | // so we don't need to manually mock every function watchman provides. |
| b69ab31 | | | 33 | class FakeWatchman { |
| b69ab31 | | | 34 | constructor() { |
| b69ab31 | | | 35 | return new Proxy(this, { |
| b69ab31 | | | 36 | get: () => () => undefined, |
| b69ab31 | | | 37 | }); |
| b69ab31 | | | 38 | } |
| b69ab31 | | | 39 | } |
| b69ab31 | | | 40 | return { |
| b69ab31 | | | 41 | Client: FakeWatchman, |
| b69ab31 | | | 42 | }; |
| b69ab31 | | | 43 | }); |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | describe('WatchForChanges - watchman', () => { |
| b69ab31 | | | 46 | const mockInfo: RepoInfo = { |
| b69ab31 | | | 47 | type: 'success', |
| b69ab31 | | | 48 | command: 'sl', |
| b69ab31 | | | 49 | repoRoot: '/testRepo', |
| b69ab31 | | | 50 | dotdir: '/testRepo/.sl', |
| b69ab31 | | | 51 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 52 | pullRequestDomain: undefined, |
| b69ab31 | | | 53 | isEdenFs: false, |
| b69ab31 | | | 54 | }; |
| b69ab31 | | | 55 | |
| b69ab31 | | | 56 | let focusTracker: PageFocusTracker; |
| b69ab31 | | | 57 | const onChange = jest.fn(); |
| b69ab31 | | | 58 | let watch: WatchForChanges; |
| b69ab31 | | | 59 | |
| b69ab31 | | | 60 | beforeEach(() => { |
| b69ab31 | | | 61 | Internal.fetchFeatureFlag = jest.fn().mockImplementation((_ctx, _flag) => { |
| b69ab31 | | | 62 | return Promise.resolve(false); |
| b69ab31 | | | 63 | }); |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | const ctx = { |
| b69ab31 | | | 66 | cmd: 'sl', |
| b69ab31 | | | 67 | cwd: '/path/to/cwd', |
| b69ab31 | | | 68 | logger: mockLogger, |
| b69ab31 | | | 69 | tracker: mockTracker, |
| b69ab31 | | | 70 | }; |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | jest.useFakeTimers(); |
| b69ab31 | | | 73 | onChange.mockClear(); |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | jest.spyOn(fs.promises, 'realpath').mockImplementation((path, _opts) => { |
| b69ab31 | | | 76 | return Promise.resolve(path as string); |
| b69ab31 | | | 77 | }); |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | focusTracker = new PageFocusTracker(); |
| b69ab31 | | | 80 | watch = new WatchForChanges(mockInfo, focusTracker, onChange, ctx); |
| b69ab31 | | | 81 | // pretend watchman is not running for most tests |
| b69ab31 | | | 82 | (watch.watchman.status as string) = 'errored'; |
| b69ab31 | | | 83 | // change is triggered on first subscription |
| b69ab31 | | | 84 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 85 | onChange.mockClear(); |
| b69ab31 | | | 86 | }); |
| b69ab31 | | | 87 | |
| b69ab31 | | | 88 | afterEach(() => { |
| b69ab31 | | | 89 | watch.dispose(); |
| b69ab31 | | | 90 | }); |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | afterAll(() => { |
| b69ab31 | | | 93 | jest.useRealTimers(); |
| b69ab31 | | | 94 | }); |
| b69ab31 | | | 95 | |
| b69ab31 | | | 96 | it('polls for changes on an interval', () => { |
| b69ab31 | | | 97 | // |-----------1-----------2-----------3-----------4-----------5-----------6-----------7-----------8-----------9----------10----------11----------12----------13----------14----------15----------16 (minutes) |
| b69ab31 | | | 98 | // ^ poll |
| b69ab31 | | | 99 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 100 | jest.advanceTimersByTime(15.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 101 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 102 | expect(onChange).toHaveBeenCalledWith('everything', undefined); |
| b69ab31 | | | 103 | }); |
| b69ab31 | | | 104 | |
| b69ab31 | | | 105 | it('polls more often when the page is visible', () => { |
| b69ab31 | | | 106 | // |-----------1-----------2-----------3-----------4---- (minutes) |
| b69ab31 | | | 107 | // | ^ | ^ (poll) |
| b69ab31 | | | 108 | // 0 poll 2 (times fetched) |
| b69ab31 | | | 109 | focusTracker.setState('page0', 'visible'); |
| b69ab31 | | | 110 | onChange.mockClear(); // ignore immediate visibility change poll |
| b69ab31 | | | 111 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 112 | jest.advanceTimersByTime(1.0 * ONE_MINUTE_MS); |
| b69ab31 | | | 113 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 114 | jest.advanceTimersByTime(1.25 * ONE_MINUTE_MS); |
| b69ab31 | | | 115 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 116 | }); |
| b69ab31 | | | 117 | it('polls more often when the page is focused', () => { |
| b69ab31 | | | 118 | // |-----------1-----------2---- (minutes) |
| b69ab31 | | | 119 | // | ^| ^| ^ ^ ^ ^ ^ ^ ^ (poll) |
| b69ab31 | | | 120 | // 0 1 (times fetched) |
| b69ab31 | | | 121 | focusTracker.setState('page0', 'focused'); |
| b69ab31 | | | 122 | onChange.mockClear(); // ignore immediate focus change poll |
| b69ab31 | | | 123 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 124 | jest.advanceTimersByTime(0.25 * ONE_MINUTE_MS); |
| b69ab31 | | | 125 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 126 | jest.advanceTimersByTime(0.25 * ONE_MINUTE_MS); |
| b69ab31 | | | 127 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 128 | }); |
| b69ab31 | | | 129 | |
| b69ab31 | | | 130 | it('polls the moment visibility is gained', () => { |
| b69ab31 | | | 131 | // |-----------1-----*-----2-----------3-----------4----------- (minutes) |
| b69ab31 | | | 132 | // | || | ^ | ^ (poll) |
| b69ab31 | | | 133 | // 0 |1 1 2 3 (times fetched) |
| b69ab31 | | | 134 | // visible |
| b69ab31 | | | 135 | // (resets interval at 2min) |
| b69ab31 | | | 136 | jest.advanceTimersByTime(1.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 137 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 138 | focusTracker.setState('page0', 'visible'); |
| b69ab31 | | | 139 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 140 | jest.advanceTimersByTime(0.75 * ONE_MINUTE_MS); |
| b69ab31 | | | 141 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 142 | jest.advanceTimersByTime(1.25 * ONE_MINUTE_MS); |
| b69ab31 | | | 143 | expect(onChange).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 144 | }); |
| b69ab31 | | | 145 | |
| b69ab31 | | | 146 | it('debounces additional polling when focus is gained', () => { |
| b69ab31 | | | 147 | // |-----------1-----*--*--2-*---------3-----------4----------- (minutes) |
| b69ab31 | | | 148 | // | || | || ^ | ^ (poll) |
| b69ab31 | | | 149 | // 0 |1 | |1 2 3 (times fetched) |
| b69ab31 | | | 150 | // visible^ | ^visible (debounce) |
| b69ab31 | | | 151 | // hide |
| b69ab31 | | | 152 | jest.advanceTimersByTime(1.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 153 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 154 | focusTracker.setState('page0', 'visible'); |
| b69ab31 | | | 155 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 156 | jest.advanceTimersByTime(0.1 * ONE_MINUTE_MS); |
| b69ab31 | | | 157 | focusTracker.setState('page0', 'hidden'); |
| b69ab31 | | | 158 | jest.advanceTimersByTime(0.15 * ONE_MINUTE_MS); // 15 seconds (0.25 min) throttle for focus |
| b69ab31 | | | 159 | focusTracker.setState('page0', 'visible'); // debounced to not fetch again |
| b69ab31 | | | 160 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 161 | jest.advanceTimersByTime(0.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 162 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 163 | jest.advanceTimersByTime(1.25 * ONE_MINUTE_MS); |
| b69ab31 | | | 164 | expect(onChange).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 165 | }); |
| b69ab31 | | | 166 | |
| b69ab31 | | | 167 | it('polls at higher frequency if any page is focused', () => { |
| b69ab31 | | | 168 | // |-----------1-----*-*---2-----------3-- (minutes) |
| b69ab31 | | | 169 | // | ||| ^ ^ ^ ^ ^ (poll) |
| b69ab31 | | | 170 | // 0 |1| 2 3 (times fetched) |
| b69ab31 | | | 171 | // hidden | |hidden |
| b69ab31 | | | 172 | // focused^ |hidden |
| b69ab31 | | | 173 | // hidden ^focused |
| b69ab31 | | | 174 | jest.advanceTimersByTime(1.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 175 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 176 | focusTracker.setState('page0', 'hidden'); |
| b69ab31 | | | 177 | focusTracker.setState('page1', 'hidden'); |
| b69ab31 | | | 178 | focusTracker.setState('page2', 'hidden'); |
| b69ab31 | | | 179 | focusTracker.setState('page1', 'focused'); |
| b69ab31 | | | 180 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 181 | jest.advanceTimersByTime(0.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 182 | expect(onChange).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 183 | focusTracker.setState('page0', 'focused'); // since 1 is still focused, this does not immediately poll |
| b69ab31 | | | 184 | focusTracker.setState('page1', 'hidden'); |
| b69ab31 | | | 185 | expect(onChange).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 186 | jest.advanceTimersByTime(0.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 187 | expect(onChange).toHaveBeenCalledTimes(3); |
| b69ab31 | | | 188 | }); |
| b69ab31 | | | 189 | |
| b69ab31 | | | 190 | it('clears out previous intervals', () => { |
| b69ab31 | | | 191 | // |-----------1-----*-----2-----*-----3--- ... --7-----------8-- (minutes) |
| b69ab31 | | | 192 | // | | ^ ^ ^ (poll) |
| b69ab31 | | | 193 | // 0 1 2 3 4 (times fetched) |
| b69ab31 | | | 194 | // focused^ ^hidden |
| b69ab31 | | | 195 | jest.advanceTimersByTime(1.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 196 | expect(onChange).not.toHaveBeenCalled(); |
| b69ab31 | | | 197 | focusTracker.setState('page0', 'focused'); |
| b69ab31 | | | 198 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 199 | jest.advanceTimersByTime(1 * ONE_MINUTE_MS); |
| b69ab31 | | | 200 | expect(onChange).toHaveBeenCalledTimes(3); |
| b69ab31 | | | 201 | focusTracker.setState('page0', 'hidden'); |
| b69ab31 | | | 202 | // fast focused interval is removed, and we revert to 15 min interval |
| b69ab31 | | | 203 | jest.advanceTimersByTime(15 * ONE_MINUTE_MS); |
| b69ab31 | | | 204 | expect(onChange).toHaveBeenCalledTimes(4); |
| b69ab31 | | | 205 | }); |
| b69ab31 | | | 206 | |
| b69ab31 | | | 207 | it('polls less when watchman appears healthy', () => { |
| b69ab31 | | | 208 | // |*----------1-----------2-----------3-----------4-----------5-----------6-----------7-----------8-----------9----------10----------11----------12----------13----------14----------15----------16 (minutes) |
| b69ab31 | | | 209 | // | ^ poll |
| b69ab31 | | | 210 | // focused |
| b69ab31 | | | 211 | |
| b69ab31 | | | 212 | (watch.watchman.status as string) = 'healthy'; |
| b69ab31 | | | 213 | focusTracker.setState('page0', 'focused'); |
| b69ab31 | | | 214 | expect(onChange).toHaveBeenCalledTimes(0); |
| b69ab31 | | | 215 | jest.advanceTimersByTime(15.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 216 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 217 | }); |
| b69ab31 | | | 218 | |
| b69ab31 | | | 219 | it('results from watchman reset polling timers', async () => { |
| b69ab31 | | | 220 | // |-----------1-----------2----- ... ----15----------16 (minutes) |
| b69ab31 | | | 221 | // | ^ (poll) |
| b69ab31 | | | 222 | // watchman result |
| b69ab31 | | | 223 | |
| b69ab31 | | | 224 | const ctx = { |
| b69ab31 | | | 225 | cmd: 'sl', |
| b69ab31 | | | 226 | cwd: '/path/to/cwd', |
| b69ab31 | | | 227 | logger: mockLogger, |
| b69ab31 | | | 228 | tracker: mockTracker, |
| b69ab31 | | | 229 | }; |
| b69ab31 | | | 230 | watch.dispose(); // don't use pre-existing WatchForChanges |
| b69ab31 | | | 231 | const emitter1 = new TypedEventEmitter(); |
| b69ab31 | | | 232 | const emitter2 = new TypedEventEmitter(); |
| b69ab31 | | | 233 | const mockWatchman: Watchman = { |
| b69ab31 | | | 234 | client: {} as unknown as Client, |
| b69ab31 | | | 235 | status: 'initializing', |
| b69ab31 | | | 236 | watchDirectoryRecursive: jest |
| b69ab31 | | | 237 | .fn() |
| b69ab31 | | | 238 | .mockImplementationOnce(() => { |
| b69ab31 | | | 239 | return Promise.resolve({emitter: emitter1}); |
| b69ab31 | | | 240 | }) |
| b69ab31 | | | 241 | .mockImplementationOnce(() => { |
| b69ab31 | | | 242 | return Promise.resolve({emitter: emitter2}); |
| b69ab31 | | | 243 | }), |
| b69ab31 | | | 244 | unwatch: jest.fn(), |
| b69ab31 | | | 245 | } as unknown as Watchman; |
| b69ab31 | | | 246 | |
| b69ab31 | | | 247 | watch = new WatchForChanges(mockInfo, focusTracker, onChange, ctx, mockWatchman); |
| b69ab31 | | | 248 | await watch.waitForDirstateSubscriptionReady(); |
| b69ab31 | | | 249 | await watch.setupSubscriptions(ctx); |
| b69ab31 | | | 250 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 251 | onChange.mockClear(); |
| b69ab31 | | | 252 | |
| b69ab31 | | | 253 | // wait an actual async tick so mock subscriptions are set up |
| b69ab31 | | | 254 | const setImmediate = jest.requireActual('timers').setImmediate; |
| b69ab31 | | | 255 | await new Promise(res => setImmediate(() => res(undefined))); |
| b69ab31 | | | 256 | |
| b69ab31 | | | 257 | jest.advanceTimersByTime(1.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 258 | expect(onChange).toHaveBeenCalledTimes(0); |
| b69ab31 | | | 259 | emitter2.emit('change', undefined); |
| b69ab31 | | | 260 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 261 | expect(onChange).toHaveBeenCalledWith('uncommitted changes'); |
| b69ab31 | | | 262 | jest.advanceTimersByTime(14.0 * ONE_MINUTE_MS); // original timer didn't cause a poll |
| b69ab31 | | | 263 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 264 | jest.advanceTimersByTime(2.0 * ONE_MINUTE_MS); // 15 minutes after watchman change, a new poll occurred |
| b69ab31 | | | 265 | expect(onChange).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 266 | expect(onChange).toHaveBeenCalledWith('everything', undefined); |
| b69ab31 | | | 267 | }); |
| b69ab31 | | | 268 | }); |
| b69ab31 | | | 269 | |
| b69ab31 | | | 270 | describe('WatchForChanges - edenfs', () => { |
| b69ab31 | | | 271 | const mockInfo: RepoInfo = { |
| b69ab31 | | | 272 | type: 'success', |
| b69ab31 | | | 273 | command: 'sl', |
| b69ab31 | | | 274 | repoRoot: '/testRepo', |
| b69ab31 | | | 275 | dotdir: '/testRepo/.sl', |
| b69ab31 | | | 276 | codeReviewSystem: {type: 'unknown'}, |
| b69ab31 | | | 277 | pullRequestDomain: undefined, |
| b69ab31 | | | 278 | isEdenFs: true, |
| b69ab31 | | | 279 | }; |
| b69ab31 | | | 280 | |
| b69ab31 | | | 281 | let focusTracker: PageFocusTracker; |
| b69ab31 | | | 282 | const onChange = jest.fn(); |
| b69ab31 | | | 283 | let watch: WatchForChanges; |
| b69ab31 | | | 284 | let emitter1: TypedEventEmitter<string, unknown>; |
| b69ab31 | | | 285 | |
| b69ab31 | | | 286 | beforeEach(async () => { |
| b69ab31 | | | 287 | Internal.fetchFeatureFlag = jest.fn().mockImplementation((_ctx, _flag) => { |
| b69ab31 | | | 288 | return Promise.resolve(true); |
| b69ab31 | | | 289 | }); |
| b69ab31 | | | 290 | const ctx = { |
| b69ab31 | | | 291 | cmd: 'sl', |
| b69ab31 | | | 292 | cwd: '/path/to/cwd', |
| b69ab31 | | | 293 | logger: mockLogger, |
| b69ab31 | | | 294 | tracker: mockTracker, |
| b69ab31 | | | 295 | }; |
| b69ab31 | | | 296 | |
| b69ab31 | | | 297 | jest.useFakeTimers(); |
| b69ab31 | | | 298 | onChange.mockClear(); |
| b69ab31 | | | 299 | |
| b69ab31 | | | 300 | jest.spyOn(fs.promises, 'realpath').mockImplementation((path, _opts) => { |
| b69ab31 | | | 301 | return Promise.resolve(path as string); |
| b69ab31 | | | 302 | }); |
| b69ab31 | | | 303 | |
| b69ab31 | | | 304 | emitter1 = new TypedEventEmitter(); |
| b69ab31 | | | 305 | const mockEdenFS: EdenFSNotifications = { |
| b69ab31 | | | 306 | watchDirectoryRecursive: jest |
| b69ab31 | | | 307 | .fn() |
| b69ab31 | | | 308 | .mockImplementation( |
| b69ab31 | | | 309 | (_localDirectoryPath, _rawSubscriptionName, _subscriptionOptions, callback) => { |
| b69ab31 | | | 310 | emitter1.on('change', change => { |
| b69ab31 | | | 311 | callback(null, change); |
| b69ab31 | | | 312 | }); |
| b69ab31 | | | 313 | emitter1.on('error', error => { |
| b69ab31 | | | 314 | callback(error, null); |
| b69ab31 | | | 315 | }); |
| b69ab31 | | | 316 | emitter1.on('close', () => { |
| b69ab31 | | | 317 | callback(null, null); |
| b69ab31 | | | 318 | }); |
| b69ab31 | | | 319 | return Promise.resolve(emitter1); |
| b69ab31 | | | 320 | }, |
| b69ab31 | | | 321 | ), |
| b69ab31 | | | 322 | unwatch: jest.fn(), |
| b69ab31 | | | 323 | } as unknown as EdenFSNotifications; |
| b69ab31 | | | 324 | |
| b69ab31 | | | 325 | focusTracker = new PageFocusTracker(); |
| b69ab31 | | | 326 | watch = new WatchForChanges(mockInfo, focusTracker, onChange, ctx, undefined, mockEdenFS); |
| b69ab31 | | | 327 | await watch.waitForDirstateSubscriptionReady(); |
| b69ab31 | | | 328 | |
| b69ab31 | | | 329 | // change is triggered on first subscription |
| b69ab31 | | | 330 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 331 | onChange.mockClear(); |
| b69ab31 | | | 332 | }); |
| b69ab31 | | | 333 | |
| b69ab31 | | | 334 | afterEach(() => { |
| b69ab31 | | | 335 | watch.dispose(); |
| b69ab31 | | | 336 | }); |
| b69ab31 | | | 337 | |
| b69ab31 | | | 338 | afterAll(() => { |
| b69ab31 | | | 339 | jest.useRealTimers(); |
| b69ab31 | | | 340 | }); |
| b69ab31 | | | 341 | |
| b69ab31 | | | 342 | it('Handles results from edenfs', async () => { |
| b69ab31 | | | 343 | // |-----------1-----------2-----------3-----------4-----------5-----------6-----------7-----------8-----------9----------10----------11----------12----------13----------14----------15----------16 (minutes) |
| b69ab31 | | | 344 | // ^ poll |
| b69ab31 | | | 345 | const ctx = { |
| b69ab31 | | | 346 | cmd: 'sl', |
| b69ab31 | | | 347 | cwd: '/path/to/cwd', |
| b69ab31 | | | 348 | logger: mockLogger, |
| b69ab31 | | | 349 | tracker: mockTracker, |
| b69ab31 | | | 350 | }; |
| b69ab31 | | | 351 | |
| b69ab31 | | | 352 | focusTracker.setState('page', 'visible'); |
| b69ab31 | | | 353 | await watch.setupSubscriptions(ctx); |
| b69ab31 | | | 354 | |
| b69ab31 | | | 355 | // wait an actual async tick so mock subscriptions are set up |
| b69ab31 | | | 356 | const setImmediate = jest.requireActual('timers').setImmediate; |
| b69ab31 | | | 357 | await new Promise(res => setImmediate(() => res(undefined))); |
| b69ab31 | | | 358 | |
| b69ab31 | | | 359 | jest.advanceTimersByTime(1.5 * ONE_MINUTE_MS); |
| b69ab31 | | | 360 | expect(onChange).toHaveBeenCalledTimes(0); |
| b69ab31 | | | 361 | emitter1.emit('change', {changes: [1]}); |
| b69ab31 | | | 362 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 363 | jest.advanceTimersByTime(13.0 * ONE_MINUTE_MS); // original timer didn't cause a poll |
| b69ab31 | | | 364 | expect(onChange).toHaveBeenCalledTimes(1); |
| b69ab31 | | | 365 | jest.advanceTimersByTime(3.0 * ONE_MINUTE_MS); // 15 minutes after watchman change, a new poll occurred |
| b69ab31 | | | 366 | expect(onChange).toHaveBeenCalledTimes(2); |
| b69ab31 | | | 367 | expect(onChange).toHaveBeenCalledWith('everything', undefined); |
| b69ab31 | | | 368 | |
| b69ab31 | | | 369 | focusTracker.setState('page', 'hidden'); |
| b69ab31 | | | 370 | jest.advanceTimersByTime(180 * ONE_MINUTE_MS); |
| b69ab31 | | | 371 | expect(onChange).toHaveBeenCalledTimes(3); |
| b69ab31 | | | 372 | expect(onChange).toHaveBeenCalledWith('everything', undefined); |
| b69ab31 | | | 373 | }); |
| b69ab31 | | | 374 | }); |