sensai
Version:
Because even AI needs a master
120 lines (119 loc) • 5.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
const _nodetest = /*#__PURE__*/ _interop_require_default(require("node:test"));
const _nodeassert = /*#__PURE__*/ _interop_require_default(require("node:assert"));
const _middlewares = /*#__PURE__*/ _interop_require_default(require("./middlewares"));
const _nodepath = require("node:path");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
(0, _nodetest.default)('should add and get middlewares', ()=>{
const { add, get } = (0, _middlewares.default)();
const path1 = (0, _nodepath.join)(_nodepath.sep, 'api', 'middleware.ts');
add(path1);
const path2 = (0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'middleware.ts');
add(path2);
_nodeassert.default.deepEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'route.get.ts')), [
path1
]);
_nodeassert.default.deepEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'route.post.ts')), [
path1,
path2
]);
});
(0, _nodetest.default)('should return middlewares in the correct order (parent to child)', ()=>{
const { add, get } = (0, _middlewares.default)();
const paths = [
(0, _nodepath.join)(_nodepath.sep, 'api', 'middleware.ts'),
(0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'middleware.ts'),
(0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'world', 'middleware.ts')
];
paths.map(add);
_nodeassert.default.deepStrictEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'world', 'route.get.ts')), paths);
});
(0, _nodetest.default)('should remove middleware from the tree', ()=>{
const { add, get, remove } = (0, _middlewares.default)();
const path1 = (0, _nodepath.join)(_nodepath.sep, 'api', 'middleware.ts');
add(path1);
const path2 = (0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'middleware.ts');
add(path2);
remove(path1);
_nodeassert.default.deepStrictEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'route.get.ts')), [
path2
]);
});
(0, _nodetest.default)('should return empty array if no middlewares apply', ()=>{
const { get } = (0, _middlewares.default)();
_nodeassert.default.deepStrictEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'route.get.ts')), []);
});
(0, _nodetest.default)('should handle paths that do not exist in the folder structure', ()=>{
const { add, get } = (0, _middlewares.default)();
const path = (0, _nodepath.join)(_nodepath.sep, 'api', 'middleware.ts');
add(path);
_nodeassert.default.deepStrictEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'nonexistent', 'route.get.ts')), [
path
]);
});
(0, _nodetest.default)('should handle middleware at deeply nested paths', ()=>{
const { add, get } = (0, _middlewares.default)();
const path1 = (0, _nodepath.join)(_nodepath.sep, 'api', 'middleware.ts');
const path2 = (0, _nodepath.join)(_nodepath.sep, 'api', 'deep', 'nested', 'path', 'middleware.ts');
add(path1);
add(path2);
_nodeassert.default.deepStrictEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'deep', 'nested', 'path', 'route.get.ts')), [
path1,
path2
]);
});
(0, _nodetest.default)('should add multiple middlewares for the same path and sort them within a folder', ()=>{
const { add, get } = (0, _middlewares.default)();
const path1 = (0, _nodepath.join)(_nodepath.sep, 'api', 'middleware.ts');
const path2 = (0, _nodepath.join)(_nodepath.sep, 'api', 'authorizer.ts');
add(path1);
add(path2);
const path3 = (0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'middleware.ts');
add(path3);
_nodeassert.default.deepEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'route.get.ts')), [
path2,
path1
]);
_nodeassert.default.deepEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'route.get.ts')), [
path2,
path1,
path3
]);
});
(0, _nodetest.default)('should not add same middleware mutliple times', ()=>{
const { add, get } = (0, _middlewares.default)();
const path1 = (0, _nodepath.join)(_nodepath.sep, 'api', 'middleware.ts');
add(path1);
add(path1);
const path2 = (0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'middleware.ts');
add(path2);
_nodeassert.default.deepEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'route.get.ts')), [
path1
]);
_nodeassert.default.deepEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'route.get.ts')), [
path1,
path2
]);
});
(0, _nodetest.default)('should remove middleware', ()=>{
const { add, get, remove } = (0, _middlewares.default)();
const path1 = (0, _nodepath.join)(_nodepath.sep, 'api', 'middleware.ts');
add(path1);
const path2 = (0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'middleware.ts');
add(path2);
_nodeassert.default.deepEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'route.post.ts')), [
path1,
path2
]);
remove(path1);
_nodeassert.default.deepEqual(get((0, _nodepath.join)(_nodepath.sep, 'api', 'hello', 'route.post.ts')), [
path2
]);
});