|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { Wildcard } from "../../src/util/wildcard" |
| 3 | + |
| 4 | +describe("Task tool subagents filtering", () => { |
| 5 | + // These tests verify the filtering logic used in task.ts |
| 6 | + // The actual filtering is: agents.filter(a => !caller?.subagents?.length || caller.subagents.some(pattern => Wildcard.match(a.name, pattern))) |
| 7 | + |
| 8 | + const mockAgents = [ |
| 9 | + { name: "general", description: "General purpose agent" }, |
| 10 | + { name: "explore", description: "Codebase exploration" }, |
| 11 | + { name: "code-reviewer", description: "Code review agent" }, |
| 12 | + { name: "code-formatter", description: "Code formatting agent" }, |
| 13 | + { name: "test-runner", description: "Test execution agent" }, |
| 14 | + { name: "docs-generator", description: "Documentation generator" }, |
| 15 | + ] |
| 16 | + |
| 17 | + const filterAgents = (agents: typeof mockAgents, subagents?: string[]) => |
| 18 | + agents.filter((a) => !subagents?.length || subagents.some((pattern) => Wildcard.match(a.name, pattern))) |
| 19 | + |
| 20 | + test("returns all agents when subagents is undefined", () => { |
| 21 | + const result = filterAgents(mockAgents, undefined) |
| 22 | + expect(result).toHaveLength(6) |
| 23 | + }) |
| 24 | + |
| 25 | + test("returns all agents when subagents is empty array", () => { |
| 26 | + const result = filterAgents(mockAgents, []) |
| 27 | + expect(result).toHaveLength(6) |
| 28 | + }) |
| 29 | + |
| 30 | + test("filters to exact matches", () => { |
| 31 | + const result = filterAgents(mockAgents, ["general", "explore"]) |
| 32 | + expect(result).toHaveLength(2) |
| 33 | + expect(result.map((a) => a.name)).toEqual(["general", "explore"]) |
| 34 | + }) |
| 35 | + |
| 36 | + test("filters using wildcard patterns", () => { |
| 37 | + const result = filterAgents(mockAgents, ["code-*"]) |
| 38 | + expect(result).toHaveLength(2) |
| 39 | + expect(result.map((a) => a.name)).toEqual(["code-reviewer", "code-formatter"]) |
| 40 | + }) |
| 41 | + |
| 42 | + test("filters using mixed exact and wildcard patterns", () => { |
| 43 | + const result = filterAgents(mockAgents, ["general", "code-*"]) |
| 44 | + expect(result).toHaveLength(3) |
| 45 | + expect(result.map((a) => a.name)).toEqual(["general", "code-reviewer", "code-formatter"]) |
| 46 | + }) |
| 47 | + |
| 48 | + test("filters using global wildcard allows all", () => { |
| 49 | + const result = filterAgents(mockAgents, ["*"]) |
| 50 | + expect(result).toHaveLength(6) |
| 51 | + }) |
| 52 | + |
| 53 | + test("filters using suffix wildcard", () => { |
| 54 | + const result = filterAgents(mockAgents, ["*-runner", "*-generator"]) |
| 55 | + expect(result).toHaveLength(2) |
| 56 | + expect(result.map((a) => a.name)).toEqual(["test-runner", "docs-generator"]) |
| 57 | + }) |
| 58 | + |
| 59 | + test("returns empty when no patterns match", () => { |
| 60 | + const result = filterAgents(mockAgents, ["nonexistent", "also-nonexistent"]) |
| 61 | + expect(result).toHaveLength(0) |
| 62 | + }) |
| 63 | + |
| 64 | + test("handles single character wildcard", () => { |
| 65 | + // ? matches single character |
| 66 | + const result = filterAgents(mockAgents, ["code-?eviewer"]) |
| 67 | + expect(result).toHaveLength(1) |
| 68 | + expect(result[0].name).toBe("code-reviewer") |
| 69 | + }) |
| 70 | +}) |
0 commit comments