import { describe, it, expect, afterEach } from 'vitest';
import { listSessions } from './sessions.js';
import { SessionStore } from '../session/index.js';
import { join } from 'path';
import { tmpdir } from 'os';
import { existsSync, unlinkSync } from 'fs';

describe('sessions command', () => {
  const dbPath = join(tmpdir(), 'flynn-test-sessions-cli.db');
  let store: SessionStore;

  afterEach(() => {
    store?.close();
    if (existsSync(dbPath)) {unlinkSync(dbPath);}
  });

  it('returns empty list when no sessions', () => {
    store = new SessionStore(dbPath);
    const sessions = listSessions(store);
    expect(sessions).toEqual([]);
  });

  it('returns session IDs with message counts', () => {
    store = new SessionStore(dbPath);
    store.addMessage('telegram:123', { role: 'user', content: 'Hello' });
    store.addMessage('telegram:123', { role: 'assistant', content: 'Hi' });
    store.addMessage('tui:local', { role: 'user', content: 'Test' });

    const sessions = listSessions(store);
    expect(sessions).toHaveLength(2);

    const telegramSession = sessions.find(s => s.id === 'telegram:123');
    expect(telegramSession).toBeDefined();
    expect(telegramSession!.messageCount).toBe(2);

    const tuiSession = sessions.find(s => s.id === 'tui:local');
    expect(tuiSession).toBeDefined();
    expect(tuiSession!.messageCount).toBe(1);
  });
});
