From 0c2466b916f37345005bb63e3ab1d330f914d069 Mon Sep 17 00:00:00 2001 From: Fuma Nama Date: Wed, 25 Dec 2024 16:03:52 +0800 Subject: [PATCH] add test for diff --- packages/cli/src/commands/translate.ts | 2 +- packages/cli/test/js.test.ts | 38 ++++++++++++++++++- packages/cli/test/resources/js-diff.js | 11 ++++++ .../cli/test/resources/js-diff.previous.js | 10 +++++ .../cli/test/resources/js-diff.translated.js | 10 +++++ packages/cli/test/snapshots/js-diff.js | 10 +++++ .../cli/test/snapshots/js-diff.prompt.txt | 31 +++++++++++++++ 7 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 packages/cli/test/resources/js-diff.js create mode 100644 packages/cli/test/resources/js-diff.previous.js create mode 100644 packages/cli/test/resources/js-diff.translated.js create mode 100644 packages/cli/test/snapshots/js-diff.js create mode 100644 packages/cli/test/snapshots/js-diff.prompt.txt diff --git a/packages/cli/src/commands/translate.ts b/packages/cli/src/commands/translate.ts index e7bc077..e5a9019 100644 --- a/packages/cli/src/commands/translate.ts +++ b/packages/cli/src/commands/translate.ts @@ -77,7 +77,7 @@ export async function translate(targetLocale?: string, force: boolean = false) { const adapter = await getTranslator(format); if (!adapter) { - throw new Error(`No available adapter for format: ${format}`); + throw new Error(`No available translator for format: ${format}`); } const options: PromptOptions = { diff --git a/packages/cli/test/js.test.ts b/packages/cli/test/js.test.ts index 7111e9a..f20fa27 100644 --- a/packages/cli/test/js.test.ts +++ b/packages/cli/test/js.test.ts @@ -8,7 +8,7 @@ import { javascript } from "../src/translators/js.js"; const dir = path.dirname(fileURLToPath(import.meta.url)); -test("JSON adapter: new", async () => { +test("JavaScript adapter: new", async () => { const result = await javascript.onNew({ config: {} as unknown as Config, content: await readFile(path.join(dir, "resources/js-new.js")).then((res) => @@ -42,3 +42,39 @@ test("JSON adapter: new", async () => { await expect(result.content).toMatchFileSnapshot("snapshots/js-new.js"); }); + +test("JavaScript adapter: diff", async () => { + const result = await javascript.onUpdate({ + config: {} as unknown as Config, + content: await readFile(path.join(dir, "resources/js-diff.js")).then((res) => + res.toString(), + ), + previousContent: (await readFile(path.join(dir, 'resources/js-diff.previous.js'))).toString(), + previousTranslation: (await readFile(path.join(dir, 'resources/js-diff.translated.js'))).toString(), + format: "js", + contentLocale: "en", + targetLocale: "cn", + model: new MockLanguageModelV1({ + defaultObjectGenerationMode: "json", + async doGenerate(v) { + await expect( + (v.prompt.at(-1) as any).content[0].text, + ).toMatchFileSnapshot("snapshots/js-diff.prompt.txt"); + + return { + rawCall: { rawPrompt: null, rawSettings: {} }, + finishReason: "stop", + usage: { promptTokens: 10, completionTokens: 20 }, + text: JSON.stringify([ + "\"title\"", + '"Updated"', + "`Updated\nUpdated`", + "`Updated ${Date.now()}`", + ]), + }; + }, + }), + }); + + await expect(result.content).toMatchFileSnapshot("snapshots/js-diff.js"); +}); diff --git a/packages/cli/test/resources/js-diff.js b/packages/cli/test/resources/js-diff.js new file mode 100644 index 0000000..04b3394 --- /dev/null +++ b/packages/cli/test/resources/js-diff.js @@ -0,0 +1,11 @@ + +export default { + "title": "Getting Stopped", + description: 'Before you begin, make sure you have the following:\nA GitHub account', + + nested: { + content: `Explore ideas and examples of +what you can build with the Midday API` + }, + dynamic: `the current time is not ${Date.now()}` +} diff --git a/packages/cli/test/resources/js-diff.previous.js b/packages/cli/test/resources/js-diff.previous.js new file mode 100644 index 0000000..5be8994 --- /dev/null +++ b/packages/cli/test/resources/js-diff.previous.js @@ -0,0 +1,10 @@ + +export default { + "title": "Introduction", + description: 'Before you begin, make sure you have the following:\nA GitHub account', + nested: { + content: `You can self-host Midday on your own cloud infrastructure for greater control over your data. + This guide will walk you through the entire process of setting up Midday.` + }, + dynamic: `the current time is ${Date.now()}` +} diff --git a/packages/cli/test/resources/js-diff.translated.js b/packages/cli/test/resources/js-diff.translated.js new file mode 100644 index 0000000..d2c44b7 --- /dev/null +++ b/packages/cli/test/resources/js-diff.translated.js @@ -0,0 +1,10 @@ + +export default { + "title": "Translated", + description: 'Translated\nTranslated', + nested: { + content: `Translated + Translated` + }, + dynamic: `Translated ${Date.now()}` +} diff --git a/packages/cli/test/snapshots/js-diff.js b/packages/cli/test/snapshots/js-diff.js new file mode 100644 index 0000000..9706b34 --- /dev/null +++ b/packages/cli/test/snapshots/js-diff.js @@ -0,0 +1,10 @@ + +export default { + "title": "Updated", + description: 'Translated\nTranslated', + nested: { + content: `Updated +Updated` + }, + dynamic: `Updated ${Date.now()}` +} diff --git a/packages/cli/test/snapshots/js-diff.prompt.txt b/packages/cli/test/snapshots/js-diff.prompt.txt new file mode 100644 index 0000000..4b88608 --- /dev/null +++ b/packages/cli/test/snapshots/js-diff.prompt.txt @@ -0,0 +1,31 @@ +You are a professional translator working with JS files. + +Task: Translate the content below from en to cn. + +Translation Requirements: +- Maintain exact file structure, indentation, and formatting +- Provide natural, culturally-adapted translations that sound native +- Keep all technical identifiers unchanged +- Keep consistent capitalization, spacing, and line breaks +- Respect existing whitespace and newline patterns +- Never add space before a ! or ? +- Preserve all object/property keys, syntax characters, and punctuation marks exactly +- Only translate text content within quotation marks + +A list of javascript codeblocks, return the translated javascript string in a JSON array of string: +```js +"title" +``` + +```js +"Getting Stopped" +``` + +```js +`Explore ideas and examples of +what you can build with the Midday API` +``` + +```js +`the current time is not ${Date.now()}` +``` \ No newline at end of file