mono/packages/kbot/tests/unit/language.test.ts

94 lines
3.3 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import * as path from 'node:path'
import { sync as exists } from "@polymech/fs/exists"
import {
getDefaultModels,
TEST_BASE_PATH,
TEST_LOGS_PATH,
TEST_PREFERENCES_PATH,
TEST_TIMEOUT,
TestResult,
runTest,
generateTestReport,
getReportPaths
} from './commons'
// Optionally override models for this specific test file
const models = getDefaultModels()
// Helper function to normalize strings for comparison by ignoring case, trimming, and stripping punctuation.
const normalize = (str: string | undefined): string => {
if (!str) return ''
return str.toLowerCase().trim().replace(/[.,'"]/g, '')
}
describe('Language Operations', () => {
let testResults: TestResult[] = []
const TEST_LOG_PATH = getReportPaths('language', 'json')
const TEST_REPORT_PATH = getReportPaths('language', 'md')
it.each(models)('should translate text with model %s', async (modelName) => {
const result = await runTest(
'Translate "Hello, world!" to Spanish. Return only the translation, no explanation.',
'¡Hola, mundo!',
'translation',
modelName,
TEST_LOG_PATH
)
testResults.push(result)
expect(normalize(result.result[0])).toEqual(normalize('¡hola, mundo!'))
}, { timeout: TEST_TIMEOUT })
it.each(models)('should correct grammar with model %s', async (modelName) => {
const result = await runTest(
'Correct the grammar in: "I goes to the store yesterday". Return only the corrected sentence, no explanation.',
'I went to the store yesterday',
'grammar',
modelName,
TEST_LOG_PATH
)
testResults.push(result)
expect(normalize(result.result[0])).toEqual(normalize('i went to the store yesterday'))
}, { timeout: TEST_TIMEOUT })
it.each(models)('should summarize text with model %s', async (modelName) => {
const result = await runTest(
'Summarize: "The quick brown fox jumps over the dog". Return only the summary, compact, no explanation.',
'A fox jumps over a dog',
'summarization',
modelName,
TEST_LOG_PATH
)
testResults.push(result)
expect(normalize(result.result[0])).toEqual(normalize('a fox jumps over a dog'))
}, { timeout: TEST_TIMEOUT })
it.each(models)('should identify language with model %s', async (modelName) => {
const result = await runTest(
'Identify the language of: "Bonjour, comment allez-vous?". Return only the language name, no explanation.',
'French',
'language_detection',
modelName,
TEST_LOG_PATH
)
testResults.push(result)
expect(normalize(result.result[0])).toEqual(normalize('french'))
}, { timeout: TEST_TIMEOUT })
it.each(models)('should generate synonyms with model %s', async (modelName) => {
const result = await runTest(
'Provide a synonym for "happy". Return only the synonym, no explanation.',
'joyful',
'synonyms',
modelName,
TEST_LOG_PATH
)
testResults.push(result)
expect(normalize(result.result[0])).toEqual(normalize('joyful'))
}, { timeout: TEST_TIMEOUT })
it('should generate markdown report', () => {
generateTestReport(testResults, 'Language Operations Test Results', TEST_REPORT_PATH)
expect(exists(TEST_REPORT_PATH) === 'file').toBe(true)
})
})