Files
claude-code-action/test/platform-detector.test.ts
T
Vlad MocanuandClaude Opus 4.6 ac58fb6c14 fix(prd-7): M1 architecture review — consolidate duplication, fix bugs, memoize detection
- F1: Consolidated updateForgejoCommentBody into unified updateCommentBody (~150 lines removed)
- F2: Fixed wrong run identifier in update-with-branch.ts (context.runId → getForgejoRunIndex())
- F3: Memoized detectPlatform() with _resetPlatformCache() for tests
- F4: Added getServerUrl() runtime helper to resolve constant-vs-function inconsistency
- F5: Fixed sticky comment bot matching on Forgejo via context.inputs.botName
- F6: Documented GHES heuristic misidentification in docs/forgejo.md
- Updated PRD M1 checkbox to complete

Progress: 17% complete — M1 done, M2 (error handling review) next

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-26 12:09:02 +02:00

96 lines
3.2 KiB
TypeScript

import { detectPlatform, _resetPlatformCache } from "../src/platform/detector";
import { describe, it, expect, beforeEach } from "bun:test";
describe("detectPlatform", () => {
beforeEach(() => {
// Reset memoized result and env vars before each test
_resetPlatformCache();
delete process.env.PLATFORM;
delete process.env.FORGEJO;
delete process.env.GITHUB_SERVER_URL;
});
// Restore original env after all tests in this file
// (bun:test doesn't have afterAll cleanup issues like jest, but let's be safe)
describe("explicit PLATFORM env var", () => {
it("should return 'forgejo' when PLATFORM is 'forgejo'", () => {
process.env.PLATFORM = "forgejo";
expect(detectPlatform()).toBe("forgejo");
});
it("should return 'github' when PLATFORM is 'github'", () => {
process.env.PLATFORM = "github";
expect(detectPlatform()).toBe("github");
});
it("should take precedence over FORGEJO env var", () => {
process.env.PLATFORM = "github";
process.env.FORGEJO = "true";
expect(detectPlatform()).toBe("github");
});
it("should take precedence over GITHUB_SERVER_URL", () => {
process.env.PLATFORM = "github";
process.env.GITHUB_SERVER_URL = "https://git.example.com";
expect(detectPlatform()).toBe("github");
});
it("should ignore unrecognized PLATFORM values and fall through", () => {
process.env.PLATFORM = "gitlab";
// No other env vars set, should default to github
expect(detectPlatform()).toBe("github");
});
});
describe("FORGEJO env var", () => {
it("should return 'forgejo' when FORGEJO is 'true'", () => {
process.env.FORGEJO = "true";
expect(detectPlatform()).toBe("forgejo");
});
it("should not detect forgejo when FORGEJO is 'false'", () => {
process.env.FORGEJO = "false";
expect(detectPlatform()).toBe("github");
});
it("should not detect forgejo when FORGEJO is empty", () => {
process.env.FORGEJO = "";
expect(detectPlatform()).toBe("github");
});
});
describe("GITHUB_SERVER_URL heuristic", () => {
it("should return 'forgejo' for non-github.com server URLs", () => {
process.env.GITHUB_SERVER_URL = "https://git.wxs.ro";
expect(detectPlatform()).toBe("forgejo");
});
it("should return 'forgejo' for gitea/forgejo instances", () => {
process.env.GITHUB_SERVER_URL = "https://forgejo.example.org";
expect(detectPlatform()).toBe("forgejo");
});
it("should return 'github' for github.com", () => {
process.env.GITHUB_SERVER_URL = "https://github.com";
expect(detectPlatform()).toBe("github");
});
it("should return 'github' for GitHub Enterprise (contains github.com)", () => {
process.env.GITHUB_SERVER_URL = "https://github.com/enterprise";
expect(detectPlatform()).toBe("github");
});
it("should return 'github' when GITHUB_SERVER_URL is empty", () => {
process.env.GITHUB_SERVER_URL = "";
expect(detectPlatform()).toBe("github");
});
});
describe("default behavior", () => {
it("should return 'github' when no env vars are set", () => {
expect(detectPlatform()).toBe("github");
});
});
});