|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { |
| 4 | + BridgedStructuredGenerator, |
| 5 | + type GenerateObjectServiceFn |
| 6 | +} from './structured-generator.js'; |
| 7 | +import type { AIPrimitiveOptions } from '../types/primitives.types.js'; |
| 8 | + |
| 9 | +describe('BridgedStructuredGenerator', () => { |
| 10 | + describe('generate', () => { |
| 11 | + it('should throw error when result is null', async () => { |
| 12 | + const mockService: GenerateObjectServiceFn = vi |
| 13 | + .fn() |
| 14 | + .mockResolvedValue(null); |
| 15 | + const generator = new BridgedStructuredGenerator(mockService); |
| 16 | + |
| 17 | + const schema = z.object({ value: z.string() }); |
| 18 | + const options: AIPrimitiveOptions & { objectName?: string } = { |
| 19 | + commandName: 'test-command', |
| 20 | + objectName: 'test-object' |
| 21 | + }; |
| 22 | + |
| 23 | + await expect( |
| 24 | + generator.generate('test prompt', schema, options) |
| 25 | + ).rejects.toThrow( |
| 26 | + 'AI service returned null or undefined result (objectName: test-object, commandName: test-command)' |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should throw error when result.mainResult is null', async () => { |
| 31 | + const mockService: GenerateObjectServiceFn = vi.fn().mockResolvedValue({ |
| 32 | + mainResult: null, |
| 33 | + modelId: 'test-model', |
| 34 | + providerName: 'test-provider' |
| 35 | + }); |
| 36 | + const generator = new BridgedStructuredGenerator(mockService); |
| 37 | + |
| 38 | + const schema = z.object({ value: z.string() }); |
| 39 | + const options: AIPrimitiveOptions & { objectName?: string } = { |
| 40 | + commandName: 'test-command', |
| 41 | + objectName: 'test-object' |
| 42 | + }; |
| 43 | + |
| 44 | + await expect( |
| 45 | + generator.generate('test prompt', schema, options) |
| 46 | + ).rejects.toThrow( |
| 47 | + 'AI service returned null or undefined result (objectName: test-object, commandName: test-command, modelId: test-model, providerName: test-provider)' |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should throw error when result.mainResult is undefined', async () => { |
| 52 | + const mockService: GenerateObjectServiceFn = vi.fn().mockResolvedValue({ |
| 53 | + mainResult: undefined, |
| 54 | + modelId: 'test-model', |
| 55 | + providerName: 'test-provider' |
| 56 | + }); |
| 57 | + const generator = new BridgedStructuredGenerator(mockService); |
| 58 | + |
| 59 | + const schema = z.object({ value: z.string() }); |
| 60 | + const options: AIPrimitiveOptions & { objectName?: string } = { |
| 61 | + commandName: 'test-command', |
| 62 | + objectName: 'test-object' |
| 63 | + }; |
| 64 | + |
| 65 | + await expect( |
| 66 | + generator.generate('test prompt', schema, options) |
| 67 | + ).rejects.toThrow( |
| 68 | + 'AI service returned null or undefined result (objectName: test-object, commandName: test-command, modelId: test-model, providerName: test-provider)' |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return valid result when mainResult is present', async () => { |
| 73 | + const mockResult = { value: 'test-value' }; |
| 74 | + const mockService: GenerateObjectServiceFn = vi.fn().mockResolvedValue({ |
| 75 | + mainResult: mockResult, |
| 76 | + modelId: 'test-model', |
| 77 | + providerName: 'test-provider', |
| 78 | + telemetryData: { |
| 79 | + inputTokens: 10, |
| 80 | + outputTokens: 20 |
| 81 | + } |
| 82 | + }); |
| 83 | + const generator = new BridgedStructuredGenerator(mockService); |
| 84 | + |
| 85 | + const schema = z.object({ value: z.string() }); |
| 86 | + const options: AIPrimitiveOptions & { objectName?: string } = { |
| 87 | + commandName: 'test-command', |
| 88 | + objectName: 'test-object' |
| 89 | + }; |
| 90 | + |
| 91 | + const result = await generator.generate('test prompt', schema, options); |
| 92 | + |
| 93 | + expect(result.data).toEqual(mockResult); |
| 94 | + expect(result.usage).toEqual({ |
| 95 | + inputTokens: 10, |
| 96 | + outputTokens: 20, |
| 97 | + model: 'test-model', |
| 98 | + provider: 'test-provider', |
| 99 | + duration: expect.any(Number) |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should use default objectName when not provided', async () => { |
| 104 | + const mockResult = { value: 'test-value' }; |
| 105 | + const mockService: GenerateObjectServiceFn = vi.fn().mockResolvedValue({ |
| 106 | + mainResult: mockResult, |
| 107 | + modelId: 'test-model', |
| 108 | + providerName: 'test-provider' |
| 109 | + }); |
| 110 | + const generator = new BridgedStructuredGenerator(mockService); |
| 111 | + |
| 112 | + const schema = z.object({ value: z.string() }); |
| 113 | + const options: AIPrimitiveOptions = { |
| 114 | + commandName: 'test-command' |
| 115 | + }; |
| 116 | + |
| 117 | + await generator.generate('test prompt', schema, options); |
| 118 | + |
| 119 | + expect(mockService).toHaveBeenCalledWith( |
| 120 | + expect.objectContaining({ |
| 121 | + objectName: 'generated_object' |
| 122 | + }) |
| 123 | + ); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should include error context without model/provider when not available', async () => { |
| 127 | + const mockService: GenerateObjectServiceFn = vi.fn().mockResolvedValue({ |
| 128 | + mainResult: null |
| 129 | + }); |
| 130 | + const generator = new BridgedStructuredGenerator(mockService); |
| 131 | + |
| 132 | + const schema = z.object({ value: z.string() }); |
| 133 | + const options: AIPrimitiveOptions & { objectName?: string } = { |
| 134 | + commandName: 'test-command', |
| 135 | + objectName: 'test-object' |
| 136 | + }; |
| 137 | + |
| 138 | + await expect( |
| 139 | + generator.generate('test prompt', schema, options) |
| 140 | + ).rejects.toThrow( |
| 141 | + 'AI service returned null or undefined result (objectName: test-object, commandName: test-command)' |
| 142 | + ); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should handle zero tokens and missing telemetry gracefully', async () => { |
| 146 | + const mockResult = { value: 'test-value' }; |
| 147 | + const mockService: GenerateObjectServiceFn = vi.fn().mockResolvedValue({ |
| 148 | + mainResult: mockResult, |
| 149 | + telemetryData: null |
| 150 | + }); |
| 151 | + const generator = new BridgedStructuredGenerator(mockService); |
| 152 | + |
| 153 | + const schema = z.object({ value: z.string() }); |
| 154 | + const options: AIPrimitiveOptions = { |
| 155 | + commandName: 'test-command' |
| 156 | + }; |
| 157 | + |
| 158 | + const result = await generator.generate('test prompt', schema, options); |
| 159 | + |
| 160 | + expect(result.usage).toEqual({ |
| 161 | + inputTokens: 0, |
| 162 | + outputTokens: 0, |
| 163 | + model: 'unknown', |
| 164 | + provider: 'unknown', |
| 165 | + duration: expect.any(Number) |
| 166 | + }); |
| 167 | + }); |
| 168 | + }); |
| 169 | +}); |
0 commit comments