|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { fireEvent, renderHook } from "@testing-library/react"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { useDraftLifecycle } from "./useDraftLifecycle"; |
| 5 | + |
| 6 | +type HookOptions = Parameters<typeof useDraftLifecycle>[0]; |
| 7 | + |
| 8 | +function makeOptions(overrides: Partial<HookOptions> = {}): HookOptions { |
| 9 | + return { |
| 10 | + initialDraft: null, |
| 11 | + viewMode: "panels", |
| 12 | + input: "", |
| 13 | + savedDraftId: null, |
| 14 | + refreshWorkspaceCatalog: vi.fn().mockResolvedValue(undefined), |
| 15 | + findSimilar: vi.fn().mockResolvedValue(undefined), |
| 16 | + loadAlternatives: vi.fn().mockResolvedValue(undefined), |
| 17 | + loadTemplates: vi.fn().mockResolvedValue(undefined), |
| 18 | + handleLoadDraft: vi.fn(), |
| 19 | + onPanelDensityModeChange: vi.fn(), |
| 20 | + setSuggestionsDismissed: vi.fn(), |
| 21 | + ...overrides, |
| 22 | + }; |
| 23 | +} |
| 24 | + |
| 25 | +describe("useDraftLifecycle", () => { |
| 26 | + afterEach(() => { |
| 27 | + vi.restoreAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("refreshes catalog and loads templates on mount", () => { |
| 31 | + const options = makeOptions(); |
| 32 | + renderHook(() => useDraftLifecycle(options)); |
| 33 | + |
| 34 | + expect(options.refreshWorkspaceCatalog).toHaveBeenCalledTimes(1); |
| 35 | + expect(options.loadTemplates).toHaveBeenCalledTimes(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it("finds similar saved responses once input has 10+ characters", () => { |
| 39 | + const options = makeOptions({ input: "short" }); |
| 40 | + const { rerender } = renderHook( |
| 41 | + (props: HookOptions) => useDraftLifecycle(props), |
| 42 | + { initialProps: options }, |
| 43 | + ); |
| 44 | + expect(options.findSimilar).not.toHaveBeenCalled(); |
| 45 | + |
| 46 | + const updated = makeOptions({ |
| 47 | + ...options, |
| 48 | + input: "longer than ten characters", |
| 49 | + }); |
| 50 | + rerender(updated); |
| 51 | + |
| 52 | + expect(updated.findSimilar).toHaveBeenCalledWith( |
| 53 | + "longer than ten characters", |
| 54 | + ); |
| 55 | + expect(updated.setSuggestionsDismissed).toHaveBeenCalledWith(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it("loads alternatives when a savedDraftId appears", () => { |
| 59 | + const options = makeOptions(); |
| 60 | + const { rerender } = renderHook( |
| 61 | + (props: HookOptions) => useDraftLifecycle(props), |
| 62 | + { initialProps: options }, |
| 63 | + ); |
| 64 | + expect(options.loadAlternatives).not.toHaveBeenCalled(); |
| 65 | + |
| 66 | + const updated = makeOptions({ ...options, savedDraftId: "draft-1" }); |
| 67 | + rerender(updated); |
| 68 | + |
| 69 | + expect(updated.loadAlternatives).toHaveBeenCalledWith("draft-1"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("fires initialDraft load exactly once when the prop is provided", () => { |
| 73 | + const initialDraft = { id: "d-1" } as HookOptions["initialDraft"]; |
| 74 | + const options = makeOptions({ initialDraft }); |
| 75 | + renderHook(() => useDraftLifecycle(options)); |
| 76 | + |
| 77 | + expect(options.handleLoadDraft).toHaveBeenCalledWith(initialDraft); |
| 78 | + }); |
| 79 | + |
| 80 | + it("maps Cmd-1/2/3 to the three panel density modes in panels view", () => { |
| 81 | + const options = makeOptions({ viewMode: "panels" }); |
| 82 | + renderHook(() => useDraftLifecycle(options)); |
| 83 | + |
| 84 | + fireEvent.keyDown(window, { key: "1", metaKey: true }); |
| 85 | + expect(options.onPanelDensityModeChange).toHaveBeenCalledWith("balanced"); |
| 86 | + |
| 87 | + fireEvent.keyDown(window, { key: "2", metaKey: true }); |
| 88 | + expect(options.onPanelDensityModeChange).toHaveBeenCalledWith( |
| 89 | + "focus-intake", |
| 90 | + ); |
| 91 | + |
| 92 | + fireEvent.keyDown(window, { key: "3", metaKey: true }); |
| 93 | + expect(options.onPanelDensityModeChange).toHaveBeenCalledWith( |
| 94 | + "focus-response", |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it("ignores keyboard shortcuts when viewMode is conversation", () => { |
| 99 | + const options = makeOptions({ viewMode: "conversation" }); |
| 100 | + renderHook(() => useDraftLifecycle(options)); |
| 101 | + |
| 102 | + fireEvent.keyDown(window, { key: "1", metaKey: true }); |
| 103 | + expect(options.onPanelDensityModeChange).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it("ignores keyboard shortcuts when focus is in an editable target", () => { |
| 107 | + const options = makeOptions({ viewMode: "panels" }); |
| 108 | + renderHook(() => useDraftLifecycle(options)); |
| 109 | + |
| 110 | + const input = document.createElement("input"); |
| 111 | + document.body.appendChild(input); |
| 112 | + input.focus(); |
| 113 | + |
| 114 | + fireEvent.keyDown(input, { key: "1", metaKey: true, bubbles: true }); |
| 115 | + expect(options.onPanelDensityModeChange).not.toHaveBeenCalled(); |
| 116 | + |
| 117 | + document.body.removeChild(input); |
| 118 | + }); |
| 119 | +}); |
0 commit comments