|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { applyDelegationCapability, resolveDelegationCapability } from "./delegation-capability.js"; |
| 3 | +import type { AnyAgentTool } from "./tools/common.js"; |
| 4 | + |
| 5 | +function createTool( |
| 6 | + name: string, |
| 7 | + execute = vi.fn(async () => ({ content: [], details: {} })), |
| 8 | +): AnyAgentTool { |
| 9 | + return { |
| 10 | + name, |
| 11 | + label: name, |
| 12 | + description: name, |
| 13 | + parameters: {} as never, |
| 14 | + execute, |
| 15 | + } as unknown as AnyAgentTool; |
| 16 | +} |
| 17 | + |
| 18 | +describe("delegation capability", () => { |
| 19 | + it("enters report-only mode only for fallback completion reports", () => { |
| 20 | + expect( |
| 21 | + resolveDelegationCapability({ |
| 22 | + fallbackActive: true, |
| 23 | + inputProvenance: { kind: "inter_session", sourceTool: "subagent_announce" }, |
| 24 | + }), |
| 25 | + ).toBe("report_only"); |
| 26 | + expect( |
| 27 | + resolveDelegationCapability({ |
| 28 | + fallbackActive: true, |
| 29 | + inputProvenance: { kind: "inter_session", sourceTool: "agent_harness_task" }, |
| 30 | + }), |
| 31 | + ).toBe("report_only"); |
| 32 | + expect( |
| 33 | + resolveDelegationCapability({ |
| 34 | + fallbackActive: false, |
| 35 | + inputProvenance: { kind: "inter_session", sourceTool: "subagent_announce" }, |
| 36 | + }), |
| 37 | + ).toBe("full"); |
| 38 | + expect( |
| 39 | + resolveDelegationCapability({ |
| 40 | + fallbackActive: true, |
| 41 | + inputProvenance: { kind: "external_user" }, |
| 42 | + }), |
| 43 | + ).toBe("full"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("removes delegation tools but retains ordinary reporting tools", () => { |
| 47 | + const tools = [ |
| 48 | + createTool("sessions_spawn"), |
| 49 | + createTool("sessions_send"), |
| 50 | + createTool("openclaw"), |
| 51 | + createTool("llm-task"), |
| 52 | + createTool("codex_session_send"), |
| 53 | + createTool("message"), |
| 54 | + createTool("gateway"), |
| 55 | + createTool("sessions_history"), |
| 56 | + ]; |
| 57 | + |
| 58 | + expect(applyDelegationCapability(tools, "report_only").map((tool) => tool.name)).toEqual([ |
| 59 | + "message", |
| 60 | + "gateway", |
| 61 | + "sessions_history", |
| 62 | + ]); |
| 63 | + expect(applyDelegationCapability(tools, "full")).toBe(tools); |
| 64 | + }); |
| 65 | + |
| 66 | + it("keeps status and cleanup actions while rejecting new background work", async () => { |
| 67 | + const cronExecute = vi.fn(async () => ({ content: [], details: {} })); |
| 68 | + const imageExecute = vi.fn(async () => ({ content: [], details: {} })); |
| 69 | + const [cron, image] = applyDelegationCapability( |
| 70 | + [createTool("cron", cronExecute), createTool("image_generate", imageExecute)], |
| 71 | + "report_only", |
| 72 | + ); |
| 73 | + |
| 74 | + await expect(cron?.execute("cron-status", { action: "status" })).resolves.toEqual({ |
| 75 | + content: [], |
| 76 | + details: {}, |
| 77 | + }); |
| 78 | + await expect(cron?.execute("cron-remove", { action: "remove" })).resolves.toEqual({ |
| 79 | + content: [], |
| 80 | + details: {}, |
| 81 | + }); |
| 82 | + await expect(cron?.execute("cron-add", { action: "add" })).rejects.toThrow( |
| 83 | + "New delegation is unavailable", |
| 84 | + ); |
| 85 | + await expect(image?.execute("image-status", { action: "status" })).resolves.toEqual({ |
| 86 | + content: [], |
| 87 | + details: {}, |
| 88 | + }); |
| 89 | + await expect(image?.execute("image-generate", {})).rejects.toThrow( |
| 90 | + "New delegation is unavailable", |
| 91 | + ); |
| 92 | + expect(cronExecute).toHaveBeenCalledTimes(2); |
| 93 | + expect(imageExecute).toHaveBeenCalledTimes(1); |
| 94 | + }); |
| 95 | +}); |
0 commit comments