mono/packages/kbot/tests/unit/language.test.ts
2025-04-02 13:04:20 +02:00

88 lines
3.1 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()
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(result.result[0]?.trim()?.toLowerCase()).toEqual('¡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(result.result[0]?.trim()?.toLowerCase()).toEqual('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 lazy dog". Return only the summary, no explanation.',
'A fox jumps over a dog',
'summarization',
modelName,
TEST_LOG_PATH
)
testResults.push(result)
expect(result.result[0]?.trim()?.toLowerCase()).toEqual('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(result.result[0]?.trim()?.toLowerCase()).toEqual('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(result.result[0]?.trim()?.toLowerCase()).toEqual('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)
})
})