Files
kandev/apps/web/hooks/use-file-editors.test.tsx
T
José Almeida 3e237e177a feat: add agent-authored code walkthroughs (#1647)
* feat: add code-walkthrough backend (MCP tool, service, persistence, WS events)

* feat: render agent code walkthroughs as line-anchored cards + editor window

* test: add code-walkthrough re-emit mock-agent scenario

* fix: rework code-walkthrough UX (floating card, line reveal, minimize, clickable file)

* fix: improve code walkthrough editor UX

* fix: backfill walkthrough launcher after websocket connect

* fix: improve walkthrough requests and editor anchors

* fix: clear walkthrough highlights on close

* feat: add walkthrough discard and prompt customization

* fix: send walkthrough requests through saved prompt

* fix: support postgres built-in prompt seeding

* fix: address walkthrough review feedback

* fix: address walkthrough review follow-ups

* fix: harden walkthrough lifecycle races

* fix: reuse walkthrough notification cleanup

* fix: guard stale walkthrough file opens

* fix: abort stale file opens before paint

* fix: stabilize github settings e2e readiness

* fix: use stable github settings e2e selector
2026-07-10 08:34:57 +01:00

133 lines
5.1 KiB
TypeScript

import { act, renderHook } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
const getMonacoInstance = vi.hoisted(() => vi.fn());
const APP_PATH = "src/app.ts";
const MISSING_PATH = "src/missing.ts";
const WORKTREE_PATH = "/worktree";
const WORKTREE_APP_PATH = "/worktree/src/app.ts";
const REPO = "frontend";
vi.mock("@/components/editors/monaco/monaco-init", () => ({
getMonacoInstance,
}));
import {
consumePendingCursorPosition,
scrollEditorIfMounted,
setPendingCursorPosition,
useOpenFileAtLine,
} from "./use-file-editors";
function createEditor(modelPath: string | null) {
return {
getModel: vi.fn(() => (modelPath ? { uri: { path: modelPath } } : null)),
setPosition: vi.fn(),
revealLineInCenter: vi.fn(),
focus: vi.fn(),
};
}
describe("scrollEditorIfMounted", () => {
afterEach(() => {
getMonacoInstance.mockReset();
consumePendingCursorPosition(APP_PATH);
consumePendingCursorPosition(APP_PATH, REPO);
consumePendingCursorPosition(MISSING_PATH);
});
it("scrolls the mounted Monaco editor that matches the worktree path", () => {
const editor = createEditor(WORKTREE_APP_PATH);
getMonacoInstance.mockReturnValue({ editor: { getEditors: () => [editor] } });
setPendingCursorPosition(APP_PATH, 12, 1);
expect(scrollEditorIfMounted(APP_PATH, WORKTREE_PATH, 42, 3)).toBe(true);
expect(editor.setPosition).toHaveBeenCalledWith({ lineNumber: 42, column: 3 });
expect(editor.revealLineInCenter).toHaveBeenCalledWith(42);
expect(editor.focus).toHaveBeenCalledTimes(1);
expect(consumePendingCursorPosition(APP_PATH)).toBeUndefined();
});
it("falls back to a path-segment suffix match when the worktree path is unknown", () => {
const editor = createEditor(WORKTREE_APP_PATH);
getMonacoInstance.mockReturnValue({ editor: { getEditors: () => [editor] } });
setPendingCursorPosition(APP_PATH, 12, 1);
expect(scrollEditorIfMounted(APP_PATH, null, 42, 3)).toBe(true);
expect(editor.setPosition).toHaveBeenCalledWith({ lineNumber: 42, column: 3 });
expect(editor.revealLineInCenter).toHaveBeenCalledWith(42);
expect(editor.focus).toHaveBeenCalledTimes(1);
expect(consumePendingCursorPosition(APP_PATH)).toBeUndefined();
});
it("requires the repo segment when repo-scoped fallback scrolling", () => {
const wrongRepoEditor = createEditor("/worktree/backend/src/app.ts");
const repoEditor = createEditor("/worktree/frontend/src/app.ts");
getMonacoInstance.mockReturnValue({
editor: { getEditors: () => [wrongRepoEditor, repoEditor] },
});
setPendingCursorPosition(APP_PATH, 12, 1, REPO);
expect(scrollEditorIfMounted(APP_PATH, null, 42, 3, REPO)).toBe(true);
expect(wrongRepoEditor.setPosition).not.toHaveBeenCalled();
expect(repoEditor.setPosition).toHaveBeenCalledWith({ lineNumber: 42, column: 3 });
expect(consumePendingCursorPosition(APP_PATH, REPO)).toBeUndefined();
});
it("does not suffix-scroll a repo-scoped request into an unscoped model path", () => {
const editor = createEditor(WORKTREE_APP_PATH);
getMonacoInstance.mockReturnValue({ editor: { getEditors: () => [editor] } });
setPendingCursorPosition(APP_PATH, 12, 1, REPO);
expect(scrollEditorIfMounted(APP_PATH, null, 42, 3, REPO)).toBe(false);
expect(editor.setPosition).not.toHaveBeenCalled();
expect(consumePendingCursorPosition(APP_PATH, REPO)).toEqual({ line: 12, column: 1 });
});
it("returns false when no mounted Monaco editor matches the path", () => {
const editor = createEditor("/worktree/src/other.ts");
getMonacoInstance.mockReturnValue({ editor: { getEditors: () => [editor] } });
setPendingCursorPosition(MISSING_PATH, 5, 1);
expect(scrollEditorIfMounted(MISSING_PATH, WORKTREE_PATH, 5, 1)).toBe(false);
expect(editor.setPosition).not.toHaveBeenCalled();
expect(consumePendingCursorPosition(MISSING_PATH)).toEqual({ line: 5, column: 1 });
});
});
describe("useOpenFileAtLine", () => {
afterEach(() => {
getMonacoInstance.mockReset();
consumePendingCursorPosition(APP_PATH);
});
it("opens and scrolls an already-mounted file when a target line is present", () => {
const openFile = vi.fn();
const editor = createEditor(WORKTREE_APP_PATH);
getMonacoInstance.mockReturnValue({ editor: { getEditors: () => [editor] } });
const { result } = renderHook(() => useOpenFileAtLine(openFile, 88, WORKTREE_PATH));
act(() => result.current(APP_PATH));
expect(openFile).toHaveBeenCalledWith(APP_PATH);
expect(editor.setPosition).toHaveBeenCalledWith({ lineNumber: 88, column: 1 });
expect(consumePendingCursorPosition(APP_PATH)).toBeUndefined();
});
it("opens without setting a pending position when the target line is missing", () => {
const openFile = vi.fn();
const { result } = renderHook(() => useOpenFileAtLine(openFile, undefined, WORKTREE_PATH));
act(() => result.current(APP_PATH));
expect(openFile).toHaveBeenCalledWith(APP_PATH);
expect(getMonacoInstance).not.toHaveBeenCalled();
expect(consumePendingCursorPosition(APP_PATH)).toBeUndefined();
});
});