diff --git a/inference/wasm/tests/stub.test.js b/inference/wasm/tests/stub.test.js deleted file mode 100644 index 3958490d9..000000000 --- a/inference/wasm/tests/stub.test.js +++ /dev/null @@ -1,7 +0,0 @@ -import { describe, it, expect } from 'vitest'; - -describe('Basic Test Suite', () => { - it('should pass a basic test', () => { - expect(1 + 1).toBe(2); - }); -}); diff --git a/inference/wasm/tests/test-cases/shared.mjs b/inference/wasm/tests/test-cases/shared.mjs new file mode 100644 index 000000000..6f5c4a51b --- /dev/null +++ b/inference/wasm/tests/test-cases/shared.mjs @@ -0,0 +1,36 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { it, expect } from "vitest"; +import { TranslationsEngine } from "./engine/translations-engine.mjs"; + +/** + * Runs a translation test, constructing a Translations Engine for the given + * sourceLanguage and targetLanguage, then asserting that the translation of + * the sourceText matches the expectedText. + * + * @param {Object} params - The parameters for the test. + * @param {string} params.sourceLanguage - The source language code. + * @param {string} params.targetLanguage - The target language code. + * @param {string} params.sourceText - The text to translate. + * @param {string} params.expectedText - The expected translated text. + * @param {boolean} params.isHTML - Whether the text to translate contains HTML tags. + */ +export function runTranslationTest({ + sourceLanguage, + targetLanguage, + sourceText, + expectedText, + isHTML = false, +}) { + it(`(${sourceLanguage} -> ${targetLanguage}): Translate "${sourceText}"`, async () => { + const translator = new TranslationsEngine(sourceLanguage, targetLanguage); + + const translatedText = await translator.translate(sourceText, isHTML); + + expect(translatedText).toBe(expectedText); + + translator.terminate(); + }); +} diff --git a/inference/wasm/tests/test-cases/translate-html-no-pivot.test.mjs b/inference/wasm/tests/test-cases/translate-html-no-pivot.test.mjs new file mode 100644 index 000000000..434642987 --- /dev/null +++ b/inference/wasm/tests/test-cases/translate-html-no-pivot.test.mjs @@ -0,0 +1,46 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { describe } from "vitest"; +import { runTranslationTest } from "./test-cases/shared.mjs"; + +/** + * This file tests the WASM bindings for non-pivot translation requests + * that contain HTML tags within the source text. + */ + +const testCases = [ + { + sourceLanguage: "es", + targetLanguage: "en", + sourceText: "El perro azul.", + expectedText: "The blue dog.", + isHTML: true, + }, + { + sourceLanguage: "en", + targetLanguage: "es", + sourceText: "The blue dog.", + expectedText: "El perro azul.", + isHTML: true, + }, + { + sourceLanguage: "fr", + targetLanguage: "en", + sourceText: "Le chien bleu.", + expectedText: "The blue dog.", + isHTML: true, + }, + { + sourceLanguage: "en", + targetLanguage: "fr", + sourceText: "The blue dog.", + expectedText: "Le chien bleu.", + isHTML: true, + }, +]; + +describe("HTML Non-Pivot Translations", () => { + testCases.forEach(runTranslationTest); +}); diff --git a/inference/wasm/tests/test-cases/translate-html-with-pivot.test.mjs b/inference/wasm/tests/test-cases/translate-html-with-pivot.test.mjs new file mode 100644 index 000000000..66462bd2c --- /dev/null +++ b/inference/wasm/tests/test-cases/translate-html-with-pivot.test.mjs @@ -0,0 +1,32 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { describe } from "vitest"; +import { runTranslationTest } from "./test-cases/shared.mjs"; + +/** + * This file tests the WASM bindings for pivot translation requests + * that contain HTML tags within the source text. + */ + +const testCases = [ + { + sourceLanguage: "es", + targetLanguage: "fr", + sourceText: "El perro azul.", + expectedText: "Le chien bleu.", + isHTML: true, + }, + { + sourceLanguage: "fr", + targetLanguage: "es", + sourceText: "Le chien bleu.", + expectedText: "El perro azul.", + isHTML: true, + }, +]; + +describe("HTML Pivot Translations", () => { + testCases.forEach(runTranslationTest); +}); diff --git a/inference/wasm/tests/test-cases/translate-plain-text-no-pivot.test.mjs b/inference/wasm/tests/test-cases/translate-plain-text-no-pivot.test.mjs new file mode 100644 index 000000000..cc39a228d --- /dev/null +++ b/inference/wasm/tests/test-cases/translate-plain-text-no-pivot.test.mjs @@ -0,0 +1,42 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { describe } from "vitest"; +import { runTranslationTest } from "./test-cases/shared.mjs"; + +/** + * This file tests the WASM bindings for non-pivot translation requests + * that contain only plain text without HTML tags. + */ + +const testCases = [ + { + sourceLanguage: "es", + targetLanguage: "en", + sourceText: "Hola mundo", + expectedText: "Hello world", + }, + { + sourceLanguage: "en", + targetLanguage: "es", + sourceText: "Hello world", + expectedText: "Hola mundo", + }, + { + sourceLanguage: "fr", + targetLanguage: "en", + sourceText: "Bonjour le monde", + expectedText: "Hello world", + }, + { + sourceLanguage: "en", + targetLanguage: "fr", + sourceText: "Hello world", + expectedText: "Bonjour le monde", + }, +]; + +describe("Plain-Text Non-Pivot Translations", () => { + testCases.forEach(runTranslationTest); +}); diff --git a/inference/wasm/tests/test-cases/translate-plain-text-with-pivot.test.mjs b/inference/wasm/tests/test-cases/translate-plain-text-with-pivot.test.mjs new file mode 100644 index 000000000..8ff148952 --- /dev/null +++ b/inference/wasm/tests/test-cases/translate-plain-text-with-pivot.test.mjs @@ -0,0 +1,32 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { describe } from "vitest"; +import { runTranslationTest } from "./test-cases/shared.mjs"; + +/** + * This file tests the WASM bindings for pivot translation requests + * that contain only plain text without HTML tags. + */ + +const testCases = [ + { + sourceLanguage: "es", + targetLanguage: "fr", + sourceText: "El perro azul.", + expectedText: "Le chien bleu.", + isHTML: true, + }, + { + sourceLanguage: "fr", + targetLanguage: "es", + sourceText: "Le chien bleu.", + expectedText: "El perro azul.", + isHTML: true, + }, +]; + +describe("Plain-Text Pivot Translations", () => { + testCases.forEach(runTranslationTest); +}); diff --git a/inference/wasm/tests/vitest.config.mjs b/inference/wasm/tests/vitest.config.mjs new file mode 100644 index 000000000..4142755f8 --- /dev/null +++ b/inference/wasm/tests/vitest.config.mjs @@ -0,0 +1,7 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + reporters: ["default"], + }, +});