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.7 KB
Raw Blame
import { mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, describe, expect, it, vi } from 'vitest'

// Wrap real reads to measure walk concurrency.
const readdirCalls = vi.hoisted(() => ({ outstanding: 0, peak: 0, count: 0 }))

vi.mock('node:fs/promises', async (importOriginal) => {
  const actual = await importOriginal<Record<string, unknown>>()
  const realReaddir = actual.readdir as (...args: unknown[]) => Promise<never>
  return {
    ...actual,
    readdir: async (...args: unknown[]) => {
      readdirCalls.outstanding += 1
      readdirCalls.count += 1
      readdirCalls.peak = Math.max(readdirCalls.peak, readdirCalls.outstanding)
      try {
        return await realReaddir(...args)
      } finally {
        readdirCalls.outstanding -= 1
      }
    }
  }
})

import { countLooseRefs } from './loose-ref-count'

const roots: string[] = []

async function makeRefsTree(counts: Record<string, number>): Promise<string> {
  const root = await mkdtemp(join(tmpdir(), 'orca-loose-refs-'))
  roots.push(root)
  const refs = join(root, 'refs')
  for (const [namespace, count] of Object.entries(counts)) {
    const directory = join(refs, namespace)
    await mkdir(directory, { recursive: true })
    for (let index = 0; index < count; index += 1) {
      await writeFile(join(directory, `ref-${index}`), 'a'.repeat(40))
    }
  }
  await mkdir(refs, { recursive: true })
  return refs
}

afterEach(async () => {
  vi.restoreAllMocks()
  await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })))
})

describe('countLooseRefs', () => {
  it('counts files across nested namespaces', async () => {
    const refs = await makeRefsTree({ heads: 3, 'remotes/origin': 4, 'remotes/fork/deep': 2 })

    await expect(countLooseRefs(refs, 100)).resolves.toEqual({ count: 9, saturated: false })
  })

  it('stops at the budget instead of walking the whole backlog', async () => {
    const refs = await makeRefsTree({ 'remotes/origin': 500 })

    const result = await countLooseRefs(refs, 10)

    expect(result).toEqual({ count: 10, saturated: true })
  })

  it('reports zero for a repository with no refs directory', async () => {
    const root = await mkdtemp(join(tmpdir(), 'orca-loose-refs-missing-'))
    roots.push(root)

    await expect(countLooseRefs(join(root, 'refs'), 100)).resolves.toEqual({
      count: 0,
      saturated: false
    })
  })

  it('never has more than one directory read outstanding', async () => {
    // Sequential reads occupy at most one shared libuv slot, leaving unrelated filesystem work unblocked.
    const refs = await makeRefsTree({
      'remotes/a': 3,
      'remotes/b': 3,
      'remotes/c': 3,
      'remotes/d': 3,
      'remotes/e/deep': 3
    })
    readdirCalls.peak = 0
    readdirCalls.count = 0

    await countLooseRefs(refs, 1000)

    expect(readdirCalls.count).toBeGreaterThan(1)
    expect(readdirCalls.peak).toBe(1)
  })

  it('reads each directory once rather than streaming it in batches', async () => {
    // One round trip per directory measured ~8x faster than streaming on a degraded repository.
    const refs = await makeRefsTree({ 'remotes/origin': 400 })
    readdirCalls.count = 0

    await countLooseRefs(refs, 1000)

    // refs/ plus refs/remotes plus refs/remotes/origin.
    expect(readdirCalls.count).toBe(3)
  })

  it('does not follow directory symlinks into a loop', async () => {
    const refs = await makeRefsTree({ heads: 2 })
    await symlink(refs, join(refs, 'loop'), 'dir')

    const result = await countLooseRefs(refs, 100)

    expect(result.saturated).toBe(false)
    // The symlink is one dirent, never a second traversal of the tree.
    expect(result.count).toBe(3)
  })
})