import { CsvAutoTranslator } from "../src/CsvAutoTranslator"; import { CSVUtils } from "../src/CSVUtils"; describe("CsvAutoTranslator", () => { test("pass", () => { expect(true).toBe(true); }) // const testRows: string[][] = [ // ["key", "zh_cn", "zh_hk"], // ["你好世界", "", ""], // ["你好吗?", "", ""], // ["非常感谢", "", ""], // ["早上好", "", ""], // ["很高兴见到你", "", ""], // ["回头见", "", ""], // ["我热爱编程", "", ""], // ["这是一个测试", "", ""], // ["祝你今天愉快", "", ""], // ["欢迎来到中国", "", ""] // ]; // let NIUTRANS_APP_ID = "kDr1772519780125"; // let NIUTRANS_API_KEY = "4cfb5525d3be1e45003910059cd7ea9b"; // test("translateCsvRows - 基本功能测试", async () => { // const apiKey = NIUTRANS_API_KEY || ""; // const appId = NIUTRANS_APP_ID || ""; // if (!apiKey || !appId) { // console.warn("未设置 NIUTRANS_API_KEY 和 NIUTRANS_APP_ID 环境变量,跳过在线翻译测试"); // console.warn("设置方式: $env:NIUTRANS_API_KEY='your-key'; $env:NIUTRANS_APP_ID='your-id'"); // expect(true).toBe(true); // return; // } // const translator = new CsvAutoTranslator(appId, apiKey); // const rows = JSON.parse(JSON.stringify(testRows)); // const translatedCount = await translator.translateCsvRows(rows, "zh_cn"); // expect(translatedCount).toBe(10); // expect(rows.length).toBe(11); // for (let i = 1; i < rows.length; i++) { // expect(rows[i][2]).toBeTruthy(); // expect(rows[i][2].trim()).not.toBe(""); // console.log(`原文: ${rows[i][0]} -> 译文: ${rows[i][2]}`); // } // }, 120000); // test("translateCsvRows - 跳过已有内容的行", async () => { // const apiKey = NIUTRANS_API_KEY || ""; // const appId = NIUTRANS_APP_ID || ""; // if (!apiKey || !appId) { // expect(true).toBe(true); // return; // } // const translator = new CsvAutoTranslator(appId, apiKey); // const rows = JSON.parse(JSON.stringify(testRows)); // rows[1][2] = "已有译文"; // const translatedCount = await translator.translateCsvRows(rows, "zh_cn"); // expect(translatedCount).toBe(10); // expect(rows[10][2]).toBe("歡迎來到中國"); // }, 120000); // test("translateCsvRows - 空CSV测试", async () => { // const translator = new CsvAutoTranslator(NIUTRANS_APP_ID, NIUTRANS_API_KEY); // const translatedCount = await translator.translateCsvRows([], "zh_cn"); // expect(translatedCount).toBe(0); // }); // test("translateCsvRows - 没有需要翻译的内容", async () => { // const translator = new CsvAutoTranslator(NIUTRANS_APP_ID, NIUTRANS_API_KEY); // const rows = [ // ["key", "en_us", "zh_hk"], // ["Hello", "", "已有译文1"], // ["World", "", "已有译文2"] // ]; // const translatedCount = await translator.translateCsvRows(rows, "zh"); // expect(translatedCount).toBe(0); // }); // describe("smartBatch - 边界回归测试", () => { // const translator = new CsvAutoTranslator(NIUTRANS_APP_ID, NIUTRANS_API_KEY); // test("空文本数组", () => { // const batches = translator.smartBatch([]); // expect(batches.length).toBe(0); // }); // test("单条短文本", () => { // const batches = translator.smartBatch(["Hello"]); // expect(batches.length).toBe(1); // expect(batches[0].length).toBe(1); // expect(translator.estimateJsonSize(batches[0])).toBeLessThanOrEqual(4900); // }); // test("刚好50条短文本", () => { // const texts = Array(50).fill("test"); // const batches = translator.smartBatch(texts); // expect(batches.length).toBe(1); // expect(batches[0].length).toBe(50); // }); // test("51条短文本", () => { // const texts = Array(51).fill("test"); // const batches = translator.smartBatch(texts); // expect(batches.length).toBe(2); // expect(batches[0].length).toBe(50); // expect(batches[1].length).toBe(1); // }); // test("单条超长文本", () => { // const longText = "a".repeat(5000); // const batches = translator.smartBatch([longText]); // expect(batches.length).toBe(1); // expect(batches[0].length).toBe(1); // }); // test("多条文本累加超过4900字节", () => { // const mediumText = "a".repeat(100); // const texts = Array(100).fill(mediumText); // const batches = translator.smartBatch(texts); // for (const batch of batches) { // expect(batch.length).toBeLessThanOrEqual(50); // expect(translator.estimateJsonSize(batch)).toBeLessThanOrEqual(4900); // } // }); // test("包含特殊字符的文本", () => { // const texts = ['Hello "world"', "Line1\nLine2", "Tab\tSeparated", "Back\\slash"]; // const batches = translator.smartBatch(texts); // expect(batches.length).toBe(1); // expect(batches[0].length).toBe(4); // }); // test("传入现有batches数组", () => { // const existingBatches = [["existing1", "existing2"]]; // const newTexts = ["new1", "new2"]; // const batches = translator.smartBatch(newTexts, existingBatches); // expect(batches.length).toBe(2); // expect(batches[0]).toEqual(["existing1", "existing2"]); // expect(batches[1]).toEqual(["new1", "new2"]); // }); // test("混合长度文本", () => { // const texts = [ // "short", // "a".repeat(100), // "medium", // "b".repeat(200), // "another short" // ]; // const batches = translator.smartBatch(texts); // for (const batch of batches) { // expect(batch.length).toBeLessThanOrEqual(50); // expect(translator.estimateJsonSize(batch)).toBeLessThanOrEqual(4900); // } // const allTexts = batches.flat(); // expect(allTexts).toEqual(texts); // }); // }); // describe("translateCsv - Auto.csv 翻译测试", () => { // const apiKey = "4cfb5525d3be1e45003910059cd7ea9b"; // const appId = "kDr1772519780125"; // const translator = new CsvAutoTranslator(apiKey, appId); // const inputFile = "test/Auto.csv"; // const outputFile = "test/Auto-Out.csv"; // test("翻译 Auto.csv 并验证输出", async () => { // await translator.translateCsv(inputFile, outputFile, "zh_cn"); // const csvUtils = new CSVUtils(outputFile); // const rows = await csvUtils.parseCsv(); // expect(rows.length).toBeGreaterThan(1); // for (let i = 1; i < rows.length; i++) { // const row = rows[i]; // expect(row[0]).toBeTruthy(); // expect(row[2]).toBeTruthy(); // expect(row[2].trim()).not.toBe(""); // } // }, 300000); // }); // describe("translateCSV", () => { // test("翻译 Auto.csv 并验证输出", async () => { // const incsv = "test/Auto.csv"; // const outcsv = "temp/Auto-Out.csv"; // const fromLang = "zh_cn"; // let result = await CsvAutoTranslator.translateCsvWithLangs("kDr1772519780125", "4cfb5525d3be1e45003910059cd7ea9b", incsv, outcsv, fromLang); // expect(result.isOk).toBe(true); // expect(result.translateCount).toBeGreaterThan(0); // }); // }); });