UNPKG

sensai

Version:

Because even AI needs a master

199 lines (198 loc) 6.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _nodetest = require("node:test"); const _nodeassert = /*#__PURE__*/ _interop_require_default(require("node:assert")); const _guard = /*#__PURE__*/ _interop_require_wildcard(require("./guard")); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } (0, _nodetest.describe)("guard", ()=>{ // Test basic functionality of guard (0, _nodetest.it)("should wrap a handler function", async ()=>{ const wrappedHandler = (0, _guard.default)((data)=>{ return { success: true, data }; }); _nodeassert.default.equal(typeof wrappedHandler, "function"); const result = await wrappedHandler({ name: "test" }); _nodeassert.default.deepStrictEqual(result, { success: true, data: { name: "test" } }); }); // Test with input schema (0, _nodetest.it)("should pass validated data to the handler", async ()=>{ const schema = { type: "object", properties: { name: { type: "string" }, age: { type: "number" } }, required: [ "name" ] }; const wrappedHandler = (0, _guard.default)((data)=>{ return { received: data }; }, { input: schema }); const result = await wrappedHandler({ name: "John", age: 30 }); _nodeassert.default.deepStrictEqual(result, { received: { name: "John", age: 30 } }); }); // Test with additional arguments (0, _nodetest.it)("should pass additional arguments to the handler", async ()=>{ const wrappedHandler = (0, _guard.default)((data, ...args)=>{ return { data, args }; }); const result = await wrappedHandler({ name: "test" }, "extra", 42); _nodeassert.default.deepStrictEqual(result, { data: { name: "test" }, args: [ "extra", 42 ] }); }); // Test with 'this' context (0, _nodetest.it)("should preserve 'this' context", async ()=>{ // Create an object with a method const obj = { value: "context value", handler (data) { return { data, contextValue: this.value }; } }; // Wrap the handler with guard const wrappedHandler = (0, _guard.default)(obj.handler); // Test with binding to the object const result = await wrappedHandler.call(obj, { name: "test" }); _nodeassert.default.deepStrictEqual(result, { data: { name: "test" }, contextValue: "context value" }); }); // Test getHandlerOptions (0, _nodetest.it)("should store and retrieve options", async ()=>{ const options = { description: "Test handler", input: { type: "object", properties: { name: { type: "string" } } } }; const wrappedHandler = (0, _guard.default)((data)=>data, options); const retrievedOptions = (0, _guard.getHandlerOptions)(wrappedHandler); _nodeassert.default.deepStrictEqual(retrievedOptions, options); }); // Test with no options (0, _nodetest.it)("should work with no options", async ()=>{ // Wrap the handler with guard but no options const wrappedHandler = (0, _guard.default)((data)=>data); // Test the wrapped handler const result = await wrappedHandler({ name: "test" }); _nodeassert.default.deepStrictEqual(result, { name: "test" }); // Verify options are an empty object const retrievedOptions = (0, _guard.getHandlerOptions)(wrappedHandler); _nodeassert.default.deepStrictEqual(retrievedOptions, {}); }); // Test that options are non-enumerable (0, _nodetest.it)("should make options non-enumerable", async ()=>{ const wrappedHandler = (0, _guard.default)((data)=>data, { description: "Test" }); // Check that the options are not enumerable const props = Object.keys(wrappedHandler); _nodeassert.default.strictEqual(props.length, 0); // But we can still get them with getHandlerOptions const options = (0, _guard.getHandlerOptions)(wrappedHandler); _nodeassert.default.strictEqual(options.description, "Test"); }); });