|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + mapQueueOutcomeToDeliveryResult, |
| 4 | + runSubagentAnnounceDispatch, |
| 5 | +} from "./subagent-announce-dispatch.js"; |
| 6 | + |
| 7 | +describe("mapQueueOutcomeToDeliveryResult", () => { |
| 8 | + it("maps steered to delivered", () => { |
| 9 | + expect(mapQueueOutcomeToDeliveryResult("steered")).toEqual({ |
| 10 | + delivered: true, |
| 11 | + path: "steered", |
| 12 | + }); |
| 13 | + }); |
| 14 | + |
| 15 | + it("maps queued to delivered", () => { |
| 16 | + expect(mapQueueOutcomeToDeliveryResult("queued")).toEqual({ |
| 17 | + delivered: true, |
| 18 | + path: "queued", |
| 19 | + }); |
| 20 | + }); |
| 21 | + |
| 22 | + it("maps none to not-delivered", () => { |
| 23 | + expect(mapQueueOutcomeToDeliveryResult("none")).toEqual({ |
| 24 | + delivered: false, |
| 25 | + path: "none", |
| 26 | + }); |
| 27 | + }); |
| 28 | +}); |
| 29 | + |
| 30 | +describe("runSubagentAnnounceDispatch", () => { |
| 31 | + it("uses queue-first ordering for non-completion mode", async () => { |
| 32 | + const queue = vi.fn(async () => "none" as const); |
| 33 | + const direct = vi.fn(async () => ({ delivered: true, path: "direct" as const })); |
| 34 | + |
| 35 | + const result = await runSubagentAnnounceDispatch({ |
| 36 | + expectsCompletionMessage: false, |
| 37 | + queue, |
| 38 | + direct, |
| 39 | + }); |
| 40 | + |
| 41 | + expect(queue).toHaveBeenCalledTimes(1); |
| 42 | + expect(direct).toHaveBeenCalledTimes(1); |
| 43 | + expect(result.delivered).toBe(true); |
| 44 | + expect(result.path).toBe("direct"); |
| 45 | + expect(result.phases).toEqual([ |
| 46 | + { phase: "queue-primary", delivered: false, path: "none", error: undefined }, |
| 47 | + { phase: "direct-primary", delivered: true, path: "direct", error: undefined }, |
| 48 | + ]); |
| 49 | + }); |
| 50 | + |
| 51 | + it("short-circuits direct send when non-completion queue delivers", async () => { |
| 52 | + const queue = vi.fn(async () => "queued" as const); |
| 53 | + const direct = vi.fn(async () => ({ delivered: true, path: "direct" as const })); |
| 54 | + |
| 55 | + const result = await runSubagentAnnounceDispatch({ |
| 56 | + expectsCompletionMessage: false, |
| 57 | + queue, |
| 58 | + direct, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(queue).toHaveBeenCalledTimes(1); |
| 62 | + expect(direct).not.toHaveBeenCalled(); |
| 63 | + expect(result.path).toBe("queued"); |
| 64 | + expect(result.phases).toEqual([ |
| 65 | + { phase: "queue-primary", delivered: true, path: "queued", error: undefined }, |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + it("uses direct-first ordering for completion mode", async () => { |
| 70 | + const queue = vi.fn(async () => "queued" as const); |
| 71 | + const direct = vi.fn(async () => ({ delivered: true, path: "direct" as const })); |
| 72 | + |
| 73 | + const result = await runSubagentAnnounceDispatch({ |
| 74 | + expectsCompletionMessage: true, |
| 75 | + queue, |
| 76 | + direct, |
| 77 | + }); |
| 78 | + |
| 79 | + expect(direct).toHaveBeenCalledTimes(1); |
| 80 | + expect(queue).not.toHaveBeenCalled(); |
| 81 | + expect(result.path).toBe("direct"); |
| 82 | + expect(result.phases).toEqual([ |
| 83 | + { phase: "direct-primary", delivered: true, path: "direct", error: undefined }, |
| 84 | + ]); |
| 85 | + }); |
| 86 | + |
| 87 | + it("falls back to queue when completion direct send fails", async () => { |
| 88 | + const queue = vi.fn(async () => "steered" as const); |
| 89 | + const direct = vi.fn(async () => ({ |
| 90 | + delivered: false, |
| 91 | + path: "direct" as const, |
| 92 | + error: "network", |
| 93 | + })); |
| 94 | + |
| 95 | + const result = await runSubagentAnnounceDispatch({ |
| 96 | + expectsCompletionMessage: true, |
| 97 | + queue, |
| 98 | + direct, |
| 99 | + }); |
| 100 | + |
| 101 | + expect(direct).toHaveBeenCalledTimes(1); |
| 102 | + expect(queue).toHaveBeenCalledTimes(1); |
| 103 | + expect(result.path).toBe("steered"); |
| 104 | + expect(result.phases).toEqual([ |
| 105 | + { phase: "direct-primary", delivered: false, path: "direct", error: "network" }, |
| 106 | + { phase: "queue-fallback", delivered: true, path: "steered", error: undefined }, |
| 107 | + ]); |
| 108 | + }); |
| 109 | + |
| 110 | + it("returns direct failure when completion fallback queue cannot deliver", async () => { |
| 111 | + const queue = vi.fn(async () => "none" as const); |
| 112 | + const direct = vi.fn(async () => ({ |
| 113 | + delivered: false, |
| 114 | + path: "direct" as const, |
| 115 | + error: "failed", |
| 116 | + })); |
| 117 | + |
| 118 | + const result = await runSubagentAnnounceDispatch({ |
| 119 | + expectsCompletionMessage: true, |
| 120 | + queue, |
| 121 | + direct, |
| 122 | + }); |
| 123 | + |
| 124 | + expect(result).toMatchObject({ |
| 125 | + delivered: false, |
| 126 | + path: "direct", |
| 127 | + error: "failed", |
| 128 | + }); |
| 129 | + expect(result.phases).toEqual([ |
| 130 | + { phase: "direct-primary", delivered: false, path: "direct", error: "failed" }, |
| 131 | + { phase: "queue-fallback", delivered: false, path: "none", error: undefined }, |
| 132 | + ]); |
| 133 | + }); |
| 134 | + |
| 135 | + it("returns none immediately when signal is already aborted", async () => { |
| 136 | + const queue = vi.fn(async () => "none" as const); |
| 137 | + const direct = vi.fn(async () => ({ delivered: true, path: "direct" as const })); |
| 138 | + const controller = new AbortController(); |
| 139 | + controller.abort(); |
| 140 | + |
| 141 | + const result = await runSubagentAnnounceDispatch({ |
| 142 | + expectsCompletionMessage: true, |
| 143 | + signal: controller.signal, |
| 144 | + queue, |
| 145 | + direct, |
| 146 | + }); |
| 147 | + |
| 148 | + expect(queue).not.toHaveBeenCalled(); |
| 149 | + expect(direct).not.toHaveBeenCalled(); |
| 150 | + expect(result).toEqual({ |
| 151 | + delivered: false, |
| 152 | + path: "none", |
| 153 | + phases: [], |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments