mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-08-22 03:18:54 +08:00
test: cover prepareContext validation error branches (#1460)
* test: cover prepareContext validation error branches create-prompt.test.ts exercised only happy paths; the ~20 validation guards in prepareContext (missing PR_NUMBER, unsupported event type, unsupported issue action, missing claude branch, etc.) had no coverage. Adds a "prepareContext validation errors" block asserting the thrown messages for the reachable guards, using the existing createMockContext helper. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> * test: cover comments/common link and body builders `src/github/operations/comments/common.ts` had no direct test coverage, though its exports are live code used by create-initial.ts and update-with-branch.ts. This adds unit tests for all four exports: SPINNER_HTML, createJobRunLink, createBranchLink, and createCommentBody. Assertions are built from the imported GITHUB_SERVER_URL so they hold on GHES as well as github.com. Pure test additions — no production changes. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
ae650f8355
commit
58dc33d9ad
@@ -6,8 +6,10 @@ import {
|
||||
getEventTypeAndContext,
|
||||
buildAllowedToolsString,
|
||||
buildDisallowedToolsString,
|
||||
prepareContext,
|
||||
} from "../src/create-prompt";
|
||||
import type { PreparedContext } from "../src/create-prompt";
|
||||
import { createMockContext } from "./mockContext";
|
||||
|
||||
beforeAll(() => {
|
||||
process.env.GITHUB_ACTION_PATH = "/test/action/path";
|
||||
@@ -1270,3 +1272,83 @@ describe("buildDisallowedToolsString", () => {
|
||||
expect(result).toBe("BadTool1,BadTool2");
|
||||
});
|
||||
});
|
||||
|
||||
describe("prepareContext validation errors", () => {
|
||||
const commentId = "12345";
|
||||
|
||||
test("throws on an unsupported event type", () => {
|
||||
const context = createMockContext({
|
||||
eventName: "deployment_status" as any,
|
||||
});
|
||||
|
||||
expect(() => prepareContext(context, commentId)).toThrow(
|
||||
"Unsupported event type: deployment_status",
|
||||
);
|
||||
});
|
||||
|
||||
test("pull_request event requires a PR number (isPR must be true)", () => {
|
||||
const context = createMockContext({
|
||||
eventName: "pull_request",
|
||||
eventAction: "opened",
|
||||
isPR: false,
|
||||
});
|
||||
|
||||
expect(() => prepareContext(context, commentId)).toThrow(
|
||||
"PR_NUMBER is required for pull_request event",
|
||||
);
|
||||
});
|
||||
|
||||
test("pull_request_review event requires a PR number", () => {
|
||||
const context = createMockContext({
|
||||
eventName: "pull_request_review",
|
||||
isPR: false,
|
||||
payload: {
|
||||
review: { body: "please fix", user: { login: "user1" } },
|
||||
} as any,
|
||||
});
|
||||
|
||||
expect(() => prepareContext(context, commentId)).toThrow(
|
||||
"PR_NUMBER is required for pull_request_review event",
|
||||
);
|
||||
});
|
||||
|
||||
test("issues event requires an event action", () => {
|
||||
const context = createMockContext({
|
||||
eventName: "issues",
|
||||
eventAction: "",
|
||||
isPR: false,
|
||||
payload: { issue: { user: { login: "user1" } } } as any,
|
||||
});
|
||||
|
||||
expect(() => prepareContext(context, commentId)).toThrow(
|
||||
"GITHUB_EVENT_ACTION is required for issues event",
|
||||
);
|
||||
});
|
||||
|
||||
test("issues event rejects an unsupported action", () => {
|
||||
const context = createMockContext({
|
||||
eventName: "issues",
|
||||
eventAction: "deleted",
|
||||
isPR: false,
|
||||
payload: { issue: { user: { login: "user1" } } } as any,
|
||||
});
|
||||
|
||||
expect(() =>
|
||||
prepareContext(context, commentId, "main", "claude/issue-1"),
|
||||
).toThrow("Unsupported issue action: deleted");
|
||||
});
|
||||
|
||||
test("issue_comment on an issue requires a claude branch", () => {
|
||||
const context = createMockContext({
|
||||
eventName: "issue_comment",
|
||||
isPR: false,
|
||||
payload: {
|
||||
comment: { id: 999, body: "@claude help", user: { login: "user1" } },
|
||||
} as any,
|
||||
});
|
||||
|
||||
expect(() => prepareContext(context, commentId)).toThrow(
|
||||
"CLAUDE_BRANCH is required for issue_comment event",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user