perThreadInput.test.tsx2.0 KBView on GitHub
import type { MessageThread } from '@/modules/cedar-os/src/store/messages/MessageTypes';
import { useCedarStore } from '@/modules/cedar-os/src/store/CedarStore';

/**
 * Phase 7 (design: chat-thread-store) — each thread owns its draft input.
 * setChatInputContent writes the active thread's inputContent; switching threads
 * restores that thread's draft into the chatInputContent mirror. Exercised headlessly.
 */
const thread = (id: string): MessageThread => ({ id, name: id, messages: [] });

describe('agentThreadsSlice — per-thread input content', () => {
  beforeEach(() => {
    useCedarStore.setState((state) => ({
      ...state,
      threadMap: { A: thread('A'), B: thread('B') },
      mainThreadId: 'A',
      activeThreadId: 'A',
      chatInputContent: '',
      messages: [],
    }));
  });

  it('setChatInputContent persists the draft on the active thread', () => {
    useCedarStore.getState().selectThread('A');
    useCedarStore.getState().setChatInputContent('draft for A');
    expect(useCedarStore.getState().threadMap['A'].inputContent).toBe('draft for A');
    expect(useCedarStore.getState().chatInputContent).toBe('draft for A');
  });

  it('typing in A, switching to B, back to A restores A’s draft', () => {
    const store = () => useCedarStore.getState();

    store().selectThread('A');
    store().setChatInputContent('draft for A');

    // Switch to B: A's draft persisted, mirror reflects B's (empty) draft.
    store().selectThread('B');
    expect(store().chatInputContent).toBe('');
    expect(store().threadMap['A'].inputContent).toBe('draft for A');

    store().setChatInputContent('draft for B');
    expect(store().threadMap['B'].inputContent).toBe('draft for B');

    // Back to A: mirror restored from A's saved draft.
    store().selectThread('A');
    expect(store().chatInputContent).toBe('draft for A');

    // And B's draft is still independently preserved.
    store().selectThread('B');
    expect(store().chatInputContent).toBe('draft for B');
  });
});