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.

4.5 KB
Raw Blame
// With no editor window, serve must read/save Markdown from disk and compare saves against the phone’s last-read version.
import { readFileSync, writeFileSync } from 'node:fs'
import path from 'node:path'
import type { RuntimeClient } from '../../src/cli/runtime/client'
import type { RuntimeMobileSessionTabsResult } from '../../src/shared/runtime-session-contracts'
import type {
  RuntimeMarkdownReadTabResult,
  RuntimeMarkdownSaveTabResult
} from '../../src/shared/mobile-markdown-document'
import { expect, test } from './helpers/orca-app'
import {
  addRepoWorktree,
  cliClient,
  orcadNodeExecutable,
  pairedClient,
  startHeadlessOracleHost,
  type HeadlessHostKind
} from './helpers/headless-layout-oracle-hosts'
import { mutateStoppedProfileState } from './helpers/persisted-profile-state'

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === 'object' && value !== null && !Array.isArray(value)
}

function saveMarkdownEditor(userDataDir: string, worktreeId: string, filePath: string): void {
  mutateStoppedProfileState(userDataDir, (state) => {
    const session = state.workspaceSession
    if (!isRecord(session)) {
      throw new Error('The profile has no workspace session')
    }
    session.openFilesByWorktree = {
      ...(isRecord(session.openFilesByWorktree) ? session.openFilesByWorktree : {}),
      [worktreeId]: [
        {
          filePath,
          relativePath: path.basename(filePath),
          worktreeId,
          language: 'markdown',
          runtimeEnvironmentId: null
        }
      ]
    }
  })
}

async function markdownTabId(phone: RuntimeClient, worktree: string): Promise<string> {
  let tabId = ''
  await expect
    .poll(
      async () => {
        const { result } = await phone.call<RuntimeMobileSessionTabsResult>('session.tabs.list', {
          worktree
        })
        tabId = result.tabs.find((tab) => tab.type === 'markdown')?.id ?? ''
        return tabId
      },
      { timeout: 30_000, message: 'the server never listed the saved Markdown tab' }
    )
    .not.toBe('')
  return tabId
}

async function readTab(phone: RuntimeClient, worktree: string, tabId: string) {
  return (await phone.call<RuntimeMarkdownReadTabResult>('markdown.readTab', { worktree, tabId }))
    .result
}

const HOSTS: HeadlessHostKind[] = ['electron', 'orcad']

for (const kind of HOSTS) {
  test(`a phone reads and saves a Markdown tab on a headless ${kind} server`, async () => {
    // Why skip in CI too: only the orcad mode-switch job builds out/orcad.
    test.skip(kind === 'orcad' && !orcadNodeExecutable(), 'out/orcad is not built')
    test.setTimeout(240_000)
    const host = await startHeadlessOracleHost(kind)
    try {
      const cli = cliClient(host)
      const phone = pairedClient(host, 'phone')
      const worktreeId = await addRepoWorktree(host, cli)
      const worktree = `id:${worktreeId}`
      const listed = await cli.call<{ worktrees: { id: string; path: string }[] }>(
        'worktree.list',
        { repo: `id:${worktreeId.split('::')[0]}` }
      )
      const repoPath = listed.result.worktrees.find((entry) => entry.id === worktreeId)!.path
      const filePath = path.join(repoPath, 'README.md')
      await host.restart({
        whileStopped: () => saveMarkdownEditor(host.userDataDir, worktreeId, filePath)
      })

      const tabId = await markdownTabId(phone, worktree)
      const read = await readTab(phone, worktree, tabId)
      expect(read).toMatchObject({
        tabId,
        filePath,
        content: 'layout oracle\n',
        source: 'file',
        isDirty: false,
        editable: true
      })

      const saved = (
        await phone.call<RuntimeMarkdownSaveTabResult>('markdown.saveTab', {
          worktree,
          tabId,
          baseVersion: read.version,
          content: '# from the phone\n'
        })
      ).result
      expect(saved).toMatchObject({ tabId, isDirty: false, content: '# from the phone\n' })
      expect(readFileSync(filePath, 'utf8')).toBe('# from the phone\n')

      writeFileSync(filePath, '# edited on the server\n')
      await expect(
        phone.call('markdown.saveTab', {
          worktree,
          tabId,
          baseVersion: saved.version,
          content: '# stale phone edit\n'
        })
      ).rejects.toThrow('conflict')
      expect(readFileSync(filePath, 'utf8')).toBe('# edited on the server\n')
      expect((await readTab(phone, worktree, tabId)).content).toBe('# edited on the server\n')
    } finally {
      await host.dispose()
    }
  })
}