diff --git a/scripts/docs/src/utils.test.ts b/scripts/docs/src/utils.test.ts index 55ce942ba..f31d5b6b4 100644 --- a/scripts/docs/src/utils.test.ts +++ b/scripts/docs/src/utils.test.ts @@ -1,4 +1,6 @@ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; +import fs from "fs"; +import path from "path"; import { getRepoRoot, getGitHubToken, @@ -40,6 +42,30 @@ describe("utils", () => { "GITHUB_TOKEN environment variable is required.", ); }); + + it("should return token when GH_TOKEN is set as a fallback", () => { + vi.stubEnv("GITHUB_TOKEN", ""); + vi.stubEnv("GH_TOKEN", "fallback-token"); + + const result = getGitHubToken(); + expect(result).toBe("fallback-token"); + }); + + it("should read token from .env file if environment variables are not set", () => { + vi.stubEnv("GITHUB_TOKEN", ""); + vi.stubEnv("GH_TOKEN", ""); + const envPath = path.join(process.cwd(), ".env"); + fs.writeFileSync(envPath, "GITHUB_TOKEN=env-file-token"); + const result = getGitHubToken(); + expect(result).toBe("env-file-token"); + fs.unlinkSync(envPath); + }); + + it("should throw detailed error when no token found", () => { + vi.stubEnv("GITHUB_TOKEN", ""); + vi.stubEnv("GH_TOKEN", ""); + expect(() => getGitHubToken()).toThrow(/Missing GitHub authentication token/); + }); }); describe("getGitHubRepo", () => {