test(server): isolate route modules in endpoint tests

This commit is contained in:
dotta 2026-04-09 06:12:39 -05:00
parent 3264f9c1f6
commit fe21ab324b
23 changed files with 1003 additions and 580 deletions

View file

@ -1,8 +1,6 @@
import express from "express";
import request from "supertest";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { routineRoutes } from "../routes/routines.js";
import { errorHandler } from "../middleware/index.js";
const companyId = "22222222-2222-4222-8222-222222222222";
const agentId = "11111111-1111-4111-8111-111111111111";
@ -85,27 +83,28 @@ const mockLogActivity = vi.hoisted(() => vi.fn());
const mockTrackRoutineCreated = vi.hoisted(() => vi.fn());
const mockGetTelemetryClient = vi.hoisted(() => vi.fn());
vi.mock("@paperclipai/shared/telemetry", async () => {
const actual = await vi.importActual<typeof import("@paperclipai/shared/telemetry")>(
"@paperclipai/shared/telemetry",
);
return {
...actual,
function registerRouteMocks() {
vi.doMock("@paperclipai/shared/telemetry", () => ({
trackRoutineCreated: mockTrackRoutineCreated,
};
});
trackErrorHandlerCrash: vi.fn(),
}));
vi.mock("../telemetry.js", () => ({
getTelemetryClient: mockGetTelemetryClient,
}));
vi.doMock("../telemetry.js", () => ({
getTelemetryClient: mockGetTelemetryClient,
}));
vi.mock("../services/index.js", () => ({
accessService: () => mockAccessService,
logActivity: mockLogActivity,
routineService: () => mockRoutineService,
}));
vi.doMock("../services/index.js", () => ({
accessService: () => mockAccessService,
logActivity: mockLogActivity,
routineService: () => mockRoutineService,
}));
}
function createApp(actor: Record<string, unknown>) {
async function createApp(actor: Record<string, unknown>) {
const [{ routineRoutes }, { errorHandler }] = await Promise.all([
import("../routes/routines.js"),
import("../middleware/index.js"),
]);
const app = express();
app.use(express.json());
app.use((req, _res, next) => {
@ -119,6 +118,8 @@ function createApp(actor: Record<string, unknown>) {
describe("routine routes", () => {
beforeEach(() => {
vi.resetModules();
registerRouteMocks();
vi.clearAllMocks();
mockGetTelemetryClient.mockReturnValue({ track: vi.fn() });
mockRoutineService.create.mockResolvedValue(routine);
@ -135,7 +136,7 @@ describe("routine routes", () => {
});
it("requires tasks:assign permission for non-admin board routine creation", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "board-user",
source: "session",
@ -157,7 +158,7 @@ describe("routine routes", () => {
});
it("requires tasks:assign permission to retarget a routine assignee", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "board-user",
source: "session",
@ -178,7 +179,7 @@ describe("routine routes", () => {
it("requires tasks:assign permission to reactivate a routine", async () => {
mockRoutineService.get.mockResolvedValue(pausedRoutine);
const app = createApp({
const app = await createApp({
type: "board",
userId: "board-user",
source: "session",
@ -198,7 +199,7 @@ describe("routine routes", () => {
});
it("requires tasks:assign permission to create a trigger", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "board-user",
source: "session",
@ -220,7 +221,7 @@ describe("routine routes", () => {
});
it("requires tasks:assign permission to update a trigger", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "board-user",
source: "session",
@ -240,7 +241,7 @@ describe("routine routes", () => {
});
it("requires tasks:assign permission to manually run a routine", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "board-user",
source: "session",
@ -259,7 +260,7 @@ describe("routine routes", () => {
it("allows routine creation when the board user has tasks:assign", async () => {
mockAccessService.canUser.mockResolvedValue(true);
const app = createApp({
const app = await createApp({
type: "board",
userId: "board-user",
source: "session",