|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const navigateToChannelNewTask = vi.fn(); |
| 4 | +const navigateToWebsiteNew = vi.fn(); |
| 5 | +const navigateToCode = vi.fn(); |
| 6 | + |
| 7 | +vi.mock("./navigationBridge", () => ({ |
| 8 | + navigateToChannelNewTask: (...args: unknown[]) => |
| 9 | + navigateToChannelNewTask(...args), |
| 10 | + navigateToWebsiteNew: () => navigateToWebsiteNew(), |
| 11 | + navigateToCode: () => navigateToCode(), |
| 12 | + navigateToChannelTask: vi.fn(), |
| 13 | + navigateToTaskDetail: vi.fn(), |
| 14 | + navigateToFolderSettings: vi.fn(), |
| 15 | +})); |
| 16 | +vi.mock("@posthog/di/container", () => ({ |
| 17 | + resolveService: vi.fn(), |
| 18 | + resolveServiceOptional: vi.fn(), |
| 19 | +})); |
| 20 | +vi.mock("@posthog/ui/shell/analytics", () => ({ |
| 21 | + track: vi.fn(), |
| 22 | + setActiveTaskContext: vi.fn(), |
| 23 | +})); |
| 24 | + |
| 25 | +import { useCurrentChannelStore } from "@posthog/ui/features/canvas/stores/currentChannelStore"; |
| 26 | +import { openTaskInput } from "./useOpenTask"; |
| 27 | + |
| 28 | +describe("openTaskInput channel scoping", () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks(); |
| 31 | + useCurrentChannelStore.setState({ currentChannelId: null }); |
| 32 | + }); |
| 33 | + |
| 34 | + // Without the channels layout nothing sets a current channel, so creates must |
| 35 | + // land where they always did rather than being pulled into a channel. |
| 36 | + it("routes to Code when no channel is current", () => { |
| 37 | + openTaskInput(); |
| 38 | + expect(navigateToCode).toHaveBeenCalledTimes(1); |
| 39 | + expect(navigateToChannelNewTask).not.toHaveBeenCalled(); |
| 40 | + }); |
| 41 | + |
| 42 | + it("still honours an explicit website space when no channel is current", () => { |
| 43 | + openTaskInput({ space: "website" }); |
| 44 | + expect(navigateToWebsiteNew).toHaveBeenCalledTimes(1); |
| 45 | + expect(navigateToChannelNewTask).not.toHaveBeenCalled(); |
| 46 | + }); |
| 47 | + |
| 48 | + it("routes into the current channel once one is scoped", () => { |
| 49 | + useCurrentChannelStore.setState({ currentChannelId: "chan-1" }); |
| 50 | + openTaskInput(); |
| 51 | + expect(navigateToChannelNewTask).toHaveBeenCalledWith("chan-1"); |
| 52 | + expect(navigateToCode).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it("carries prefill into the channel route", () => { |
| 56 | + useCurrentChannelStore.setState({ currentChannelId: "chan-1" }); |
| 57 | + openTaskInput({ initialPrompt: "ship it" }); |
| 58 | + expect(navigateToChannelNewTask).toHaveBeenCalledWith("chan-1"); |
| 59 | + }); |
| 60 | +}); |
0 commit comments