fileMentionToItem.test.tsx2.1 KBView on GitHub
import { DEFAULT_THREAD_ID } from '@/modules/cedar-os/src/store/messages/MessageTypes';
import { useCedarStore } from '@/modules/cedar-os/src/store/CedarStore';
import { attachFileMention } from '@/modules/cedar-os/src/components/chatInput/fileMentionSuggestion';

/**
 * `[[` file mention — selecting a file commits an explicit `file` ContextItem to the active
 * thread (like `@` for conversations) and inserts a mention node stamped { kind:'file', id }.
 */
function makeEditorStub() {
  const inserted: unknown[] = [];
  const chain = {
    focus: () => chain,
    insertContentAt: (_range: unknown, content: unknown) => {
      inserted.push(content);
      return chain;
    },
    run: () => true,
  };
  return { editor: { chain: () => chain }, inserted };
}

describe('attachFileMention — [[ file → committed file ContextItem', () => {
  beforeEach(() => {
    useCedarStore.setState((state) => ({
      ...state,
      threadMap: {
        [DEFAULT_THREAD_ID]: { id: DEFAULT_THREAD_ID, messages: [], chatContext: { items: [] } },
      },
      mainThreadId: DEFAULT_THREAD_ID,
      activeThreadId: DEFAULT_THREAD_ID,
      messages: [],
    }));
  });

  it('adds a file ContextItem to the active thread and stamps { kind:file, id } on the node', () => {
    const { editor, inserted } = makeEditorStub();

    attachFileMention(editor as never, { from: 0, to: 0 } as never, {
      id: 'doc_1',
      label: 'Numeral QBR deck',
      path: '/docs/numeral',
      documentType: 'document',
      description: null,
      lastOpened: null,
      updatedAt: '2026-07-06T00:00:00Z',
    });

    const items = useCedarStore.getState().threadMap[DEFAULT_THREAD_ID].chatContext?.items ?? [];
    const attached = items.find((i) => i.kind === 'file' && i.id === 'doc_1');
    expect(attached).toBeTruthy();
    expect(attached?.label).toBe('Numeral QBR deck');
    expect(attached?.addedBy).toBe('user');

    const node = (inserted[0] as Array<{ type: string; attrs?: Record<string, unknown> }>)[0];
    expect(node.type).toBe('mention');
    expect(node.attrs?.kind).toBe('file');
    expect(node.attrs?.id).toBe('doc_1');
  });
});