Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
3.2 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";

import { useChatSessionRealtime } from "./use-chat-session-realtime";
import { chatKeys } from "@/data/queries/chat";
import { useChatSessionsRealtime } from "./use-chat-sessions-realtime";

const { invalidateQueries, subscriptionSetups } = vi.hoisted(() => ({
  invalidateQueries: vi.fn(),
  subscriptionSetups: [] as ((ws: MockWS, wsId: string) => (() => void)[])[],
}));

type EventHandler = (payload: unknown) => void;

interface MockWS {
  on: ReturnType<typeof vi.fn>;
  onReconnect: ReturnType<typeof vi.fn>;
}

vi.mock("@tanstack/react-query", () => ({
  useQueryClient: () => ({ invalidateQueries }),
}));

vi.mock("@/lib/use-ws-subscriptions", () => ({
  useWSSubscriptions: (setup: (ws: MockWS, wsId: string) => (() => void)[]) => {
    subscriptionSetups.push(setup);
  },
}));

vi.mock("@/data/api", () => ({ api: {} }));


describe("useChatSessionsRealtime", () => {
  beforeEach(() => {
    invalidateQueries.mockReset();
    subscriptionSetups.length = 0;
  });

  it("refreshes a late cancellation outcome without requiring a reconnect", () => {
    useChatSessionsRealtime();
    useChatSessionRealtime("session-1");
    const handlers = new Map<string, EventHandler[]>();
    const ws: MockWS = {
      on: vi.fn((event: string, handler: EventHandler) => {
        handlers.set(event, [...(handlers.get(event) ?? []), handler]);
        return () => {};
      }),
      onReconnect: vi.fn(() => () => {}),
    };
    for (const setup of subscriptionSetups) setup(ws, "workspace-1");
    for (const handle of handlers.get("chat:cancel_finalized") ?? []) {
      handle({ chat_session_id: "session-1", outcome: "stopped" });
    }
    expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: chatKeys.sessions("workspace-1") });
    expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: chatKeys.messages("session-1") });
    expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: chatKeys.pendingTask("session-1") });
    invalidateQueries.mockClear();
    for (const handle of handlers.get("chat:cancel_finalized") ?? []) {
      handle({ chat_session_id: "another-session", outcome: "stopped" });
    }
    expect(invalidateQueries).toHaveBeenCalledTimes(1);
    expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: chatKeys.sessions("workspace-1") });
  });

  it("invalidates the workspace session list for channel-created chats", () => {
    useChatSessionsRealtime();
    expect(subscriptionSetups).toHaveLength(1);

    const handlers = new Map<string, EventHandler>();
    const ws: MockWS = {
      on: vi.fn((event: string, handler: EventHandler) => {
        handlers.set(event, handler);
        return () => {};
      }),
      onReconnect: vi.fn(() => () => {}),
    };

    subscriptionSetups[0](ws, "workspace-1");
    handlers.get("chat:session_created")?.({
      workspace_id: "workspace-1",
      chat_session_id: "channel-session-1",
    });

    expect(invalidateQueries).toHaveBeenCalledWith({
      queryKey: chatKeys.sessions("workspace-1"),
    });

    invalidateQueries.mockClear();
    handlers.get("chat:session_created")?.({
      workspace_id: "workspace-2",
      chat_session_id: "other-workspace-session",
    });
    expect(invalidateQueries).not.toHaveBeenCalled();
  });
});