|
| 1 | +// npx vitest webview-ui/src/components/chat/hooks/__tests__/useChatTextDraft.spec.ts |
| 2 | + |
| 3 | +import { renderHook, act } from "@testing-library/react" |
| 4 | +import { useChatTextDraft } from "../useChatTextDraft" |
| 5 | +import { vi } from "vitest" |
| 6 | + |
| 7 | +describe("useChatTextDraft", () => { |
| 8 | + const draftKey = "test-draft-key" |
| 9 | + let setInputValue: (v: string) => void |
| 10 | + let onSend: () => void |
| 11 | + |
| 12 | + let getItemMock: ReturnType<typeof vi.fn> |
| 13 | + let setItemMock: ReturnType<typeof vi.fn> |
| 14 | + let removeItemMock: ReturnType<typeof vi.fn> |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + setInputValue = vi.fn((_: string) => {}) |
| 18 | + onSend = vi.fn() |
| 19 | + |
| 20 | + getItemMock = vi.fn() |
| 21 | + setItemMock = vi.fn() |
| 22 | + removeItemMock = vi.fn() |
| 23 | + |
| 24 | + // @ts-expect-error override readonly |
| 25 | + global.localStorage = { |
| 26 | + getItem: getItemMock, |
| 27 | + setItem: setItemMock, |
| 28 | + removeItem: removeItemMock, |
| 29 | + } |
| 30 | + vi.useFakeTimers() |
| 31 | + }) |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + vi.clearAllTimers() |
| 35 | + vi.useRealTimers() |
| 36 | + vi.restoreAllMocks() |
| 37 | + }) |
| 38 | + |
| 39 | + describe("Draft restoration on mount", () => { |
| 40 | + it("should restore draft from localStorage on mount if inputValue is empty", () => { |
| 41 | + getItemMock.mockReturnValue("restored draft") |
| 42 | + renderHook(() => useChatTextDraft(draftKey, "", setInputValue, onSend)) |
| 43 | + expect(getItemMock).toHaveBeenCalledWith(draftKey) |
| 44 | + expect(setInputValue).toHaveBeenCalledWith("restored draft") |
| 45 | + }) |
| 46 | + |
| 47 | + it("should not restore draft if inputValue is not empty", () => { |
| 48 | + getItemMock.mockReturnValue("restored draft") |
| 49 | + renderHook(() => useChatTextDraft(draftKey, "already typed", setInputValue, onSend)) |
| 50 | + expect(getItemMock).toHaveBeenCalledWith(draftKey) |
| 51 | + expect(setInputValue).not.toHaveBeenCalled() |
| 52 | + }) |
| 53 | + |
| 54 | + it("should ignore errors from localStorage.getItem", () => { |
| 55 | + getItemMock.mockImplementation(() => { |
| 56 | + throw new Error("getItem error") |
| 57 | + }) |
| 58 | + expect(() => renderHook(() => useChatTextDraft(draftKey, "", setInputValue, onSend))).not.toThrow() |
| 59 | + expect(setInputValue).not.toHaveBeenCalled() |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + describe("Auto-save functionality with debounce", () => { |
| 64 | + it("should auto-save draft to localStorage after 3 seconds of inactivity if inputValue is non-empty", () => { |
| 65 | + renderHook(({ value }) => useChatTextDraft(draftKey, value, setInputValue, onSend), { |
| 66 | + initialProps: { value: "hello world" }, |
| 67 | + }) |
| 68 | + expect(setItemMock).not.toHaveBeenCalled() |
| 69 | + act(() => { |
| 70 | + vi.advanceTimersByTime(2999) |
| 71 | + }) |
| 72 | + expect(setItemMock).not.toHaveBeenCalled() |
| 73 | + act(() => { |
| 74 | + vi.advanceTimersByTime(1) |
| 75 | + }) |
| 76 | + expect(setItemMock).toHaveBeenCalledWith(draftKey, "hello world") |
| 77 | + }) |
| 78 | + |
| 79 | + it("should reset debounce timer when inputValue changes before debounce delay", () => { |
| 80 | + const { rerender } = renderHook(({ value }) => useChatTextDraft(draftKey, value, setInputValue, onSend), { |
| 81 | + initialProps: { value: "foo" }, |
| 82 | + }) |
| 83 | + act(() => { |
| 84 | + vi.advanceTimersByTime(2000) |
| 85 | + }) |
| 86 | + // Should not save before debounce delay |
| 87 | + expect(setItemMock).not.toHaveBeenCalled() |
| 88 | + rerender({ value: "bar" }) |
| 89 | + act(() => { |
| 90 | + vi.advanceTimersByTime(2999) |
| 91 | + }) |
| 92 | + expect(setItemMock).not.toHaveBeenCalled() |
| 93 | + act(() => { |
| 94 | + vi.advanceTimersByTime(1) |
| 95 | + }) |
| 96 | + expect(setItemMock).toHaveBeenCalledWith(draftKey, "bar") |
| 97 | + }) |
| 98 | + |
| 99 | + it("should remove draft from localStorage if inputValue is empty", () => { |
| 100 | + renderHook(({ value }) => useChatTextDraft(draftKey, value, setInputValue, onSend), { |
| 101 | + initialProps: { value: "" }, |
| 102 | + }) |
| 103 | + expect(removeItemMock).toHaveBeenCalledWith(draftKey) |
| 104 | + }) |
| 105 | + |
| 106 | + it("should ignore errors from localStorage.setItem", () => { |
| 107 | + setItemMock.mockImplementation(() => { |
| 108 | + throw new Error("setItem error") |
| 109 | + }) |
| 110 | + renderHook(({ value }) => useChatTextDraft(draftKey, value, setInputValue, onSend), { |
| 111 | + initialProps: { value: "err" }, |
| 112 | + }) |
| 113 | + act(() => { |
| 114 | + vi.advanceTimersByTime(5000) |
| 115 | + }) |
| 116 | + expect(setItemMock).toHaveBeenCalled() |
| 117 | + }) |
| 118 | + |
| 119 | + it("should ignore errors from localStorage.removeItem", () => { |
| 120 | + removeItemMock.mockImplementation(() => { |
| 121 | + throw new Error("removeItem error") |
| 122 | + }) |
| 123 | + renderHook(({ value }) => useChatTextDraft(draftKey, value, setInputValue, onSend), { |
| 124 | + initialProps: { value: "" }, |
| 125 | + }) |
| 126 | + expect(removeItemMock).toHaveBeenCalledWith(draftKey) |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + describe("Draft clearing on send", () => { |
| 131 | + it("should remove draft and call onSend when handleSendAndClearDraft is called", () => { |
| 132 | + const { result } = renderHook(() => useChatTextDraft(draftKey, "msg", setInputValue, onSend)) |
| 133 | + act(() => { |
| 134 | + result.current.handleSendAndClearDraft() |
| 135 | + }) |
| 136 | + expect(removeItemMock).toHaveBeenCalledWith(draftKey) |
| 137 | + expect(onSend).toHaveBeenCalled() |
| 138 | + }) |
| 139 | + |
| 140 | + it("should ignore errors from localStorage.removeItem on send", () => { |
| 141 | + removeItemMock.mockImplementation(() => { |
| 142 | + throw new Error("removeItem error") |
| 143 | + }) |
| 144 | + const { result } = renderHook(() => useChatTextDraft(draftKey, "msg", setInputValue, onSend)) |
| 145 | + act(() => { |
| 146 | + expect(() => result.current.handleSendAndClearDraft()).not.toThrow() |
| 147 | + }) |
| 148 | + expect(onSend).toHaveBeenCalled() |
| 149 | + }) |
| 150 | + }) |
| 151 | + |
| 152 | + /** |
| 153 | + * @description |
| 154 | + * Complex scenario: multiple inputValue changes, ensure debounce timer cleanup and localStorage operations have no side effects. |
| 155 | + */ |
| 156 | + it("should handle rapid inputValue changes and cleanup debounce timers", () => { |
| 157 | + const { rerender } = renderHook(({ value }) => useChatTextDraft(draftKey, value, setInputValue, onSend), { |
| 158 | + initialProps: { value: "first" }, |
| 159 | + }) |
| 160 | + act(() => { |
| 161 | + vi.advanceTimersByTime(2999) |
| 162 | + }) |
| 163 | + expect(setItemMock).not.toHaveBeenCalled() |
| 164 | + act(() => { |
| 165 | + vi.advanceTimersByTime(1) |
| 166 | + }) |
| 167 | + expect(setItemMock).toHaveBeenCalledWith(draftKey, "first") |
| 168 | + rerender({ value: "second" }) |
| 169 | + act(() => { |
| 170 | + vi.advanceTimersByTime(2999) |
| 171 | + }) |
| 172 | + expect(setItemMock).toHaveBeenCalledTimes(1) |
| 173 | + act(() => { |
| 174 | + vi.advanceTimersByTime(1) |
| 175 | + }) |
| 176 | + expect(setItemMock).toHaveBeenCalledWith(draftKey, "second") |
| 177 | + rerender({ value: "" }) |
| 178 | + expect(removeItemMock).toHaveBeenCalledWith(draftKey) |
| 179 | + }) |
| 180 | + it("should not save and should warn if inputValue exceeds 100KB (ASCII)", () => { |
| 181 | + const draftKey = "large-draft-key" |
| 182 | + const MAX_DRAFT_BYTES = 102400 |
| 183 | + // Generate a string larger than 100KB (1 byte per char, simple ASCII) |
| 184 | + const largeStr = "a".repeat(MAX_DRAFT_BYTES + 5000) |
| 185 | + const setItemMock = vi.fn() |
| 186 | + const getItemMock = vi.fn() |
| 187 | + const removeItemMock = vi.fn() |
| 188 | + // @ts-expect-error override readonly |
| 189 | + global.localStorage = { |
| 190 | + getItem: getItemMock, |
| 191 | + setItem: setItemMock, |
| 192 | + removeItem: removeItemMock, |
| 193 | + } |
| 194 | + const warnMock = vi.spyOn(console, "warn").mockImplementation(() => {}) |
| 195 | + const setInputValue = vi.fn() |
| 196 | + const onSend = vi.fn() |
| 197 | + renderHook(() => useChatTextDraft(draftKey, largeStr, setInputValue, onSend)) |
| 198 | + act(() => { |
| 199 | + vi.advanceTimersByTime(3000) |
| 200 | + }) |
| 201 | + expect(setItemMock).not.toHaveBeenCalled() |
| 202 | + expect(warnMock).toHaveBeenCalledWith(expect.stringContaining("exceeds 100KB")) |
| 203 | + warnMock.mockRestore() |
| 204 | + }) |
| 205 | + |
| 206 | + it("should not save and should warn if inputValue exceeds 100KB (UTF-8 multi-byte)", () => { |
| 207 | + const draftKey = "utf8-draft-key" |
| 208 | + // Each emoji is 4 bytes in UTF-8, 汉字 is 3 bytes |
| 209 | + const emoji = "😀" |
| 210 | + const hanzi = "汉" |
| 211 | + // Compose a string: 20,000 emojis (~80KB) + 10,000 汉 (~30KB) + some ASCII |
| 212 | + const utf8Str = emoji.repeat(20000) + hanzi.repeat(10000) + "abc" |
| 213 | + const setItemMock = vi.fn() |
| 214 | + const getItemMock = vi.fn() |
| 215 | + const removeItemMock = vi.fn() |
| 216 | + // @ts-expect-error override readonly |
| 217 | + global.localStorage = { |
| 218 | + getItem: getItemMock, |
| 219 | + setItem: setItemMock, |
| 220 | + removeItem: removeItemMock, |
| 221 | + } |
| 222 | + const warnMock = vi.spyOn(console, "warn").mockImplementation(() => {}) |
| 223 | + const setInputValue = vi.fn() |
| 224 | + const onSend = vi.fn() |
| 225 | + renderHook(() => useChatTextDraft(draftKey, utf8Str, setInputValue, onSend)) |
| 226 | + act(() => { |
| 227 | + vi.advanceTimersByTime(3000) |
| 228 | + }) |
| 229 | + expect(setItemMock).not.toHaveBeenCalled() |
| 230 | + expect(warnMock).toHaveBeenCalledWith(expect.stringContaining("exceeds 100KB")) |
| 231 | + warnMock.mockRestore() |
| 232 | + }) |
| 233 | +}) |
0 commit comments