Files
ai-inference/__tests__/helpers-inference.test.ts
T

170 lines
4.7 KiB
TypeScript
Raw Normal View History

2025-07-24 19:11:15 +10:00
import {describe, it, expect} from 'vitest'
import {buildMessages, buildResponseFormat, buildInferenceRequest} from '../src/helpers'
import {PromptConfig} from '../src/prompt'
2025-07-21 00:11:26 +00:00
describe('helpers.ts - inference request building', () => {
describe('buildMessages', () => {
it('should build messages from prompt config', () => {
const promptConfig: PromptConfig = {
messages: [
2025-07-24 19:11:15 +10:00
{role: 'system', content: 'System message'},
{role: 'user', content: 'User message'},
],
2025-07-21 00:11:26 +00:00
}
const result = buildMessages(promptConfig)
expect(result).toEqual([
2025-07-24 19:11:15 +10:00
{role: 'system', content: 'System message'},
{role: 'user', content: 'User message'},
2025-07-21 00:11:26 +00:00
])
})
it('should build messages from legacy format', () => {
const result = buildMessages(undefined, 'System prompt', 'User prompt')
expect(result).toEqual([
2025-07-24 19:11:15 +10:00
{role: 'system', content: 'System prompt'},
{role: 'user', content: 'User prompt'},
2025-07-21 00:11:26 +00:00
])
})
it('should use default system prompt when none provided', () => {
const result = buildMessages(undefined, undefined, 'User prompt')
expect(result).toEqual([
2025-07-24 19:11:15 +10:00
{role: 'system', content: 'You are a helpful assistant'},
{role: 'user', content: 'User prompt'},
2025-07-21 00:11:26 +00:00
])
})
})
describe('buildResponseFormat', () => {
it('should build JSON schema response format', () => {
const promptConfig: PromptConfig = {
messages: [],
responseFormat: 'json_schema',
jsonSchema: JSON.stringify({
name: 'test_schema',
2025-07-24 19:11:15 +10:00
schema: {type: 'object'},
}),
2025-07-21 00:11:26 +00:00
}
const result = buildResponseFormat(promptConfig)
expect(result).toEqual({
type: 'json_schema',
json_schema: {
name: 'test_schema',
2025-07-24 19:11:15 +10:00
schema: {type: 'object'},
},
2025-07-21 00:11:26 +00:00
})
})
it('should return undefined for text format', () => {
const promptConfig: PromptConfig = {
messages: [],
2025-07-24 19:11:15 +10:00
responseFormat: 'text',
2025-07-21 00:11:26 +00:00
}
const result = buildResponseFormat(promptConfig)
expect(result).toBeUndefined()
})
it('should return undefined when no response format specified', () => {
const promptConfig: PromptConfig = {
2025-07-24 19:11:15 +10:00
messages: [],
2025-07-21 00:11:26 +00:00
}
const result = buildResponseFormat(promptConfig)
expect(result).toBeUndefined()
})
it('should throw error for invalid JSON schema', () => {
const promptConfig: PromptConfig = {
messages: [],
responseFormat: 'json_schema',
2025-07-24 19:11:15 +10:00
jsonSchema: 'invalid json',
2025-07-21 00:11:26 +00:00
}
2025-07-24 19:11:15 +10:00
expect(() => buildResponseFormat(promptConfig)).toThrow('Invalid JSON schema')
2025-07-21 00:11:26 +00:00
})
})
describe('buildInferenceRequest', () => {
it('should build complete inference request from prompt config', () => {
const promptConfig: PromptConfig = {
messages: [
2025-07-24 19:11:15 +10:00
{role: 'system', content: 'System message'},
{role: 'user', content: 'User message'},
2025-07-21 00:11:26 +00:00
],
responseFormat: 'json_schema',
jsonSchema: JSON.stringify({
name: 'test_schema',
2025-07-24 19:11:15 +10:00
schema: {type: 'object'},
}),
2025-07-21 00:11:26 +00:00
}
const result = buildInferenceRequest(
promptConfig,
undefined,
undefined,
'gpt-4',
undefined,
undefined,
2025-07-21 00:11:26 +00:00
100,
undefined,
2025-07-21 00:11:26 +00:00
'https://api.test.com',
2025-07-24 19:11:15 +10:00
'test-token',
2025-07-21 00:11:26 +00:00
)
expect(result).toEqual({
messages: [
2025-07-24 19:11:15 +10:00
{role: 'system', content: 'System message'},
{role: 'user', content: 'User message'},
2025-07-21 00:11:26 +00:00
],
modelName: 'gpt-4',
temperature: undefined,
topP: undefined,
2025-07-21 00:11:26 +00:00
maxTokens: 100,
maxCompletionTokens: undefined,
2025-07-21 00:11:26 +00:00
endpoint: 'https://api.test.com',
token: 'test-token',
responseFormat: {
type: 'json_schema',
json_schema: {
name: 'test_schema',
2025-07-24 19:11:15 +10:00
schema: {type: 'object'},
},
},
2025-07-21 00:11:26 +00:00
})
})
it('should build inference request from legacy format', () => {
const result = buildInferenceRequest(
undefined,
'System prompt',
'User prompt',
'gpt-4',
undefined,
undefined,
2025-07-21 00:11:26 +00:00
100,
undefined,
2025-07-21 00:11:26 +00:00
'https://api.test.com',
2025-07-24 19:11:15 +10:00
'test-token',
2025-07-21 00:11:26 +00:00
)
expect(result).toEqual({
messages: [
2025-07-24 19:11:15 +10:00
{role: 'system', content: 'System prompt'},
{role: 'user', content: 'User prompt'},
2025-07-21 00:11:26 +00:00
],
modelName: 'gpt-4',
temperature: undefined,
topP: undefined,
2025-07-21 00:11:26 +00:00
maxTokens: 100,
maxCompletionTokens: undefined,
2025-07-21 00:11:26 +00:00
endpoint: 'https://api.test.com',
token: 'test-token',
2025-07-24 19:11:15 +10:00
responseFormat: undefined,
2025-07-21 00:11:26 +00:00
})
})
})
})