mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-11 05:41:08 -07:00
refactor(cli,core): foundational layout, identity management, and type safety (#23286)
This commit is contained in:
@@ -39,6 +39,56 @@ describe('useHistoryManager', () => {
|
||||
expect(result.current.history[0].id).toBeGreaterThanOrEqual(timestamp);
|
||||
});
|
||||
|
||||
it('should generate strictly increasing IDs even if baseTimestamp goes backwards', async () => {
|
||||
const { result } = await renderHook(() => useHistory());
|
||||
const timestamp = 1000000;
|
||||
const itemData: Omit<HistoryItem, 'id'> = { type: 'info', text: 'First' };
|
||||
|
||||
let id1!: number;
|
||||
let id2!: number;
|
||||
|
||||
act(() => {
|
||||
id1 = result.current.addItem(itemData, timestamp);
|
||||
// Try to add with a smaller timestamp
|
||||
id2 = result.current.addItem(itemData, timestamp - 500);
|
||||
});
|
||||
|
||||
expect(id1).toBe(timestamp);
|
||||
expect(id2).toBe(id1 + 1);
|
||||
expect(result.current.history[1].id).toBe(id2);
|
||||
});
|
||||
|
||||
it('should ensure new IDs start after existing IDs when resuming a session', async () => {
|
||||
const initialItems: HistoryItem[] = [
|
||||
{ id: 5000, type: 'info', text: 'Existing' },
|
||||
];
|
||||
const { result } = await renderHook(() => useHistory({ initialItems }));
|
||||
|
||||
let newId!: number;
|
||||
act(() => {
|
||||
// Try to add with a timestamp smaller than the highest existing ID
|
||||
newId = result.current.addItem({ type: 'info', text: 'New' }, 2000);
|
||||
});
|
||||
|
||||
expect(newId).toBe(5001);
|
||||
expect(result.current.history[1].id).toBe(5001);
|
||||
});
|
||||
|
||||
it('should update lastIdRef when loading new history', async () => {
|
||||
const { result } = await renderHook(() => useHistory());
|
||||
|
||||
act(() => {
|
||||
result.current.loadHistory([{ id: 8000, type: 'info', text: 'Loaded' }]);
|
||||
});
|
||||
|
||||
let newId!: number;
|
||||
act(() => {
|
||||
newId = result.current.addItem({ type: 'info', text: 'New' }, 1000);
|
||||
});
|
||||
|
||||
expect(newId).toBe(8001);
|
||||
});
|
||||
|
||||
it('should generate unique IDs for items added with the same base timestamp', async () => {
|
||||
const { result } = await renderHook(() => useHistory());
|
||||
const timestamp = Date.now();
|
||||
@@ -215,8 +265,8 @@ describe('useHistoryManager', () => {
|
||||
const after = Date.now();
|
||||
|
||||
expect(result.current.history).toHaveLength(1);
|
||||
// ID should be >= before + 1 (since counter starts at 0 and increments to 1)
|
||||
expect(result.current.history[0].id).toBeGreaterThanOrEqual(before + 1);
|
||||
// ID should be >= before (since baseTimestamp defaults to Date.now())
|
||||
expect(result.current.history[0].id).toBeGreaterThanOrEqual(before);
|
||||
expect(result.current.history[0].id).toBeLessThanOrEqual(after + 1);
|
||||
});
|
||||
|
||||
|
||||
@@ -42,16 +42,22 @@ export function useHistory({
|
||||
initialItems?: HistoryItem[];
|
||||
} = {}): UseHistoryManagerReturn {
|
||||
const [history, setHistory] = useState<HistoryItem[]>(initialItems);
|
||||
const messageIdCounterRef = useRef(0);
|
||||
const lastIdRef = useRef(
|
||||
initialItems.reduce((max, item) => Math.max(max, item.id), 0),
|
||||
);
|
||||
|
||||
// Generates a unique message ID based on a timestamp and a counter.
|
||||
// Generates a unique message ID based on a timestamp, ensuring it is always
|
||||
// greater than any previously assigned ID.
|
||||
const getNextMessageId = useCallback((baseTimestamp: number): number => {
|
||||
messageIdCounterRef.current += 1;
|
||||
return baseTimestamp + messageIdCounterRef.current;
|
||||
const nextId = Math.max(baseTimestamp, lastIdRef.current + 1);
|
||||
lastIdRef.current = nextId;
|
||||
return nextId;
|
||||
}, []);
|
||||
|
||||
const loadHistory = useCallback((newHistory: HistoryItem[]) => {
|
||||
setHistory(newHistory);
|
||||
const maxId = newHistory.reduce((max, item) => Math.max(max, item.id), 0);
|
||||
lastIdRef.current = Math.max(lastIdRef.current, maxId);
|
||||
}, []);
|
||||
|
||||
// Adds a new item to the history state with a unique ID.
|
||||
@@ -153,7 +159,7 @@ export function useHistory({
|
||||
// Clears the entire history state and resets the ID counter.
|
||||
const clearItems = useCallback(() => {
|
||||
setHistory([]);
|
||||
messageIdCounterRef.current = 0;
|
||||
lastIdRef.current = 0;
|
||||
}, []);
|
||||
|
||||
return useMemo(
|
||||
|
||||
@@ -6,17 +6,30 @@
|
||||
|
||||
import { renderHook } from '../../test-utils/render.js';
|
||||
import { act } from 'react';
|
||||
import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest';
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
expect,
|
||||
vi,
|
||||
beforeEach,
|
||||
afterEach,
|
||||
type Mock,
|
||||
} from 'vitest';
|
||||
import { useInlineEditBuffer } from './useInlineEditBuffer.js';
|
||||
|
||||
describe('useEditBuffer', () => {
|
||||
let mockOnCommit: Mock;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.clearAllMocks();
|
||||
mockOnCommit = vi.fn();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('should initialize with empty state', async () => {
|
||||
const { result } = await renderHook(() =>
|
||||
useInlineEditBuffer({ onCommit: mockOnCommit }),
|
||||
|
||||
Reference in New Issue
Block a user