import { describe, it, expect } from 'vitest';
import { CmuxClient } from '../../src/bridge/cmux-client.js';

describe('CmuxClient', () => {
  describe('parseTree', () => {
    it('parses real cmux tree output with tree-drawing chars', () => {
      const client = new CmuxClient();
      const output = `window window:1 [current]
├── workspace workspace:1 "My Project" [selected]
│   ├── pane pane:2
│   │   ├── surface surface:2 [terminal] "zsh"
│   │   └── surface surface:13 [terminal] "node"
│   └── pane pane:3 [focused]
│       └── surface surface:4 [browser] "localhost"
└── workspace workspace:2 "Notes"
    └── pane pane:6 [focused]
        └── surface surface:6 [terminal] "vim" [selected]`;

      const result = client.parseTree(output);

      expect(result).toHaveLength(2);
      expect(result[0].ref).toBe('workspace:1');
      expect(result[0].name).toBe('My Project');
      expect(result[0].panes).toHaveLength(2);
      expect(result[0].panes[0].surfaces).toHaveLength(2);
      expect(result[0].panes[0].surfaces[0].type).toBe('terminal');
      expect(result[0].panes[0].surfaces[0].title).toBe('zsh');
      expect(result[0].panes[1].surfaces[0].type).toBe('browser');
      expect(result[0].panes[1].surfaces[0].title).toBe('localhost');
      expect(result[1].panes[0].surfaces[0].title).toBe('vim');
    });

    it('handles empty output', () => {
      const client = new CmuxClient();
      expect(client.parseTree('')).toEqual([]);
    });

    it('handles workspace without quoted name', () => {
      const client = new CmuxClient();
      const output = `window window:1
├── workspace workspace:1 [selected]
│   └── pane pane:1
│       └── surface surface:1 [terminal] [selected]`;

      const result = client.parseTree(output);
      expect(result[0].name).toBeUndefined();
    });

    it('extracts titles with special characters', () => {
      const client = new CmuxClient();
      const output = `window window:1
└── workspace workspace:5 "✳ cmux-remote-plugin"
    └── pane pane:9 [focused]
        └── surface surface:14 [terminal] "⠂ cmux-remote-plugin" [selected]`;

      const result = client.parseTree(output);
      expect(result[0].name).toBe('✳ cmux-remote-plugin');
      expect(result[0].panes[0].surfaces[0].title).toBe('⠂ cmux-remote-plugin');
    });
  });

  describe('parseSurfaces', () => {
    it('parses surface list', () => {
      const client = new CmuxClient();
      const output = `surface:1 [terminal] zsh
surface:2 [browser] localhost`;

      const result = client.parseSurfaces(output);
      expect(result).toHaveLength(2);
      expect(result[0].ref).toBe('surface:1');
      expect(result[0].type).toBe('terminal');
      expect(result[1].type).toBe('browser');
    });
  });
});
