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.8 KB
Raw Blame
// Project switches must retain workspace-scoped Jira/Linear issues and clear repo-scoped GitHub/GitLab sources.
// Drive the real ProjectCombobox → handleProjectChange → pill render path, including main’s repo resolution.

import { execFileSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import type { Page } from '@stablyai/playwright-test'
import { test, expect } from './helpers/orca-app'
import { waitForActiveWorktree, waitForSessionReady } from './helpers/store'
import type { LinkedWorkItemSummary } from '../../src/renderer/src/lib/new-workspace'
import type { TaskSourceContext } from '../../src/shared/task-source-context'

const SECOND_PROJECT_NAME = 'linked-item-second-project'

function runGit(repoPath: string, args: string[]): void {
  execFileSync('git', args, { cwd: repoPath, stdio: 'pipe' })
}

function createGitRepo(repoPath: string): void {
  mkdirSync(repoPath, { recursive: true })
  runGit(repoPath, ['init'])
  runGit(repoPath, ['config', 'user.email', '[email protected]'])
  runGit(repoPath, ['config', 'user.name', 'E2E Test'])
  writeFileSync(path.join(repoPath, 'README.md'), '# Linked item project switch E2E\n')
  runGit(repoPath, ['add', '-A'])
  runGit(repoPath, ['commit', '-m', 'Initial commit'])
}

async function addSecondProject(page: Page, repoPath: string): Promise<void> {
  await page.evaluate(async (targetRepoPath) => {
    const store = window.__store
    if (!store) {
      throw new Error('window.__store is not available')
    }
    const addedRepo = await store.getState().addRepoPath(targetRepoPath)
    if (!addedRepo) {
      throw new Error(`Failed to add repo at ${targetRepoPath}`)
    }
  }, repoPath)
}

async function openComposerWithLinkedWorkItem(
  page: Page,
  linkedWorkItem: LinkedWorkItemSummary,
  prefilledName: string,
  taskSourceContext: TaskSourceContext | null = null
): Promise<void> {
  await page.evaluate(
    ({ linkedWorkItem, prefilledName, taskSourceContext }) => {
      const store = window.__store
      if (!store) {
        throw new Error('window.__store is not available')
      }
      store.getState().openModal('new-workspace-composer', {
        linkedWorkItem,
        prefilledName,
        taskSourceContext
      })
    },
    { linkedWorkItem, prefilledName, taskSourceContext }
  )
}

async function getJiraSourceContext(page: Page): Promise<TaskSourceContext> {
  return page.evaluate(() => {
    const state = window.__store?.getState()
    const activeWorktreeId = state?.activeWorktreeId
    const setup = state?.projectHostSetups.find((candidate) =>
      state.worktreesByRepo[candidate.repoId]?.some((worktree) => worktree.id === activeWorktreeId)
    )
    if (!setup) {
      throw new Error('Active project host setup is unavailable')
    }
    return {
      kind: 'task-source',
      provider: 'jira',
      projectId: setup.projectId,
      hostId: setup.hostId,
      projectHostSetupId: setup.id,
      repoId: setup.repoId,
      providerIdentity: {
        provider: 'jira',
        siteId: 'e2e-jira-site',
        siteUrl: 'https://example.atlassian.net',
        projectKey: 'RDG'
      }
    }
  })
}

async function switchComposerProject(page: Page, projectName: string): Promise<void> {
  const composer = page.getByRole('dialog')
  const combobox = composer.getByRole('combobox', { name: 'Project' })
  const comboboxRoot = composer.locator('div[data-project-combobox-root="true"]')
  await combobox.click()
  await page.getByRole('option', { name: new RegExp(projectName) }).click()
  await expect(comboboxRoot).toContainText(projectName)
}

test.describe('New workspace composer linked item across project switches', () => {
  let tempRoot: string
  let secondRepoPath: string

  test.beforeEach(async ({ orcaPage }) => {
    await waitForSessionReady(orcaPage)
    await waitForActiveWorktree(orcaPage)
    tempRoot = mkdtempSync(path.join(os.tmpdir(), 'orca-e2e-linked-item-'))
    secondRepoPath = path.join(tempRoot, SECOND_PROJECT_NAME)
    createGitRepo(secondRepoPath)
    await addSecondProject(orcaPage, secondRepoPath)
  })

  test.afterEach(() => {
    rmSync(tempRoot, { recursive: true, force: true })
  })

  test('keeps a Jira issue linked when the project changes', async ({ orcaPage }) => {
    const jiraSourceContext = await getJiraSourceContext(orcaPage)
    await openComposerWithLinkedWorkItem(
      orcaPage,
      {
        type: 'issue',
        provider: 'jira',
        number: 0,
        title: 'RDG-344 Migrate homepage from NuxtJS to NextJS',
        url: 'https://example.atlassian.net/browse/RDG-344',
        jiraIdentifier: 'RDG-344'
      },
      'rdg-344-nuxtjs-nextjs',
      jiraSourceContext
    )

    const composer = orcaPage.getByRole('dialog')
    await expect(composer).toBeVisible()
    const sourcePill = composer.locator('[data-workspace-source-pill="true"]')
    await expect(sourcePill).toContainText('RDG-344 Migrate homepage from NuxtJS to NextJS')

    await switchComposerProject(orcaPage, SECOND_PROJECT_NAME)

    await expect(sourcePill).toContainText('RDG-344 Migrate homepage from NuxtJS to NextJS')
  })

  test('clears a repo-scoped GitHub issue when the project changes', async ({ orcaPage }) => {
    await openComposerWithLinkedWorkItem(
      orcaPage,
      {
        type: 'issue',
        provider: 'github',
        number: 41,
        title: 'Fix crash on launch',
        url: 'https://github.com/acme/app/issues/41'
      },
      'fix-crash-on-launch'
    )

    const composer = orcaPage.getByRole('dialog')
    await expect(composer).toBeVisible()
    const sourcePill = composer.locator('[data-workspace-source-pill="true"]')
    await expect(sourcePill).toContainText('#41 Fix crash on launch')

    await switchComposerProject(orcaPage, SECOND_PROJECT_NAME)

    await expect(sourcePill).toHaveCount(0)
  })
})