| 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 {ServerSideTracker} from '../analytics/serverSideTracker'; |
| b69ab31 | | | 9 | import type {FullTrackData} from '../analytics/types'; |
| b69ab31 | | | 10 | import type {ServerPlatform} from '../serverPlatform'; |
| b69ab31 | | | 11 | import type {RepositoryContext} from '../serverTypes'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import * as ejeca from 'shared/ejeca'; |
| b69ab31 | | | 14 | import {mockLogger} from 'shared/testUtils'; |
| b69ab31 | | | 15 | import {defer} from 'shared/utils'; |
| b69ab31 | | | 16 | import {Repository} from '../Repository'; |
| b69ab31 | | | 17 | import {makeServerSideTracker} from '../analytics/serverSideTracker'; |
| b69ab31 | | | 18 | import {setConfigOverrideForTests} from '../commands'; |
| b69ab31 | | | 19 | |
| b69ab31 | | | 20 | /** Matches any non-empty string */ |
| b69ab31 | | | 21 | const anyActualString = expect.stringMatching(/.+/); |
| 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 | const mockCtx: RepositoryContext = { |
| b69ab31 | | | 31 | logger: mockLogger, |
| b69ab31 | | | 32 | tracker: mockTracker, |
| b69ab31 | | | 33 | cwd: '/test', |
| b69ab31 | | | 34 | cmd: 'sl', |
| b69ab31 | | | 35 | }; |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | jest.mock('../WatchForChanges', () => { |
| b69ab31 | | | 38 | class MockWatchForChanges { |
| b69ab31 | | | 39 | dispose = jest.fn(); |
| b69ab31 | | | 40 | } |
| b69ab31 | | | 41 | return {WatchForChanges: MockWatchForChanges}; |
| b69ab31 | | | 42 | }); |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | function mockEjeca( |
| b69ab31 | | | 45 | cmds: Array<[RegExp, (() => {stdout: string} | Error) | {stdout: string} | Error]>, |
| b69ab31 | | | 46 | ) { |
| b69ab31 | | | 47 | return jest.spyOn(ejeca, 'ejeca').mockImplementation(((cmd: string, args: Array<string>) => { |
| b69ab31 | | | 48 | const argStr = cmd + ' ' + args?.join(' '); |
| b69ab31 | | | 49 | const ejecaOther = { |
| b69ab31 | | | 50 | kill: jest.fn(), |
| b69ab31 | | | 51 | on: jest.fn((event, cb) => { |
| b69ab31 | | | 52 | // immediately call exit cb to teardown timeout |
| b69ab31 | | | 53 | if (event === 'exit') { |
| b69ab31 | | | 54 | cb(); |
| b69ab31 | | | 55 | } |
| b69ab31 | | | 56 | }), |
| b69ab31 | | | 57 | }; |
| b69ab31 | | | 58 | for (const [regex, output] of cmds) { |
| b69ab31 | | | 59 | if (regex.test(argStr)) { |
| b69ab31 | | | 60 | let value = output; |
| b69ab31 | | | 61 | if (typeof output === 'function') { |
| b69ab31 | | | 62 | value = output(); |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | if (value instanceof Error) { |
| b69ab31 | | | 65 | throw value; |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | return {...ejecaOther, ...value}; |
| b69ab31 | | | 68 | } |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | return {...ejecaOther, stdout: ''}; |
| b69ab31 | | | 71 | }) as unknown as typeof ejeca.ejeca); |
| b69ab31 | | | 72 | } |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | describe('track', () => { |
| b69ab31 | | | 75 | const mockSendData = jest.fn(); |
| b69ab31 | | | 76 | let tracker: ServerSideTracker; |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | beforeEach(() => { |
| b69ab31 | | | 79 | mockSendData.mockClear(); |
| b69ab31 | | | 80 | tracker = makeServerSideTracker( |
| b69ab31 | | | 81 | mockLogger, |
| b69ab31 | | | 82 | {platformName: 'test'} as ServerPlatform, |
| b69ab31 | | | 83 | '0.1', |
| b69ab31 | | | 84 | mockSendData, |
| b69ab31 | | | 85 | ); |
| b69ab31 | | | 86 | }); |
| b69ab31 | | | 87 | it('tracks events', () => { |
| b69ab31 | | | 88 | tracker.track('ClickedRefresh'); |
| b69ab31 | | | 89 | expect(mockSendData).toHaveBeenCalledWith( |
| b69ab31 | | | 90 | expect.objectContaining({eventName: 'ClickedRefresh'}), |
| b69ab31 | | | 91 | mockLogger, |
| b69ab31 | | | 92 | ); |
| b69ab31 | | | 93 | }); |
| b69ab31 | | | 94 | |
| b69ab31 | | | 95 | it('defines all fields', () => { |
| b69ab31 | | | 96 | tracker.track('ClickedRefresh'); |
| b69ab31 | | | 97 | expect(mockSendData).toHaveBeenCalledWith( |
| b69ab31 | | | 98 | { |
| b69ab31 | | | 99 | eventName: anyActualString, |
| b69ab31 | | | 100 | timestamp: expect.anything(), |
| b69ab31 | | | 101 | id: expect.anything(), |
| b69ab31 | | | 102 | |
| b69ab31 | | | 103 | platform: 'test', |
| b69ab31 | | | 104 | version: '0.1', |
| b69ab31 | | | 105 | sessionId: anyActualString, |
| b69ab31 | | | 106 | unixname: anyActualString, |
| b69ab31 | | | 107 | repo: undefined, |
| b69ab31 | | | 108 | osType: anyActualString, |
| b69ab31 | | | 109 | osArch: anyActualString, |
| b69ab31 | | | 110 | osRelease: anyActualString, |
| b69ab31 | | | 111 | hostname: anyActualString, |
| b69ab31 | | | 112 | }, |
| b69ab31 | | | 113 | mockLogger, |
| b69ab31 | | | 114 | ); |
| b69ab31 | | | 115 | }); |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | it('allows setting repository', () => { |
| b69ab31 | | | 118 | // No need to call the actual command lines to test tracking |
| b69ab31 | | | 119 | setConfigOverrideForTests([ |
| b69ab31 | | | 120 | ['path.default', 'https://github.com/facebook/sapling.git'], |
| b69ab31 | | | 121 | ['github.pull_request_domain', 'github.com'], |
| b69ab31 | | | 122 | ]); |
| b69ab31 | | | 123 | const ejecaSpy = mockEjeca([ |
| b69ab31 | | | 124 | [/^sl root --dotdir/, {stdout: '/path/to/myRepo/.sl'}], |
| b69ab31 | | | 125 | [/^sl root/, {stdout: '/path/to/myRepo'}], |
| b69ab31 | | | 126 | [ |
| b69ab31 | | | 127 | /^gh auth status --hostname gitlab.myCompany.com/, |
| b69ab31 | | | 128 | new Error('not authenticated on this hostname'), |
| b69ab31 | | | 129 | ], |
| b69ab31 | | | 130 | [/^gh auth status --hostname ghe.myCompany.com/, {stdout: ''}], |
| b69ab31 | | | 131 | [/^gh api graphql/, {stdout: '{}'}], |
| b69ab31 | | | 132 | ]); |
| b69ab31 | | | 133 | |
| b69ab31 | | | 134 | const repo = new Repository( |
| b69ab31 | | | 135 | { |
| b69ab31 | | | 136 | type: 'success', |
| b69ab31 | | | 137 | codeReviewSystem: { |
| b69ab31 | | | 138 | type: 'github', |
| b69ab31 | | | 139 | repo: 'sapling', |
| b69ab31 | | | 140 | owner: 'facebook', |
| b69ab31 | | | 141 | hostname: 'github.com', |
| b69ab31 | | | 142 | }, |
| b69ab31 | | | 143 | command: 'sl', |
| b69ab31 | | | 144 | repoRoot: '/path', |
| b69ab31 | | | 145 | dotdir: '/path/.sl', |
| b69ab31 | | | 146 | pullRequestDomain: undefined, |
| b69ab31 | | | 147 | isEdenFs: false, |
| b69ab31 | | | 148 | }, |
| b69ab31 | | | 149 | mockCtx, |
| b69ab31 | | | 150 | ); |
| b69ab31 | | | 151 | tracker.context.setRepo(repo); |
| b69ab31 | | | 152 | tracker.track('ClickedRefresh'); |
| b69ab31 | | | 153 | expect(mockSendData).toHaveBeenCalledWith( |
| b69ab31 | | | 154 | expect.objectContaining({ |
| b69ab31 | | | 155 | repo: 'github:github.com/facebook/sapling', |
| b69ab31 | | | 156 | }), |
| b69ab31 | | | 157 | mockLogger, |
| b69ab31 | | | 158 | ); |
| b69ab31 | | | 159 | repo.dispose(); |
| b69ab31 | | | 160 | ejecaSpy.mockClear(); |
| b69ab31 | | | 161 | }); |
| b69ab31 | | | 162 | |
| b69ab31 | | | 163 | it('uses consistent session id, but different track ids', () => { |
| b69ab31 | | | 164 | tracker.track('ClickedRefresh'); |
| b69ab31 | | | 165 | tracker.track('ClickedRefresh'); |
| b69ab31 | | | 166 | const call0 = mockSendData.mock.calls[0][0] as FullTrackData; |
| b69ab31 | | | 167 | const call1 = mockSendData.mock.calls[1][0] as FullTrackData; |
| b69ab31 | | | 168 | expect(call0.id).not.toEqual(call1.id); |
| b69ab31 | | | 169 | expect(call0.sessionId).toEqual(call1.sessionId); |
| b69ab31 | | | 170 | }); |
| b69ab31 | | | 171 | |
| b69ab31 | | | 172 | it('supports trees of events via trackAsParent', () => { |
| b69ab31 | | | 173 | const childTracker = tracker.trackAsParent('ClickedRefresh'); |
| b69ab31 | | | 174 | childTracker.track('ClickedRefresh'); |
| b69ab31 | | | 175 | const call0 = mockSendData.mock.calls[0][0]; |
| b69ab31 | | | 176 | const call1 = mockSendData.mock.calls[1][0]; |
| b69ab31 | | | 177 | expect(call0.id).toEqual(call1.parentId); |
| b69ab31 | | | 178 | }); |
| b69ab31 | | | 179 | |
| b69ab31 | | | 180 | describe('trackError', () => { |
| b69ab31 | | | 181 | it('handles synchronous operations throwing', () => { |
| b69ab31 | | | 182 | tracker.error('ClickedRefresh', 'RepositoryError', new Error('uh oh'), {}); |
| b69ab31 | | | 183 | expect(mockSendData).toHaveBeenCalledWith( |
| b69ab31 | | | 184 | expect.objectContaining({ |
| b69ab31 | | | 185 | eventName: 'ClickedRefresh', |
| b69ab31 | | | 186 | errorName: 'RepositoryError', |
| b69ab31 | | | 187 | errorMessage: 'uh oh', |
| b69ab31 | | | 188 | }), |
| b69ab31 | | | 189 | mockLogger, |
| b69ab31 | | | 190 | ); |
| b69ab31 | | | 191 | }); |
| b69ab31 | | | 192 | }); |
| b69ab31 | | | 193 | |
| b69ab31 | | | 194 | describe('trackOperation', () => { |
| b69ab31 | | | 195 | it('handles synchronous operations', () => { |
| b69ab31 | | | 196 | const op = jest.fn(); |
| b69ab31 | | | 197 | tracker.operation('ClickedRefresh', 'RepositoryError', {}, op); |
| b69ab31 | | | 198 | expect(mockSendData).toHaveBeenCalledWith( |
| b69ab31 | | | 199 | expect.objectContaining({ |
| b69ab31 | | | 200 | eventName: 'ClickedRefresh', |
| b69ab31 | | | 201 | }), |
| b69ab31 | | | 202 | mockLogger, |
| b69ab31 | | | 203 | ); |
| b69ab31 | | | 204 | // there should not be an error field filled out |
| b69ab31 | | | 205 | expect(mockSendData).not.toHaveBeenCalledWith( |
| b69ab31 | | | 206 | expect.objectContaining({ |
| b69ab31 | | | 207 | errorName: anyActualString, |
| b69ab31 | | | 208 | errorMessage: anyActualString, |
| b69ab31 | | | 209 | }), |
| b69ab31 | | | 210 | mockLogger, |
| b69ab31 | | | 211 | ); |
| b69ab31 | | | 212 | expect(op).toHaveBeenCalled(); |
| b69ab31 | | | 213 | }); |
| b69ab31 | | | 214 | |
| b69ab31 | | | 215 | it('handles synchronous operations throwing', () => { |
| b69ab31 | | | 216 | const op = jest.fn().mockImplementation(() => { |
| b69ab31 | | | 217 | throw new Error('uh oh'); |
| b69ab31 | | | 218 | }); |
| b69ab31 | | | 219 | expect(() => tracker.operation('ClickedRefresh', 'RepositoryError', {}, op)).toThrow(); |
| b69ab31 | | | 220 | expect(mockSendData).toHaveBeenCalledWith( |
| b69ab31 | | | 221 | expect.objectContaining({ |
| b69ab31 | | | 222 | eventName: 'ClickedRefresh', |
| b69ab31 | | | 223 | errorName: 'RepositoryError', |
| b69ab31 | | | 224 | errorMessage: 'uh oh', |
| b69ab31 | | | 225 | }), |
| b69ab31 | | | 226 | mockLogger, |
| b69ab31 | | | 227 | ); |
| b69ab31 | | | 228 | expect(op).toHaveBeenCalled(); |
| b69ab31 | | | 229 | }); |
| b69ab31 | | | 230 | |
| b69ab31 | | | 231 | it('handles async operations', async () => { |
| b69ab31 | | | 232 | const d = defer(); |
| b69ab31 | | | 233 | const op = jest.fn().mockImplementation(() => { |
| b69ab31 | | | 234 | return d.promise; |
| b69ab31 | | | 235 | }); |
| b69ab31 | | | 236 | |
| b69ab31 | | | 237 | const promise = tracker.operation('ClickedRefresh', 'RepositoryError', {}, op); |
| b69ab31 | | | 238 | expect(mockSendData).not.toHaveBeenCalled(); |
| b69ab31 | | | 239 | |
| b69ab31 | | | 240 | d.resolve(1); |
| b69ab31 | | | 241 | |
| b69ab31 | | | 242 | await promise; |
| b69ab31 | | | 243 | |
| b69ab31 | | | 244 | expect(mockSendData).toHaveBeenCalledWith( |
| b69ab31 | | | 245 | expect.objectContaining({ |
| b69ab31 | | | 246 | eventName: 'ClickedRefresh', |
| b69ab31 | | | 247 | }), |
| b69ab31 | | | 248 | mockLogger, |
| b69ab31 | | | 249 | ); |
| b69ab31 | | | 250 | // there should not be an error field filled out |
| b69ab31 | | | 251 | expect(mockSendData).not.toHaveBeenCalledWith( |
| b69ab31 | | | 252 | expect.objectContaining({ |
| b69ab31 | | | 253 | errorName: anyActualString, |
| b69ab31 | | | 254 | errorMessage: anyActualString, |
| b69ab31 | | | 255 | }), |
| b69ab31 | | | 256 | mockLogger, |
| b69ab31 | | | 257 | ); |
| b69ab31 | | | 258 | expect(op).toHaveBeenCalled(); |
| b69ab31 | | | 259 | }); |
| b69ab31 | | | 260 | |
| b69ab31 | | | 261 | it('handles async operations throwing', async () => { |
| b69ab31 | | | 262 | const d = defer(); |
| b69ab31 | | | 263 | const op = jest.fn().mockImplementation(() => { |
| b69ab31 | | | 264 | return d.promise; |
| b69ab31 | | | 265 | }); |
| b69ab31 | | | 266 | |
| b69ab31 | | | 267 | const promise = tracker.operation('ClickedRefresh', 'RepositoryError', {}, op); |
| b69ab31 | | | 268 | expect(mockSendData).not.toHaveBeenCalled(); |
| b69ab31 | | | 269 | |
| b69ab31 | | | 270 | d.reject(new Error('oh no')); |
| b69ab31 | | | 271 | |
| b69ab31 | | | 272 | await expect(promise).rejects.toEqual(new Error('oh no')); |
| b69ab31 | | | 273 | |
| b69ab31 | | | 274 | expect(mockSendData).toHaveBeenCalledWith( |
| b69ab31 | | | 275 | expect.objectContaining({ |
| b69ab31 | | | 276 | eventName: 'ClickedRefresh', |
| b69ab31 | | | 277 | errorName: 'RepositoryError', |
| b69ab31 | | | 278 | errorMessage: 'oh no', |
| b69ab31 | | | 279 | }), |
| b69ab31 | | | 280 | mockLogger, |
| b69ab31 | | | 281 | ); |
| b69ab31 | | | 282 | expect(op).toHaveBeenCalled(); |
| b69ab31 | | | 283 | }); |
| b69ab31 | | | 284 | }); |
| b69ab31 | | | 285 | }); |