import { test, expect, describe } from "bun:test";
import { detectExternalLinks, externalLinkPromptBlock } from "@bbbug/shared/external_links";
/**
* The two properties that matter here are both about NOT doing something.
*
* 1. A Meego link must never be read as a cloud doc. They share a domain family,
* and mistaking one for the other would tell the agent to open a work item
* with read_lark_doc — the failure mode that motivates the path whitelist.
* 2. The prompt block must be EMPTY when nothing is linked. It rides on the eval
* prompt's `description` variable (see evaluator.evalVars), so a block that
* always rendered would change every prompt in the RSI replay corpus and cost
* tokens on the ~95% of tasks that link nothing.
*/
const LARK = "https://bytedance.larkoffice.com/docx/XFLEdHT7BoS0dCxSvG0ukKj9sbe";
const WIKI = "https://bytedance.feishu.cn/wiki/Abcdefg12345678xyz";
const FIGMA = "https://figma.com/design/Q2t0GgS4vT5SKEQfry0D3X/Group-chat-2026?node-id=4456-4";
const MEEGO = "https://meego.larkoffice.com/tiktok/issue/detail/7357180292";
describe("detectExternalLinks", () => {
test("classifies each surface by its path, not its host", () => {
const found = detectExternalLinks(`${LARK}\n${WIKI}\n${FIGMA}\n${MEEGO}`);
expect(found.map((l) => l.kind).sort()).toEqual(["figma", "lark-doc", "lark-doc", "meego"]);
});
test("a Meego work item is never a doc", () => {
const found = detectExternalLinks(MEEGO);
expect(found).toHaveLength(1);
expect(found[0].kind).toBe("meego");
expect(found[0].token).toBe("7357180292");
});
test("keeps the docx token and the wiki surface apart", () => {
const [docx] = detectExternalLinks(LARK);
expect(docx.surface).toBe("docx");
expect(docx.token).toBe("XFLEdHT7BoS0dCxSvG0ukKj9sbe");
const [wiki] = detectExternalLinks(WIKI);
expect(wiki.surface).toBe("wiki");
});
test("pulls the Figma frame out of node-id, colon form included", () => {
expect(detectExternalLinks(FIGMA)[0].nodeId).toBe("4456-4");
expect(detectExternalLinks("https://www.figma.com/design/Q2t0GgS4vT5SKEQ/X?node-id=12%3A34")[0].nodeId)
.toBe("12-34");
});
test("de-duplicates a link pasted twice", () => {
expect(detectExternalLinks(`${LARK} and again ${LARK}`)).toHaveLength(1);
});
test("ignores prose and unrelated URLs", () => {
expect(detectExternalLinks("see https://example.com/docx/whatever for context")).toEqual([]);
expect(detectExternalLinks("no links at all")).toEqual([]);
});
});
describe("externalLinkPromptBlock", () => {
test("is empty when nothing linked — Meego is expanded elsewhere", () => {
expect(externalLinkPromptBlock("plain text", "eval")).toBe("");
expect(externalLinkPromptBlock(MEEGO, "run")).toBe("");
});
test("a wiki-only description still asks to open pictures and Figma in the cloud document", () => {
const block = externalLinkPromptBlock(LARK, "eval");
expect(block).toContain("
");
expect(block).toContain("docs +media-download");
expect(block).toContain("docs +media-preview");
expect(block).toContain("bug_workspace//lark-media/");
expect(block).not.toContain("feature-");
expect(externalLinkPromptBlock(LARK, "eval", "zh", "/Users/x/bug_workspace/7328764565"))
.toContain("/Users/x/bug_workspace/7328764565/lark-media/");
expect(externalLinkPromptBlock(LARK, "eval", "zh", "/Users/x/bug_workspace/feature-1"))
.toContain("/Users/x/bug_workspace/feature-1/lark-media/");
expect(block).toContain("权限错误");
expect(block).toContain("download_figma_images");
expect(block).toContain("NEEDS_INPUT");
expect(block).not.toContain("嵌套");
expect(block).not.toContain("lark-cli");
expect(block).not.toContain("read_lark_doc");
expect(externalLinkPromptBlock(LARK, "run")).toContain("验证局限");
});
test("asks for the design export when a design is linked", () => {
expect(externalLinkPromptBlock(FIGMA, "run")).toContain("download_figma_images");
expect(externalLinkPromptBlock(FIGMA, "run")).not.toContain("
");
});
test("a Lark link next to a Figma link does not name a Lark reader", () => {
const block = externalLinkPromptBlock(`${LARK}\n${FIGMA}`, "run");
expect(block).toContain("download_figma_images");
expect(block).toContain("
");
expect(block).toContain("docs +media-download");
expect(block).not.toContain("lark-cli");
expect(block).not.toContain("read_lark_doc");
expect(block).not.toContain("飞书");
expect(block).not.toContain("嵌套");
});
test("evaluators fall back to NEEDS_INPUT, runners report the limitation", () => {
expect(externalLinkPromptBlock(FIGMA, "eval")).toContain("NEEDS_INPUT");
expect(externalLinkPromptBlock(FIGMA, "run")).toContain("验证局限");
});
test("follows the submitter's language", () => {
expect(externalLinkPromptBlock(FIGMA, "run", "en")).toContain("Figma design");
expect(externalLinkPromptBlock(FIGMA, "run", "en")).not.toContain("设计稿");
});
});