Files
streamline/satellite/test/readable_translate_prompt.test.ts
T
2026-11-16 17:26:44 -08:00

81 lines
3.8 KiB
TypeScript

import { test, expect, describe } from "bun:test";
import {
renderReadableTranslatePrompt, otherLang, READABLE_TRANSLATE_MAX_CHARS,
} from "@bbbug/shared/readable_prompt";
/**
* 双语报告的第二份是**翻译**出来的,不是重新生成的 —— 两次独立生成会各自决定用哪张
* 主图、留哪几张卡片,于是中/EN 一切换就是两份说法不一样的报告。
*
* 这里钉住的正是「让翻译能替代重生成」的那几条约束:结构冻结、data-evidence 文件名
* 冻结、<code> 冻结、数字冻结。任何一条在提示词里丢了,都会以「译文渲染不出图」或
* 「两种语言的结论对不上」的形式在生产上出现,而那时已经没人看得出是提示词改了。
*/
const FRAGMENT = `<section class="llm-report">
<header class="llm-report__header">
<div class="llm-report__badges"><span class="llm-report__badge llm-report__badge--bug">BUG</span></div>
<h1 class="llm-report__title">点赞按钮在无权限时仍高亮</h1>
</header>
<section class="llm-report__shots">
<figure class="llm-report__shot llm-report__shot--before" data-evidence="before-1-story.png">
<figcaption><b>修复前</b><span>按钮仍是高亮态</span></figcaption>
</figure>
</section>
</section>`;
describe("renderReadableTranslatePrompt", () => {
test("翻译成英文时带上 zh→en 的固定标签表", () => {
const p = renderReadableTranslatePrompt({ html: FRAGMENT, targetLang: "en" });
expect(p).toContain("Translate it into ENGLISH.");
expect(p).toContain("修复历程→Fix Timeline");
expect(p).toContain("未修复→Not Fixed");
});
test("翻译成中文时标签表整体反向,而不是照抄英文那份", () => {
const p = renderReadableTranslatePrompt({ html: FRAGMENT, targetLang: "zh" });
expect(p).toContain("Translate it into SIMPLIFIED CHINESE");
expect(p).toContain("Fix Timeline→修复历程");
expect(p).toContain("Not Fixed→未修复");
// 反向表里不能残留正向条目 —— 那意味着某一行没被翻转。
expect(p).not.toContain("修复历程→Fix Timeline");
});
test("原样带上待翻译片段,且不加围栏", () => {
const p = renderReadableTranslatePrompt({ html: FRAGMENT, targetLang: "en" });
expect(p).toContain(FRAGMENT);
expect(p).not.toContain("```");
});
test("四条冻结约束都在:结构 / data-evidence / <code> / 数字", () => {
const p = renderReadableTranslatePrompt({ html: FRAGMENT, targetLang: "en" });
expect(p).toContain("Keep the DOM EXACTLY as given");
expect(p).toContain(`data-evidence="<filename>"`);
expect(p).toContain("Keep the contents of <code> and <pre> UNCHANGED");
expect(p).toContain("Keep every number");
// 只输出片段本身 —— 前端把返回值直接当 HTML 用。
expect(p).toContain("Return ONLY the translated HTML fragment");
});
test("超长片段被截断并留下痕迹", () => {
const huge = `${FRAGMENT}${"x".repeat(READABLE_TRANSLATE_MAX_CHARS)}`;
const p = renderReadableTranslatePrompt({ html: huge, targetLang: "en" });
expect(p).toContain("TRUNCATED");
expect(p).not.toContain(huge);
expect(p).toContain(huge.slice(0, 200));
});
test("标题只作为上下文出现,没有标题时整段不出", () => {
const withTitle = renderReadableTranslatePrompt({ html: FRAGMENT, targetLang: "en", title: "点赞按钮" });
expect(withTitle).toContain("Report title (context only)");
const without = renderReadableTranslatePrompt({ html: FRAGMENT, targetLang: "en" });
expect(without).not.toContain("Report title (context only)");
});
});
describe("otherLang", () => {
test("成对互换 —— 英文条目缺的是中文版,不是又一份英文", () => {
expect(otherLang("zh")).toBe("en");
expect(otherLang("en")).toBe("zh");
});
});