| 1 | import { expect, test } from "bun:test"; |
| 2 | |
| 3 | import type { Config |
| 4 | import { createApp } |
| 5 | import type { Job |
| 6 | |
| 7 | const config: Config |
| 8 | PORT: 0, |
| 9 | IMPORTER_SECRET: "a-secret-long-enough", |
| 10 | GHOST_API_URL: "http://api:3001", |
| 11 | IMPORTER_CONCURRENCY: 1, |
| 12 | IMPORTER_WORKDIR: "/tmp/unused", |
| 13 | }; |
| 14 | |
| 15 | const job: Job |
| 16 | importId: "import_1", |
| 17 | attempt: "4b0b6b8e-3c4f-4b77-9a4e-1c2d3e4f5a6b", |
| 18 | source: "octo/repo", |
| 19 | destination: "me/repo", |
| 20 | githubToken: "gh", |
| 21 | ghostToken: "ghost", |
| 22 | }; |
| 23 | |
| 24 | function post( |
| 25 | app: ReturnType |
| 26 | body: unknown, |
| 27 | secret = config. |
| 28 | ) { |
| 29 | return app.request |
| 30 | method: "POST", |
| 31 | headers: { |
| 32 | Authorization: `Bearer ${secret |
| 33 | "Content-Type": "application/json", |
| 34 | }, |
| 35 | body: JSON.stringify |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | test("refuses a wrong secret", async |
| 40 | const app = |
| 41 | expect((await post |
| 42 | }); |
| 43 | |
| 44 | test("refuses a malformed job", async |
| 45 | const app = |
| 46 | expect((await post |
| 47 | }); |
| 48 | |
| 49 | test("accepts a job, runs it, and turns jobs away while full", async |
| 50 | let finish = |
| 51 | const started: |
| 52 | const app = |
| 53 | started.push(accepted |
| 54 | return new Promise |
| 55 | finish = resolve; |
| 56 | }); |
| 57 | }); |
| 58 | |
| 59 | expect((await post |
| 60 | expect(started).toEqual |
| 61 | expect( |
| 62 | ( |
| 63 | await post(app |
| 64 | ...job, |
| 65 | attempt: "5b0b6b8e-3c4f-4b77-9a4e-1c2d3e4f5a6b", |
| 66 | }) |
| 67 | ).status, |
| 68 | ).toBe(503); |
| 69 | |
| 70 | finish(); |
| 71 | await Bun.sleep |
| 72 | expect((await post |
| 73 | }); |
| 74 | |
| 75 | test("health answers", async |
| 76 | expect( |
| 77 | await (await createApp |
| 78 | ).toBe("ok" |
| 79 | }); |