UNPKG

@clipwhisperer/common

Version:

ClipWhisperer Common - Shared library providing core utilities, database schemas, authentication, bucket management, and common functionality across all ClipWhisperer microservices

114 lines (113 loc) 4.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const util_1 = require("../../src/api/util"); const globals_1 = require("@jest/globals"); const zod_1 = require("zod"); // Mock VaultClient for testing const mockVaultClient = { get: globals_1.jest.fn(), set: globals_1.jest.fn(), delete: globals_1.jest.fn(), }; // Mock options provider const mockOptionsProvider = async () => ({ vault: mockVaultClient, }); (0, globals_1.describe)('API Utility Functions', () => { (0, globals_1.beforeEach)(() => { globals_1.jest.clearAllMocks(); }); (0, globals_1.describe)('setOptionsProvider', () => { (0, globals_1.it)('should set the options provider successfully', () => { (0, globals_1.expect)(() => (0, util_1.setOptionsProvider)(mockOptionsProvider)).not.toThrow(); }); }); (0, globals_1.describe)('createEndpoint', () => { (0, globals_1.it)('should create an endpoint with correct configuration', () => { const inputSchema = zod_1.z.object({ name: zod_1.z.string(), }); const outputSchema = zod_1.z.object({ success: zod_1.z.boolean(), message: zod_1.z.string(), }); const handler = async (input) => ({ success: true, message: `Hello, ${input.name}!`, }); (0, util_1.setOptionsProvider)(mockOptionsProvider); const endpoint = (0, util_1.createEndpoint)({ method: 'post', input: inputSchema, output: outputSchema, handler, }); (0, globals_1.expect)(endpoint).toBeDefined(); (0, globals_1.expect)(typeof endpoint).toBe('object'); }); (0, globals_1.it)('should handle different HTTP methods', () => { const schema = zod_1.z.object({ test: zod_1.z.string() }); const handler = async () => ({ test: 'response' }); (0, util_1.setOptionsProvider)(mockOptionsProvider); const methods = ['get', 'post', 'put', 'delete']; methods.forEach(method => { (0, globals_1.expect)(() => { (0, util_1.createEndpoint)({ method, input: schema, output: schema, handler, }); }).not.toThrow(); }); }); }); (0, globals_1.describe)('createRoute', () => { (0, globals_1.it)('should create a route object with multiple methods', () => { const inputSchema = zod_1.z.object({ id: zod_1.z.string() }); const outputSchema = zod_1.z.object({ result: zod_1.z.string() }); const handler = async () => ({ result: 'test' }); (0, util_1.setOptionsProvider)(mockOptionsProvider); const getEndpoint = (0, util_1.createEndpoint)({ method: 'get', input: inputSchema, output: outputSchema, handler, }); const postEndpoint = (0, util_1.createEndpoint)({ method: 'post', input: inputSchema, output: outputSchema, handler, }); const routes = (0, util_1.createRoute)({ get: getEndpoint, post: postEndpoint, }); (0, globals_1.expect)(routes).toBeDefined(); (0, globals_1.expect)(routes.get).toBeDefined(); (0, globals_1.expect)(routes.post).toBeDefined(); }); (0, globals_1.it)('should handle empty routes object', () => { const routes = (0, util_1.createRoute)({}); (0, globals_1.expect)(routes).toEqual({}); }); (0, globals_1.it)('should handle single method route', () => { const inputSchema = zod_1.z.object({ test: zod_1.z.string() }); const outputSchema = zod_1.z.object({ result: zod_1.z.string() }); const handler = async () => ({ result: 'test' }); (0, util_1.setOptionsProvider)(mockOptionsProvider); const endpoint = (0, util_1.createEndpoint)({ method: 'get', input: inputSchema, output: outputSchema, handler, }); const routes = (0, util_1.createRoute)({ get: endpoint, }); (0, globals_1.expect)(routes.get).toBeDefined(); (0, globals_1.expect)(routes.post).toBeUndefined(); }); }); });