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.

8.7 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { snapshotNode, recreateNodeFromSnapshot, type ReopenNodeSnapshot } from './reopenNode'

// createAgentNode reads settings/CLI-caps singletons — none of that matters for these tests,
// so stub the pieces snapshotNode/recreateNodeFromSnapshot actually touch.
vi.mock('@renderer/state/settings', () => ({
  useSettings: { getState: () => ({ settings: { customAgents: [], defaultNodeWidth: undefined, defaultNodeHeight: undefined, agentLaunchCommands: {} } }) }
}))
vi.mock('@renderer/state/permissionMode', () => ({ claudeCliCapsNow: () => ({ sessionIdFlag: false }) }))
vi.mock('@renderer/state/codexIdentity', () => ({ codexSharedIdentity: () => undefined }))

const baseCtx = () => ({
  liveNodeIds: new Set<string>(),
  project: undefined,
  resolveAccountId: (id: string | undefined) => id,
  permissionModeFor: () => undefined
})

describe('whiteboard reopen', () => {
  const node = {
    id: 'whiteboard-closed',
    type: 'whiteboard' as const,
    position: { x: 5, y: 6 },
    width: 900,
    height: 600,
    data: { title: 'Board', color: '#64d2ff', group: null }
  }

  it('a snapshot remembers the closed board by id, so the rebuilt node can read its file', () => {
    const snap = snapshotNode(node, [{ id: node.id, position: node.position }])
    expect(snap?.data.whiteboardSource).toBe('whiteboard-closed')
  })

  it('rebuilds a whiteboard that reads the old board and does not jump into editing', () => {
    const snap = snapshotNode(node, [{ id: node.id, position: node.position }]) as ReopenNodeSnapshot
    const rebuilt = recreateNodeFromSnapshot(snap, baseCtx())
    expect(rebuilt?.type).toBe('whiteboard')
    expect(rebuilt?.id).not.toBe('whiteboard-closed')
    expect(rebuilt?.data.whiteboardSource).toBe('whiteboard-closed')
    expect(rebuilt?.data.autoEdit).toBeUndefined()
  })
})

describe('snapshotNode', () => {
  it('captures a leaf node, resolving absolute position through its parent chain', () => {
    const all = [
      { id: 'group-1', position: { x: 100, y: 200 }, parentId: undefined },
      { id: 'sticky-1', position: { x: 10, y: 20 }, parentId: 'group-1' }
    ]
    const node = {
      type: 'sticky' as const,
      position: { x: 10, y: 20 },
      parentId: 'group-1',
      extent: 'parent' as const,
      width: 240,
      height: 200,
      data: { title: 'Note', color: '#ffd60a', group: null, text: 'hi' }
    }
    const snap = snapshotNode(node, all)
    expect(snap).toEqual({
      type: 'sticky',
      position: { x: 10, y: 20 },
      absolutePosition: { x: 110, y: 220 },
      parentId: 'group-1',
      extent: 'parent',
      size: { width: 240, height: 200 },
      data: node.data
    })
  })

  it('returns null for group/subagent/loop/trigger nodes', () => {
    for (const type of ['group', 'subagent', 'loop', 'trigger'] as const) {
      const node = { type, position: { x: 0, y: 0 }, data: { title: 'x', color: '#fff', group: null } }
      expect(snapshotNode(node, [])).toBeNull()
    }
  })

  it('returns null for an account-login node', () => {
    const node = {
      type: 'terminal' as const,
      position: { x: 0, y: 0 },
      data: { title: 'Claude login', color: '#fff', group: null, initialCommand: 'claude /login' }
    }
    expect(snapshotNode(node, [])).toBeNull()
  })
})

describe('recreateNodeFromSnapshot', () => {
  const snap = (over: Partial<ReopenNodeSnapshot>): ReopenNodeSnapshot => ({
    type: 'sticky',
    position: { x: 10, y: 20 },
    absolutePosition: { x: 10, y: 20 },
    data: { title: 'Note', color: '#ffd60a', group: null, text: 'hi' },
    ...over
  })

  it('recreates a sticky at its absolute position when the parent is gone', () => {
    const node = recreateNodeFromSnapshot(snap({ parentId: 'dead-group' }), baseCtx())
    expect(node).not.toBeNull()
    expect(node!.type).toBe('sticky')
    expect(node!.position).toEqual({ x: 10, y: 20 })
    expect(node!.parentId).toBeUndefined()
    expect(node!.data.text).toBe('hi')
    expect(node!.data.title).toBe('Note')
  })

  it('reattaches to the parent group when it still exists, using the RELATIVE position', () => {
    const ctx = { ...baseCtx(), liveNodeIds: new Set(['group-1']) }
    const node = recreateNodeFromSnapshot(
      snap({ parentId: 'group-1', extent: 'parent', position: { x: 5, y: 5 }, absolutePosition: { x: 105, y: 205 } }),
      ctx
    )
    expect(node!.parentId).toBe('group-1')
    expect(node!.extent).toBe('parent')
    expect(node!.position).toEqual({ x: 5, y: 5 })
  })

  it('carries over size when the snapshot has one', () => {
    const node = recreateNodeFromSnapshot(snap({ size: { width: 900, height: 500 } }), baseCtx())
    expect(node!.width).toBe(900)
    expect(node!.height).toBe(500)
  })

  it('recreates an editor node from filePath, carrying the custom title', () => {
    const node = recreateNodeFromSnapshot(
      snap({
        type: 'editor',
        data: { title: 'My renamed editor', color: '#6ac4dc', group: null, filePath: '/tmp/a.ts' }
      }),
      baseCtx()
    )
    expect(node!.type).toBe('editor')
    expect(node!.data.filePath).toBe('/tmp/a.ts')
    expect(node!.data.title).toBe('My renamed editor')
  })

  it('returns null for an editor snapshot missing filePath (never a silently blank node)', () => {
    const node = recreateNodeFromSnapshot(
      snap({ type: 'editor', data: { title: 'x', color: '#fff', group: null } }),
      baseCtx()
    )
    expect(node).toBeNull()
  })

  // ⇧⌘T used to bring a session back without its icon: `icon` was not a cosmetic key.
  it('carries the node icon over, re-validated', () => {
    const withIcon = (icon: unknown) =>
      recreateNodeFromSnapshot(
        snap({ type: 'terminal', data: { title: 'db', color: '#fff', group: null, icon: icon as never } }),
        baseCtx()
      )
    expect(withIcon({ type: 'lucide', name: 'database' })!.data.icon).toEqual({
      type: 'lucide',
      name: 'database'
    })
    expect(withIcon({ type: 'lucide', name: 'nope' })!.data.icon).toBeUndefined()
  })

  // ⇧⌘T used to bring a terminal back at the global size: the per-terminal override (issue #915)
  // was in the snapshot but not a cosmetic key.
  it('carries the per-terminal font size over, re-validated', () => {
    const withSize = (terminalFontSize: unknown) =>
      recreateNodeFromSnapshot(
        snap({ type: 'terminal', data: { title: 't', color: '#fff', group: null, terminalFontSize: terminalFontSize as never } }),
        baseCtx()
      )
    expect(withSize(18)!.data.terminalFontSize).toBe(18)
    expect(withSize(999)!.data.terminalFontSize).toBeUndefined()
    expect(withSize('x')!.data.terminalFontSize).toBeUndefined()
  })

  it('recreates a dino node carrying its high score', () => {
    const node = recreateNodeFromSnapshot(
      snap({ type: 'dino', data: { title: 'Dino', color: '#a2a2a2', group: null, highScore: 42 } }),
      baseCtx()
    )
    expect(node!.data.highScore).toBe(42)
  })

  // A kind in neither UNRESTORABLE nor buildBase's switch is the trap the 'trigger' comment in
  // reopenNode.ts warns about: snapshotNode happily records it, the ⇧⌘T entry and its persisted
  // closedSessions twin are both written, and buildBase's `default` then returns null — a dead,
  // clickable history entry that typechecks. So the pairing is asserted from BOTH ends.
  it('recreates a files node from its cwd (a deleted file manager is not a dead reopen entry)', () => {
    const node = recreateNodeFromSnapshot(
      snap({
        type: 'files',
        data: { title: 'nodes', color: '#ffd60a', group: null, cwd: '/repo/src/renderer/nodes' }
      }),
      baseCtx()
    )
    expect(node).not.toBeNull()
    expect(node!.type).toBe('files')
    expect(node!.data.cwd).toBe('/repo/src/renderer/nodes')
    expect(node!.data.title).toBe('nodes')
  })

  it('carries a files node’s sshFs flag, so a remote listing does not come back local', () => {
    const node = recreateNodeFromSnapshot(
      snap({
        type: 'files',
        data: { title: 'src', color: '#ffd60a', group: null, cwd: '/srv/app/src', sshFs: true }
      }),
      baseCtx()
    )
    expect(node!.data.sshFs).toBe(true)
  })

  it('returns null for a files snapshot missing cwd (never a silently blank listing)', () => {
    const node = recreateNodeFromSnapshot(
      snap({ type: 'files', data: { title: 'x', color: '#fff', group: null } }),
      baseCtx()
    )
    expect(node).toBeNull()
  })

  it('snapshots a files node rather than treating it as unrestorable', () => {
    const snapshot = snapshotNode(
      {
        type: 'files',
        position: { x: 0, y: 0 },
        width: 340,
        height: 460,
        data: { title: 'docs', color: '#ffd60a', group: null, cwd: '/repo/docs' }
      },
      []
    )
    expect(snapshot).not.toBeNull()
    expect(snapshot!.type).toBe('files')
  })
})