|
| 1 | +import type { Mock } from 'vitest' |
| 2 | +import type { SponsorkitConfig } from '../types.ts' |
| 3 | +import { $fetch } from 'ofetch' |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | +import { |
| 6 | + fetchGitHubSponsors, |
| 7 | + makeQuery, |
| 8 | + makeSponsoringQuery, |
| 9 | + makeSponsoringTotalAmountQuery, |
| 10 | +} from './github.ts' |
| 11 | + |
| 12 | +vi.mock('ofetch', () => ({ $fetch: vi.fn() })) |
| 13 | + |
| 14 | +const fetchMock = $fetch as unknown as Mock |
| 15 | + |
| 16 | +beforeEach(() => { |
| 17 | + fetchMock.mockReset() |
| 18 | +}) |
| 19 | + |
| 20 | +describe('makeQuery', () => { |
| 21 | + it('embeds the login and account type', () => { |
| 22 | + expect(makeQuery('antfu', 'user')).toContain('user(login: "antfu")') |
| 23 | + expect(makeQuery('antfu', 'organization')).toContain('organization(login: "antfu")') |
| 24 | + }) |
| 25 | + |
| 26 | + it('queries sponsorships as a maintainer', () => { |
| 27 | + expect(makeQuery('antfu', 'user')).toContain('sponsorshipsAsMaintainer(') |
| 28 | + }) |
| 29 | + |
| 30 | + it('interpolates the activeOnly flag', () => { |
| 31 | + expect(makeQuery('antfu', 'user', true)).toContain('activeOnly: true') |
| 32 | + expect(makeQuery('antfu', 'user', false)).toContain('activeOnly: false') |
| 33 | + }) |
| 34 | + |
| 35 | + it('adds an after cursor only when provided', () => { |
| 36 | + expect(makeQuery('antfu', 'user', true)).not.toContain('after:') |
| 37 | + expect(makeQuery('antfu', 'user', true, 'CURSOR')).toContain('after: "CURSOR"') |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('makeSponsoringQuery', () => { |
| 42 | + it('queries sponsorships as a sponsor', () => { |
| 43 | + const query = makeSponsoringQuery('antfu', 'user', false, 'CURSOR') |
| 44 | + expect(query).toContain('sponsorshipsAsSponsor(') |
| 45 | + expect(query).toContain('activeOnly: false') |
| 46 | + expect(query).toContain('after: "CURSOR"') |
| 47 | + }) |
| 48 | +}) |
| 49 | + |
| 50 | +describe('makeSponsoringTotalAmountQuery', () => { |
| 51 | + it('omits parameters when no options are given', () => { |
| 52 | + const query = makeSponsoringTotalAmountQuery('antfu', 'user') |
| 53 | + expect(query).toContain('totalSponsorshipAmountAsSponsorInCents\n') |
| 54 | + expect(query).not.toContain('totalSponsorshipAmountAsSponsorInCents(') |
| 55 | + }) |
| 56 | + |
| 57 | + it('serializes since, until and sponsorableLogins', () => { |
| 58 | + const query = makeSponsoringTotalAmountQuery('antfu', 'user', { |
| 59 | + since: '2024-01-01', |
| 60 | + until: '2024-12-31', |
| 61 | + sponsorableLogins: ['vuejs', 'vitejs'], |
| 62 | + }) |
| 63 | + expect(query).toContain('since: "2024-01-01"') |
| 64 | + expect(query).toContain('until: "2024-12-31"') |
| 65 | + expect(query).toContain('sponsorableLogins: ["vuejs", "vitejs"]') |
| 66 | + }) |
| 67 | +}) |
| 68 | + |
| 69 | +describe('fetchGitHubSponsors', () => { |
| 70 | + it('validates its required arguments', async () => { |
| 71 | + await expect(fetchGitHubSponsors('', 'antfu', 'user', {})) |
| 72 | + .rejects |
| 73 | + .toThrow('GitHub token is required') |
| 74 | + await expect(fetchGitHubSponsors('token', '', 'user', {})) |
| 75 | + .rejects |
| 76 | + .toThrow('GitHub login is required') |
| 77 | + await expect(fetchGitHubSponsors('token', 'antfu', 'invalid' as any, {})) |
| 78 | + .rejects |
| 79 | + .toThrow('GitHub type must be either `user` or `organization`') |
| 80 | + }) |
| 81 | + |
| 82 | + it('paginates and maps the response into sponsorships', async () => { |
| 83 | + const page = (login: string, hasNextPage: boolean, endCursor: string | null) => ({ |
| 84 | + data: { |
| 85 | + user: { |
| 86 | + sponsorshipsAsMaintainer: { |
| 87 | + totalCount: 2, |
| 88 | + pageInfo: { hasNextPage, endCursor }, |
| 89 | + nodes: [ |
| 90 | + { |
| 91 | + createdAt: '2024-01-01T00:00:00Z', |
| 92 | + privacyLevel: 'PUBLIC', |
| 93 | + isActive: true, |
| 94 | + tier: { name: 'Gold', isOneTime: false, monthlyPriceInCents: 1000, monthlyPriceInDollars: 10 }, |
| 95 | + sponsorEntity: { __typename: 'User', login, name: login, avatarUrl: 'a', websiteUrl: 'example.com' }, |
| 96 | + }, |
| 97 | + ], |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }) |
| 102 | + |
| 103 | + fetchMock |
| 104 | + .mockResolvedValueOnce(page('alice', true, 'CURSOR')) |
| 105 | + .mockResolvedValueOnce(page('bob', false, null)) |
| 106 | + |
| 107 | + const sponsors = await fetchGitHubSponsors('token', 'antfu', 'user', {}) |
| 108 | + |
| 109 | + expect(fetchMock).toHaveBeenCalledTimes(2) |
| 110 | + // second request carries the cursor from the first page |
| 111 | + expect(fetchMock.mock.calls[1][1].body.query).toContain('after: "CURSOR"') |
| 112 | + |
| 113 | + expect(sponsors.map(s => s.sponsor.login)).toEqual(['alice', 'bob']) |
| 114 | + expect(sponsors[0]).toMatchObject({ |
| 115 | + monthlyDollars: 10, |
| 116 | + tierName: 'Gold', |
| 117 | + isOneTime: false, |
| 118 | + sponsor: { |
| 119 | + login: 'alice', |
| 120 | + type: 'User', |
| 121 | + websiteUrl: 'https://example.com', |
| 122 | + linkUrl: 'https://github.com/alice', |
| 123 | + }, |
| 124 | + }) |
| 125 | + }) |
| 126 | + |
| 127 | + it('marks inactive non-prorated sponsors as past sponsors', async () => { |
| 128 | + fetchMock.mockResolvedValueOnce({ |
| 129 | + data: { |
| 130 | + user: { |
| 131 | + sponsorshipsAsMaintainer: { |
| 132 | + totalCount: 1, |
| 133 | + pageInfo: { hasNextPage: false, endCursor: null }, |
| 134 | + nodes: [ |
| 135 | + { |
| 136 | + createdAt: '2024-01-01T00:00:00Z', |
| 137 | + privacyLevel: 'PUBLIC', |
| 138 | + isActive: false, |
| 139 | + tier: { name: 'Gold', isOneTime: false, monthlyPriceInCents: 1000, monthlyPriceInDollars: 10 }, |
| 140 | + sponsorEntity: { __typename: 'User', login: 'past', name: 'past', avatarUrl: 'a' }, |
| 141 | + }, |
| 142 | + ], |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }) |
| 147 | + |
| 148 | + const sponsors = await fetchGitHubSponsors('token', 'antfu', 'user', {}) |
| 149 | + expect(sponsors[0].monthlyDollars).toBe(-1) |
| 150 | + }) |
| 151 | + |
| 152 | + it('throws when the API returns errors', async () => { |
| 153 | + fetchMock.mockResolvedValueOnce({ errors: [{ type: 'INSUFFICIENT_SCOPES' }] }) |
| 154 | + await expect(fetchGitHubSponsors('token', 'antfu', 'user', {} as SponsorkitConfig)) |
| 155 | + .rejects |
| 156 | + .toThrow('read:user') |
| 157 | + }) |
| 158 | +}) |
0 commit comments