Update router and tests to fix issues
This commit is contained in:
1 parent
223ab79d21
commit
af4f5d87ca
2 files changed
+120
-50
No files matched your search
@@ -1,35 +1,104 @@
|
|||||||
import { describe, it } from 'node:test';
|
import { describe, it } from 'node:test';
|
||||||
import assert from 'node:assert/strict';
|
import assert from 'node:assert/strict';
|
||||||
import { buildCategoryTree, CategoryNode } from './tree.utility.ts';
|
import { createCategoryRouter } from './category.router.ts';
|
||||||
|
import { CategoryRepository } from './category.repository.ts';
|
||||||
|
import { Request, Response } from 'express';
|
||||||
|
|
||||||
describe('Tree Utility - buildCategoryTree()', () => {
|
// 1. Generate a mock factory for the Express Response object
|
||||||
it('should transform a flat list of ltree rows into a nested JSON structure', () => {
|
function createMockResponse() {
|
||||||
const flatNodes: CategoryNode[] = [
|
const res: Partial<Response> = {};
|
||||||
{ id: 1, name: 'Root', path: 'Top' },
|
const data: { statusCode: number; jsonPayload: any } = { statusCode: 200, jsonPayload: null };
|
||||||
{ id: 3, name: 'Astronomy', path: 'Top.Science.Astronomy' },
|
|
||||||
{ id: 2, name: 'Science', path: 'Top.Science' },
|
|
||||||
{ id: 4, name: 'Gardening', path: 'Top.Hobbies.Gardening' },
|
|
||||||
{ id: 5, name: 'Hobbies', path: 'Top.Hobbies' }
|
|
||||||
];
|
|
||||||
|
|
||||||
const tree = buildCategoryTree(flatNodes);
|
res.status = function (code: number) {
|
||||||
|
data.statusCode = code;
|
||||||
|
return this as Response;
|
||||||
|
};
|
||||||
|
|
||||||
// Verify root nodes count
|
res.json = function (payload: any) {
|
||||||
assert.equal(tree.length, 1);
|
data.jsonPayload = payload;
|
||||||
assert.equal(tree[0].name, 'Root');
|
return this as Response;
|
||||||
|
};
|
||||||
|
|
||||||
// Verify second level ("Science" and "Hobbies")
|
return { mockRes: res as Response, data };
|
||||||
const rootChildren = tree[0].children;
|
}
|
||||||
assert.equal(rootChildren.length, 2);
|
|
||||||
|
|
||||||
const scienceNode = rootChildren.find(c => c.name === 'Science');
|
// 2. Generate a mock factory for the repository layer
|
||||||
const hobbiesNode = rootChildren.find(c => c.name === 'Hobbies');
|
function createMockRepository(behavior: 'success' | 'failure') {
|
||||||
|
return {
|
||||||
|
findAllDescendants: async (path: string) => {
|
||||||
|
if (behavior === 'failure') throw new Error('Database connection failed');
|
||||||
|
// Return a valid single-root array format mimicking real ltree database outputs
|
||||||
|
return [
|
||||||
|
{ id: 1, name: 'Top', path: 'Top' },
|
||||||
|
{ id: 2, name: 'Science', path: 'Top.Science' }
|
||||||
|
];
|
||||||
|
}
|
||||||
|
} as unknown as CategoryRepository;
|
||||||
|
}
|
||||||
|
|
||||||
assert.ok(scienceNode);
|
// Helper utility to safely extract the Express handler bypassing strict undefined types
|
||||||
assert.ok(hobbiesNode);
|
function getRouteHandler(router: any): Function {
|
||||||
|
const routeLayer = router.stack.find((layer: any) => layer.route);
|
||||||
|
if (!routeLayer || !routeLayer.route || !routeLayer.route.stack) {
|
||||||
|
throw new Error('Could not find route handler in mock router stack');
|
||||||
|
}
|
||||||
|
return routeLayer.route.stack[0].handle; // Target the primary callback handler array element
|
||||||
|
}
|
||||||
|
|
||||||
// Verify third level deeply nested child ("Astronomy")
|
describe('Category Router Endpoints', () => {
|
||||||
assert.equal(scienceNode.children.length, 1);
|
|
||||||
assert.equal(scienceNode.children[0].name, 'Astronomy');
|
it('GET /tree{/:path} - should return 200 and structural JSON array data on valid requests', async () => {
|
||||||
});
|
const mockRepo = createMockRepository('success');
|
||||||
|
const router = createCategoryRouter(mockRepo);
|
||||||
|
|
||||||
|
const mockReq = { params: { path: 'Top' } } as unknown as Request;
|
||||||
|
const { mockRes, data } = createMockResponse();
|
||||||
|
|
||||||
|
const routeHandler = getRouteHandler(router);
|
||||||
|
await routeHandler(mockReq, mockRes);
|
||||||
|
|
||||||
|
assert.equal(data.statusCode, 200);
|
||||||
|
assert.equal(data.jsonPayload.success, true);
|
||||||
|
|
||||||
|
// Correct alignment: buildCategoryTree returns an ARRAY of root nodes
|
||||||
|
assert.equal(Array.isArray(data.jsonPayload.data), true);
|
||||||
|
assert.equal(data.jsonPayload.data[0].name, 'Top');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('GET /tree{/:path} - should return 400 Bad Request if ltree path contains malformed formatting', async () => {
|
||||||
|
const mockRepo = createMockRepository('success');
|
||||||
|
const router = createCategoryRouter(mockRepo);
|
||||||
|
|
||||||
|
const mockReq = { params: { path: 'Top.Bad Path!' } } as unknown as Request;
|
||||||
|
const { mockRes, data } = createMockResponse();
|
||||||
|
|
||||||
|
const routeHandler = getRouteHandler(router);
|
||||||
|
await routeHandler(mockReq, mockRes);
|
||||||
|
|
||||||
|
assert.equal(data.statusCode, 400);
|
||||||
|
assert.equal(data.jsonPayload.success, false);
|
||||||
|
assert.match(data.jsonPayload.error, /Invalid path format/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('GET /tree{/:path} - should handle runtime database crashes gracefully with a 500 error status', async () => {
|
||||||
|
const mockRepo = createMockRepository('failure');
|
||||||
|
const router = createCategoryRouter(mockRepo);
|
||||||
|
|
||||||
|
const mockReq = { params: { path: 'Top' } } as unknown as Request;
|
||||||
|
const { mockRes, data } = createMockResponse();
|
||||||
|
|
||||||
|
// Prevent console.error from cluttering the test runner feedback during intentional failures
|
||||||
|
const originalConsoleError = console.error;
|
||||||
|
console.error = () => {};
|
||||||
|
|
||||||
|
const routeHandler = getRouteHandler(router);
|
||||||
|
await routeHandler(mockReq, mockRes);
|
||||||
|
|
||||||
|
// Restore original logger tracking
|
||||||
|
console.error = originalConsoleError;
|
||||||
|
|
||||||
|
assert.equal(data.statusCode, 500);
|
||||||
|
assert.equal(data.jsonPayload.success, false);
|
||||||
|
assert.equal(data.jsonPayload.error, 'Internal Server Error');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
+26
-25
@@ -2,34 +2,35 @@ import { Router, Request, Response } from 'express';
|
|||||||
import { CategoryRepository } from './category.repository.ts';
|
import { CategoryRepository } from './category.repository.ts';
|
||||||
import { buildCategoryTree } from './tree.utility.ts';
|
import { buildCategoryTree } from './tree.utility.ts';
|
||||||
|
|
||||||
|
// Clean regex pattern matching valid Postgres ltree structures
|
||||||
|
const LTREE_REGEX = /^[A-Za-z0-9_]+(\.[A-Za-z0-9_]+)*$/;
|
||||||
|
|
||||||
export function createCategoryRouter(repository: CategoryRepository): Router {
|
export function createCategoryRouter(repository: CategoryRepository): Router {
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
/**
|
router.get('/tree{/:path}', async (req: Request, res: Response): Promise<void> => {
|
||||||
* GET /api/categories/tree/:path?
|
try {
|
||||||
* Fetches all descendants of a path and returns them formatted as a tree.
|
const rawPath = req.params.path;
|
||||||
* If no path parameter is provided, it defaults to the root level.
|
const parentPath = Array.isArray(rawPath) ? rawPath[0] : (rawPath || 'Top');
|
||||||
*/
|
|
||||||
router.get('/tree/:path?', async (req: Request, params: Response): Promise<void> => {
|
|
||||||
try {
|
|
||||||
const rawPath = req.params.path;
|
|
||||||
|
|
||||||
const parentPath = Array.isArray(rawPath)
|
// 1. INPUT VALIDATION: Stop malicious or broken ltree strings early
|
||||||
? rawPath[0]
|
if (!LTREE_REGEX.test(parentPath)) {
|
||||||
: (rawPath || 'Top'); // default fallback root value in the database
|
res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Invalid path format. Path elements must be alphanumeric separated by dots.'
|
||||||
|
});
|
||||||
|
return; // Break execution early
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch flat database rows from our repository
|
const flatRows = await repository.findAllDescendants(parentPath);
|
||||||
const flatRows = await repository.findAllDescendants(parentPath);
|
const nestedTree = buildCategoryTree(flatRows);
|
||||||
|
|
||||||
|
res.json({ success: true, data: nestedTree });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to resolve category tree:', error);
|
||||||
|
res.status(500).json({ success: false, error: 'Internal Server Error' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Format data into nested layout
|
return router;
|
||||||
const nestedTree = buildCategoryTree(flatRows);
|
|
||||||
|
|
||||||
params.json({ success: true, data: nestedTree });
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Failed to resolve category tree:', error);
|
|
||||||
params.status(500).json({ success: false, error: 'Internal Server Error' });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return router;
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user