From 1b4ebec820ec77b2d4eab24fe69906bc3101382f Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Tue, 24 Nov 2026 19:13:48 -0800 Subject: [PATCH] test: load remaining route modules during setup (#22872) ## Thinking Path > - Paperclip exposes company portability and routine APIs. > - Route fixtures install mocks before loading those APIs. > - Their first test currently pays the cold module-load cost inside its test timer. > - Exact-source verification can time out before that first request is checked. > - This PR moves module loading into the existing setup hooks. > - Each company app keeps a fresh module instance, and routine mocks still reset for every test. ## Linked Issues or Issue Description Refs #22870. **What happened?** The first company-portability and routine-route tests time out at 15 seconds during cold module loading. Both cases failed again in [exact-source verification](https://github.com/paperclipai/paperclip/actions/runs/45534861756/job/138942393280). The attachment fixture fixed in #22870 passed that run. **Expected behavior** Load each route graph after its mocks are installed, in setup. Keep the existing request assertions, test limits, and per-app state isolation. **Steps to reproduce** Run the first case from each original fixture in a cold process with a three-second test budget. Both time out. With the setup change, all 101 tests across the company-portability, routine, and attachment fixtures pass at that same limit. ## What Changed - Share the company route loader across both app factories and `beforeAll`. Keep the existing unique import query for each app, so its import-job map stays separate. - Load routine modules at the end of `beforeEach`, after module reset, mock registration, and default mock values. Keep the sequential imports and the per-test reset. - Reuse each loader in its app factory. Keep the existing test limits and assertions. ## Verification - Node 25.6.1 red/green: both original first cases time out with `--testTimeout=3000`. - `pnpm exec vitest run server/src/__tests__/company-portability-routes.test.ts server/src/__tests__/routines-routes.test.ts server/src/__tests__/issue-attachment-routes.test.ts --testTimeout=3000 --maxWorkers=1 --no-file-parallelism`: all 101 tests passed. - `pnpm exec tsc --noEmit -p server/tsconfig.json` passed. - `pnpm verify --base origin/master --explain` retained 1,921 of 2,276 test files conservatively and requires broad CI coverage. The focused checks above passed; required CI supplies the final-head coverage. No full local suite pass is claimed. - Added-line secret and PII scan passed. - Greptile scored `a22ce8a1358d479e08beb49622278428fe2d3dd4` 5/5 with no unresolved findings. All current-head CI checks passed. GitHub reports the branch clean and mergeable. ## Risks The fixture must load modules after installing its mocks. Routine imports still follow each test's reset. Company imports still use a new query key per app, preserving independent job maps and cross-user tests. Setup remains bounded by the existing hook timeout. The helpers stay in these large test files because they own the fixture mocks and app state. ## Model Used OpenAI Codex, `gpt-6.1-sol`, xhigh reasoning, with tool use and code execution. ## Checklist - [x] Thinking path and issue description included - [x] Model specified - [x] Red/green reproduction and all 101 related route tests pass - [x] Server typecheck passes - [x] Scope is two test fixtures; no run artifacts or secrets in the diff - [x] All required CI gates are green on the current head - [x] Greptile is 5/5 with no unresolved findings Co-authored-by: Paperclip --- .../company-portability-routes.test.ts | 23 ++++++++++--------- server/src/__tests__/routines-routes.test.ts | 10 ++++++-- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/server/src/__tests__/company-portability-routes.test.ts b/server/src/__tests__/company-portability-routes.test.ts index a04c2e87ff2..68474c51575 100644 --- a/server/src/__tests__/company-portability-routes.test.ts +++ b/server/src/__tests__/company-portability-routes.test.ts @@ -1,6 +1,6 @@ import express from "express"; import request from "supertest"; -import { beforeEach, describe, expect, it, vi } from "vitest"; +import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; const mockCompanyService = vi.hoisted(() => ({ list: vi.fn(), @@ -104,15 +104,19 @@ function registerCompanyRouteMocks() { let appImportCounter = 0; -async function createApp(actor: Record) { +async function loadCompanyRouteModules() { registerCompanyRouteMocks(); appImportCounter += 1; const routeModulePath = `../routes/companies.js?company-portability-routes-${appImportCounter}`; const middlewareModulePath = `../middleware/index.js?company-portability-routes-${appImportCounter}`; - const [{ companyRoutes }, { errorHandler }] = await Promise.all([ + return Promise.all([ import(routeModulePath) as Promise, import(middlewareModulePath) as Promise, ]); +} + +async function createApp(actor: Record) { + const [{ companyRoutes }, { errorHandler }] = await loadCompanyRouteModules(); const app = express(); app.use(express.json()); app.use((req, _res, next) => { @@ -224,14 +228,7 @@ function boardActor(userId: string) { // actor is chosen per request from the `x-test-user-id` header. This lets one // app exercise cross-user isolation and the per-user duplicate guard. async function createBoardApp() { - registerCompanyRouteMocks(); - appImportCounter += 1; - const routeModulePath = `../routes/companies.js?company-portability-routes-${appImportCounter}`; - const middlewareModulePath = `../middleware/index.js?company-portability-routes-${appImportCounter}`; - const [{ companyRoutes }, { errorHandler }] = await Promise.all([ - import(routeModulePath) as Promise, - import(middlewareModulePath) as Promise, - ]); + const [{ companyRoutes }, { errorHandler }] = await loadCompanyRouteModules(); const app = express(); app.use(express.json()); app.use((req, _res, next) => { @@ -335,6 +332,10 @@ const importMeta = { }; describe("company portability routes", () => { + beforeAll(async () => { + await loadCompanyRouteModules(); + }); + beforeEach(() => { vi.clearAllMocks(); mockAgentService.getById.mockImplementation(async (id: string) => ({ diff --git a/server/src/__tests__/routines-routes.test.ts b/server/src/__tests__/routines-routes.test.ts index 2e0de518189..1a3edb51b7c 100644 --- a/server/src/__tests__/routines-routes.test.ts +++ b/server/src/__tests__/routines-routes.test.ts @@ -174,11 +174,16 @@ function registerModuleMocks() { })); } -async function createApp(actor: Record) { +async function loadRouteModules() { // Load the route after its mocks are installed. Concurrent importActual // calls through the middleware barrel can cache unmocked dependencies. const { errorHandler } = await import("../middleware/error-handler.js"); const { routineRoutes } = await import("../routes/routines.js"); + return { errorHandler, routineRoutes }; +} + +async function createApp(actor: Record) { + const { errorHandler, routineRoutes } = await loadRouteModules(); const app = express(); app.use(express.json({ verify: (req, _res, buf) => { (req as any).rawBody = buf; } })); app.use((req, _res, next) => { @@ -203,7 +208,7 @@ describe("routine routes", () => { })); }); - beforeEach(() => { + beforeEach(async () => { vi.resetModules(); vi.doUnmock("@paperclipai/shared/telemetry"); vi.doUnmock("../telemetry.js"); @@ -327,6 +332,7 @@ describe("routine routes", () => { status: "resolved", }); mockAnnotationService.remapOpenThreadsForRoutineDocument.mockResolvedValue([]); + await loadRouteModules(); }); it("passes project filters to the routine list service", async () => {