diff --git a/src/api.ts b/src/api.ts index 86540db..c8da672 100644 --- a/src/api.ts +++ b/src/api.ts @@ -10,7 +10,13 @@ import { generateTranslationRequestData, generateSplitSentencesRequestData, } from './generators.js' -import { API_URL, AUTO, SourceLanguage, TargetLanguage } from './settings.js' +import { + API_URL, + AUTO, + FormalityTone, + SourceLanguage, + TargetLanguage, +} from './settings.js' import { abbreviateLanguage } from './utils.js' const got = _got.extend({ @@ -72,7 +78,7 @@ export async function requestTranslation( sourceLanguage: SourceLanguage, identifier?: number, alternatives?: number, - formalityTone?: 'formal' | 'informal', + formalityTone?: FormalityTone, ) { const res = await splitSentences(text, sourceLanguage, identifier) const data = generateTranslationRequestData( @@ -96,7 +102,7 @@ export async function translate( sourceLanguage?: SourceLanguage, identifier?: number, alternatives?: number, - formalityTone?: 'formal' | 'informal', + formalityTone?: FormalityTone, ): Promise export async function translate( text: string, @@ -104,7 +110,7 @@ export async function translate( sourceLanguage?: SourceLanguage, identifier?: number, alternatives?: number, - formalityTone?: 'formal' | 'informal', + formalityTone?: FormalityTone, ): Promise export async function translate( text: string | null | undefined, @@ -112,7 +118,7 @@ export async function translate( sourceLanguage?: SourceLanguage, identifier?: number, alternatives?: number, - formalityTone?: 'formal' | 'informal', + formalityTone?: FormalityTone, ): Promise export async function translate( text: string | null | undefined, @@ -120,7 +126,7 @@ export async function translate( sourceLanguage: SourceLanguage = AUTO, identifier?: number, alternatives?: number, - formalityTone?: 'formal' | 'informal', + formalityTone?: FormalityTone, ) { text = text?.trim() if (!text) { diff --git a/src/generators.ts b/src/generators.ts index 880f3c7..dbb8b20 100644 --- a/src/generators.ts +++ b/src/generators.ts @@ -2,7 +2,7 @@ import { generateId, generateTimestamp } from './hacks.js' import { AUTO, SUPPORTED_FORMALITY_TONES, - Formality, + FormalityTone, SourceLanguage, TargetLanguage, } from './settings.js' @@ -45,13 +45,13 @@ export function generateJobs(sentences: string[], beams = 1) { })) } -function generateCommonJobParams(formality?: Formality) { +function generateCommonJobParams(formality?: FormalityTone) { if (!formality) { return {} } if (!SUPPORTED_FORMALITY_TONES.includes(formality)) { - throw new Error(`Formality tone ${formality} not supported.`) + throw new Error(`Formality tone \`${formality}\` not supported.`) } return { formality } @@ -63,7 +63,7 @@ export function generateTranslationRequestData( sentences: string[], identifier = generateId(), alternatives = 1, - formality?: Formality, + formality?: FormalityTone, ) { return { jsonrpc: '2.0', diff --git a/src/settings.ts b/src/settings.ts index c5f8e59..194bd2b 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -43,4 +43,4 @@ export type SourceLanguage = TargetLanguage | 'auto' export const SUPPORTED_FORMALITY_TONES = ['formal', 'informal'] as const -export type Formality = (typeof SUPPORTED_FORMALITY_TONES)[number] +export type FormalityTone = (typeof SUPPORTED_FORMALITY_TONES)[number] diff --git a/test/translation.spec.ts b/test/translation.spec.ts index aff2746..d64426b 100644 --- a/test/translation.spec.ts +++ b/test/translation.spec.ts @@ -15,34 +15,34 @@ test('translate russian', async () => { const source_language = 'RU' const target_language = 'EN' const text = 'Я сошла с ума' - const expected_translation = "I'm out of my mind." const translation = await translate(text, target_language, source_language) - expect(translation).toEqual(expected_translation) + expect(translation).toMatchInlineSnapshot(`"I've lost my mind"`) }) test('translate chinese', async () => { const source_language = 'ZH' const target_language = 'dutch' const text = '你好' - const expected_translation = 'Hallo' const translation = await translate(text, target_language, source_language) - expect(translation).toContain(expected_translation) + expect(translation).toMatchInlineSnapshot(`"Hoe gaat het met je?"`) }) test('translate greek romanian', async () => { const source_language = 'gReEk' const target_language = 'rO' const text = 'Γεια σας' - const expected_translation = 'bună ziua' // @ts-expect-error -- only upper, lower or capitalize case languages are supported in TypeScript const translation = await translate(text, target_language, source_language) - expect((translation as string).toLowerCase()).toContain(expected_translation) + expect((translation as string).toLowerCase()).toMatchInlineSnapshot( + `"bună ziua."`, + ) }) test('translate sentence', async () => { const text = 'Up and down.' - const expected_translation = 'Op en neer.' - expect(await translate(text, 'NL', 'EN')).toBe(expected_translation) + expect(await translate(text, 'NL', 'EN')).toMatchInlineSnapshot( + `"Op en neer."`, + ) }) test('translate sentences', async () => { @@ -103,5 +103,5 @@ test('invalid_formal_tone', () => // @ts-expect-error translate('ABC', 'DE', 'EN', undefined, undefined, 'politically incorrect'), ).rejects.toThrowErrorMatchingInlineSnapshot( - `"Formality tone '{formality_tone}' not supported."`, + `[Error: Formality tone \`politically incorrect\` not supported.]`, )) diff --git a/vitest.config.ts b/vitest.config.ts index c5c143b..eb78e54 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -15,6 +15,7 @@ export default defineConfig({ test: { coverage: { include: ['src'], + exclude: ['src/cli.ts'], provider: 'istanbul', reporter: ['lcov', 'json', 'text'], },