Skip to content

Commit

Permalink
add test for diff
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Dec 25, 2024
1 parent 122d343 commit 0c2466b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/commands/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
38 changes: 37 additions & 1 deletion packages/cli/test/js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down Expand Up @@ -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");
});
11 changes: 11 additions & 0 deletions packages/cli/test/resources/js-diff.js
Original file line number Diff line number Diff line change
@@ -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()}`
}
10 changes: 10 additions & 0 deletions packages/cli/test/resources/js-diff.previous.js
Original file line number Diff line number Diff line change
@@ -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()}`
}
10 changes: 10 additions & 0 deletions packages/cli/test/resources/js-diff.translated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

export default {
"title": "Translated",
description: 'Translated\nTranslated',
nested: {
content: `Translated
Translated`
},
dynamic: `Translated ${Date.now()}`
}
10 changes: 10 additions & 0 deletions packages/cli/test/snapshots/js-diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

export default {
"title": "Updated",
description: 'Translated\nTranslated',
nested: {
content: `Updated
Updated`
},
dynamic: `Updated ${Date.now()}`
}
31 changes: 31 additions & 0 deletions packages/cli/test/snapshots/js-diff.prompt.txt
Original file line number Diff line number Diff line change
@@ -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()}`
```

0 comments on commit 0c2466b

Please sign in to comment.