Skip to content

Commit 4288c7f

Browse files
committed
test(canvas): cover the workflow-built session-event scan
Export findWorkflowBuilt and test it directly: live-stream and replay method variants, request/response and foreign notifications ignored, malformed payloads skipped. Generated-By: PostHog Code Task-Id: 3b50883f-cab5-443a-8b92-3d0c948688da
1 parent 320d310 commit 4288c7f

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
});

packages/ui/src/features/canvas/freeform/useCanvasGenerationToasts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ function emitCanvasGenerationNotification(
6565
// created a workflow and published the canvas; we read it off the session's
6666
// event stream - the same source SessionResourcesBar folds for resource chips -
6767
// and persist it onto the dashboard row. Absent for regular canvas generations.
68-
function findWorkflowBuilt(
68+
// Exported for tests.
69+
export function findWorkflowBuilt(
6970
events: readonly AcpMessage[] | undefined,
7071
): WorkflowBuiltPayload | null {
7172
if (!events) return null;

0 commit comments

Comments
 (0)