mirror of
https://github.com/alkimake/paperclip.git
synced 2026-06-14 01:50:39 +09:00
141 lines
4.2 KiB
TypeScript
141 lines
4.2 KiB
TypeScript
import express from "express";
|
|
import request from "supertest";
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mockIssueService = vi.hoisted(() => ({
|
|
getById: vi.fn(),
|
|
getWakeableParentAfterChildCompletion: vi.fn(),
|
|
listWakeableBlockedDependents: vi.fn(),
|
|
update: vi.fn(),
|
|
}));
|
|
|
|
const mockAgentService = vi.hoisted(() => ({
|
|
getById: vi.fn(),
|
|
}));
|
|
|
|
const mockTrackAgentTaskCompleted = vi.hoisted(() => vi.fn());
|
|
const mockGetTelemetryClient = vi.hoisted(() => vi.fn());
|
|
|
|
function registerRouteMocks() {
|
|
vi.doMock("@paperclipai/shared/telemetry", () => ({
|
|
trackAgentTaskCompleted: mockTrackAgentTaskCompleted,
|
|
trackErrorHandlerCrash: vi.fn(),
|
|
}));
|
|
|
|
vi.doMock("../telemetry.js", () => ({
|
|
getTelemetryClient: mockGetTelemetryClient,
|
|
}));
|
|
|
|
vi.doMock("../services/index.js", () => ({
|
|
accessService: () => ({
|
|
canUser: vi.fn(),
|
|
hasPermission: vi.fn(),
|
|
}),
|
|
agentService: () => mockAgentService,
|
|
documentService: () => ({}),
|
|
executionWorkspaceService: () => ({}),
|
|
feedbackService: () => ({}),
|
|
goalService: () => ({}),
|
|
heartbeatService: () => ({
|
|
wakeup: vi.fn(async () => undefined),
|
|
reportRunActivity: vi.fn(async () => undefined),
|
|
}),
|
|
instanceSettingsService: () => ({}),
|
|
issueApprovalService: () => ({}),
|
|
issueService: () => mockIssueService,
|
|
logActivity: vi.fn(async () => undefined),
|
|
projectService: () => ({}),
|
|
routineService: () => ({
|
|
syncRunStatusForIssue: vi.fn(async () => undefined),
|
|
}),
|
|
workProductService: () => ({}),
|
|
}));
|
|
}
|
|
|
|
function makeIssue(status: "todo" | "done") {
|
|
return {
|
|
id: "11111111-1111-4111-8111-111111111111",
|
|
companyId: "company-1",
|
|
status,
|
|
assigneeAgentId: "22222222-2222-4222-8222-222222222222",
|
|
assigneeUserId: null,
|
|
createdByUserId: "local-board",
|
|
identifier: "PAP-1018",
|
|
title: "Telemetry test",
|
|
};
|
|
}
|
|
|
|
async function createApp(actor: Record<string, unknown>) {
|
|
const [{ issueRoutes }, { errorHandler }] = await Promise.all([
|
|
import("../routes/issues.js"),
|
|
import("../middleware/index.js"),
|
|
]);
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => {
|
|
(req as any).actor = actor;
|
|
next();
|
|
});
|
|
app.use("/api", issueRoutes({} as any, {} as any));
|
|
app.use(errorHandler);
|
|
return app;
|
|
}
|
|
|
|
describe("issue telemetry routes", () => {
|
|
beforeEach(() => {
|
|
vi.resetModules();
|
|
registerRouteMocks();
|
|
vi.resetAllMocks();
|
|
mockGetTelemetryClient.mockReturnValue({ track: vi.fn() });
|
|
mockIssueService.getById.mockResolvedValue(makeIssue("todo"));
|
|
mockIssueService.getWakeableParentAfterChildCompletion.mockResolvedValue(null);
|
|
mockIssueService.listWakeableBlockedDependents.mockResolvedValue([]);
|
|
mockIssueService.update.mockImplementation(async (_id: string, patch: Record<string, unknown>) => ({
|
|
...makeIssue("todo"),
|
|
...patch,
|
|
}));
|
|
});
|
|
|
|
it("emits task-completed telemetry with the agent role", async () => {
|
|
mockAgentService.getById.mockResolvedValue({
|
|
id: "agent-1",
|
|
companyId: "company-1",
|
|
role: "engineer",
|
|
adapterType: "codex_local",
|
|
});
|
|
|
|
const app = await createApp({
|
|
type: "agent",
|
|
agentId: "agent-1",
|
|
companyId: "company-1",
|
|
runId: null,
|
|
});
|
|
const res = await request(app)
|
|
.patch("/api/issues/11111111-1111-4111-8111-111111111111")
|
|
.send({ status: "done" });
|
|
|
|
expect(res.status).toBe(200);
|
|
await vi.waitFor(() => {
|
|
expect(mockTrackAgentTaskCompleted).toHaveBeenCalledWith(expect.anything(), {
|
|
agentRole: "engineer",
|
|
});
|
|
});
|
|
}, 10_000);
|
|
|
|
it("does not emit agent task-completed telemetry for board-driven completions", async () => {
|
|
const app = await createApp({
|
|
type: "board",
|
|
userId: "local-board",
|
|
companyIds: ["company-1"],
|
|
source: "local_implicit",
|
|
isInstanceAdmin: false,
|
|
});
|
|
const res = await request(app)
|
|
.patch("/api/issues/11111111-1111-4111-8111-111111111111")
|
|
.send({ status: "done" });
|
|
|
|
expect(res.status).toBe(200);
|
|
expect(mockTrackAgentTaskCompleted).not.toHaveBeenCalled();
|
|
expect(mockAgentService.getById).not.toHaveBeenCalled();
|
|
});
|
|
});
|