Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/sandboxes/__tests__/runClaudeCodeAgent.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

vi.mock("@trigger.dev/sdk/v3", () => ({
logger: { log: vi.fn(), error: vi.fn() },
Expand All @@ -12,6 +12,8 @@ vi.mock("../logStep", () => ({
const { runClaudeCodeAgent } = await import("../runClaudeCodeAgent");
const { logStep } = await import("../logStep");

const originalEnv = { ...process.env };

function mockDetachedCommand(finished: {
exitCode: number;
stdout: () => Promise<string>;
Expand All @@ -26,10 +28,15 @@ function createMockSandbox() {

beforeEach(() => {
vi.clearAllMocks();
process.env.CLAUDE_CODE_OAUTH_TOKEN = "test-oauth-token";
});

afterEach(() => {
process.env = { ...originalEnv };
});

describe("runClaudeCodeAgent", () => {
it("calls claude --print with the message", async () => {
it("calls claude --print with the message and injects CLAUDE_CODE_OAUTH_TOKEN", async () => {
const sandbox = createMockSandbox();
sandbox.runCommand.mockResolvedValueOnce(
mockDetachedCommand({
Expand All @@ -48,10 +55,11 @@ describe("runClaudeCodeAgent", () => {
cmd: "claude",
args: ["-p", "--dangerously-skip-permissions", "Fix the bug"],
detached: true,
env: { CLAUDE_CODE_OAUTH_TOKEN: "test-oauth-token" },
});
});

it("passes env vars when provided", async () => {
it("merges CLAUDE_CODE_OAUTH_TOKEN into provided env", async () => {
const sandbox = createMockSandbox();
sandbox.runCommand.mockResolvedValueOnce(
mockDetachedCommand({
Expand All @@ -71,7 +79,10 @@ describe("runClaudeCodeAgent", () => {
cmd: "claude",
args: ["-p", "--dangerously-skip-permissions", "Update the README"],
detached: true,
env: { GITHUB_TOKEN: "ghp_test" },
env: {
GITHUB_TOKEN: "ghp_test",
CLAUDE_CODE_OAUTH_TOKEN: "test-oauth-token",
},
});
});

Expand Down
8 changes: 4 additions & 4 deletions src/sandboxes/runClaudeCodeAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export async function runClaudeCodeAgent(
cmd: "claude",
args,
detached: true,
env: {
...env,
CLAUDE_CODE_OAUTH_TOKEN: process.env.CLAUDE_CODE_OAUTH_TOKEN,
},
};

if (env) {
commandOpts.env = env;
}

const command = await sandbox.runCommand(commandOpts as any);
const result = await command.wait();

Expand Down
Loading