import { describe, it, expect } from 'vitest' import { Type } from 'typebox' import { v } from 'suretype' import { computeSchema } from './compute-schema.js' import { ProcedureRegistrationError } from '../errors.js' describe('computeSchema', () => { it('should return empty schema and validations when no schema provided', () => { const result = computeSchema('test-procedure') expect(result).toEqual({ jsonSchema: { args: undefined, data: undefined }, validations: {} }) }) describe('with Typebox schema', () => { it('should correctly process args schema', () => { const schema = { args: Type.Object({ name: Type.String(), age: Type.Number() }) } const result = computeSchema('test-procedure', schema) expect(result.jsonSchema.args).toBeDefined() expect(result.validations.args).toBeDefined() // Test validation function const validInput = { name: 'John', age: 30 } expect(result.validations.args?.(validInput).errors).toBeUndefined() // Test invalid input const invalidInput = { name: 123, age: 'invalid' } expect(result.validations.args?.(invalidInput).errors).toBeDefined() }) it('should correctly process data schema', () => { const schema = { data: Type.Object({ result: Type.Boolean() }) } const result = computeSchema('test-procedure', schema) expect(result.jsonSchema.data).toBeDefined() expect(result.validations.args).toBeUndefined() }) }) describe('with Suretype schema', () => { it('should correctly process args schema', () => { const schema = { args: v.object({ name: v.string(), age: v.number() }) } const result = computeSchema('test-procedure', schema) expect(result.jsonSchema.args).toBeDefined() expect(result.validations.args).toBeDefined() // Test validation function const validInput = { name: 'John', age: 30 } expect(result.validations.args?.(validInput).errors).toBeUndefined() // Test invalid input const invalidInput = { name: 123, age: 'invalid' } expect(result.validations.args?.(invalidInput).errors).toBeDefined() }) }) describe('error handling', () => { it('should throw ProcedureRegistrationError for invalid schema', () => { const invalidSchema = { args: { type: 'invalid-schema-type' } } expect(() => computeSchema('test-procedure', invalidSchema)) .toThrow(ProcedureRegistrationError) }) it('should include procedure name in error message', () => { const invalidSchema = { args: { type: 'invalid-schema-type' } } try { computeSchema('test-procedure', invalidSchema) } catch (error: any) { expect(error instanceof ProcedureRegistrationError).toBe(true) expect(error.message).toContain('test-procedure') } }) }) describe('combined schemas', () => { it('should handle both args and data schemas', () => { const schema = { args: Type.Object({ input: Type.String() }), data: Type.Object({ output: Type.Boolean() }) } const result = computeSchema('test-procedure', schema) expect(result.jsonSchema.args).toBeDefined() expect(result.jsonSchema.data).toBeDefined() expect(result.validations.args).toBeDefined() // Test args validation const validInput = { input: 'test' } expect(result.validations.args?.(validInput).errors).toBeUndefined() }) }) })