| 1 | import { expect, test } from "bun:test"; |
| 2 | |
| 3 | import { StaleAttemptError } |
| 4 | import { GhostCallbacks } |
| 5 | |
| 6 | function callbacks(responses: |
| 7 | const calls: |
| 8 | const impl = |
| 9 | calls.push({ |
| 10 | const next = |
| 11 | if (!next) |
| 12 | if (next instanceof |
| 13 | return next; |
| 14 | }) as unknown |
| 15 | const sleeps: |
| 16 | const ghost = |
| 17 | "http://api:3001", |
| 18 | "s3cret", |
| 19 | "import_1", |
| 20 | "attempt-1", |
| 21 | impl, |
| 22 | async (ms) |
| 23 | sleeps.push(ms |
| 24 | }, |
| 25 | ); |
| 26 | return { ghost, |
| 27 | } |
| 28 | |
| 29 | test("posts the attempt with the secret", async |
| 30 | const { ghost, |
| 31 | await ghost.releases |
| 32 | expect(calls[0] |
| 33 | "http://api:3001/api/internal/imports/import_1/releases", |
| 34 | ); |
| 35 | expect(JSON.parse |
| 36 | attempt: "attempt-1", |
| 37 | releases: [], |
| 38 | }); |
| 39 | expect((calls[0] |
| 40 | "Bearer s3cret", |
| 41 | ); |
| 42 | }); |
| 43 | |
| 44 | test("replaces NUL, which Postgres cannot store, in every string", async |
| 45 | const { ghost, |
| 46 | await ghost.comments |
| 47 | { |
| 48 | githubId: 1, |
| 49 | issueNumber: 1, |
| 50 | authorLogin: "octocat", |
| 51 | body: "Unexpected character '\0' (1:0)", |
| 52 | createdAt: "2024-01-01T00:00:00Z", |
| 53 | updatedAt: "2024-01-01T00:00:00Z", |
| 54 | }, |
| 55 | ]); |
| 56 | const body = |
| 57 | expect(body).not |
| 58 | expect(JSON.parse |
| 59 | "Unexpected character '\uFFFD' (1:0)", |
| 60 | ); |
| 61 | }); |
| 62 | |
| 63 | test("a 409 means the attempt is stale", async |
| 64 | const { ghost |
| 65 | await expect(ghost |
| 66 | }); |
| 67 | |
| 68 | test("retries server errors and network failures, then succeeds", async |
| 69 | const { ghost, |
| 70 | new Response(null, |
| 71 | new Error("ECONNREFUSED |
| 72 | new Response(null, |
| 73 | ]); |
| 74 | await ghost.issues |
| 75 | expect(sleeps).toEqual |
| 76 | }); |
| 77 | |
| 78 | test("gives up after the last retry", async |
| 79 | const { ghost |
| 80 | new Error("down |
| 81 | new Error("down |
| 82 | new Error("down |
| 83 | new Error("down |
| 84 | ]); |
| 85 | await expect(ghost |
| 86 | "The API did not take comments: down", |
| 87 | ); |
| 88 | }); |
| 89 | |
| 90 | test("a 4xx other than 409 is not retried", async |
| 91 | const { ghost, |
| 92 | await expect( |
| 93 | ghost.finish({ |
| 94 | ).rejects.toThrow( |
| 95 | expect(sleeps).toEqual |
| 96 | }); |