refactor: extract static page routes into routes/pages.ts (TDD)
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 18m9s
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 18m9s
- Created src/routes/pages.ts with pagesRouter consolidating all page-serving routes: /, /docs, /impressum, /privacy, /terms, /examples, /status, /favicon.ico, /openapi.json, /api - Reduced index.ts from 391 to 314 lines (20% reduction) - Removed unused imports (createRequire, APP_VERSION, swaggerSpec) from index.ts - 4 TDD tests verifying router exports and route definitions - 622 tests passing, 0 tsc errors
This commit is contained in:
parent
76b2179be9
commit
7ae20ea280
3 changed files with 161 additions and 81 deletions
77
src/__tests__/pages-router.test.ts
Normal file
77
src/__tests__/pages-router.test.ts
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
// Mock sendFile to track calls
|
||||
const sendFileMock = vi.fn();
|
||||
const setHeaderMock = vi.fn().mockReturnThis();
|
||||
|
||||
vi.mock("express", async () => {
|
||||
const actual = await vi.importActual("express");
|
||||
return actual;
|
||||
});
|
||||
|
||||
describe("pages router", () => {
|
||||
it("exports a pagesRouter express Router", async () => {
|
||||
const { pagesRouter } = await import("../routes/pages.js");
|
||||
expect(pagesRouter).toBeDefined();
|
||||
expect(typeof pagesRouter).toBe("function"); // Express routers are functions
|
||||
});
|
||||
|
||||
it("defines GET routes for all static pages", async () => {
|
||||
const { pagesRouter } = await import("../routes/pages.js");
|
||||
// Express Router stores routes in router.stack
|
||||
const routes = (pagesRouter as any).stack
|
||||
.filter((layer: any) => layer.route)
|
||||
.map((layer: any) => ({
|
||||
path: layer.route.path,
|
||||
method: Object.keys(layer.route.methods)[0],
|
||||
}));
|
||||
|
||||
const expectedPages = [
|
||||
{ path: "/", method: "get" },
|
||||
{ path: "/docs", method: "get" },
|
||||
{ path: "/impressum", method: "get" },
|
||||
{ path: "/privacy", method: "get" },
|
||||
{ path: "/terms", method: "get" },
|
||||
{ path: "/examples", method: "get" },
|
||||
{ path: "/status", method: "get" },
|
||||
{ path: "/favicon.ico", method: "get" },
|
||||
];
|
||||
|
||||
for (const expected of expectedPages) {
|
||||
const found = routes.find(
|
||||
(r: any) => r.path === expected.path && r.method === expected.method
|
||||
);
|
||||
expect(found, `Missing route: GET ${expected.path}`).toBeDefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("defines GET /openapi.json route", async () => {
|
||||
const { pagesRouter } = await import("../routes/pages.js");
|
||||
const routes = (pagesRouter as any).stack
|
||||
.filter((layer: any) => layer.route)
|
||||
.map((layer: any) => ({
|
||||
path: layer.route.path,
|
||||
method: Object.keys(layer.route.methods)[0],
|
||||
}));
|
||||
|
||||
const found = routes.find(
|
||||
(r: any) => r.path === "/openapi.json" && r.method === "get"
|
||||
);
|
||||
expect(found, "Missing route: GET /openapi.json").toBeDefined();
|
||||
});
|
||||
|
||||
it("defines GET /api route", async () => {
|
||||
const { pagesRouter } = await import("../routes/pages.js");
|
||||
const routes = (pagesRouter as any).stack
|
||||
.filter((layer: any) => layer.route)
|
||||
.map((layer: any) => ({
|
||||
path: layer.route.path,
|
||||
method: Object.keys(layer.route.methods)[0],
|
||||
}));
|
||||
|
||||
const found = routes.find(
|
||||
(r: any) => r.path === "/api" && r.method === "get"
|
||||
);
|
||||
expect(found, "Missing route: GET /api").toBeDefined();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue