import { describe, expect, it } from "vitest"; import { AgentInteractionService } from "@/runtime/coordination/agent-interaction-service"; const base = { interaction_id: "interaction-1", call_id: "call-1", agent_id: "agent-1", conversation_id: "conversation-1", interact_instance_id: "chat-1", created_at: "2026-08-05T00:00:00.000Z", }; describe("AgentInteractionService", () => { it("creates, presents and accepts notice exactly once", () => { const service = new AgentInteractionService(); expect(service.create({ ...base, request: { kind: "notice", title: "已保存", message: "任务已保存" } })).toMatchObject({ disposition: "accepted" }); expect(service.present("interaction-1", "2026-08-05T00:00:01.000Z")).toMatchObject({ disposition: "accepted", record: { status: "completed", result: { outcome: "accepted" } } }); expect(service.present("interaction-1", "2026-08-05T00:00:02.000Z")).toMatchObject({ disposition: "invalid_transition", record: { status: "completed" } }); }); it("lets only the first answer/dismiss/expiry transition win", () => { const service = new AgentInteractionService(); service.create({ ...base, request: { kind: "confirm", title: "确认", prompt: "继续吗?", expires_in_ms: 60_000 } }); service.present("interaction-1", "2026-08-05T00:00:01.000Z"); expect(service.submit("interaction-1", { approved: true }, "2026-08-05T00:00:02.000Z")).toMatchObject({ record: { status: "completed", result: { outcome: "answered" } } }); expect(service.dismiss({ call_id: "call-1", agent_id: "agent-1", conversation_id: "conversation-1" }, "2026-08-05T00:00:03.000Z")).toMatchObject({ disposition: "invalid_transition", record: { status: "completed" } }); }); it("rejects cross-agent dismiss and restores expired records", () => { const service = new AgentInteractionService(); service.create({ ...base, request: { kind: "input", title: "描述", prompt: "输入", expires_in_ms: 60_000, field: { id: "text", label: "描述", type: "textarea" } } }); expect(service.dismiss({ call_id: "call-1", agent_id: "other-agent", conversation_id: "conversation-1" }, "2026-08-05T00:00:01.000Z")).toEqual({ disposition: "invalid_transition" }); service.restore(service.list(), "2026-08-05T00:01:00.000Z"); expect(service.get("interaction-1")).toMatchObject({ status: "expired", result: { outcome: "expired" } }); }); });