Skip to content

Commit

Permalink
improve unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Dec 25, 2024
1 parent 0c2466b commit 37c7369
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 34 deletions.
27 changes: 16 additions & 11 deletions packages/cli/test/js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath } from "node:url";
import path from "node:path";
import { MockLanguageModelV1 } from "ai/test";
import { javascript } from "../src/translators/js.js";
import { getPromptText } from "./test-utils.js";

const dir = path.dirname(fileURLToPath(import.meta.url));

Expand All @@ -20,9 +21,9 @@ test("JavaScript adapter: new", async () => {
model: new MockLanguageModelV1({
defaultObjectGenerationMode: "json",
async doGenerate(v) {
await expect(
(v.prompt.at(-1) as any).content[0].text,
).toMatchFileSnapshot("snapshots/js-new.prompt.txt");
await expect(getPromptText(v.prompt)).toMatchFileSnapshot(
"snapshots/js-new.prompt.txt",
);

return {
rawCall: { rawPrompt: null, rawSettings: {} },
Expand All @@ -46,27 +47,31 @@ test("JavaScript adapter: new", async () => {
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(),
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(),
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");
await expect(getPromptText(v.prompt)).toMatchFileSnapshot(
"snapshots/js-diff.prompt.txt",
);

return {
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: "stop",
usage: { promptTokens: 10, completionTokens: 20 },
text: JSON.stringify([
"\"title\"",
'"title"',
'"Updated"',
"`Updated\nUpdated`",
"`Updated ${Date.now()}`",
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/test/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { fileURLToPath } from "node:url";
import path from "node:path";
import { MockLanguageModelV1 } from "ai/test";
import { json } from "../src/translators/json.js";
import { getPromptText } from "./test-utils.js";

const dir = path.dirname(fileURLToPath(import.meta.url));

Expand Down Expand Up @@ -58,7 +59,7 @@ test("JSON adapter: diff", async () => {
model: new MockLanguageModelV1({
defaultObjectGenerationMode: "json",
async doGenerate(v) {
await expect(v.prompt.at(-1)).toMatchFileSnapshot(
await expect(getPromptText(v.prompt)).toMatchFileSnapshot(
"snapshots/json-diff.prompt.txt",
);

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/test/md.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getPromptText } from "./test-utils.js";
import { expect, test } from "vitest";
import { markdown } from "../src/translators/md.js";
import { Config } from "../src/types.js";
Expand Down Expand Up @@ -47,7 +48,7 @@ test("markdown adapter: diff", async () => {
model: new MockLanguageModelV1({
defaultObjectGenerationMode: "json",
async doGenerate(v) {
await expect(v.prompt.at(-1)).toMatchFileSnapshot(
await expect(getPromptText(v.prompt)).toMatchFileSnapshot(
"snapshots/md-diff.prompt.txt",
);

Expand Down
11 changes: 1 addition & 10 deletions packages/cli/test/snapshots/json-diff.prompt.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"content": [
{
"text": "You are a professional translator working with JSON files.
You are a professional translator working with JSON files.

Task: Translate the content below from en to cn.

Expand All @@ -25,10 +22,4 @@ Source content (JSON), Return only the translated content with identical structu
"nested": {
"description": "nothing is happening"
}
}",
"type": "text",
},
],
"providerMetadata": undefined,
"role": "user",
}
12 changes: 1 addition & 11 deletions packages/cli/test/snapshots/md-diff.prompt.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{
"content": [
{
"text": "You are a professional translator working with MD files.
You are a professional translator working with MD files.

Task: Translate the content below from en to cn.

Expand Down Expand Up @@ -30,10 +27,3 @@ export default function Layout() {
```

<div>Hello World</div>
",
"type": "text",
},
],
"providerMetadata": undefined,
"role": "user",
}
11 changes: 11 additions & 0 deletions packages/cli/test/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { LanguageModelV1Prompt } from "ai";

export function getPromptText(v: LanguageModelV1Prompt) {
const content = v.at(-1)?.content;
if (!content || !Array.isArray(content)) return content;

return content
.filter((v) => v.type === "text")
.map((v) => v.text)
.join("\n");
}

0 comments on commit 37c7369

Please sign in to comment.