-
Notifications
You must be signed in to change notification settings - Fork 3.8k
fix: useHistory currentIndex more than MAX_HISTORY_LENGTH cause replace fail #8740
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TomIsion
wants to merge
2
commits into
continuedev:main
Choose a base branch
from
TomIsion:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+371
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,370 @@ | ||
| import { act, renderHook } from "@testing-library/react"; | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { getLocalStorage, setLocalStorage } from "../../util/localStorage"; | ||
| import { useInputHistory } from "../useInputHistory"; | ||
|
|
||
| // Define JSONContent type locally to avoid import issues in test environment | ||
| interface JSONContent { | ||
| type?: string; | ||
| attrs?: Record<string, any>; | ||
| content?: JSONContent[]; | ||
| marks?: Array<{ | ||
| type: string; | ||
| attrs?: Record<string, any>; | ||
| }>; | ||
| text?: string; | ||
| } | ||
|
|
||
| // Mock localStorage utilities | ||
| vi.mock("../../util/localStorage", () => ({ | ||
| getLocalStorage: vi.fn(), | ||
| setLocalStorage: vi.fn(), | ||
| })); | ||
|
|
||
| const mockGetLocalStorage = vi.mocked(getLocalStorage); | ||
| const mockSetLocalStorage = vi.mocked(setLocalStorage); | ||
|
|
||
| describe("useInputHistory", () => { | ||
| const historyKey = "test-history"; | ||
| const MAX_HISTORY_LENGTH = 100; | ||
|
|
||
| const createJsonContent = (text: string): JSONContent => ({ | ||
| type: "doc", | ||
| content: [{ type: "paragraph", content: [{ type: "text", text }] }], | ||
| }); | ||
|
|
||
| const emptyJsonContent = (): JSONContent => ({ | ||
| type: "doc", | ||
| content: [{ type: "paragraph", content: [{ type: "text", text: "" }] }], | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| localStorage.clear(); | ||
| mockGetLocalStorage.mockReturnValue(null); | ||
| }); | ||
|
|
||
| describe("Initialization", () => { | ||
| it("should initialize with empty history when no localStorage data exists", () => { | ||
| mockGetLocalStorage.mockReturnValue(null); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
|
|
||
| expect(mockGetLocalStorage).toHaveBeenCalledWith(`inputHistory_${historyKey}`); | ||
| expect(result.current).toHaveProperty("prevRef"); | ||
| expect(result.current).toHaveProperty("nextRef"); | ||
| expect(result.current).toHaveProperty("addRef"); | ||
| }); | ||
|
|
||
| it("should initialize with existing history from localStorage", () => { | ||
| const existingHistory = [ | ||
| createJsonContent("previous input 1"), | ||
| createJsonContent("previous input 2"), | ||
| ]; | ||
| mockGetLocalStorage.mockReturnValue(existingHistory); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
|
|
||
| expect(mockGetLocalStorage).toHaveBeenCalledWith(`inputHistory_${historyKey}`); | ||
|
|
||
| // Test that we can navigate through the existing history | ||
| act(() => { | ||
| const content1 = result.current.prevRef.current(emptyJsonContent()); | ||
| expect(content1).toEqual(existingHistory[1]); // Latest item | ||
| }); | ||
| }); | ||
|
|
||
| it("should slice history to MAX_HISTORY_LENGTH when loading from localStorage", () => { | ||
| const longHistory = Array.from({ length: 120 }, (_, i) => | ||
| createJsonContent(`input ${i + 1}`) | ||
| ); | ||
| mockGetLocalStorage.mockReturnValue(longHistory); | ||
|
|
||
| renderHook(() => useInputHistory(historyKey)); | ||
|
|
||
| expect(mockGetLocalStorage).toHaveBeenCalledWith(`inputHistory_${historyKey}`); | ||
|
|
||
| // The slice should happen during initialization | ||
| // We can't directly test the internal state, but we can test that localStorage receives the sliced data | ||
| }); | ||
| }); | ||
|
|
||
| describe("Adding items", () => { | ||
| it("should add new input to history", () => { | ||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
| const newInput = createJsonContent("new input"); | ||
|
|
||
| act(() => { | ||
| result.current.addRef.current(newInput); | ||
| }); | ||
|
|
||
| expect(mockSetLocalStorage).toHaveBeenCalledWith( | ||
| `inputHistory_${historyKey}`, | ||
| [newInput] | ||
| ); | ||
| }); | ||
|
|
||
| it("should not add duplicate consecutive inputs", () => { | ||
| const existingInput = createJsonContent("existing input"); | ||
| mockGetLocalStorage.mockReturnValue([existingInput]); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
|
|
||
| act(() => { | ||
| result.current.addRef.current(existingInput); | ||
| }); | ||
|
|
||
| // Should not call setLocalStorage since it's a duplicate | ||
| expect(mockSetLocalStorage).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("should add different inputs even if similar", () => { | ||
| const existingInput = createJsonContent("input 1"); | ||
| const newInput = createJsonContent("input 2"); | ||
| mockGetLocalStorage.mockReturnValue([existingInput]); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
|
|
||
| act(() => { | ||
| result.current.addRef.current(newInput); | ||
| }); | ||
|
|
||
| expect(mockSetLocalStorage).toHaveBeenCalledWith( | ||
| `inputHistory_${historyKey}`, | ||
| [existingInput, newInput] | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("History eviction (MAX_HISTORY_LENGTH)", () => { | ||
| it("should evict oldest items when history reaches MAX_HISTORY_LENGTH", () => { | ||
| // Create a history at the maximum length | ||
| const fullHistory = Array.from({ length: MAX_HISTORY_LENGTH }, (_, i) => | ||
| createJsonContent(`input ${i + 1}`) | ||
| ); | ||
| mockGetLocalStorage.mockReturnValue(fullHistory); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
| const newInput = createJsonContent("new input that exceeds max"); | ||
|
|
||
| act(() => { | ||
| result.current.addRef.current(newInput); | ||
| }); | ||
|
|
||
| // Should save only the last MAX_HISTORY_LENGTH items, evicting the oldest | ||
| const expectedHistory = [...fullHistory, newInput].slice(-MAX_HISTORY_LENGTH); | ||
| expect(mockSetLocalStorage).toHaveBeenCalledWith( | ||
| `inputHistory_${historyKey}`, | ||
| expectedHistory | ||
| ); | ||
|
|
||
| // Verify that the first item was evicted and the new item was added | ||
| expect(expectedHistory).not.toContain(fullHistory[0]); | ||
| expect(expectedHistory).toContain(newInput); | ||
| expect(expectedHistory).toHaveLength(MAX_HISTORY_LENGTH); | ||
| }); | ||
|
|
||
| it("should allow navigation after history eviction", () => { | ||
| // Create a history at the maximum length | ||
| const fullHistory = Array.from({ length: MAX_HISTORY_LENGTH }, (_, i) => | ||
| createJsonContent(`input ${i + 1}`) | ||
| ); | ||
| mockGetLocalStorage.mockReturnValue(fullHistory); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
| const newInput = createJsonContent("newest input"); | ||
|
|
||
| // Add new input that should trigger eviction | ||
| act(() => { | ||
| result.current.addRef.current(newInput); | ||
| }); | ||
|
|
||
| // Test navigation after eviction - should be able to access the newest items | ||
| act(() => { | ||
| const latestInput = result.current.prevRef.current(emptyJsonContent()); | ||
| expect(latestInput).toEqual(newInput); | ||
| }); | ||
| }); | ||
|
|
||
| it("should handle multiple additions beyond MAX_HISTORY_LENGTH", () => { | ||
| // Start with full history | ||
| const fullHistory = Array.from({ length: MAX_HISTORY_LENGTH }, (_, i) => | ||
| createJsonContent(`input ${i + 1}`) | ||
| ); | ||
| mockGetLocalStorage.mockReturnValue(fullHistory); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
|
|
||
| // Add multiple new inputs | ||
| const newInputs = [ | ||
| createJsonContent("new input 1"), | ||
| createJsonContent("new input 2"), | ||
| createJsonContent("new input 3"), | ||
| ]; | ||
|
|
||
| newInputs.forEach(input => { | ||
| act(() => { | ||
| result.current.addRef.current(input); | ||
| }); | ||
| }); | ||
|
|
||
| // The final localStorage call should contain only MAX_HISTORY_LENGTH items | ||
| const lastCall = mockSetLocalStorage.mock.calls[mockSetLocalStorage.mock.calls.length - 1]; | ||
| expect(lastCall[1]).toHaveLength(MAX_HISTORY_LENGTH); | ||
| expect(lastCall[1]).toContain(newInputs[2]); // Latest should be included | ||
| }); | ||
| }); | ||
|
|
||
| describe("Navigation", () => { | ||
| it("should navigate backwards through history", () => { | ||
| const history = [ | ||
| createJsonContent("input 1"), | ||
| createJsonContent("input 2"), | ||
| createJsonContent("input 3"), | ||
| ]; | ||
| mockGetLocalStorage.mockReturnValue(history); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
|
|
||
| // Navigate backwards | ||
| act(() => { | ||
| const content1 = result.current.prevRef.current(emptyJsonContent()); | ||
| expect(content1).toEqual(history[2]); // Latest item | ||
| }); | ||
|
|
||
| act(() => { | ||
| const content2 = result.current.prevRef.current(emptyJsonContent()); | ||
| expect(content2).toEqual(history[1]); // Second latest | ||
| }); | ||
|
|
||
| act(() => { | ||
| const content3 = result.current.prevRef.current(emptyJsonContent()); | ||
| expect(content3).toEqual(history[0]); // Oldest | ||
| }); | ||
| }); | ||
|
|
||
| it("should navigate forwards through history", () => { | ||
| const history = [ | ||
| createJsonContent("input 1"), | ||
| createJsonContent("input 2"), | ||
| createJsonContent("input 3"), | ||
| ]; | ||
| mockGetLocalStorage.mockReturnValue(history); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
|
|
||
| // First, navigate backwards to the beginning | ||
| act(() => { | ||
| result.current.prevRef.current(emptyJsonContent()); | ||
| result.current.prevRef.current(emptyJsonContent()); | ||
| result.current.prevRef.current(emptyJsonContent()); | ||
| }); | ||
|
|
||
| // Then navigate forwards | ||
| act(() => { | ||
| const content1 = result.current.nextRef.current(); | ||
| expect(content1).toEqual(history[1]); | ||
| }); | ||
|
|
||
| act(() => { | ||
| const content2 = result.current.nextRef.current(); | ||
| expect(content2).toEqual(history[2]); | ||
| }); | ||
| }); | ||
|
|
||
| it("should preserve pending input when navigating back from current position", () => { | ||
| const history = [createJsonContent("input 1")]; | ||
| mockGetLocalStorage.mockReturnValue(history); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
| const currentInput = createJsonContent("current typing"); | ||
|
|
||
| // Navigate back from current input | ||
| act(() => { | ||
| const prevContent = result.current.prevRef.current(currentInput); | ||
| expect(prevContent).toEqual(history[0]); | ||
| }); | ||
|
|
||
| // Navigate forward should return the pending input | ||
| act(() => { | ||
| const nextContent = result.current.nextRef.current(); | ||
| expect(nextContent).toEqual(currentInput); | ||
| }); | ||
| }); | ||
|
|
||
| it("should handle navigation boundaries gracefully", () => { | ||
| const history = [createJsonContent("only input")]; | ||
| mockGetLocalStorage.mockReturnValue(history); | ||
|
|
||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
|
|
||
| // Navigate back beyond beginning | ||
| act(() => { | ||
| result.current.prevRef.current(emptyJsonContent()); | ||
| const content = result.current.prevRef.current(emptyJsonContent()); | ||
| expect(content).toBeUndefined(); // Should not crash | ||
| }); | ||
|
|
||
| // Navigate forward beyond end | ||
| act(() => { | ||
| const content = result.current.nextRef.current(); | ||
| expect(content).toBeUndefined(); // Should not crash | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Edge cases", () => { | ||
| it("should handle empty string inputs", () => { | ||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
| const emptyInput = emptyJsonContent(); | ||
|
|
||
| act(() => { | ||
| result.current.addRef.current(emptyInput); | ||
| }); | ||
|
|
||
| expect(mockSetLocalStorage).toHaveBeenCalledWith( | ||
| `inputHistory_${historyKey}`, | ||
| [emptyInput] | ||
| ); | ||
| }); | ||
|
|
||
| it("should handle complex JSONContent structures", () => { | ||
| const { result } = renderHook(() => useInputHistory(historyKey)); | ||
| const complexInput: JSONContent = { | ||
| type: "doc", | ||
| content: [ | ||
| { type: "paragraph", content: [{ type: "text", text: "Hello " }] }, | ||
| { type: "paragraph", content: [{ type: "text", text: "World!" }] }, | ||
| ], | ||
| }; | ||
|
|
||
| act(() => { | ||
| result.current.addRef.current(complexInput); | ||
| }); | ||
|
|
||
| expect(mockSetLocalStorage).toHaveBeenCalledWith( | ||
| `inputHistory_${historyKey}`, | ||
| [complexInput] | ||
| ); | ||
| }); | ||
|
|
||
| it("should handle different history keys independently", () => { | ||
| const key1 = "history-1"; | ||
| const key2 = "history-2"; | ||
|
|
||
| const { result: result1 } = renderHook(() => useInputHistory(key1)); | ||
| const { result: result2 } = renderHook(() => useInputHistory(key2)); | ||
|
|
||
| const input1 = createJsonContent("input for key 1"); | ||
| const input2 = createJsonContent("input for key 2"); | ||
|
|
||
| act(() => { | ||
| result1.current.addRef.current(input1); | ||
| result2.current.addRef.current(input2); | ||
| }); | ||
|
|
||
| expect(mockSetLocalStorage).toHaveBeenCalledWith(`inputHistory_${key1}`, [input1]); | ||
| expect(mockSetLocalStorage).toHaveBeenCalledWith(`inputHistory_${key2}`, [input2]); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nextRef.current()returns the pending input when you move forward from the last stored history entry, but this test asserts it isundefined, so it will fail whenever the hook returns the stored pending input. Update the expectation to match the hook's behavior.Prompt for AI agents