Skip to content

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

Go
3.7 KB
Raw Blame
import { afterEach, describe, expect, it } from "vitest";
import { act, renderHook } from "@testing-library/react";
import { useChatInputFocus } from "./use-chat-input-focus";

describe("useChatInputFocus", () => {
  const roots: HTMLElement[] = [];
  afterEach(() => {
    for (const root of roots.splice(0)) root.remove();
  });

  function makeWindow() {
    const root = document.createElement("div");
    const editor = document.createElement("div");
    editor.contentEditable = "true";
    editor.tabIndex = 0;
    root.append(editor);
    document.body.append(root);
    roots.push(root);
    return { root, editor };
  }

  it("stays inert on mount, whether the window starts closed or open", () => {
    expect(renderHook(() => useChatInputFocus(false)).result.current.focusRequest).toBe(0);
    // A persisted "open" preference must not steal focus from the page the user
    // just loaded — ChatWindow is mounted (hidden) even while closed.
    expect(renderHook(() => useChatInputFocus(true)).result.current.focusRequest).toBe(0);
  });

  it("requests focus on every closed → open transition", () => {
    const { result, rerender } = renderHook(
      ({ isOpen }: { isOpen: boolean }) => useChatInputFocus(isOpen),
      { initialProps: { isOpen: false } },
    );

    rerender({ isOpen: true });
    expect(result.current.focusRequest).toBe(1);

    // Re-renders that don't change `isOpen` must not re-focus: the user may
    // have clicked into the message list or the agent picker since.
    rerender({ isOpen: true });
    expect(result.current.focusRequest).toBe(1);

    rerender({ isOpen: false });
    expect(result.current.focusRequest).toBe(1);

    rerender({ isOpen: true });
    expect(result.current.focusRequest).toBe(2);
  });

  it("lets callers bump the nonce for new chats and agent switches", () => {
    const { result } = renderHook(() => useChatInputFocus(true));

    act(() => result.current.requestInputFocus());
    expect(result.current.focusRequest).toBe(1);
    act(() => result.current.requestInputFocus());
    expect(result.current.focusRequest).toBe(2);
  });

  it("requests focus for each new Ask Multica chat, even in a window already open", () => {
    // Mounting with an earlier bump must not count as a new one.
    const { result, rerender } = renderHook(
      ({ request }: { request: number }) => useChatInputFocus(true, undefined, request),
      { initialProps: { request: 3 } },
    );
    expect(result.current.focusRequest).toBe(0);

    rerender({ request: 4 });
    expect(result.current.focusRequest).toBe(1);

    rerender({ request: 4 });
    expect(result.current.focusRequest).toBe(1);
  });

  it("releases the hidden composer's focus when the window closes", () => {
    const { root, editor } = makeWindow();
    const windowRef = { current: root };
    const { result, rerender } = renderHook(
      ({ isOpen }: { isOpen: boolean }) => useChatInputFocus(isOpen, windowRef),
      { initialProps: { isOpen: true } },
    );
    editor.focus();
    expect(document.activeElement).toBe(editor);

    rerender({ isOpen: false });
    expect(document.activeElement).toBe(document.body);

    rerender({ isOpen: true });
    expect(result.current.focusRequest).toBe(1);
  });

  it("does not steal focus from a control outside the closing window", () => {
    const { root } = makeWindow();
    const outside = document.createElement("input");
    document.body.append(outside);
    roots.push(outside);
    const windowRef = { current: root };
    const { rerender } = renderHook(
      ({ isOpen }: { isOpen: boolean }) => useChatInputFocus(isOpen, windowRef),
      { initialProps: { isOpen: true } },
    );
    outside.focus();
    rerender({ isOpen: false });
    expect(document.activeElement).toBe(outside);
  });
});