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 { errorHandler } from "../middleware/index.js";
import { instanceSettingsRoutes } from "../routes/instance-settings.js";
const mockInstanceSettingsService = vi.hoisted(() => ({
getGeneral: vi.fn(),
@ -13,12 +11,18 @@ const mockInstanceSettingsService = vi.hoisted(() => ({
}));
const mockLogActivity = vi.hoisted(() => vi.fn());
vi.mock("../services/index.js", () => ({
instanceSettingsService: () => mockInstanceSettingsService,
logActivity: mockLogActivity,
}));
function registerRouteMocks() {
vi.doMock("../services/index.js", () => ({
instanceSettingsService: () => mockInstanceSettingsService,
logActivity: mockLogActivity,
}));
}
function createApp(actor: any) {
async function createApp(actor: any) {
const [{ instanceSettingsRoutes }, { errorHandler }] = await Promise.all([
import("../routes/instance-settings.js"),
import("../middleware/index.js"),
]);
const app = express();
app.use(express.json());
app.use((req, _res, next) => {
@ -32,6 +36,8 @@ function createApp(actor: any) {
describe("instance settings routes", () => {
beforeEach(() => {
vi.resetModules();
registerRouteMocks();
vi.clearAllMocks();
mockInstanceSettingsService.getGeneral.mockResolvedValue({
censorUsernameInLogs: false,
@ -61,7 +67,7 @@ describe("instance settings routes", () => {
});
it("allows local board users to read and update experimental settings", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "local-board",
source: "local_implicit",
@ -87,7 +93,7 @@ describe("instance settings routes", () => {
});
it("allows local board users to update guarded dev-server auto-restart", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "local-board",
source: "local_implicit",
@ -105,7 +111,7 @@ describe("instance settings routes", () => {
});
it("allows local board users to read and update general settings", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "local-board",
source: "local_implicit",
@ -138,7 +144,7 @@ describe("instance settings routes", () => {
});
it("allows non-admin board users to read general settings", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "user-1",
source: "session",
@ -153,7 +159,7 @@ describe("instance settings routes", () => {
});
it("rejects non-admin board users from updating general settings", async () => {
const app = createApp({
const app = await createApp({
type: "board",
userId: "user-1",
source: "session",
@ -170,7 +176,7 @@ describe("instance settings routes", () => {
});
it("rejects agent callers", async () => {
const app = createApp({
const app = await createApp({
type: "agent",
agentId: "agent-1",
companyId: "company-1",