Skip to content

World's first open-source enterprise world model.

4.4 KB
Raw Blame
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import Markdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { describe, expect, it } from "vitest";
import type { Source } from "./api";
import { rehypeCitations } from "./citations";
import { answeredWithoutSources, citedSources, type Turn } from "./liveAnswer";

const answer = (over: Partial<Turn> = {}): Turn => ({
  role: "assistant",
  content: "以下是我找到的内容",
  ...over,
});
const source = (n: number) => ({ n, kind: "chunk" }) as unknown as Source;
const six = [1, 2, 3, 4, 5, 6].map(source);

describe("citedSources", () => {
  it("lists only the sources the text points at, numbers unchanged", () => {
    const t = answer({ content: "营收增长 [2],毛利下降 [5]。", sources: six });
    expect(citedSources(t).map((s) => s.n)).toEqual([2, 5]);
  });

  it("reads adjacent and comma-separated markers", () => {
    const t = answer({ content: "见 [1][3] 与 [4, 6],另见 [2,5]", sources: six });
    expect(citedSources(t).map((s) => s.n)).toEqual([1, 2, 3, 4, 5, 6]);
  });

  it("lists nothing when a greeting cites nothing it retrieved", () => {
    const t = answer({ content: "你好!我是 Utopia 的知识助手。", sources: six });
    expect(citedSources(t)).toEqual([]);
  });

  it("ignores a number that matches no source", () => {
    const t = answer({ content: "见 [9]", sources: six });
    expect(citedSources(t)).toEqual([]);
  });
});

/** 正文渲染出来的角标:与 Chat 页同一组插件 */
function marks(text: string): number[] {
  const html = renderToStaticMarkup(
    createElement(Markdown, { remarkPlugins: [remarkGfm], rehypePlugins: [rehypeCitations] }, text),
  );
  const out = new Set<number>();
  for (const m of html.matchAll(/href="#cite-([\d,]+)"/g)) {
    for (const n of m[1].split(",")) out.add(Number(n));
  }
  return [...out].sort((a, b) => a - b);
}

describe("citedSources lists what the answer shows as a citation mark", () => {
  const at = (content: string) => citedSources(answer({ content, sources: six })).map((s) => s.n);

  it("skips numbers in code blocks, inline code and link text", () => {
    expect(at("```json\n[4, 5]\n```\nThe figure is in [1].")).toEqual([1]);
    expect(at("Inline `rows[2]` and `[3]` are code; [6] is a mark.")).toEqual([6]);
    expect(at("A link [4](https://example.com) is not a mark; [5] is.")).toEqual([5]);
  });

  it("counts an escaped bracket, which renders as a mark", () => {
    expect(at("An escaped \\[2\\] still reads as a mark.")).toEqual([2]);
  });

  it.each([
    "Revenue grew [1]; see also [2, 3].",
    "```json\n[4, 5]\n```\nThe figure is in [1].",
    "Inline `rows[2]` and `[3]` are code; [6] is a mark.",
    "A link [4](https://example.com) is not a mark; [5] is.",
    "An escaped \\[2\\] still reads as a mark.",
    "| metric | source |\n|---|---|\n| revenue | [3] |",
    "> quoted [1]\n\n- item [2]\n- item [4]",
  ])("agrees with the marks the answer renders: %j", (content) => {
    expect(at(content)).toEqual(marks(content));
  });
});

describe("answeredWithoutSources (#547)", () => {
  it("marks a finished answer with no sources", () => {
    expect(answeredWithoutSources(answer({ sources: [] }), false)).toBe(true);
  });

  it("marks an answer that retrieved sources but cites none", () => {
    expect(answeredWithoutSources(answer({ sources: six }), false)).toBe(true);
  });

  it("does not mark an answer that cites something", () => {
    const t = answer({ content: "见 [1]", sources: [source(1)] });
    expect(answeredWithoutSources(t, false)).toBe(false);
  });

  it("waits for the stream to finish before deciding", () => {
    expect(answeredWithoutSources(answer(), true)).toBe(false);
    expect(answeredWithoutSources(answer(), false)).toBe(true);
  });

  it("marks a replayed turn, whose empty sources are stored as undefined", () => {
    expect(answeredWithoutSources(answer({ sources: undefined }), false)).toBe(true);
  });

  it("leaves user turns and bare errors alone", () => {
    expect(answeredWithoutSources({ role: "user", content: "hi" }, false)).toBe(false);
    expect(answeredWithoutSources(answer({ content: "", error: "boom" }), false)).toBe(false);
    expect(answeredWithoutSources(answer({ error: "stopped" }), false)).toBe(true);
    expect(answeredWithoutSources(answer({ content: "", stopped: true }), false)).toBe(false);
  });
});