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
10 changes: 10 additions & 0 deletions lib/coding-agent/__tests__/buildPRStateKey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, it, expect } from "vitest";
import { buildPRStateKey } from "../prState/buildPRStateKey";

describe("buildPRStateKey", () => {
it("builds the correct key", () => {
expect(buildPRStateKey("recoupable/api", "agent/fix-bug")).toBe(
"coding-agent:pr:recoupable/api:agent/fix-bug",
);
});
});
22 changes: 22 additions & 0 deletions lib/coding-agent/__tests__/deleteCodingAgentPRState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, it, expect, vi, beforeEach } from "vitest";

const mockDel = vi.fn();

vi.mock("@/lib/redis/connection", () => ({
default: {
del: (...args: unknown[]) => mockDel(...args),
},
}));

const { deleteCodingAgentPRState } = await import("../prState/deleteCodingAgentPRState");

beforeEach(() => {
vi.clearAllMocks();
});

describe("deleteCodingAgentPRState", () => {
it("deletes the key from Redis", async () => {
await deleteCodingAgentPRState("recoupable/api", "agent/fix-bug");
expect(mockDel).toHaveBeenCalledWith("coding-agent:pr:recoupable/api:agent/fix-bug");
});
});
45 changes: 45 additions & 0 deletions lib/coding-agent/__tests__/getCodingAgentPRState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { describe, it, expect, vi, beforeEach } from "vitest";

const mockGet = vi.fn();

vi.mock("@/lib/redis/connection", () => ({
default: {
get: (...args: unknown[]) => mockGet(...args),
},
}));

const { getCodingAgentPRState } = await import("../prState/getCodingAgentPRState");

beforeEach(() => {
vi.clearAllMocks();
});

describe("getCodingAgentPRState", () => {
it("returns null when key does not exist", async () => {
mockGet.mockResolvedValue(null);
const result = await getCodingAgentPRState("recoupable/api", "agent/fix-bug");
expect(result).toBeNull();
expect(mockGet).toHaveBeenCalledWith("coding-agent:pr:recoupable/api:agent/fix-bug");
});

it("returns parsed state when key exists", async () => {
const state = {
status: "pr_created",
snapshotId: "snap_abc",
branch: "agent/fix-bug",
repo: "recoupable/api",
prs: [
{
repo: "recoupable/api",
number: 42,
url: "https://github.com/recoupable/api/pull/42",
baseBranch: "test",
},
],
};
mockGet.mockResolvedValue(JSON.stringify(state));

const result = await getCodingAgentPRState("recoupable/api", "agent/fix-bug");
expect(result).toEqual(state);
});
});
28 changes: 21 additions & 7 deletions lib/coding-agent/__tests__/handleCodingAgentCallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@ vi.mock("chat", () => {
const parts = threadId.split(":");
return `${parts[0]}:${parts[1]}`;
}),
Card: vi.fn((opts) => ({ type: "card", ...opts })),
CardText: vi.fn((text) => ({ type: "text", text })),
Actions: vi.fn((children) => ({ type: "actions", children })),
Button: vi.fn((opts) => ({ type: "button", ...opts })),
LinkButton: vi.fn((opts) => ({ type: "link-button", ...opts })),
Card: vi.fn(opts => ({ type: "card", ...opts })),
CardText: vi.fn(text => ({ type: "text", text })),
Actions: vi.fn(children => ({ type: "actions", children })),
Button: vi.fn(opts => ({ type: "button", ...opts })),
LinkButton: vi.fn(opts => ({ type: "link-button", ...opts })),
};
});

vi.mock("../bot", () => ({
codingAgentBot: {},
}));

const mockSetPRState = vi.fn();
vi.mock("../prState", () => ({
setCodingAgentPRState: (...args: unknown[]) => mockSetPRState(...args),
}));

const { handleCodingAgentCallback } = await import("../handleCodingAgentCallback");

beforeEach(() => {
Expand Down Expand Up @@ -136,7 +141,14 @@ describe("handleCodingAgentCallback", () => {
it("posts updated card with PR buttons for updated status", async () => {
mockState = {
status: "updating",
prs: [{ repo: "recoupable/api", number: 42, url: "https://github.com/recoupable/api/pull/42", baseBranch: "test" }],
prs: [
{
repo: "recoupable/api",
number: 42,
url: "https://github.com/recoupable/api/pull/42",
baseBranch: "test",
},
],
};

const body = {
Expand All @@ -149,7 +161,9 @@ describe("handleCodingAgentCallback", () => {
const response = await handleCodingAgentCallback(request);

expect(response.status).toBe(200);
expect(mockSetState).toHaveBeenCalledWith(expect.objectContaining({ status: "pr_created", snapshotId: "snap_new" }));
expect(mockSetState).toHaveBeenCalledWith(
expect.objectContaining({ status: "pr_created", snapshotId: "snap_new" }),
);
expect(mockPost).toHaveBeenCalledWith(expect.objectContaining({ card: expect.anything() }));
});
});
39 changes: 32 additions & 7 deletions lib/coding-agent/__tests__/handlePRCreated.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ vi.mock("../getThread", () => ({
}));

vi.mock("chat", () => ({
Card: vi.fn((opts) => ({ type: "card", ...opts })),
CardText: vi.fn((text) => ({ type: "text", text })),
Actions: vi.fn((children) => ({ type: "actions", children })),
Button: vi.fn((opts) => ({ type: "button", ...opts })),
LinkButton: vi.fn((opts) => ({ type: "link-button", ...opts })),
Card: vi.fn(opts => ({ type: "card", ...opts })),
CardText: vi.fn(text => ({ type: "text", text })),
Actions: vi.fn(children => ({ type: "actions", children })),
Button: vi.fn(opts => ({ type: "button", ...opts })),
LinkButton: vi.fn(opts => ({ type: "link-button", ...opts })),
}));

const mockSetPRState = vi.fn();
vi.mock("../prState", () => ({
setCodingAgentPRState: (...args: unknown[]) => mockSetPRState(...args),
}));

describe("handlePRCreated", () => {
Expand All @@ -26,10 +31,19 @@ describe("handlePRCreated", () => {
status: "pr_created",
branch: "agent/fix-bug",
snapshotId: "snap_abc",
prs: [{ repo: "recoupable/api", number: 42, url: "https://github.com/recoupable/api/pull/42", baseBranch: "test" }],
prs: [
{
repo: "recoupable/api",
number: 42,
url: "https://github.com/recoupable/api/pull/42",
baseBranch: "test",
},
],
});

expect(mockThread.post).toHaveBeenCalledWith(expect.objectContaining({ card: expect.anything() }));
expect(mockThread.post).toHaveBeenCalledWith(
expect.objectContaining({ card: expect.anything() }),
);

const { Button } = await import("chat");
expect(Button).toHaveBeenCalledWith(
Expand All @@ -43,5 +57,16 @@ describe("handlePRCreated", () => {
snapshotId: "snap_abc",
}),
);

expect(mockSetPRState).toHaveBeenCalledWith(
"recoupable/api",
"agent/fix-bug",
expect.objectContaining({
status: "pr_created",
snapshotId: "snap_abc",
branch: "agent/fix-bug",
repo: "recoupable/api",
}),
);
});
});
75 changes: 67 additions & 8 deletions lib/coding-agent/__tests__/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ vi.mock("@/lib/trigger/triggerUpdatePR", () => ({
}));

vi.mock("chat", () => ({
Card: vi.fn((opts) => ({ type: "card", ...opts })),
CardText: vi.fn((text) => ({ type: "text", text })),
Actions: vi.fn((children) => ({ type: "actions", children })),
LinkButton: vi.fn((opts) => ({ type: "link-button", ...opts })),
Card: vi.fn(opts => ({ type: "card", ...opts })),
CardText: vi.fn(text => ({ type: "text", text })),
Actions: vi.fn(children => ({ type: "actions", children })),
LinkButton: vi.fn(opts => ({ type: "link-button", ...opts })),
}));

vi.mock("../prState", () => ({
getCodingAgentPRState: vi.fn().mockResolvedValue(null),
setCodingAgentPRState: vi.fn(),
}));

const { registerOnNewMention } = await import("../handlers/onNewMention");
Expand Down Expand Up @@ -61,7 +66,9 @@ describe("registerOnNewMention", () => {

expect(mockThread.subscribe).toHaveBeenCalledOnce();
expect(mockTriggerCodingAgent).toHaveBeenCalled();
expect(mockThread.post).toHaveBeenCalledWith(expect.objectContaining({ card: expect.anything() }));
expect(mockThread.post).toHaveBeenCalledWith(
expect.objectContaining({ card: expect.anything() }),
);
expect(mockThread.setState).toHaveBeenCalledWith(
expect.objectContaining({
status: "running",
Expand All @@ -82,7 +89,14 @@ describe("registerOnNewMention", () => {
prompt: "original prompt",
snapshotId: "snap_abc",
branch: "agent/fix-bug",
prs: [{ repo: "recoupable/tasks", number: 56, url: "https://github.com/recoupable/tasks/pull/56", baseBranch: "main" }],
prs: [
{
repo: "recoupable/tasks",
number: 56,
url: "https://github.com/recoupable/tasks/pull/56",
baseBranch: "main",
},
],
}),
subscribe: vi.fn(),
post: vi.fn(),
Expand All @@ -104,8 +118,53 @@ describe("registerOnNewMention", () => {
repo: "recoupable/tasks",
}),
);
expect(mockThread.post).toHaveBeenCalledWith(expect.objectContaining({ card: expect.anything() }));
expect(mockThread.setState).toHaveBeenCalledWith(expect.objectContaining({ status: "updating" }));
expect(mockThread.post).toHaveBeenCalledWith(
expect.objectContaining({ card: expect.anything() }),
);
expect(mockThread.setState).toHaveBeenCalledWith(
expect.objectContaining({ status: "updating" }),
);
});

it("resolves PR state from shared key when thread state is null and raw has repo/branch", async () => {
const { getCodingAgentPRState } = await import("../prState");
vi.mocked(getCodingAgentPRState).mockResolvedValue({
status: "pr_created",
snapshotId: "snap_abc",
branch: "agent/fix-bug",
repo: "recoupable/api",
prs: [{ repo: "recoupable/api", number: 42, url: "url", baseBranch: "test" }],
});

const bot = createMockBot();
registerOnNewMention(bot);
const handler = bot.onNewMention.mock.calls[0][0];

const mockThread = {
id: "github:recoupable/api:42",
state: Promise.resolve(null),
subscribe: vi.fn(),
post: vi.fn(),
setState: vi.fn(),
};
const mockMessage = {
text: "make the button blue",
author: { id: "sweetmantech" },
raw: { repo: "recoupable/api", branch: "agent/fix-bug" },
};

await handler(mockThread, mockMessage);

expect(getCodingAgentPRState).toHaveBeenCalledWith("recoupable/api", "agent/fix-bug");
expect(mockTriggerUpdatePR).toHaveBeenCalledWith(
expect.objectContaining({
feedback: "make the button blue",
snapshotId: "snap_abc",
branch: "agent/fix-bug",
repo: "recoupable/api",
}),
);
expect(mockTriggerCodingAgent).not.toHaveBeenCalled();
});

it("tells user to wait when thread is already running", async () => {
Expand Down
12 changes: 11 additions & 1 deletion lib/coding-agent/__tests__/onMergeAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ import { describe, it, expect, vi, beforeEach } from "vitest";

global.fetch = vi.fn();

const mockDeletePRState = vi.fn();
vi.mock("../prState", () => ({
deleteCodingAgentPRState: (...args: unknown[]) => mockDeletePRState(...args),
}));

const { registerOnMergeAction } = await import("../handlers/onMergeAction");

beforeEach(() => {
vi.clearAllMocks();
process.env.GITHUB_TOKEN = "ghp_test";
});

/**
*
*/
function createMockBot() {
return {
onAction: vi.fn(),
Expand All @@ -22,7 +30,7 @@ describe("registerOnMergeAction", () => {
expect(bot.onAction).toHaveBeenCalledWith("merge_all_prs", expect.any(Function));
});

it("squash-merges PRs and posts results", async () => {
it("squash-merges PRs, cleans up shared state, and posts results", async () => {
vi.mocked(fetch).mockResolvedValue({ ok: true } as Response);

const bot = createMockBot();
Expand All @@ -33,6 +41,7 @@ describe("registerOnMergeAction", () => {
state: Promise.resolve({
status: "pr_created",
prompt: "fix bug",
branch: "agent/fix-bug",
prs: [{ repo: "recoupable/api", number: 42, url: "url", baseBranch: "test" }],
}),
post: vi.fn(),
Expand All @@ -46,6 +55,7 @@ describe("registerOnMergeAction", () => {
expect.objectContaining({ method: "PUT" }),
);
expect(mockThread.setState).toHaveBeenCalledWith({ status: "merged" });
expect(mockDeletePRState).toHaveBeenCalledWith("recoupable/api", "agent/fix-bug");
expect(mockThread.post).toHaveBeenCalledWith(expect.stringContaining("merged"));
});

Expand Down
15 changes: 13 additions & 2 deletions lib/coding-agent/__tests__/onSubscribedMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ vi.mock("@/lib/trigger/triggerUpdatePR", () => ({
triggerUpdatePR: vi.fn().mockResolvedValue({ id: "run_456" }),
}));

vi.mock("../prState", () => ({
setCodingAgentPRState: vi.fn(),
}));

const { registerOnSubscribedMessage } = await import("../handlers/onSubscribedMessage");

beforeEach(() => {
vi.clearAllMocks();
});

/**
*
*/
function createMockBot() {
return {
onSubscribedMessage: vi.fn(),
Expand Down Expand Up @@ -45,8 +52,12 @@ describe("registerOnSubscribedMessage", () => {

await handler(mockThread, { text: "make the button blue", author: { userId: "U111" } });

expect(mockThread.post).toHaveBeenCalledWith(expect.objectContaining({ card: expect.anything() }));
expect(mockThread.setState).toHaveBeenCalledWith(expect.objectContaining({ status: "updating" }));
expect(mockThread.post).toHaveBeenCalledWith(
expect.objectContaining({ card: expect.anything() }),
);
expect(mockThread.setState).toHaveBeenCalledWith(
expect.objectContaining({ status: "updating" }),
);
expect(triggerUpdatePR).toHaveBeenCalledWith(
expect.objectContaining({
feedback: "make the button blue",
Expand Down
Loading