refactor(cli,core): foundational layout, identity management, and type safety (#23286)

This commit is contained in:
Jarrod Whelan
2026-03-23 18:49:51 -07:00
committed by GitHub
parent 57a66f5f0d
commit 89ca78837e
31 changed files with 477 additions and 182 deletions
@@ -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);
});