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.

3.4 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import {
  resolveMobileBranchBaseRefs,
  resolveMobileBranchCompareBaseRef
} from './mobile-branch-base-ref'
import type { RpcClient } from '../transport/rpc-client'

type RpcResponse =
  | { ok: true; result: unknown }
  | { ok: false; error: { code: string; message: string } }

function ok(result: unknown): RpcResponse {
  return { ok: true, result }
}

function fail(message: string): RpcResponse {
  return { ok: false, error: { code: 'runtime_error', message } }
}

function clientWith(responses: RpcResponse[]) {
  const calls: { method: string; params?: Record<string, unknown> }[] = []
  return {
    calls,
    client: {
      sendRequest: async (method: string, params?: Record<string, unknown>) => {
        calls.push({ method, params })
        return responses.shift() ?? fail(`unexpected ${method}`)
      }
    } as Pick<RpcClient, 'sendRequest'> as RpcClient
  }
}

describe('resolveMobileBranchCompareBaseRef', () => {
  it('prefers the per-worktree base ref over the repo default', async () => {
    const { client, calls } = clientWith([
      ok({ worktree: { baseRef: 'origin/release' } }),
      ok({ repos: [{ id: 'repo-1', worktreeBaseRef: 'origin/main' }] })
    ])

    await expect(resolveMobileBranchCompareBaseRef(client, 'repo-1::/tmp/wt')).resolves.toBe(
      'origin/release'
    )
    expect(calls.map((call) => call.method)).toEqual(['worktree.show', 'repo.list'])
  })

  it('falls back to repo worktreeBaseRef when the worktree has no pinned base', async () => {
    const { client } = clientWith([
      ok({ worktree: { baseRef: null } }),
      ok({ repos: [{ id: 'repo-1', worktreeBaseRef: 'origin/main' }] })
    ])

    await expect(resolveMobileBranchCompareBaseRef(client, 'repo-1::/tmp/wt')).resolves.toBe(
      'origin/main'
    )
  })

  it('falls back to repo.baseRefDefault when metadata reads have no base', async () => {
    const { client, calls } = clientWith([
      fail('old host'),
      ok({ repos: [{ id: 'repo-1', worktreeBaseRef: null }] }),
      ok({ defaultBaseRef: 'origin/main' })
    ])

    await expect(resolveMobileBranchCompareBaseRef(client, 'repo-1::/tmp/wt')).resolves.toBe(
      'origin/main'
    )
    expect(calls.map((call) => call.method)).toEqual([
      'worktree.show',
      'repo.list',
      'repo.baseRefDefault'
    ])
  })

  it('compares against the upstream but rebases from the default when nothing is pinned', async () => {
    const { client } = clientWith([
      ok({ worktree: { baseRef: null } }),
      ok({ repos: [{ id: 'repo-1', worktreeBaseRef: null }] }),
      ok({ defaultBaseRef: 'origin/main' })
    ])

    const refs = await resolveMobileBranchBaseRefs(client, 'repo-1::/tmp/wt', {
      compareAgainstUpstream: true,
      upstreamName: 'origin/feature'
    })
    expect(refs.compareBaseRef).toBe('origin/feature')
    await expect(refs.readEffectiveBaseRef()).resolves.toBe('origin/main')
  })

  it('still compares against the upstream when the default lookup fails', async () => {
    const { client } = clientWith([
      ok({ worktree: { baseRef: null } }),
      ok({ repos: [{ id: 'repo-1', worktreeBaseRef: null }] }),
      fail('lookup failed')
    ])

    const refs = await resolveMobileBranchBaseRefs(client, 'repo-1::/tmp/wt', {
      compareAgainstUpstream: true,
      upstreamName: 'origin/feature'
    })
    expect(refs.compareBaseRef).toBe('origin/feature')
    await expect(refs.readEffectiveBaseRef()).resolves.toBeNull()
  })
})