Skip to content

Commit

Permalink
Add test cases for current translations WASM bindings
Browse files Browse the repository at this point in the history
Adds test cases that test the current translation functionality
end-to-end, including plaint-text translations and HTML translations.
  • Loading branch information
nordzilla committed Nov 12, 2024
1 parent eeb9d98 commit 9009504
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 7 deletions.
7 changes: 0 additions & 7 deletions inference/wasm/tests/stub.test.js

This file was deleted.

36 changes: 36 additions & 0 deletions inference/wasm/tests/test-cases/shared.mjs
Original file line number Diff line number Diff line change
@@ -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();
});
}
46 changes: 46 additions & 0 deletions inference/wasm/tests/test-cases/translate-html-no-pivot.test.mjs
Original file line number Diff line number Diff line change
@@ -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: "<b>El perro</b> azul.",
expectedText: "<b>The</b> blue <b>dog</b>.",
isHTML: true,
},
{
sourceLanguage: "en",
targetLanguage: "es",
sourceText: "<b>The blue</b> dog.",
expectedText: "<b>El</b> perro <b>azul</b>.",
isHTML: true,
},
{
sourceLanguage: "fr",
targetLanguage: "en",
sourceText: "<b>Le chien</b> bleu.",
expectedText: "<b>The</b> blue <b>dog</b>.",
isHTML: true,
},
{
sourceLanguage: "en",
targetLanguage: "fr",
sourceText: "<b>The blue</b> dog.",
expectedText: "<b>Le</b> chien <b>bleu</b>.",
isHTML: true,
},
];

describe("HTML Non-Pivot Translations", () => {
testCases.forEach(runTranslationTest);
});
32 changes: 32 additions & 0 deletions inference/wasm/tests/test-cases/translate-html-with-pivot.test.mjs
Original file line number Diff line number Diff line change
@@ -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: "<b>El perro</b> azul.",
expectedText: "<b>Le chien</b> bleu.",
isHTML: true,
},
{
sourceLanguage: "fr",
targetLanguage: "es",
sourceText: "<b>Le chien</b> bleu.",
expectedText: "<b>El perro</b> azul.",
isHTML: true,
},
];

describe("HTML Pivot Translations", () => {
testCases.forEach(runTranslationTest);
});
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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);
});
7 changes: 7 additions & 0 deletions inference/wasm/tests/vitest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
reporters: ["default"],
},
});

0 comments on commit 9009504

Please sign in to comment.