|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | + |
| 3 | +import {createConfiguredGetOctokit} from '../src/create-configured-getoctokit' |
| 4 | + |
| 5 | +describe('createConfiguredGetOctokit', () => { |
| 6 | + const mockRetryPlugin = jest.fn() |
| 7 | + const mockRequestLogPlugin = jest.fn() |
| 8 | + |
| 9 | + function makeMockGetOctokit() { |
| 10 | + return jest.fn().mockReturnValue('mock-client') |
| 11 | + } |
| 12 | + |
| 13 | + test('passes token and merged defaults to underlying getOctokit', () => { |
| 14 | + const raw = makeMockGetOctokit() |
| 15 | + const defaults = { |
| 16 | + userAgent: 'actions/github-script actions_orchestration_id/abc', |
| 17 | + retry: {enabled: true}, |
| 18 | + request: {retries: 3} |
| 19 | + } |
| 20 | + |
| 21 | + const wrapped = createConfiguredGetOctokit( |
| 22 | + raw as any, |
| 23 | + defaults, |
| 24 | + mockRetryPlugin, |
| 25 | + mockRequestLogPlugin |
| 26 | + ) |
| 27 | + wrapped('my-token' as any) |
| 28 | + |
| 29 | + expect(raw).toHaveBeenCalledWith( |
| 30 | + 'my-token', |
| 31 | + expect.objectContaining({ |
| 32 | + userAgent: 'actions/github-script actions_orchestration_id/abc', |
| 33 | + retry: {enabled: true}, |
| 34 | + request: {retries: 3} |
| 35 | + }), |
| 36 | + mockRetryPlugin, |
| 37 | + mockRequestLogPlugin |
| 38 | + ) |
| 39 | + }) |
| 40 | + |
| 41 | + test('user options override top-level defaults', () => { |
| 42 | + const raw = makeMockGetOctokit() |
| 43 | + const defaults = { |
| 44 | + userAgent: 'default-agent', |
| 45 | + previews: ['v3'] |
| 46 | + } |
| 47 | + |
| 48 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 49 | + wrapped('tok' as any, {userAgent: 'custom-agent'} as any) |
| 50 | + |
| 51 | + expect(raw).toHaveBeenCalledWith( |
| 52 | + 'tok', |
| 53 | + expect.objectContaining({userAgent: 'custom-agent', previews: ['v3']}) |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + test('deep-merges request so partial overrides preserve retries', () => { |
| 58 | + const raw = makeMockGetOctokit() |
| 59 | + const defaults = { |
| 60 | + request: {retries: 3, agent: 'proxy-agent', fetch: 'proxy-fetch'} |
| 61 | + } |
| 62 | + |
| 63 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 64 | + wrapped('tok' as any, {request: {timeout: 5000}} as any) |
| 65 | + |
| 66 | + expect(raw).toHaveBeenCalledWith( |
| 67 | + 'tok', |
| 68 | + expect.objectContaining({ |
| 69 | + request: { |
| 70 | + retries: 3, |
| 71 | + agent: 'proxy-agent', |
| 72 | + fetch: 'proxy-fetch', |
| 73 | + timeout: 5000 |
| 74 | + } |
| 75 | + }) |
| 76 | + ) |
| 77 | + }) |
| 78 | + |
| 79 | + test('deep-merges retry so partial overrides preserve existing settings', () => { |
| 80 | + const raw = makeMockGetOctokit() |
| 81 | + const defaults = { |
| 82 | + retry: {enabled: true, retries: 3} |
| 83 | + } |
| 84 | + |
| 85 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 86 | + wrapped('tok' as any, {retry: {retries: 5}} as any) |
| 87 | + |
| 88 | + expect(raw).toHaveBeenCalledWith( |
| 89 | + 'tok', |
| 90 | + expect.objectContaining({ |
| 91 | + retry: {enabled: true, retries: 5} |
| 92 | + }) |
| 93 | + ) |
| 94 | + }) |
| 95 | + |
| 96 | + test('user can override request.retries explicitly', () => { |
| 97 | + const raw = makeMockGetOctokit() |
| 98 | + const defaults = {request: {retries: 3}} |
| 99 | + |
| 100 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 101 | + wrapped('tok' as any, {request: {retries: 0}} as any) |
| 102 | + |
| 103 | + expect(raw).toHaveBeenCalledWith( |
| 104 | + 'tok', |
| 105 | + expect.objectContaining({request: {retries: 0}}) |
| 106 | + ) |
| 107 | + }) |
| 108 | + |
| 109 | + test('user plugins are appended after default plugins', () => { |
| 110 | + const raw = makeMockGetOctokit() |
| 111 | + const customPlugin = jest.fn() |
| 112 | + |
| 113 | + const wrapped = createConfiguredGetOctokit( |
| 114 | + raw as any, |
| 115 | + {}, |
| 116 | + mockRetryPlugin, |
| 117 | + mockRequestLogPlugin |
| 118 | + ) |
| 119 | + wrapped('tok' as any, {} as any, customPlugin as any) |
| 120 | + |
| 121 | + expect(raw).toHaveBeenCalledWith( |
| 122 | + 'tok', |
| 123 | + expect.any(Object), |
| 124 | + mockRetryPlugin, |
| 125 | + mockRequestLogPlugin, |
| 126 | + customPlugin |
| 127 | + ) |
| 128 | + }) |
| 129 | + |
| 130 | + test('duplicate plugins are deduplicated', () => { |
| 131 | + const raw = makeMockGetOctokit() |
| 132 | + |
| 133 | + const wrapped = createConfiguredGetOctokit( |
| 134 | + raw as any, |
| 135 | + {}, |
| 136 | + mockRetryPlugin, |
| 137 | + mockRequestLogPlugin |
| 138 | + ) |
| 139 | + // User passes retry again — should not duplicate |
| 140 | + wrapped('tok' as any, {} as any, mockRetryPlugin as any) |
| 141 | + |
| 142 | + expect(raw).toHaveBeenCalledWith( |
| 143 | + 'tok', |
| 144 | + expect.any(Object), |
| 145 | + mockRetryPlugin, |
| 146 | + mockRequestLogPlugin |
| 147 | + ) |
| 148 | + }) |
| 149 | + |
| 150 | + test('applies defaults when no user options provided', () => { |
| 151 | + const raw = makeMockGetOctokit() |
| 152 | + const defaults = { |
| 153 | + userAgent: 'actions/github-script', |
| 154 | + retry: {enabled: true}, |
| 155 | + request: {retries: 3} |
| 156 | + } |
| 157 | + |
| 158 | + const wrapped = createConfiguredGetOctokit( |
| 159 | + raw as any, |
| 160 | + defaults, |
| 161 | + mockRetryPlugin |
| 162 | + ) |
| 163 | + wrapped('tok' as any) |
| 164 | + |
| 165 | + expect(raw).toHaveBeenCalledWith( |
| 166 | + 'tok', |
| 167 | + { |
| 168 | + userAgent: 'actions/github-script', |
| 169 | + retry: {enabled: true}, |
| 170 | + request: {retries: 3} |
| 171 | + }, |
| 172 | + mockRetryPlugin |
| 173 | + ) |
| 174 | + }) |
| 175 | + |
| 176 | + test('baseUrl: undefined from user does not clobber default', () => { |
| 177 | + const raw = makeMockGetOctokit() |
| 178 | + const defaults = {baseUrl: 'https://ghes.example.com/api/v3'} |
| 179 | + |
| 180 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 181 | + wrapped('tok' as any, {baseUrl: undefined} as any) |
| 182 | + |
| 183 | + const calledOpts = raw.mock.calls[0][1] |
| 184 | + expect(calledOpts.baseUrl).toBe('https://ghes.example.com/api/v3') |
| 185 | + }) |
| 186 | + |
| 187 | + test('undefined values in nested request are stripped', () => { |
| 188 | + const raw = makeMockGetOctokit() |
| 189 | + const defaults = {request: {retries: 3, agent: 'proxy'}} |
| 190 | + |
| 191 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 192 | + wrapped('tok' as any, {request: {retries: undefined, timeout: 5000}} as any) |
| 193 | + |
| 194 | + const calledOpts = raw.mock.calls[0][1] |
| 195 | + expect(calledOpts.request).toEqual({ |
| 196 | + retries: 3, |
| 197 | + agent: 'proxy', |
| 198 | + timeout: 5000 |
| 199 | + }) |
| 200 | + }) |
| 201 | + |
| 202 | + test('undefined values in nested retry are stripped', () => { |
| 203 | + const raw = makeMockGetOctokit() |
| 204 | + const defaults = {retry: {enabled: true, retries: 3}} |
| 205 | + |
| 206 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 207 | + wrapped('tok' as any, {retry: {enabled: undefined, retries: 5}} as any) |
| 208 | + |
| 209 | + const calledOpts = raw.mock.calls[0][1] |
| 210 | + expect(calledOpts.retry).toEqual({enabled: true, retries: 5}) |
| 211 | + }) |
| 212 | + |
| 213 | + test('each call creates an independent client', () => { |
| 214 | + const raw = jest |
| 215 | + .fn() |
| 216 | + .mockReturnValueOnce('client-a') |
| 217 | + .mockReturnValueOnce('client-b') |
| 218 | + |
| 219 | + const wrapped = createConfiguredGetOctokit(raw as any, {}) |
| 220 | + const a = wrapped('token-a' as any) |
| 221 | + const b = wrapped('token-b' as any) |
| 222 | + |
| 223 | + expect(a).toBe('client-a') |
| 224 | + expect(b).toBe('client-b') |
| 225 | + expect(raw).toHaveBeenCalledTimes(2) |
| 226 | + }) |
| 227 | + |
| 228 | + test('does not mutate defaultOptions between calls', () => { |
| 229 | + const raw = makeMockGetOctokit() |
| 230 | + const defaults = { |
| 231 | + request: {retries: 3}, |
| 232 | + retry: {enabled: true} |
| 233 | + } |
| 234 | + const originalDefaults = JSON.parse(JSON.stringify(defaults)) |
| 235 | + |
| 236 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 237 | + wrapped( |
| 238 | + 'tok' as any, |
| 239 | + {request: {timeout: 5000}, retry: {retries: 10}} as any |
| 240 | + ) |
| 241 | + wrapped('tok' as any, {request: {timeout: 9000}} as any) |
| 242 | + |
| 243 | + expect(defaults).toEqual(originalDefaults) |
| 244 | + }) |
| 245 | + |
| 246 | + test('undefined values in defaults are stripped (e.g. log: undefined)', () => { |
| 247 | + const raw = makeMockGetOctokit() |
| 248 | + const defaults = { |
| 249 | + userAgent: 'actions/github-script', |
| 250 | + log: undefined, |
| 251 | + previews: undefined, |
| 252 | + retry: {enabled: true} |
| 253 | + } |
| 254 | + |
| 255 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 256 | + wrapped('tok' as any) |
| 257 | + |
| 258 | + const calledOpts = raw.mock.calls[0][1] |
| 259 | + expect(calledOpts.userAgent).toBe('actions/github-script') |
| 260 | + expect(calledOpts.retry).toEqual({enabled: true}) |
| 261 | + // undefined defaults should be stripped, not passed through |
| 262 | + expect('log' in calledOpts).toBe(false) |
| 263 | + expect('previews' in calledOpts).toBe(false) |
| 264 | + }) |
| 265 | + |
| 266 | + test('falsy-but-valid values are preserved, only undefined is stripped', () => { |
| 267 | + const raw = makeMockGetOctokit() |
| 268 | + const defaults = {baseUrl: 'https://ghes.example.com/api/v3'} |
| 269 | + |
| 270 | + const wrapped = createConfiguredGetOctokit(raw as any, defaults) |
| 271 | + wrapped( |
| 272 | + 'tok' as any, |
| 273 | + { |
| 274 | + log: null, |
| 275 | + retries: 0, |
| 276 | + debug: false, |
| 277 | + userAgent: '' |
| 278 | + } as any |
| 279 | + ) |
| 280 | + |
| 281 | + const calledOpts = raw.mock.calls[0][1] |
| 282 | + expect(calledOpts.log).toBeNull() |
| 283 | + expect(calledOpts.retries).toBe(0) |
| 284 | + expect(calledOpts.debug).toBe(false) |
| 285 | + expect(calledOpts.userAgent).toBe('') |
| 286 | + expect(calledOpts.baseUrl).toBe('https://ghes.example.com/api/v3') |
| 287 | + }) |
| 288 | +}) |
0 commit comments