|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | + |
| 3 | +import {callAsyncFunction} from '../src/async-function' |
| 4 | + |
| 5 | +// Create a mock getOctokit that returns Octokit-like objects. |
| 6 | +// Real @actions/github integration is tested in the CI workflow |
| 7 | +// (integration.yml test-get-octokit job). Here we verify the |
| 8 | +// script context wiring — getOctokit is passed through and |
| 9 | +// callable from user scripts. |
| 10 | +function mockGetOctokit(token: string, options?: any) { |
| 11 | + return { |
| 12 | + _token: token, |
| 13 | + _options: options, |
| 14 | + rest: { |
| 15 | + issues: {get: async () => ({data: {id: 1}})}, |
| 16 | + pulls: {get: async () => ({data: {id: 2}})} |
| 17 | + }, |
| 18 | + graphql: async () => ({}), |
| 19 | + request: async () => ({}) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +describe('getOctokit integration via callAsyncFunction', () => { |
| 24 | + test('getOctokit creates a functional client in script scope', async () => { |
| 25 | + const result = await callAsyncFunction( |
| 26 | + {getOctokit: mockGetOctokit} as any, |
| 27 | + ` |
| 28 | + const client = getOctokit('fake-token-for-test') |
| 29 | + return { |
| 30 | + hasRest: typeof client.rest === 'object', |
| 31 | + hasGraphql: typeof client.graphql === 'function', |
| 32 | + hasRequest: typeof client.request === 'function', |
| 33 | + hasIssues: typeof client.rest.issues === 'object', |
| 34 | + hasPulls: typeof client.rest.pulls === 'object' |
| 35 | + } |
| 36 | + ` |
| 37 | + ) |
| 38 | + |
| 39 | + expect(result).toEqual({ |
| 40 | + hasRest: true, |
| 41 | + hasGraphql: true, |
| 42 | + hasRequest: true, |
| 43 | + hasIssues: true, |
| 44 | + hasPulls: true |
| 45 | + }) |
| 46 | + }) |
| 47 | + |
| 48 | + test('secondary client is independent from primary github client', async () => { |
| 49 | + const primary = mockGetOctokit('primary-token') |
| 50 | + |
| 51 | + const result = await callAsyncFunction( |
| 52 | + {github: primary, getOctokit: mockGetOctokit} as any, |
| 53 | + ` |
| 54 | + const secondary = getOctokit('secondary-token') |
| 55 | + return { |
| 56 | + bothHaveRest: typeof github.rest === 'object' && typeof secondary.rest === 'object', |
| 57 | + areDistinct: github !== secondary |
| 58 | + } |
| 59 | + ` |
| 60 | + ) |
| 61 | + |
| 62 | + expect(result).toEqual({ |
| 63 | + bothHaveRest: true, |
| 64 | + areDistinct: true |
| 65 | + }) |
| 66 | + }) |
| 67 | + |
| 68 | + test('getOctokit accepts options for GHES base URL', async () => { |
| 69 | + const result = await callAsyncFunction( |
| 70 | + {getOctokit: mockGetOctokit} as any, |
| 71 | + ` |
| 72 | + const client = getOctokit('fake-token', { |
| 73 | + baseUrl: 'https://ghes.example.com/api/v3' |
| 74 | + }) |
| 75 | + return typeof client.rest === 'object' |
| 76 | + ` |
| 77 | + ) |
| 78 | + |
| 79 | + expect(result).toBe(true) |
| 80 | + }) |
| 81 | + |
| 82 | + test('multiple getOctokit calls produce independent clients with different tokens', async () => { |
| 83 | + const result = await callAsyncFunction( |
| 84 | + {getOctokit: mockGetOctokit} as any, |
| 85 | + ` |
| 86 | + const clientA = getOctokit('token-a') |
| 87 | + const clientB = getOctokit('token-b') |
| 88 | + return { |
| 89 | + aHasRest: typeof clientA.rest === 'object', |
| 90 | + bHasRest: typeof clientB.rest === 'object', |
| 91 | + areDistinct: clientA !== clientB |
| 92 | + } |
| 93 | + ` |
| 94 | + ) |
| 95 | + |
| 96 | + expect(result).toEqual({ |
| 97 | + aHasRest: true, |
| 98 | + bHasRest: true, |
| 99 | + areDistinct: true |
| 100 | + }) |
| 101 | + }) |
| 102 | +}) |
0 commit comments