Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

7.4 KB
Raw Blame
import { describe, expect, it } from 'vitest'

import type { GlobalSettings } from './global-settings-types'
import type { Repo } from './repo-types'
import type { DetectedWorktree, DetectedWorktreeListResult, Worktree } from './worktree/types'
import {
  getHiddenExternalWorktrees,
  getHiddenImportableExternalWorktrees,
  getNewExternalWorktreeInboxWorktrees,
  getVisibleExternalWorktrees,
  getVisibleNonOrcaWorktrees,
  mergeExternalWorktreeInboxPaths,
  shouldOfferNewExternalWorktreeInbox
} from './external-worktree-inbox'
import {
  buildKnownOrcaWorkspaceLayouts,
  EXTERNAL_WORKTREE_VISIBILITY_ROLLOUT_AT,
  toDetectedWorktree
} from './worktree/ownership'

const repo: Repo = {
  id: 'repo-1',
  path: '/repo',
  displayName: 'orca',
  badgeColor: '#000000',
  addedAt: Date.UTC(2026, 4, 24),
  externalWorktreeVisibility: 'hide',
  externalWorktreeVisibilityPromptDismissedAt: 1
}

function detectedWorktree(overrides: Partial<DetectedWorktree> = {}): DetectedWorktree {
  return {
    id: 'repo-1::/repo-worktree',
    repoId: repo.id,
    path: '/repo-worktree',
    displayName: 'repo-worktree',
    branch: 'refs/heads/feature',
    head: 'abc123',
    isBare: false,
    isMainWorktree: false,
    comment: '',
    linkedIssue: null,
    linkedPR: null,
    linkedLinearIssue: null,
    isArchived: false,
    isUnread: false,
    isPinned: false,
    sortOrder: 0,
    lastActivityAt: 0,
    ownership: 'external',
    selectedCheckout: false,
    visible: false,
    ...overrides
  }
}

function detectedResult(worktrees: DetectedWorktree[]): DetectedWorktreeListResult {
  return {
    repoId: repo.id,
    authoritative: true,
    source: 'git',
    worktrees
  }
}

function makeSettings(): GlobalSettings {
  return {
    workspaceDir: '/orca/workspaces',
    nestWorkspaces: true,
    workspaceDirHistory: [],
    refreshLocalBaseRefOnWorktreeCreate: false,
    localBaseRefSuggestionDismissed: false,
    branchPrefix: 'none',
    branchPrefixCustom: '',
    theme: 'system',
    appFontFamily: 'Geist',
    editorAutoSave: false,
    editorAutoSaveDelayMs: 1000,
    editorMinimapEnabled: false,
    markdownReviewToolsEnabled: true,
    terminalFontSize: 14,
    terminalFontFamily: 'monospace',
    terminalFontWeight: 400,
    terminalLineHeight: 1.2
  } as unknown as GlobalSettings
}

function makeGitWorktree(overrides: Partial<Worktree> = {}): Worktree {
  return {
    id: `repo-1::${overrides.path ?? '/repo'}`,
    repoId: repo.id,
    path: '/repo',
    displayName: 'repo',
    branch: 'refs/heads/main',
    head: 'abc123',
    isBare: false,
    isMainWorktree: true,
    comment: '',
    linkedIssue: null,
    linkedPR: null,
    linkedLinearIssue: null,
    linkedGitLabMR: null,
    linkedGitLabIssue: null,
    isArchived: false,
    isUnread: false,
    isPinned: false,
    sortOrder: 0,
    lastActivityAt: 0,
    workspaceStatus: 'todo',
    ...overrides
  }
}

describe('external worktree inbox', () => {
  it('merges inbox paths without duplicates when paths match after normalization', () => {
    expect(mergeExternalWorktreeInboxPaths(['/repo/one/'], ['/repo/one', '/repo/two'])).toEqual([
      '/repo/one/',
      '/repo/two'
    ])
  })

  it('offers the inbox only after the initial prompt is dismissed and discovery is not suppressed', () => {
    expect(shouldOfferNewExternalWorktreeInbox(repo)).toBe(true)
    expect(
      shouldOfferNewExternalWorktreeInbox({
        ...repo,
        externalWorktreeVisibilityPromptDismissedAt: undefined
      })
    ).toBe(false)
    expect(
      shouldOfferNewExternalWorktreeInbox({
        ...repo,
        externalWorktreeDiscoverySuppressedAt: 1
      })
    ).toBe(false)
    expect(
      shouldOfferNewExternalWorktreeInbox({
        ...repo,
        externalWorktreeVisibility: 'show'
      })
    ).toBe(false)
  })

  it('returns only hidden external worktrees outside the inbox baseline', () => {
    const hidden = detectedWorktree({ id: 'hidden', path: '/scratch/new-one' })
    const baselined = detectedWorktree({ id: 'baselined', path: '/scratch/old-one' })
    const detected = detectedResult([
      hidden,
      baselined,
      detectedWorktree({ id: 'visible', visible: true }),
      detectedWorktree({ id: 'orca-managed', ownership: 'orca-managed' })
    ])

    expect(getHiddenExternalWorktrees(detected)).toEqual([hidden, baselined])
    expect(
      getNewExternalWorktreeInboxWorktrees(detected, {
        ...repo,
        externalWorktreeInboxBaselinePaths: ['/scratch/old-one']
      })
    ).toEqual([hidden])
  })

  it('excludes explicitly visible agent scratch from visibility-control counts', () => {
    const visible = detectedWorktree({ id: 'visible', visible: true })
    const scratch = detectedWorktree({
      id: 'agent-scratch',
      ownership: 'agent-scratch',
      visible: true
    })

    expect(getVisibleExternalWorktrees(detectedResult([visible, scratch]))).toEqual([visible])
    expect(getVisibleNonOrcaWorktrees(detectedResult([visible, scratch]))).toEqual([
      visible,
      scratch
    ])
  })

  it('lists hidden agent scratch among importable worktrees so recovery stays reachable', () => {
    // Scratch cannot be revealed by the visibility toggle (#9388); per-path import preserves recovery (#10324).
    const hiddenScratch = detectedWorktree({
      id: 'hidden-scratch',
      path: '/repo/.claude/worktrees/scratch-1',
      ownership: 'agent-scratch'
    })
    const hiddenExternal = detectedWorktree({ id: 'hidden-external' })
    const detected = detectedResult([
      hiddenScratch,
      hiddenExternal,
      detectedWorktree({ id: 'visible-scratch', ownership: 'agent-scratch', visible: true }),
      detectedWorktree({ id: 'orca-managed', ownership: 'orca-managed' }),
      detectedWorktree({ id: 'checked-out', selectedCheckout: true })
    ])

    expect(getHiddenImportableExternalWorktrees(detected)).toEqual([hiddenScratch, hiddenExternal])
    // Scratch remains outside discovery counts despite dialog control.
    expect(getHiddenExternalWorktrees(detected)).toEqual([hiddenExternal])
  })

  it('offers no importable worktrees from a non-authoritative snapshot', () => {
    const scratch = detectedWorktree({ id: 'scratch', ownership: 'agent-scratch' })

    expect(
      getHiddenImportableExternalWorktrees({ ...detectedResult([scratch]), authoritative: false })
    ).toEqual([])
    expect(getHiddenImportableExternalWorktrees(undefined)).toEqual([])
  })

  it('offers metadata-free nested Orca workspace worktrees through the inbox', () => {
    const settings = makeSettings()
    const manual = toDetectedWorktree({
      repo,
      settings,
      worktree: makeGitWorktree({
        path: '/orca/workspaces/orca/manual-from-git',
        displayName: 'manual-from-git',
        branch: 'refs/heads/manual-from-git',
        isMainWorktree: false
      }),
      knownOrcaLayouts: buildKnownOrcaWorkspaceLayouts(settings, repo)
    })

    expect(manual.ownership).toBe('external')
    expect(manual.visible).toBe(false)
    expect(getNewExternalWorktreeInboxWorktrees(detectedResult([manual]), repo)).toEqual([manual])
  })

  it('suppresses non-authoritative detected results', () => {
    expect(
      getNewExternalWorktreeInboxWorktrees(detectedResult([detectedWorktree()]), {
        ...repo,
        addedAt: EXTERNAL_WORKTREE_VISIBILITY_ROLLOUT_AT + 1
      })
    ).toEqual([detectedWorktree()])
    expect(
      getNewExternalWorktreeInboxWorktrees(
        { ...detectedResult([detectedWorktree()]), authoritative: false },
        repo
      )
    ).toEqual([])
  })
})