From 5cceddd71c0be161ed30309590779b8089bb44e0 Mon Sep 17 00:00:00 2001 From: Massiles Ghernaout <749-gm213204@users.noreply.www-apps.univ-lehavre.fr> Date: Sun, 15 Feb 2026 13:54:39 +0100 Subject: [PATCH] updated the tests to handle the multi layer router stack, since now we have a middelware for caching --- backend/tests/hashRoutes.test.js | 39 ++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/backend/tests/hashRoutes.test.js b/backend/tests/hashRoutes.test.js index 9737e4c..273f659 100644 --- a/backend/tests/hashRoutes.test.js +++ b/backend/tests/hashRoutes.test.js @@ -4,27 +4,37 @@ import assert from "node:assert/strict"; import hashRouter from "../src/routes/hash.js"; import { redis } from "../src/clients.js"; -function findRouteHandler(router, method, path) { +/** Returns the full middleware/handler stack for the route (so middleware + final handler run with proper next). */ +function getRouteStack(router, method, path) { const targetMethod = method.toLowerCase(); - for (const layer of router.stack) { const route = layer.route; if (!route) continue; - if ( route.path === path && route.methods && route.methods[targetMethod] === true ) { - // Simple routes have a single layer in route.stack const stack = route.stack || []; if (stack.length > 0) { - return stack[0].handle; + return stack.map((l) => l.handle); } } } + throw new Error(`Route for [${method.toUpperCase()} ${path}] not found`); +} - throw new Error(`Route handler for [${method.toUpperCase()} ${path}] not found`); +/** Run the full route stack (middleware + handler) with a proper next(). */ +async function runRoute(req, res, router, method, path) { + const stack = getRouteStack(router, method, path); + let i = 0; + function next() { + if (i >= stack.length) return; + const fn = stack[i++]; + const out = fn(req, res, next); + return Promise.resolve(out); + } + return next(); } function createMockRes() { @@ -43,15 +53,13 @@ function createMockRes() { } test("POST /hash/manual validates that hash is required", async () => { - const handler = findRouteHandler(hashRouter, "post", "/manual"); - const req = { body: {} }; const res = createMockRes(); const lpushMock = mock.method(redis, "lpush"); const hsetMock = mock.method(redis, "hset"); - await handler(req, res); + await runRoute(req, res, hashRouter, "post", "/manual"); assert.equal(res.statusCode, 400); assert.deepEqual(res.body, { error: "hash is required" }); @@ -60,20 +68,21 @@ test("POST /hash/manual validates that hash is required", async () => { }); test("POST /hash/manual enqueues a job and returns 202 with an id", async () => { - const handler = findRouteHandler(hashRouter, "post", "/manual"); - const req = { body: { hash: "abc123" } }; const res = createMockRes(); + const hgetMock = mock.method(redis, "hget", async () => null); const lpushMock = mock.method(redis, "lpush", async () => 1); const hsetMock = mock.method(redis, "hset", async () => "OK"); + const ltrimMock = mock.method(redis, "ltrim", async () => "OK"); - await handler(req, res); + await runRoute(req, res, hashRouter, "post", "/manual"); assert.equal(res.statusCode, 202); assert.ok(res.body.id, "response should contain a job id"); - - assert.equal(lpushMock.mock.callCount(), 1); - assert.equal(hsetMock.mock.callCount(), 1); + assert.equal(hgetMock.mock.callCount(), 1); + assert.equal(lpushMock.mock.callCount(), 2); + assert.equal(hsetMock.mock.callCount(), 2); + assert.equal(ltrimMock.mock.callCount(), 1); }); -- GitLab