askTrace.test.ts 3.45 KB
import { describe, expect, it } from "vitest";
import { applyEvent, buildGraph, emptyTrace, foldEvents, traceSummary } from "@/features/ask/trace";
import { normalizeTurn } from "@/lib/onto";

const EVENTS = [
  { type: "link", concepts: [{ id: "c1", name: "成交金额", kind: "function", type_key: "aggregate", matched: ["销售额"] }], instances: [], time: null, ts: 1 },
  { type: "agent_start", span: "lead", agent: "lead", label: "主 Agent", ts: 2 },
  { type: "thought", span: "lead", agent: "lead", text: "先取数,", ts: 3 },
  { type: "thought", span: "lead", agent: "lead", text: "再看原因。", ts: 4 },
  { type: "agent_start", span: "agent1", parent: "lead", agent: "knowledge", label: "知识 Agent", task: "查口径", ts: 5 },
  { type: "tool_start", span: "k1", parent: "agent1", agent: "knowledge", tool: "search_knowledge", label: "知识图谱多跳检索", args: {}, ts: 6 },
  { type: "tool_event", span: "k1", event: { type: "link", links: [{ term: "成交", matches: [{ id: "e1", label: "成交金额", type: "指标" }] }] }, ts: 7 },
  { type: "tool_event", span: "k1", event: { type: "hop", round: 1, paths: [{ id: "p1", source: "e1", target: "e2", relation: "依据", occurrence: 2 }] }, ts: 8 },
  { type: "tool_done", span: "k1", ok: true, summary: "命中 3 段", result: { chunks: [] }, ts: 9 },
  { type: "agent_done", span: "agent1", ok: true, summary: "口径是含税", confidence: 0.8, ts: 10 },
  { type: "memory", cases: [{ id: "mc1", question: "x", score: 0.95, status: "verified" }], facts: [], fast: "mc1", ts: 11 },
];

describe("提问推理轨迹", () => {
  it("实时事件与回放用同一个 reducer,思考逐段拼接", () => {
    const s = foldEvents(EVENTS);
    expect(s.order).toEqual(["lead", "agent1"]);
    expect(s.agents.lead.thought).toBe("先取数,再看原因。");
    expect(s.agents.agent1.tools).toEqual(["k1"]);
    expect(s.tools.k1.status).toBe("done");
    expect(s.tools.k1.events.length).toBe(2);
    expect(s.agents.agent1.confidence).toBe(0.8);
    expect(traceSummary(s, 12300)).toContain("调度 1 个智能体");
    expect(traceSummary(s, 12300)).toContain("复用已验证路径");
  });

  it("未知或残缺事件不会让界面崩", () => {
    let s = emptyTrace();
    for (const ev of [null, 1, {}, { type: "tool_done", span: "nope" }, { type: "agent_done", span: "x" }, { type: "tool_event" }]) {
      s = applyEvent(s, ev as any);
    }
    expect(s.order).toEqual([]);
  });

  it("本体图谱:概念、检索入口、多跳路径都画出来,同名节点连起来", () => {
    const g = buildGraph(foldEvents(EVENTS));
    const keys = g.payload!.nodes.map((n) => n.key);
    expect(keys).toEqual(expect.arrayContaining(["c:c1", "e1", "e2"]));
    expect(g.payload!.edges.some((e) => e.source === "e1" && e.target === "e2")).toBe(true);
    expect(g.payload!.edges.some((e) => e.attributes.relation === "即")).toBe(true);
    expect(buildGraph(emptyTrace()).payload).toBeNull();
  });

  it("历史轮次归一化:中断轮次的空 links、缺字段都补默认值", () => {
    const t = normalizeTurn({ id: "t1", question: "q", status: "aborted", links: {}, plan: null });
    expect(t.links.concepts).toEqual([]);
    expect(t.links.instances).toEqual([]);
    expect(t.steps).toEqual([]);
    expect(t.citations).toEqual([]);
    expect(t.status).toBe("aborted");
    const t2 = normalizeTurn({ links: { concepts: [{ id: "c", name: "n" }] } });
    expect(t2.links.concepts[0].matched).toEqual([]);
  });
});