| 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("a 409 means the attempt is stale", async |
| 45 | const { ghost |
| 46 | await expect(ghost |
| 47 | }); |
| 48 | |
| 49 | test("retries server errors and network failures, then succeeds", async |
| 50 | const { ghost, |
| 51 | new Response(null, |
| 52 | new Error("ECONNREFUSED |
| 53 | new Response(null, |
| 54 | ]); |
| 55 | await ghost.issues |
| 56 | expect(sleeps).toEqual |
| 57 | }); |
| 58 | |
| 59 | test("gives up after the last retry", async |
| 60 | const { ghost |
| 61 | new Error("down |
| 62 | new Error("down |
| 63 | new Error("down |
| 64 | new Error("down |
| 65 | ]); |
| 66 | await expect(ghost |
| 67 | "The API did not take comments: down", |
| 68 | ); |
| 69 | }); |
| 70 | |
| 71 | test("a 4xx other than 409 is not retried", async |
| 72 | const { ghost, |
| 73 | await expect( |
| 74 | ghost.finish({ |
| 75 | ).rejects.toThrow( |
| 76 | expect(sleeps).toEqual |
| 77 | }); |