| 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 | lastAttempt: false, |
| 23 | }; |
| 24 | |
| 25 | function post( |
| 26 | app: ReturnType |
| 27 | body: unknown, |
| 28 | secret = config. |
| 29 | ) { |
| 30 | return app.request |
| 31 | method: "POST", |
| 32 | headers: { |
| 33 | Authorization: `Bearer ${secret |
| 34 | "Content-Type": "application/json", |
| 35 | }, |
| 36 | body: JSON.stringify |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | test("refuses a wrong secret", async |
| 41 | const app = |
| 42 | expect((await post |
| 43 | }); |
| 44 | |
| 45 | test("refuses a malformed job", async |
| 46 | const app = |
| 47 | expect((await post |
| 48 | // the id names a directory under the workdir |
| 49 | expect((await post |
| 50 | }); |
| 51 | |
| 52 | test("accepts a job, runs it, and turns jobs away while full", async |
| 53 | let finish = |
| 54 | const started: |
| 55 | const app = |
| 56 | started.push(accepted |
| 57 | return new Promise |
| 58 | finish = resolve; |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | expect((await post |
| 63 | expect(started).toEqual |
| 64 | expect( |
| 65 | ( |
| 66 | await post(app |
| 67 | ...job, |
| 68 | attempt: "5b0b6b8e-3c4f-4b77-9a4e-1c2d3e4f5a6b", |
| 69 | }) |
| 70 | ).status, |
| 71 | ).toBe(503); |
| 72 | |
| 73 | finish(); |
| 74 | await Bun.sleep |
| 75 | expect((await post |
| 76 | }); |
| 77 | |
| 78 | test("runs one attempt of an import at a time, whatever the room", async |
| 79 | const app = |
| 80 | { ...config, |
| 81 | () => new |
| 82 | ); |
| 83 | const retry = |
| 84 | expect((await post |
| 85 | expect((await post |
| 86 | expect((await post |
| 87 | 202, |
| 88 | ); |
| 89 | }); |
| 90 | |
| 91 | test("health answers", async |
| 92 | expect( |
| 93 | await (await createApp |
| 94 | ).toBe("ok" |
| 95 | }); |