| 1 | import { ConfigService } from '@nestjs/config'; |
| 2 | import { afterEach, describe |
| 3 | |
| 4 | import { attributed } |
| 5 | import { |
| 6 | dispatchToImporter, |
| 7 | githubImportConfig, |
| 8 | retryDelayMs, |
| 9 | } from './importer.js'; |
| 10 | import { ImporterUnavailableError } |
| 11 | |
| 12 | const complete = { |
| 13 | IMPORTER_URL: 'http://importer:3004', |
| 14 | IMPORTER_SECRET: 'secret', |
| 15 | GITHUB_CLIENT_ID: 'id', |
| 16 | GITHUB_CLIENT_SECRET: 'client-secret', |
| 17 | }; |
| 18 | |
| 19 | afterEach(() => vi |
| 20 | |
| 21 | describe('githubImportConfig', () |
| 22 | it('is set only when all four values are', |
| 23 | expect(githubImportConfig(new |
| 24 | importerUrl: 'http://importer:3004', |
| 25 | importerSecret: 'secret', |
| 26 | github: { clientId: ' |
| 27 | }); |
| 28 | for (const key |
| 29 | expect( |
| 30 | githubImportConfig(new ConfigService |
| 31 | ).toBeNull(); |
| 32 | } |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | describe('retryDelayMs', () |
| 37 | it('doubles per attempt up to half an hour, scaled by the jitter', |
| 38 | expect(retryDelayMs(1, |
| 39 | expect(retryDelayMs(3, |
| 40 | expect(retryDelayMs(20, |
| 41 | expect(retryDelayMs(3, |
| 42 | }); |
| 43 | }); |
| 44 | |
| 45 | describe('attributed', () |
| 46 | it('credits the GitHub author above the body', |
| 47 | expect(attributed('octocat |
| 48 | '_Opened by [@octocat](https://github.com/octocat) on GitHub._\n\nBody', |
| 49 | ); |
| 50 | expect(attributed('octocat |
| 51 | '_Posted by [@octocat](https://github.com/octocat) on GitHub._', |
| 52 | ); |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | describe('dispatchToImporter', () |
| 57 | const config = |
| 58 | const job = |
| 59 | importId: 'import_1', |
| 60 | attempt: 'a', |
| 61 | source: 'octo/repo', |
| 62 | destination: 'me/repo', |
| 63 | githubToken: 'gh', |
| 64 | ghostToken: 'ghost', |
| 65 | }; |
| 66 | |
| 67 | it('posts the job with the secret', |
| 68 | const fetch = |
| 69 | .fn() |
| 70 | .mockResolvedValue(new Response |
| 71 | vi.stubGlobal('fetch |
| 72 | await dispatchToImporter(config |
| 73 | const [url, |
| 74 | expect(String(url |
| 75 | expect(init.headers |
| 76 | expect(JSON.parse |
| 77 | }); |
| 78 | |
| 79 | it('fails when the importer refuses or cannot be reached', |
| 80 | vi.stubGlobal( |
| 81 | 'fetch', |
| 82 | vi.fn().mockResolvedValue |
| 83 | ); |
| 84 | await expect(dispatchToImporter |
| 85 | ImporterUnavailableError, |
| 86 | ); |
| 87 | vi.stubGlobal( |
| 88 | 'fetch', |
| 89 | vi.fn().mockRejectedValue |
| 90 | ); |
| 91 | await expect(dispatchToImporter |
| 92 | ImporterUnavailableError, |
| 93 | ); |
| 94 | }); |
| 95 | }); |