|
| 1 | +import { SessionChannelRouter, WaitpointTimeoutError } from "@trigger.dev/core/v3"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { waitForChatRouteAfterIdle } from "./chatRouteWait.js"; |
| 4 | + |
| 5 | +function inputRouter() { |
| 6 | + return new SessionChannelRouter({ |
| 7 | + kindOf: (data) => (data as { kind?: string }).kind, |
| 8 | + routes: [ |
| 9 | + { name: "messages", delivery: "queue", replayable: true, kinds: ["message"] }, |
| 10 | + { name: "control", delivery: "queue", replayable: false, kinds: ["control"] }, |
| 11 | + ], |
| 12 | + }); |
| 13 | +} |
| 14 | + |
| 15 | +describe("chat route waits after suspension", () => { |
| 16 | + it("re-suspends after a wake that has no record for the requested route", async () => { |
| 17 | + const router = inputRouter(); |
| 18 | + const timeouts: Array<string | undefined> = []; |
| 19 | + let now = 0; |
| 20 | + |
| 21 | + const result = await waitForChatRouteAfterIdle(router, "messages", { |
| 22 | + timeout: "10s", |
| 23 | + postWakeTimeoutMs: 0, |
| 24 | + now: () => now, |
| 25 | + wake: async (timeout) => { |
| 26 | + timeouts.push(timeout); |
| 27 | + if (timeouts.length === 1) { |
| 28 | + router.ingest({ id: "control-1", seqNum: 1, data: { kind: "control" } }); |
| 29 | + now = 4_000; |
| 30 | + return { ok: true, waitpointId: "waitpoint-1" }; |
| 31 | + } |
| 32 | + return { ok: false, error: new WaitpointTimeoutError("Timed out") }; |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + expect(result.ok).toBe(false); |
| 37 | + expect(timeouts).toEqual(["10s", "6s"]); |
| 38 | + }); |
| 39 | + |
| 40 | + it("does not re-wake on an unrelated queued message while waiting for another route", async () => { |
| 41 | + const router = inputRouter(); |
| 42 | + const cursors: Array<number | undefined> = []; |
| 43 | + |
| 44 | + const result = await waitForChatRouteAfterIdle(router, "control", { |
| 45 | + postWakeTimeoutMs: 0, |
| 46 | + wake: async (_timeout, lastSeqNum) => { |
| 47 | + cursors.push(lastSeqNum); |
| 48 | + if (cursors.length === 1) { |
| 49 | + router.ingest({ id: "message-1", seqNum: 1, data: { kind: "message" } }); |
| 50 | + return { ok: true, waitpointId: "waitpoint-1" }; |
| 51 | + } |
| 52 | + if (lastSeqNum === undefined || lastSeqNum < 1) { |
| 53 | + return { ok: true, waitpointId: "waitpoint-repeated" }; |
| 54 | + } |
| 55 | + return { ok: false, error: new WaitpointTimeoutError("Timed out") }; |
| 56 | + }, |
| 57 | + }); |
| 58 | + |
| 59 | + expect(result.ok).toBe(false); |
| 60 | + expect(cursors).toEqual([undefined, 1]); |
| 61 | + expect(router.resumeFloor()).toBe(0); |
| 62 | + expect(router.hasPending("messages")).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it("expires an absolute timeout after an unmatched wake", async () => { |
| 66 | + const router = inputRouter(); |
| 67 | + const deadline = Date.parse("2026-09-28T10:15:00Z"); |
| 68 | + let now = deadline - 1_000; |
| 69 | + const timeouts: Array<string | undefined> = []; |
| 70 | + |
| 71 | + const result = await waitForChatRouteAfterIdle(router, "messages", { |
| 72 | + timeout: "2026-09-28T10:15:00Z", |
| 73 | + postWakeTimeoutMs: 0, |
| 74 | + now: () => now, |
| 75 | + wake: async (timeout) => { |
| 76 | + timeouts.push(timeout); |
| 77 | + now = deadline + 1; |
| 78 | + return { ok: true, waitpointId: "waitpoint-1" }; |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + expect(result.ok).toBe(false); |
| 83 | + expect(timeouts).toEqual(["1s"]); |
| 84 | + }); |
| 85 | + |
| 86 | + it("returns a matching record delivered after the channel wakes", async () => { |
| 87 | + const router = inputRouter(); |
| 88 | + const message = { id: "message-1", seqNum: 1, data: { kind: "message", text: "hello" } }; |
| 89 | + |
| 90 | + const result = await waitForChatRouteAfterIdle(router, "messages", { |
| 91 | + timeout: "10s", |
| 92 | + postWakeTimeoutMs: 0, |
| 93 | + wake: async () => { |
| 94 | + router.ingest(message); |
| 95 | + return { ok: true, waitpointId: "waitpoint-1" }; |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + expect(result).toEqual({ ok: true, record: message }); |
| 100 | + }); |
| 101 | +}); |
0 commit comments