From abc16101452ed55574f36bb93d6edf1c38538861 Mon Sep 17 00:00:00 2001 From: Nightt <87569709+nightt5879@users.noreply.github.com> Date: Sat, 9 May 2026 11:50:46 +0800 Subject: [PATCH] Add extractSubagentIds tests --- __tests__/lib/extract-subagent-ids.test.ts | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 __tests__/lib/extract-subagent-ids.test.ts diff --git a/__tests__/lib/extract-subagent-ids.test.ts b/__tests__/lib/extract-subagent-ids.test.ts new file mode 100644 index 0000000..a29709e --- /dev/null +++ b/__tests__/lib/extract-subagent-ids.test.ts @@ -0,0 +1,56 @@ +import { describe, it, expect } from "vitest"; +import { extractSubagentIds } from "@/lib/extract-subagent-ids"; + +describe("extractSubagentIds", () => { + it("returns deduped ids from valid user entries", () => { + const fileContent = [ + JSON.stringify({ type: "user", toolUseResult: { agentId: "abc123" } }), + JSON.stringify({ type: "user", toolUseResult: { agentId: "def456" } }), + JSON.stringify({ type: "user", toolUseResult: { agentId: "abc123" } }), + ].join("\n"); + + expect(extractSubagentIds(fileContent)).toEqual(["abc123", "def456"]); + }); + + it("skips malformed JSON lines silently", () => { + const fileContent = [ + JSON.stringify({ type: "user", toolUseResult: { agentId: "abc123" } }), + "{bad json", + JSON.stringify({ type: "user", toolUseResult: { agentId: "def456" } }), + ].join("\n"); + + expect(extractSubagentIds(fileContent)).toEqual(["abc123", "def456"]); + }); + + it("ignores entries where type is not user", () => { + const fileContent = [ + JSON.stringify({ type: "assistant", toolUseResult: { agentId: "abc123" } }), + JSON.stringify({ type: "system", toolUseResult: { agentId: "def456" } }), + ].join("\n"); + + expect(extractSubagentIds(fileContent)).toEqual([]); + }); + + it("rejects non-hex agent ids", () => { + const fileContent = [ + JSON.stringify({ type: "user", toolUseResult: { agentId: "abc123" } }), + JSON.stringify({ type: "user", toolUseResult: { agentId: "abc123g" } }), + JSON.stringify({ type: "user", toolUseResult: { agentId: "ABC123" } }), + ].join("\n"); + + expect(extractSubagentIds(fileContent)).toEqual(["abc123"]); + }); + + it("ignores entries missing toolUseResult", () => { + const fileContent = [ + JSON.stringify({ type: "user" }), + JSON.stringify({ type: "user", toolUseResult: null }), + ].join("\n"); + + expect(extractSubagentIds(fileContent)).toEqual([]); + }); + + it("returns an empty array for empty input", () => { + expect(extractSubagentIds("")).toEqual([]); + }); +});