Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

4.4 KB
Raw Blame
import { describe, it, expect } from 'vitest'
import {
  accountChipLabel,
  accountsForProject,
  isAccountLoginNode,
  sshAccountsHint,
  systemAccountDisplay
} from './workspace'
import type { ClaudeAccount } from '@shared/types'

const acct = (over: Partial<ClaudeAccount>): ClaudeAccount => ({
  id: 'a1',
  label: '[email protected]',
  email: '[email protected]',
  createdAt: 0,
  ...over
})

describe('accountChipLabel', () => {
  it('returns null when there is no accountId (no chip)', () => {
    expect(accountChipLabel(undefined, [acct({})])).toBeNull()
    expect(accountChipLabel('', [acct({})])).toBeNull()
  })

  it('takes the part before @ and tooltips as "label (email)"', () => {
    const r = accountChipLabel('a1', [acct({ label: '[email protected]', email: '[email protected]' })])
    expect(r).toEqual({ short: 'work', tooltip: '[email protected] ([email protected])' })
  })

  it('caps the short label at 10 chars with an ellipsis', () => {
    const r = accountChipLabel('a1', [acct({ label: '[email protected]' })])
    expect(r?.short).toBe('verylongac…')
    expect(r?.short.length).toBe(11) // 10 chars + ellipsis
  })

  it('does not truncate a base of exactly 10 chars', () => {
    const r = accountChipLabel('a1', [acct({ label: '[email protected]' })])
    expect(r?.short).toBe('tenletters')
  })

  it('omits the "(email)" suffix when the account has no email', () => {
    const r = accountChipLabel('a1', [acct({ label: 'personal', email: undefined })])
    expect(r).toEqual({ short: 'personal', tooltip: 'personal' })
  })

  it('falls back to "Unknown account" when the id no longer resolves', () => {
    expect(accountChipLabel('gone', [acct({ id: 'a1' })])).toEqual({
      short: 'Unknown account',
      tooltip: 'Unknown account'
    })
    expect(accountChipLabel('gone', [])).toEqual({
      short: 'Unknown account',
      tooltip: 'Unknown account'
    })
  })
})

describe('systemAccountDisplay', () => {
  it('prefers the custom label', () => {
    expect(systemAccountDisplay('Kişisel', '[email protected]')).toBe('Kişisel')
  })

  it('falls back to the detected email when the label is empty/whitespace', () => {
    expect(systemAccountDisplay('', '[email protected]')).toBe('[email protected]')
    expect(systemAccountDisplay('   ', '[email protected]')).toBe('[email protected]')
    expect(systemAccountDisplay(undefined, '[email protected]')).toBe('[email protected]')
  })

  it('falls back to the generic name when nothing is known', () => {
    expect(systemAccountDisplay('', null)).toBe('System account')
    expect(systemAccountDisplay(undefined, undefined)).toBe('System account')
  })
})

describe('isAccountLoginNode', () => {
  it('matches the factory title (the only persisted signature)', () => {
    expect(isAccountLoginNode({ title: 'Claude login' })).toBe(true)
  })

  it('matches a live node by its one-shot initialCommand', () => {
    expect(isAccountLoginNode({ title: 'renamed', initialCommand: 'claude /login' })).toBe(true)
  })

  it('does not match ordinary claude nodes', () => {
    expect(isAccountLoginNode({ title: 'My session', initialCommand: 'claude' })).toBe(false)
    expect(isAccountLoginNode({ title: 'Terminal' })).toBe(false)
    expect(isAccountLoginNode({})).toBe(false)
  })
})

// An SSH project's account pickers list ONLY accounts created on that host — local accounts are
// (correctly) invisible there, which read as "multi-account is broken on SSH" without a hint row
// explaining where accounts for this host come from.
describe('sshAccountsHint', () => {
  const sshProject = { ssh: { server: { host: 'box', user: 'me' } } }

  it('hints on an SSH project whose host has no eligible accounts', () => {
    const localOnly = [acct({ id: 'a1' })] // no `host` → local → filtered out for SSH
    const eligible = accountsForProject(localOnly, sshProject)
    expect(eligible).toEqual([])
    expect(sshAccountsHint(sshProject, eligible)).toMatch(/Settings → Accounts/)
  })

  it('is null once the host has a matching account', () => {
    const hosted = [acct({ id: 'a2', host: 'me@box' })]
    const eligible = accountsForProject(hosted, sshProject)
    expect(eligible).toHaveLength(1)
    expect(sshAccountsHint(sshProject, eligible)).toBeNull()
  })

  it('is null for local projects (empty list there just means no managed accounts)', () => {
    expect(sshAccountsHint(undefined, [])).toBeNull()
    expect(sshAccountsHint({}, [])).toBeNull()
  })
})