| 1 | import { describe, expect, test } from "bun:test"; |
| 2 | |
| 3 | import { PermanentImportError } |
| 4 | import { |
| 5 | GitHubClient, |
| 6 | issueNumberOf, |
| 7 | loginOf, |
| 8 | nextLink, |
| 9 | rateLimitDelay, |
| 10 | wait, |
| 11 | } from "./github.ts"; |
| 12 | |
| 13 | const never = new |
| 14 | |
| 15 | function fakeFetch(responses: |
| 16 | const urls: |
| 17 | const impl = |
| 18 | urls.push(url |
| 19 | const next = |
| 20 | if (!next) |
| 21 | return next; |
| 22 | }) as unknown |
| 23 | return { impl, |
| 24 | } |
| 25 | |
| 26 | describe("rateLimitDelay", () |
| 27 | test("reads retry-after", |
| 28 | expect( |
| 29 | rateLimitDelay( |
| 30 | new Response(null, |
| 31 | 0, |
| 32 | ), |
| 33 | ).toBe(7000); |
| 34 | }); |
| 35 | |
| 36 | test("waits for the reset when the quota is spent", |
| 37 | const response = |
| 38 | status: 403, |
| 39 | headers: { "x-ratelimit-remaining": |
| 40 | }); |
| 41 | expect(rateLimitDelay(response |
| 42 | }); |
| 43 | |
| 44 | test("is null for a 403 that is not a rate limit, and for other statuses", |
| 45 | expect(rateLimitDelay(new |
| 46 | expect(rateLimitDelay(new |
| 47 | }); |
| 48 | }); |
| 49 | |
| 50 | test("nextLink finds rel=next and nothing else", () |
| 51 | expect( |
| 52 | nextLink( |
| 53 | '<https://api.github.com/x?page=2>; rel="next", <https://api.github.com/x?page=9>; rel="last"', |
| 54 | )?.toString(), |
| 55 | ).toBe("https://api.github.com/x?page=2" |
| 56 | expect(nextLink('<https://api.github.com/x?page=1>; rel="prev" |
| 57 | expect(nextLink(null)) |
| 58 | }); |
| 59 | |
| 60 | test("issueNumberOf and loginOf", () |
| 61 | expect(issueNumberOf("https://api.github.com/repos/o/r/issues/42 |
| 62 | expect(loginOf({ |
| 63 | expect(loginOf(null)) |
| 64 | }); |
| 65 | |
| 66 | describe("GitHubClient", () |
| 67 | test("pages follow the Link header", |
| 68 | const { impl, |
| 69 | Response.json([1, |
| 70 | headers: { link: ' |
| 71 | }), |
| 72 | Response.json([3]) |
| 73 | ]); |
| 74 | const pages: |
| 75 | for await (const |
| 76 | "/items", |
| 77 | )) |
| 78 | pages.push(page |
| 79 | expect(pages).toEqual |
| 80 | expect(urls[0]) |
| 81 | }); |
| 82 | |
| 83 | test("waits out a short rate limit and tries again", |
| 84 | const waited: |
| 85 | const { impl |
| 86 | new Response(null, |
| 87 | Response.json({ |
| 88 | ]); |
| 89 | const client = |
| 90 | waited.push(ms |
| 91 | }); |
| 92 | expect(await client |
| 93 | expect(waited).toEqual |
| 94 | }); |
| 95 | |
| 96 | test("gives up on a long rate limit with a retryable error", |
| 97 | const { impl |
| 98 | new Response(null, |
| 99 | ]); |
| 100 | const error = |
| 101 | .get("/x") |
| 102 | .catch((e) |
| 103 | expect(error).toBeInstanceOf |
| 104 | expect(error).not |
| 105 | }); |
| 106 | |
| 107 | test("a token refused from the start is permanent, one that expires mid-attempt is not", |
| 108 | const never401 = |
| 109 | "t", |
| 110 | never, |
| 111 | fakeFetch([new Response |
| 112 | ) |
| 113 | .get("/x") |
| 114 | .catch((e) |
| 115 | expect(never401).toBeInstanceOf |
| 116 | |
| 117 | const client = |
| 118 | "t", |
| 119 | never, |
| 120 | fakeFetch([ |
| 121 | Response.json({}) |
| 122 | new Response("Bad credentials |
| 123 | ]).impl, |
| 124 | ); |
| 125 | await client.get |
| 126 | const expired = |
| 127 | () => null, |
| 128 | (e: Error |
| 129 | ); |
| 130 | expect(expired).toBeInstanceOf |
| 131 | expect(expired).not |
| 132 | expect(expired?.message |
| 133 | }); |
| 134 | |
| 135 | test("a missing repository is permanent, a server error is not", |
| 136 | const missing = |
| 137 | "t", |
| 138 | never, |
| 139 | fakeFetch([new Response |
| 140 | ) |
| 141 | .get("/x") |
| 142 | .catch((e) |
| 143 | expect(missing).toBeInstanceOf |
| 144 | const broken = |
| 145 | "t", |
| 146 | never, |
| 147 | fakeFetch([new Response |
| 148 | ) |
| 149 | .get("/x") |
| 150 | .catch((e) |
| 151 | expect(broken).not |
| 152 | }); |
| 153 | }); |
| 154 | |
| 155 | describe("wait", () |
| 156 | test("resolves after the delay", |
| 157 | await wait(1, |
| 158 | }); |
| 159 | |
| 160 | test("rejects when aborted, before or during", |
| 161 | const before = |
| 162 | before.abort(new |
| 163 | await expect(wait |
| 164 | |
| 165 | const during = |
| 166 | const pending = |
| 167 | during.abort(new |
| 168 | await expect(pending |
| 169 | }); |
| 170 | }); |