test: update suites for gitea mode flow

This commit is contained in:
Mark Wylde
2025-09-30 17:31:54 +01:00
parent 3305a16297
commit 5bcd15c520
7 changed files with 228 additions and 1777 deletions

View File

@@ -1,50 +1,51 @@
import { describe, test, expect, beforeEach, afterEach, spyOn } from "bun:test";
import { checkAndDeleteEmptyBranch } from "../src/github/operations/branch-cleanup";
import type { Octokits } from "../src/github/api/client";
import type { GitHubClient } from "../src/github/api/client";
import { GITEA_SERVER_URL } from "../src/github/api/config";
describe("checkAndDeleteEmptyBranch", () => {
let consoleLogSpy: any;
let consoleErrorSpy: any;
const originalEnv = { ...process.env };
beforeEach(() => {
// Spy on console methods
consoleLogSpy = spyOn(console, "log").mockImplementation(() => {});
consoleErrorSpy = spyOn(console, "error").mockImplementation(() => {});
delete process.env.GITEA_API_URL; // ensure GitHub mode for predictable behaviour
});
afterEach(() => {
consoleLogSpy.mockRestore();
consoleErrorSpy.mockRestore();
process.env = { ...originalEnv };
});
const createMockOctokit = (
compareResponse?: any,
deleteRefError?: Error,
): Octokits => {
const createMockClient = (
options: { branchSha?: string; baseSha?: string; error?: Error } = {},
): GitHubClient => {
const { branchSha = "branch-sha", baseSha = "base-sha", error } = options;
return {
rest: {
repos: {
compareCommitsWithBasehead: async () => ({
data: compareResponse || { total_commits: 0 },
}),
},
git: {
deleteRef: async () => {
if (deleteRefError) {
throw deleteRefError;
}
return { data: {} };
},
api: {
getBranch: async (_owner: string, _repo: string, branch: string) => {
if (error) {
throw error;
}
return {
data: {
commit: {
sha: branch.includes("claude/") ? branchSha : baseSha,
},
},
};
},
},
} as any as Octokits;
} as unknown as GitHubClient;
};
test("should return no branch link and not delete when branch is undefined", async () => {
const mockOctokit = createMockOctokit();
test("returns defaults when no claude branch provided", async () => {
const client = createMockClient();
const result = await checkAndDeleteEmptyBranch(
mockOctokit,
client,
"owner",
"repo",
undefined,
@@ -56,94 +57,65 @@ describe("checkAndDeleteEmptyBranch", () => {
expect(consoleLogSpy).not.toHaveBeenCalled();
});
test("should delete branch and return no link when branch has no commits", async () => {
const mockOctokit = createMockOctokit({ total_commits: 0 });
test("marks branch for deletion when SHAs match", async () => {
const client = createMockClient({ branchSha: "same", baseSha: "same" });
const result = await checkAndDeleteEmptyBranch(
mockOctokit,
client,
"owner",
"repo",
"claude/issue-123-20240101_123456",
"claude/issue-123",
"main",
);
expect(result.shouldDeleteBranch).toBe(true);
expect(result.branchLink).toBe("");
expect(consoleLogSpy).toHaveBeenCalledWith(
"Branch claude/issue-123-20240101_123456 has no commits from Claude, will delete it",
"Branch claude/issue-123 has same SHA as base, marking for deletion",
);
expect(consoleLogSpy).toHaveBeenCalledWith(
"✅ Deleted empty branch: claude/issue-123-20240101_123456",
"Skipping branch deletion - not reliably supported across all Git platforms: claude/issue-123",
);
});
test("should not delete branch and return link when branch has commits", async () => {
const mockOctokit = createMockOctokit({ total_commits: 3 });
test("returns branch link when branch has commits", async () => {
const client = createMockClient({ branchSha: "feature", baseSha: "main" });
const result = await checkAndDeleteEmptyBranch(
mockOctokit,
client,
"owner",
"repo",
"claude/issue-123-20240101_123456",
"claude/issue-123",
"main",
);
expect(result.shouldDeleteBranch).toBe(false);
expect(result.branchLink).toBe(
`\n[View branch](${GITEA_SERVER_URL}/owner/repo/src/branch/claude/issue-123-20240101_123456)`,
`\n[View branch](${GITEA_SERVER_URL}/owner/repo/src/branch/claude/issue-123)`,
);
expect(consoleLogSpy).not.toHaveBeenCalledWith(
expect.stringContaining("has no commits"),
expect(consoleLogSpy).toHaveBeenCalledWith(
"Branch claude/issue-123 appears to have commits (different SHA from base)",
);
});
test("should handle branch comparison errors gracefully", async () => {
const mockOctokit = {
rest: {
repos: {
compareCommitsWithBasehead: async () => {
throw new Error("API error");
},
},
git: {
deleteRef: async () => ({ data: {} }),
},
},
} as any as Octokits;
test("falls back to branch link when API call fails", async () => {
const client = createMockClient({ error: Object.assign(new Error("boom"), { status: 500 }) });
const result = await checkAndDeleteEmptyBranch(
mockOctokit,
client,
"owner",
"repo",
"claude/issue-123-20240101_123456",
"claude/issue-123",
"main",
);
expect(result.shouldDeleteBranch).toBe(false);
expect(result.branchLink).toBe(
`\n[View branch](${GITEA_SERVER_URL}/owner/repo/src/branch/claude/issue-123-20240101_123456)`,
`\n[View branch](${GITEA_SERVER_URL}/owner/repo/src/branch/claude/issue-123)`,
);
expect(consoleErrorSpy).toHaveBeenCalledWith(
"Error checking for commits on Claude branch:",
"Error checking branch:",
expect.any(Error),
);
});
test("should handle branch deletion errors gracefully", async () => {
const deleteError = new Error("Delete failed");
const mockOctokit = createMockOctokit({ total_commits: 0 }, deleteError);
const result = await checkAndDeleteEmptyBranch(
mockOctokit,
"owner",
"repo",
"claude/issue-123-20240101_123456",
"main",
);
expect(result.shouldDeleteBranch).toBe(true);
expect(result.branchLink).toBe("");
expect(consoleErrorSpy).toHaveBeenCalledWith(
"Failed to delete branch claude/issue-123-20240101_123456:",
deleteError,
expect(consoleLogSpy).toHaveBeenCalledWith(
"Assuming branch exists due to non-404 error",
);
});
});