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.

5.9 KB
Raw Blame
import { test, expect } from './helpers/orca-app'
import { waitForActiveWorktree, waitForSessionReady } from './helpers/store'
import type { Locator, Page } from '@playwright/test'

type SeededUntrackedFile = {
  fileName: string
}

async function openSourceControl(page: Page): Promise<void> {
  await page.evaluate(() => {
    const state = window.__store?.getState()
    state?.setRightSidebarOpen(true)
  })
  await page.getByRole('button', { name: /Source Control/ }).click()
  await page.getByTestId('source-control-filter-toggle').click()
  await expect(page.getByPlaceholder(/Filter files/)).toBeVisible()
}

async function seedUntrackedFile(
  page: Page,
  requestedFileName?: string
): Promise<SeededUntrackedFile> {
  const seeded = await page.evaluate(async (requestedFileName) => {
    const store = window.__store
    if (!store) {
      throw new Error('window.__store is not available')
    }

    const state = store.getState()
    const worktreeId = state.activeWorktreeId
    const worktree = Object.values(state.worktreesByRepo)
      .flat()
      .find((entry) => entry.id === worktreeId)
    if (!worktree) {
      throw new Error('active worktree not found')
    }

    const separator = worktree.path.includes('\\') ? '\\' : '/'
    const fileName = requestedFileName ?? `orca-discard-confirm-${Date.now()}.txt`
    const relativePath = fileName
    await window.api.fs.writeFile({
      filePath: `${worktree.path}${separator}${relativePath}`,
      content: 'delete me\n'
    })

    return {
      fileName
    }
  }, requestedFileName)

  // Why: a status read already in flight when the file was written can be joined and miss it.
  await expect
    .poll(
      async () => (await refreshGitStatus(page)).some((path) => path.endsWith(seeded.fileName)),
      { timeout: 10_000, message: `git status did not include ${seeded.fileName}` }
    )
    .toBe(true)
  return seeded
}

async function refreshGitStatus(page: Page): Promise<string[]> {
  return page.evaluate(async () => {
    const store = window.__store
    if (!store) {
      throw new Error('window.__store is not available')
    }
    const state = store.getState()
    const worktreeId = state.activeWorktreeId
    const worktree = Object.values(state.worktreesByRepo)
      .flat()
      .find((entry) => entry.id === worktreeId)
    if (!worktree) {
      throw new Error('active worktree not found')
    }
    const status = await window.api.git.status({ worktreePath: worktree.path })
    state.setGitStatus(worktree.id, status)
    return status.entries.map((entry) => entry.path)
  })
}

async function deleteUntrackedFileFromRow(row: Locator): Promise<void> {
  const deleteButton = row.getByRole('button', { name: 'Delete untracked file' })
  // Use keyboard activation for hover/focus row actions to avoid CI hit-test drift.
  await deleteButton.focus()
  await expect(deleteButton).toBeFocused()
  await deleteButton.press('Enter')
}

async function confirmPendingDelete(page: Page): Promise<void> {
  // Target the dialog’s auto-focused confirm; Enter on the original row button only reopens it.
  const confirmButton = page.getByRole('button', { name: 'Delete' }).last()
  await expect(confirmButton).toBeVisible()
  await confirmButton.click()
}

async function expectDeleteDialogLayout(page: Page, fileName: string): Promise<void> {
  const dialog = page.getByRole('dialog', { name: `Delete "${fileName}"?` })
  await expect(dialog).toBeVisible()
  await expect
    .poll(
      async () =>
        dialog.evaluate((element) => {
          const panel = element.getBoundingClientRect()
          const title = element.querySelector<HTMLElement>('[data-slot="dialog-title"]')
          const footer = element.querySelector<HTMLElement>('[data-slot="dialog-footer"]')
          if (!title || !footer) {
            return false
          }
          const titleRect = title.getBoundingClientRect()
          const footerRect = footer.getBoundingClientRect()
          const lineHeight = Number.parseFloat(getComputedStyle(title).lineHeight) || 16
          const buttonsFit = [...footer.querySelectorAll<HTMLElement>('button')].every((button) => {
            const rect = button.getBoundingClientRect()
            return rect.left >= panel.left && rect.right <= panel.right
          })
          return (
            titleRect.height > lineHeight * 1.5 &&
            titleRect.left >= panel.left &&
            titleRect.right <= panel.right &&
            footerRect.left >= panel.left &&
            footerRect.right <= panel.right &&
            buttonsFit &&
            element.scrollWidth <= element.clientWidth
          )
        }),
      { timeout: 5_000, message: 'long delete-dialog title or footer escaped the panel' }
    )
    .toBe(true)
}

test.describe('Source Control discard confirmation', () => {
  test.beforeEach(async ({ orcaPage }) => {
    await waitForSessionReady(orcaPage)
    await waitForActiveWorktree(orcaPage)
  })

  test('keeps long untracked-file confirmation usable and deletes on confirm', async ({
    orcaPage
  }) => {
    const seededFile = await seedUntrackedFile(
      orcaPage,
      `orca-discard-confirm-${'x'.repeat(96)}.txt`
    )
    await openSourceControl(orcaPage)

    const row = orcaPage
      .locator('[data-testid="source-control-entry"]')
      .filter({ hasText: seededFile.fileName })
    await expect(row).toBeVisible()

    await deleteUntrackedFileFromRow(row)
    await expectDeleteDialogLayout(orcaPage, seededFile.fileName)

    await orcaPage.getByRole('button', { name: 'Cancel' }).click()
    await expect(row).toBeVisible()

    await deleteUntrackedFileFromRow(row)
    await confirmPendingDelete(orcaPage)

    await expect(
      orcaPage.getByRole('dialog', { name: `Delete "${seededFile.fileName}"?` })
    ).toHaveCount(0)
    await expect(row).toHaveCount(0, { timeout: 10_000 })

    await refreshGitStatus(orcaPage)
    await expect(
      orcaPage.locator('[data-testid="source-control-entry"]').filter({
        hasText: seededFile.fileName
      })
    ).toHaveCount(0)
  })
})