master
  1import { describe, test, expect } from 'vitest'
  2import { presentBadges } from './present-badges.js'
  3import { Badge, define } from './badges.js'
  4import { Data } from './data.js'
  5
  6describe('present-badges', () => {
  7  const data: Data = {
  8    user: {
  9      publicKeys: {
 10        totalCount: 1,
 11      },
 12    } as Data['user'],
 13    pulls: [] as Data['pulls'],
 14    issues: [] as Data['issues'],
 15    repos: [
 16      {
 17        stargazers: {
 18          totalCount: 1000,
 19        },
 20        name: 'bar',
 21        owner: {
 22          login: 'foo',
 23        },
 24        commits: [],
 25      },
 26      {
 27        stargazers: {
 28          totalCount: 2000,
 29        },
 30        name: 'qux',
 31        owner: {
 32          login: 'foo',
 33        },
 34        commits: [] as any[],
 35      },
 36    ] as Data['repos'],
 37  } as Data
 38
 39  test('presentBadges() applies `pick`', async () => {
 40    const userBadges = presentBadges(
 41      [await import('#badges/stars/stars.js')].map((m) => m.default),
 42      data,
 43      [],
 44      ['stars-100', 'stars-500'],
 45      [],
 46      false,
 47    )
 48
 49    expect(userBadges).toEqual([
 50      {
 51        id: 'stars-100',
 52        tier: 1,
 53        desc: 'I collected 100 stars.',
 54        body:
 55          'Repos:\n' +
 56          '\n' +
 57          '* <a href="https://github.com/foo/bar">foo/bar: ★1000</a>\n' +
 58          '\n' +
 59          "<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>\n",
 60        image: 'https://my-badges.github.io/my-badges/stars-100.png',
 61      },
 62      {
 63        id: 'stars-500',
 64        tier: 2,
 65        desc: 'I collected 500 stars.',
 66        body:
 67          'Repos:\n' +
 68          '\n' +
 69          '* <a href="https://github.com/foo/bar">foo/bar: ★1000</a>\n' +
 70          '\n' +
 71          "<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>\n",
 72        image: 'https://my-badges.github.io/my-badges/stars-500.png',
 73      },
 74    ])
 75  })
 76
 77  test('presentBadges() applies `omit`', async () => {
 78    const userBadges = presentBadges(
 79      [await import('#badges/stars/stars.js')].map((m) => m.default),
 80      data,
 81      [],
 82      ['stars-100', 'stars-500'],
 83      ['stars-500'],
 84      false,
 85    )
 86
 87    expect(userBadges).toEqual([
 88      {
 89        id: 'stars-100',
 90        tier: 1,
 91        desc: 'I collected 100 stars.',
 92        body:
 93          'Repos:\n' +
 94          '\n' +
 95          '* <a href="https://github.com/foo/bar">foo/bar: ★1000</a>\n' +
 96          '\n' +
 97          "<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>\n",
 98        image: 'https://my-badges.github.io/my-badges/stars-100.png',
 99      },
100    ])
101  })
102
103  test('presentBadges() supports masks for `omit` && `pick`', async () => {
104    const userBadges = presentBadges(
105      [await import('#badges/stars/stars.js')].map((m) => m.default),
106      data,
107      [],
108      ['stars-*'],
109      ['stars-*000'],
110      false,
111    )
112
113    expect(userBadges).toEqual([
114      {
115        id: 'stars-100',
116        tier: 1,
117        desc: 'I collected 100 stars.',
118        body:
119          'Repos:\n' +
120          '\n' +
121          '* <a href="https://github.com/foo/bar">foo/bar: ★1000</a>\n' +
122          '\n' +
123          "<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>\n",
124        image: 'https://my-badges.github.io/my-badges/stars-100.png',
125      },
126      {
127        id: 'stars-500',
128        tier: 2,
129        desc: 'I collected 500 stars.',
130        body:
131          'Repos:\n' +
132          '\n' +
133          '* <a href="https://github.com/foo/bar">foo/bar: ★1000</a>\n' +
134          '\n' +
135          "<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>\n",
136        image: 'https://my-badges.github.io/my-badges/stars-500.png',
137      },
138    ])
139  })
140
141  test('presentBadges() applies `compact`', async () => {
142    const userBadges = presentBadges(
143      [await import('#badges/stars/stars.js')].map((m) => m.default),
144      data,
145      [],
146      ['stars-1000', 'stars-2000', 'stars-5000'],
147      [],
148      true,
149    )
150
151    expect(userBadges).toEqual([
152      {
153        id: 'stars-2000',
154        tier: 4,
155        desc: 'I collected 2000 stars.',
156        body:
157          'Repos:\n' +
158          '\n' +
159          '* <a href="https://github.com/foo/qux">foo/qux: ★2000</a>\n' +
160          '* <a href="https://github.com/foo/bar">foo/bar: ★1000</a>\n' +
161          '\n' +
162          "<sup>I have push, maintainer or admin permissions, so I'm definitely an author.<sup>\n",
163        image: 'https://my-badges.github.io/my-badges/stars-2000.png',
164      },
165    ])
166  })
167
168  test('presentBadges() keeps existing order of badges', async () => {
169    const dumpPresenter1 = define({
170      url: 'file:///tmp/dump.js',
171      badges: ['a-commit', 'ab-commit', 'abc-commit'] as const,
172      present: (_, grant) => {
173        grant('a-commit', 'a')
174        grant('ab-commit', 'ab')
175        grant('abc-commit', 'abc')
176      },
177    })
178    const dumpPresenter2 = define({
179      url: 'file:///tmp/dump.js',
180      badges: ['this-is-fine'] as const,
181      present: (_, grant) => {
182        grant('this-is-fine', 'this is fine')
183      },
184    })
185
186    const oldUserBadges: Badge[] = [
187      {
188        id: 'a-commit',
189        tier: 0,
190        desc: 'a',
191        body: '',
192        image: '',
193      },
194      {
195        id: 'abc-commit',
196        tier: 0,
197        desc: 'abc',
198        body: '',
199        image: '',
200      },
201      {
202        id: 'this-is-fine',
203        tier: 0,
204        desc: 'this is fine',
205        body: '',
206        image: '',
207      },
208    ]
209
210    const userBadges = presentBadges(
211      [dumpPresenter1, dumpPresenter2],
212      data,
213      oldUserBadges,
214      [],
215      [],
216      false,
217    )
218    expect(userBadges.map((x) => x.id)).toEqual([
219      'a-commit',
220      'abc-commit',
221      'this-is-fine',
222      'ab-commit',
223    ])
224  })
225})