// Drive conversion through a saved Docker SSH profile, then exercise managed-terminal journeys through production UI paths. import type { ElectronApplication, Page, TestInfo } from '@stablyai/playwright-test' import { expect, test } from './orca-app' import { createRestartSession } from './orca-restart' import type { LaunchOptions } from './orca-restart-launch-options' import { startOrcadConvertHost, type OrcadConvertHost } from './orcad-convert-host' import { seedRelayEraProfile, type RelayEraProfile } from './orcad-upgrade-profile' import { convertAndRetain } from './orcad-convert-flow' import { readPairedPaneContent } from './paired-host-terminal' import { waitForSessionReady, waitForStartupWorktreeRefresh } from './store' import { dismissTransientAnnouncement } from './ssh-config-host-picker' import { focusActiveTerminalInput } from './terminal' import { toRuntimeExecutionHostId } from '../../../src/shared/execution-host' import { getManagedOrcadSshTargetId } from '../../../src/shared/execution-host/managed-orcad-execution-host' export const ORCAD_TEMPLATE = process.env.ORCA_E2E_ORCAD_CONVERT_TEMPLATE export function skipUnlessManagedDockerHost(): void { test.skip( !ORCAD_TEMPLATE || process.env.ORCA_E2E_SSH_DOCKER !== '1', 'Needs Docker and server template' ) } export type ManagedHostFixture = { host: OrcadConvertHost session: ReturnType /** Set by the body to whichever launch is live, so teardown closes it. */ app: ElectronApplication | null } export async function withManagedHost( testInfo: TestInfo, body: (fixture: ManagedHostFixture) => Promise, /** A writable template copy, for a spec that rebuilds it as an app update would. */ templatePath: string = ORCAD_TEMPLATE! ): Promise { const host = startOrcadConvertHost('docker', testInfo) const session = createRestartSession(testInfo, { ORCA_ORCAD_TEMPLATE_PATH: templatePath }) const fixture: ManagedHostFixture = { host, session, app: null } try { await body(fixture) } finally { try { await session.dispose(fixture.app ?? undefined) } finally { host.cleanup() } } } export type ManagedWorkspace = { app: ElectronApplication page: Page seeded: RelayEraProfile environmentId: string } export type ManagedProfileOptions = { /** Overrides the host's repository, e.g. a same-path desktop collision. */ repoPath?: string /** Overrides the host's folder workspace, e.g. to keep /tmp outside every workspace. */ folderPath?: string /** Passed through to the relay-era seed, e.g. an unsaved editor draft. */ seed?: Pick[3]>, 'draftContent'> /** Runs on the profile-creating launch, before the SSH profile is seeded. */ beforeSeed?: (page: Page) => Promise /** Runs as soon as the converting launch starts, before the session is ready. */ onLaunch?: (page: Page, seeded: RelayEraProfile) => void | Promise } /** Seeds a relay-era profile and converts it; each launch is recorded on `fixture` for teardown. */ export async function launchConvertedProfile( fixture: ManagedHostFixture, options: ManagedProfileOptions = {} ): Promise { const { host, session } = fixture // The first launch only creates the profile the saved SSH host is written into. const first = await session.launch() fixture.app = first.app await waitForSessionReady(first.page) await options.beforeSeed?.(first.page) await session.close(first.app) fixture.app = null const seeded = seedRelayEraProfile( session.userDataDir, host.input, { repoPath: options.repoPath ?? host.remoteRepoPath, folderPath: options.folderPath ?? host.remoteFolderPath }, options.seed ) const { app, page } = await session.launch() fixture.app = app await options.onLaunch?.(page, seeded) await waitForSessionReady(page) await convertAndRetain(page, session.userDataDir, seeded) const environmentId = await managedEnvironmentId(page, seeded.targetId) return { app, page, seeded, environmentId } } export async function launchManagedWorkspace( fixture: ManagedHostFixture, options: ManagedProfileOptions = {} ): Promise { const workspace = await launchConvertedProfile(fixture, options) await activateManagedWorkspace( workspace.page, workspace.seeded.worktreeId, workspace.environmentId ) return workspace } export async function relaunchManagedApp( fixture: ManagedHostFixture, onLaunch?: (page: Page) => void | Promise, launchOptions?: LaunchOptions ): Promise { if (!fixture.app) { throw new Error('No live launch to close') } await fixture.session.close(fixture.app) fixture.app = null const { app, page } = await fixture.session.launch(launchOptions) fixture.app = app await onLaunch?.(page) await waitForSessionReady(page) await waitForStartupWorktreeRefresh(page) return page } export async function managedEnvironmentId(page: Page, targetId: string): Promise { const environment = (await page.evaluate(() => window.api.runtimeEnvironments.list())).find( (entry) => getManagedOrcadSshTargetId(entry) === targetId ) if (!environment) { throw new Error(`No managed server registered for ${targetId}`) } return environment.id } export async function activateManagedWorkspace( page: Page, worktreeId: string, environmentId: string ): Promise { await page.evaluate( ({ worktreeId, hostId }) => { const state = window.__store!.getState() state.setActiveView('terminal') state.setActiveWorktree(worktreeId, hostId) }, { worktreeId, hostId: toRuntimeExecutionHostId(environmentId) } ) await dismissTransientAnnouncement(page) } export function terminalTabIds(page: Page, worktreeId: string): Promise { return page.evaluate( (id) => (window.__store?.getState().tabsByWorktree[id] ?? []).map((tab) => tab.id), worktreeId ) } export async function openTerminalTab(page: Page, worktreeId: string): Promise { const before = new Set(await terminalTabIds(page, worktreeId)) await page.evaluate(async (worktreeId) => { const state = window.__store!.getState() const groupId = state.activeGroupIdByWorktree[worktreeId] if (!groupId) { throw new Error(`No active tab group for ${worktreeId}`) } await state.openNewTerminalTabInActiveWorkspace(groupId) }, worktreeId) let created: string | undefined await expect .poll( async () => { created = (await terminalTabIds(page, worktreeId)).find((id) => !before.has(id)) return created ?? null }, { timeout: 60_000, message: 'the new terminal tab never appeared' } ) .not.toBeNull() await waitForPane(page, created!) // No prompt wait: keys typed before the first prompt must reach the shell, as for a user. return created! } export async function waitForPane(page: Page, tabId: string): Promise { await expect .poll(() => page.evaluate((id) => window.__paneManagers?.has(id) ?? false, tabId), { timeout: 60_000, message: `pane for ${tabId} did not mount` }) .toBe(true) } export function selectTerminalTab(page: Page, tabId: string): Promise { return page.evaluate((id) => { const state = window.__store!.getState() state.setActiveTab(id) state.setActiveTabType('terminal', state.activeWorktreeId) }, tabId) } export async function typeInTerminal(page: Page, tabId: string, command: string): Promise { await selectTerminalTab(page, tabId) await waitForPane(page, tabId) await focusActiveTerminalInput(page) await page.keyboard.insertText(command) await page.keyboard.press('Enter') } export async function expectPaneText( page: Page, tabId: string, text: string, timeout = 60_000 ): Promise { await expect .poll(() => readPairedPaneContent(page, tabId), { timeout, message: `pane ${tabId} never showed ${text}` }) .toContain(text) } export async function echoMarker( page: Page, tabId: string, prefix: string, timeout = 60_000 ): Promise { const marker = `${prefix}_${Date.now()}` // Quoted apart so the typed command line itself can never satisfy the check. await typeInTerminal(page, tabId, `echo ${prefix}''_${marker.slice(prefix.length + 1)}`) await expectPaneText(page, tabId, marker, timeout) return marker } /** Proves the shell reads input again: retypes a fresh marker until one is echoed back. */ export async function expectTerminalAnswers( page: Page, tabId: string, timeout = 120_000 ): Promise { await expect(async () => { await echoMarker(page, tabId, 'ANSWER', 10_000) }).toPass({ timeout }) }