|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | + |
| 3 | +import { gateAuth, resetAuthCache } from "./github-auth.js"; |
| 4 | + |
| 5 | +describe("gateAuth", () => { |
| 6 | + test("returns github_auth_missing when no token is available", () => { |
| 7 | + const origGH = process.env.GITHUB_TOKEN; |
| 8 | + const origGHT = process.env.GH_TOKEN; |
| 9 | + delete process.env.GITHUB_TOKEN; |
| 10 | + delete process.env.GH_TOKEN; |
| 11 | + resetAuthCache(); |
| 12 | + |
| 13 | + const result = gateAuth(); |
| 14 | + // If gh CLI is installed and authed, this will succeed — that's fine. |
| 15 | + // We only assert the shape is correct either way. |
| 16 | + expect(result).toHaveProperty("ok"); |
| 17 | + if (!result.ok) { |
| 18 | + expect(result.body).toHaveProperty("error", "github_auth_missing"); |
| 19 | + } |
| 20 | + |
| 21 | + // Restore |
| 22 | + if (origGH !== undefined) process.env.GITHUB_TOKEN = origGH; |
| 23 | + if (origGHT !== undefined) process.env.GH_TOKEN = origGHT; |
| 24 | + resetAuthCache(); |
| 25 | + }); |
| 26 | + |
| 27 | + test("returns ok when GITHUB_TOKEN is set", () => { |
| 28 | + const origGH = process.env.GITHUB_TOKEN; |
| 29 | + const origGHT = process.env.GH_TOKEN; |
| 30 | + process.env.GITHUB_TOKEN = "test-token-123"; |
| 31 | + delete process.env.GH_TOKEN; |
| 32 | + resetAuthCache(); |
| 33 | + |
| 34 | + const result = gateAuth(); |
| 35 | + expect(result.ok).toBe(true); |
| 36 | + if (result.ok) { |
| 37 | + expect(result.token).toBe("test-token-123"); |
| 38 | + } |
| 39 | + |
| 40 | + // Restore |
| 41 | + if (origGH !== undefined) { |
| 42 | + process.env.GITHUB_TOKEN = origGH; |
| 43 | + } else { |
| 44 | + delete process.env.GITHUB_TOKEN; |
| 45 | + } |
| 46 | + if (origGHT !== undefined) process.env.GH_TOKEN = origGHT; |
| 47 | + resetAuthCache(); |
| 48 | + }); |
| 49 | + |
| 50 | + test("falls back to GH_TOKEN", () => { |
| 51 | + const origGH = process.env.GITHUB_TOKEN; |
| 52 | + const origGHT = process.env.GH_TOKEN; |
| 53 | + delete process.env.GITHUB_TOKEN; |
| 54 | + process.env.GH_TOKEN = "gh-token-456"; |
| 55 | + resetAuthCache(); |
| 56 | + |
| 57 | + const result = gateAuth(); |
| 58 | + expect(result.ok).toBe(true); |
| 59 | + if (result.ok) { |
| 60 | + expect(result.token).toBe("gh-token-456"); |
| 61 | + } |
| 62 | + |
| 63 | + // Restore |
| 64 | + if (origGH !== undefined) process.env.GITHUB_TOKEN = origGH; |
| 65 | + if (origGHT !== undefined) { |
| 66 | + process.env.GH_TOKEN = origGHT; |
| 67 | + } else { |
| 68 | + delete process.env.GH_TOKEN; |
| 69 | + } |
| 70 | + resetAuthCache(); |
| 71 | + }); |
| 72 | +}); |
0 commit comments