|
| 1 | +import type { AcpMessage } from "@posthog/shared"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { findWorkflowBuilt } from "./useCanvasGenerationToasts"; |
| 4 | + |
| 5 | +function acp(message: AcpMessage["message"]): AcpMessage { |
| 6 | + return { type: "acp_message", ts: 1, message }; |
| 7 | +} |
| 8 | + |
| 9 | +describe("findWorkflowBuilt", () => { |
| 10 | + const builtNotification = acp({ |
| 11 | + jsonrpc: "2.0", |
| 12 | + method: "_posthog/workflow_built", |
| 13 | + params: { |
| 14 | + sessionId: "s1", |
| 15 | + dashboardId: "dash-1", |
| 16 | + workflowId: "wf-1", |
| 17 | + workflowStatus: "draft", |
| 18 | + workflowName: "Welcome email after signup", |
| 19 | + }, |
| 20 | + }); |
| 21 | + |
| 22 | + it("finds the workflow link in a session's event stream", () => { |
| 23 | + const events: AcpMessage[] = [ |
| 24 | + acp({ jsonrpc: "2.0", method: "session/update", params: {} }), |
| 25 | + builtNotification, |
| 26 | + ]; |
| 27 | + expect(findWorkflowBuilt(events)).toEqual({ |
| 28 | + dashboardId: "dash-1", |
| 29 | + workflowId: "wf-1", |
| 30 | + workflowStatus: "draft", |
| 31 | + workflowName: "Welcome email after signup", |
| 32 | + workflowType: undefined, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("matches the underscore-prefixed replay variant too", () => { |
| 37 | + // Stored-log replay can surface ext notifications with a leading |
| 38 | + // underscore on the method; isNotification matches both. |
| 39 | + const events: AcpMessage[] = [ |
| 40 | + acp({ |
| 41 | + jsonrpc: "2.0", |
| 42 | + method: "__posthog/workflow_built", |
| 43 | + params: { dashboardId: "dash-2", workflowId: "wf-2" }, |
| 44 | + }), |
| 45 | + ]; |
| 46 | + expect(findWorkflowBuilt(events)?.dashboardId).toBe("dash-2"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("ignores requests, responses, and other notifications", () => { |
| 50 | + const events: AcpMessage[] = [ |
| 51 | + // A request (has an id) with the right method must not match. |
| 52 | + acp({ |
| 53 | + jsonrpc: "2.0", |
| 54 | + id: 1, |
| 55 | + method: "_posthog/workflow_built", |
| 56 | + params: { dashboardId: "d", workflowId: "w" }, |
| 57 | + }), |
| 58 | + acp({ jsonrpc: "2.0", method: "_posthog/resources_used", params: {} }), |
| 59 | + ]; |
| 60 | + expect(findWorkflowBuilt(events)).toBeNull(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("skips a malformed payload rather than returning a partial link", () => { |
| 64 | + const events: AcpMessage[] = [ |
| 65 | + acp({ |
| 66 | + jsonrpc: "2.0", |
| 67 | + method: "_posthog/workflow_built", |
| 68 | + params: { workflowId: "wf-only" }, |
| 69 | + }), |
| 70 | + ]; |
| 71 | + expect(findWorkflowBuilt(events)).toBeNull(); |
| 72 | + }); |
| 73 | + |
| 74 | + it("returns null for empty or missing events", () => { |
| 75 | + expect(findWorkflowBuilt(undefined)).toBeNull(); |
| 76 | + expect(findWorkflowBuilt([])).toBeNull(); |
| 77 | + }); |
| 78 | +}); |
0 commit comments