Skip to content

Commit b4def84

Browse files
committed
Add test cases for current translations WASM bindings
Adds test cases that test the current translation functionality end-to-end, including plaint-text translations and HTML translations.
1 parent b70a176 commit b4def84

7 files changed

+195
-7
lines changed

inference/wasm/tests/stub.test.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import { it, expect } from "vitest";
6+
import { TranslationsEngine } from "./engine/translations-engine.mjs";
7+
8+
/**
9+
* Runs a translation test, constructing a Translations Engine for the given
10+
* sourceLanguage and targetLanguage, then asserting that the translation of
11+
* the sourceText matches the expectedText.
12+
*
13+
* @param {Object} params - The parameters for the test.
14+
* @param {string} params.sourceLanguage - The source language code.
15+
* @param {string} params.targetLanguage - The target language code.
16+
* @param {string} params.sourceText - The text to translate.
17+
* @param {string} params.expectedText - The expected translated text.
18+
* @param {boolean} params.isHTML - Whether the text to translate contains HTML tags.
19+
*/
20+
export function runTranslationTest({
21+
sourceLanguage,
22+
targetLanguage,
23+
sourceText,
24+
expectedText,
25+
isHTML = false,
26+
}) {
27+
it(`(${sourceLanguage} -> ${targetLanguage}): Translate "${sourceText}"`, async () => {
28+
const translator = new TranslationsEngine(sourceLanguage, targetLanguage);
29+
30+
const translatedText = await translator.translate(sourceText, isHTML);
31+
32+
expect(translatedText).toBe(expectedText);
33+
34+
translator.terminate();
35+
});
36+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import { describe } from "vitest";
6+
import { runTranslationTest } from "./test-cases/shared.mjs";
7+
8+
/**
9+
* This file tests the WASM bindings for non-pivot translation requests
10+
* that contain HTML tags within the source text.
11+
*/
12+
13+
const testCases = [
14+
{
15+
sourceLanguage: "es",
16+
targetLanguage: "en",
17+
sourceText: "<b>El perro</b> azul.",
18+
expectedText: "<b>The</b> blue <b>dog</b>.",
19+
isHTML: true,
20+
},
21+
{
22+
sourceLanguage: "en",
23+
targetLanguage: "es",
24+
sourceText: "<b>The blue</b> dog.",
25+
expectedText: "<b>El</b> perro <b>azul</b>.",
26+
isHTML: true,
27+
},
28+
{
29+
sourceLanguage: "fr",
30+
targetLanguage: "en",
31+
sourceText: "<b>Le chien</b> bleu.",
32+
expectedText: "<b>The</b> blue <b>dog</b>.",
33+
isHTML: true,
34+
},
35+
{
36+
sourceLanguage: "en",
37+
targetLanguage: "fr",
38+
sourceText: "<b>The blue</b> dog.",
39+
expectedText: "<b>Le</b> chien <b>bleu</b>.",
40+
isHTML: true,
41+
},
42+
];
43+
44+
describe("HTML Non-Pivot Translations", () => {
45+
testCases.forEach(runTranslationTest);
46+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import { describe } from "vitest";
6+
import { runTranslationTest } from "./test-cases/shared.mjs";
7+
8+
/**
9+
* This file tests the WASM bindings for pivot translation requests
10+
* that contain HTML tags within the source text.
11+
*/
12+
13+
const testCases = [
14+
{
15+
sourceLanguage: "es",
16+
targetLanguage: "fr",
17+
sourceText: "<b>El perro</b> azul.",
18+
expectedText: "<b>Le chien</b> bleu.",
19+
isHTML: true,
20+
},
21+
{
22+
sourceLanguage: "fr",
23+
targetLanguage: "es",
24+
sourceText: "<b>Le chien</b> bleu.",
25+
expectedText: "<b>El perro</b> azul.",
26+
isHTML: true,
27+
},
28+
];
29+
30+
describe("HTML Pivot Translations", () => {
31+
testCases.forEach(runTranslationTest);
32+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import { describe } from "vitest";
6+
import { runTranslationTest } from "./test-cases/shared.mjs";
7+
8+
/**
9+
* This file tests the WASM bindings for non-pivot translation requests
10+
* that contain only plain text without HTML tags.
11+
*/
12+
13+
const testCases = [
14+
{
15+
sourceLanguage: "es",
16+
targetLanguage: "en",
17+
sourceText: "Hola mundo",
18+
expectedText: "Hello world",
19+
},
20+
{
21+
sourceLanguage: "en",
22+
targetLanguage: "es",
23+
sourceText: "Hello world",
24+
expectedText: "Hola mundo",
25+
},
26+
{
27+
sourceLanguage: "fr",
28+
targetLanguage: "en",
29+
sourceText: "Bonjour le monde",
30+
expectedText: "Hello world",
31+
},
32+
{
33+
sourceLanguage: "en",
34+
targetLanguage: "fr",
35+
sourceText: "Hello world",
36+
expectedText: "Bonjour le monde",
37+
},
38+
];
39+
40+
describe("Plain-Text Non-Pivot Translations", () => {
41+
testCases.forEach(runTranslationTest);
42+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import { describe } from "vitest";
6+
import { runTranslationTest } from "./test-cases/shared.mjs";
7+
8+
/**
9+
* This file tests the WASM bindings for pivot translation requests
10+
* that contain only plain text without HTML tags.
11+
*/
12+
13+
const testCases = [
14+
{
15+
sourceLanguage: "es",
16+
targetLanguage: "fr",
17+
sourceText: "El perro azul.",
18+
expectedText: "Le chien bleu.",
19+
isHTML: true,
20+
},
21+
{
22+
sourceLanguage: "fr",
23+
targetLanguage: "es",
24+
sourceText: "Le chien bleu.",
25+
expectedText: "El perro azul.",
26+
isHTML: true,
27+
},
28+
];
29+
30+
describe("Plain-Text Pivot Translations", () => {
31+
testCases.forEach(runTranslationTest);
32+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
reporters: ["default"],
6+
},
7+
});

0 commit comments

Comments
 (0)