From cf47e79147da6b8fde6f078a8a2600731e655db0 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:25:30 -0800 Subject: [PATCH] test(e2e): isolate same-path editor models across two real hosts (#39208) The spec forged an SSH owner for a local file and relied on the read falling back to local transport. Since #31617 a tab reads only from its captured owner, so the forged tab correctly shows 'Couldn't verify which host owns this file'. Use a paired client that opens the same checkout locally and on its runtime host instead, and route editor model identity sources to the spec. --- .../pr-editor-host-model-e2e-route.test.mjs | 20 ++ .../pr-editor-preview-e2e-source-routes.mjs | 13 ++ tests/e2e/editor-model-host-isolation.spec.ts | 215 ++++++++++-------- 3 files changed, 155 insertions(+), 93 deletions(-) create mode 100644 config/scripts/workflow/source-routing/pr-editor-host-model-e2e-route.test.mjs diff --git a/config/scripts/workflow/source-routing/pr-editor-host-model-e2e-route.test.mjs b/config/scripts/workflow/source-routing/pr-editor-host-model-e2e-route.test.mjs new file mode 100644 index 00000000000..ce279521c96 --- /dev/null +++ b/config/scripts/workflow/source-routing/pr-editor-host-model-e2e-route.test.mjs @@ -0,0 +1,20 @@ +import { describe, expect, it } from 'vitest' +import { selectPrE2eSpecs } from './pr-e2e-source-routing.mjs' + +describe('editor host model identity E2E routing', () => { + const spec = 'tests/e2e/editor-model-host-isolation.spec.ts' + it.each([ + 'src/renderer/src/components/editor/model-lifetime/identity/editor-model-uri.ts', + 'src/renderer/src/components/editor/useEditorPanelFileContentLoader.ts', + 'src/renderer/src/lib/editor-file-operation-provenance.ts' + ])('routes %s', (path) => { + expect(selectPrE2eSpecs([path])).toContain(spec) + }) + it('does not route unit tests of those sources', () => { + expect( + selectPrE2eSpecs([ + 'src/renderer/src/components/editor/model-lifetime/identity/editor-model-uri.test.ts' + ]) + ).not.toContain(spec) + }) +}) diff --git a/config/scripts/workflow/source-routing/pr-editor-preview-e2e-source-routes.mjs b/config/scripts/workflow/source-routing/pr-editor-preview-e2e-source-routes.mjs index 0e2e418ca10..a6a96aca2cd 100644 --- a/config/scripts/workflow/source-routing/pr-editor-preview-e2e-source-routes.mjs +++ b/config/scripts/workflow/source-routing/pr-editor-preview-e2e-source-routes.mjs @@ -3,6 +3,19 @@ import { isUnitTestSupportSource } from './pr-code-change-scope.mjs' const isProductSource = (file) => !/\.test\.tsx?$/.test(file) && !isUnitTestSupportSource(file) export const PR_EDITOR_PREVIEW_E2E_SOURCE_ROUTES = [ + { + // Same-path files on two hosts must keep separate Monaco models, and each tab reads from its captured owner. + id: 'editor.host-model-identity', + specs: ['tests/e2e/editor-model-host-isolation.spec.ts'], + matches: (file) => + isProductSource(file) && + (file.startsWith('src/renderer/src/components/editor/model-lifetime/identity/') || + [ + 'src/renderer/src/components/editor/useEditorPanelFileContentLoader.ts', + 'src/renderer/src/lib/editor-file-operation-owner.ts', + 'src/renderer/src/lib/editor-file-operation-provenance.ts' + ].includes(file)) + }, { // Image, PDF and media viewers read orca-media grants; the PDF reloads exercise remint and range reads. id: 'editor.byte-range-preview', diff --git a/tests/e2e/editor-model-host-isolation.spec.ts b/tests/e2e/editor-model-host-isolation.spec.ts index 3566b92818d..76afb565bc4 100644 --- a/tests/e2e/editor-model-host-isolation.spec.ts +++ b/tests/e2e/editor-model-host-isolation.spec.ts @@ -1,110 +1,139 @@ import { randomUUID } from 'node:crypto' import { rm, writeFile } from 'node:fs/promises' import path from 'node:path' +import type { Page } from '@stablyai/playwright-test' import { test, expect } from './helpers/orca-app' -import { getActiveWorktreeContext } from './helpers/markdown-editor-fixture' +import { + createRuntimeDesktopPairingOffer, + launchPairedElectronClient, + type PairedElectronClient +} from './helpers/paired-electron-client' import { waitForActiveWorktree, waitForSessionReady } from './helpers/store' -import { buildOwnedEditorFileId } from '../../src/renderer/src/store/slices/editor/file-ids/editor-file-ids' +async function openWorkspaceFile( + page: Page, + file: { + filePath: string + worktreeId: string + executionHostId: 'local' | `runtime:${string}` + runtimeEnvironmentId: string | null + } +): Promise { + return page.evaluate(({ filePath, worktreeId, executionHostId, runtimeEnvironmentId }) => { + const store = window.__store + if (!store) { + throw new Error('Editor store unavailable') + } + store.getState().setActiveWorktree(worktreeId, executionHostId) + return store.getState().openFile({ + filePath, + relativePath: filePath.split(/[\\/]/).pop() ?? filePath, + worktreeId, + runtimeEnvironmentId, + language: 'plaintext', + mode: 'edit' + }) + }, file) +} + +// The same checkout path is open on the paired client's own host and on the runtime host it is paired with. test('keeps same-path host models and undo history separate', async ({ orcaPage, + testRepoPath, registerPostElectronShutdownCleanup }, testInfo) => { + // Launching and pairing a second Electron client takes most of this budget. + test.setTimeout(240_000) await waitForSessionReady(orcaPage) await waitForActiveWorktree(orcaPage) - const context = await getActiveWorktreeContext(orcaPage) - const filePath = path.join(context.rootPath, `host-model-${randomUUID()}.txt`) - await writeFile(filePath, 'LOCAL_MARKER', 'utf8') + const filePath = path.join(testRepoPath, `host-model-${randomUUID()}.txt`) + await writeFile(filePath, 'MARKER', 'utf8') registerPostElectronShutdownCleanup(() => rm(filePath, { force: true })) - const remoteId = buildOwnedEditorFileId(filePath, context.worktreeId, 'host-model-fixture') - // Open through the user path so main owns both tabs; key the second runtime separately before redirecting its unselected tab to local transport. - const openedIds = await orcaPage.evaluate( - ({ filePath, worktreeId }) => { - const store = window.__store - if (!store) { - throw new Error('Editor store unavailable') - } - const generation = { - runtimeConnectionGeneration: null, - runtimePairingRevision: undefined, - runtimeSshGeneration: null, - nestedSshGeneration: null, - directSshGeneration: null - } - const provenance = ( - executionHostId: 'local' | 'ssh:fixture-target', - runtimeEnvironmentId: string | null - ) => ({ - ownershipProjection: 'explicit' as const, - generation: { ...generation, route: { executionHostId, runtimeEnvironmentId } } - }) - const file = { - filePath, - relativePath: filePath.split(/[\\/]/).pop() ?? filePath, - worktreeId, - language: 'plaintext', - mode: 'edit' as const - } - const localId = store.getState().openFile({ - ...file, - runtimeEnvironmentId: null, - operationProvenance: provenance('local', null) - }) - const remoteId = store.getState().openFile( - { - ...file, - runtimeEnvironmentId: 'host-model-fixture', - operationProvenance: provenance('ssh:fixture-target', 'host-model-fixture') - }, - { selection: 'none' } + let client: PairedElectronClient | null = null + try { + client = await launchPairedElectronClient( + await createRuntimeDesktopPairingOffer(orcaPage), + testInfo, + 'Host model isolation' + ) + const { page, environmentId } = client + const findRemoteWorktreeId = (): Promise => + page.evaluate( + (repoPath) => + window.__store + ?.getState() + .allWorktrees() + .find((worktree) => worktree.path === repoPath)?.id ?? null, + testRepoPath ) - store.getState().markFileDirty(localId, true) - store.getState().markFileDirty(remoteId, true) - store.setState((state) => ({ - openFiles: state.openFiles.map((entry) => - entry.id === remoteId - ? { - ...entry, - runtimeEnvironmentId: undefined, - operationProvenance: provenance('ssh:fixture-target', null) - } - : entry - ), - editorDrafts: { - ...state.editorDrafts, - [localId]: 'LOCAL_MARKER', - [remoteId]: 'REMOTE_MARKER' - } - })) - store.getState().setActiveFile(localId) - return { localId, remoteId } - }, - { filePath, worktreeId: context.worktreeId } - ) - expect(openedIds).toEqual({ localId: filePath, remoteId }) + await expect.poll(findRemoteWorktreeId, { timeout: 120_000 }).not.toBeNull() + const remoteWorktreeId = await findRemoteWorktreeId() + if (!remoteWorktreeId) { + throw new Error('Paired client never received the host worktree') + } + const localWorktreeId = await page.evaluate(async (repoPath) => { + const store = window.__store! + await store.getState().setActiveRuntimeEnvironmentPreference(null) + const added = await window.api.repos.add({ path: repoPath }) + if ('error' in added) { + throw new Error(added.error) + } + await store.getState().fetchReposForAllHosts() + await store.getState().fetchWorktrees(added.repo.id, { forceLocalOwner: true }) + store.getState().setVisibleWorkspaceHostIds(null) + const worktree = store + .getState() + .worktreesByRepo[added.repo.id]?.find((row) => row.isMainWorktree) + if (!worktree) { + throw new Error('Local checkout missing') + } + return worktree.id + }, testRepoPath) + expect(localWorktreeId).not.toBe(remoteWorktreeId) + const local = { + filePath, + worktreeId: localWorktreeId, + executionHostId: 'local' as const, + runtimeEnvironmentId: null + } + const remote = { + filePath, + worktreeId: remoteWorktreeId, + executionHostId: `runtime:${environmentId}` as const, + runtimeEnvironmentId: environmentId + } - const editor = orcaPage.locator('.monaco-editor').first() - await expect(editor).toBeVisible({ timeout: 25_000 }) - const valueTail = () => orcaPage.evaluate(() => window.__monacoEditorE2E?.snapshot().valueTail) - await expect.poll(valueTail).toBe('LOCAL_MARKER') - await editor.click() - await orcaPage.keyboard.press('ControlOrMeta+End') - await orcaPage.keyboard.type('!') - await expect.poll(valueTail).toBe('LOCAL_MARKER!') - await orcaPage.evaluate((id) => window.__store?.getState().setActiveFile(id), remoteId) - await expect.poll(valueTail).toBe('REMOTE_MARKER') - await orcaPage.screenshot({ path: testInfo.outputPath('remote-before-undo.png') }) - await editor.click() - await orcaPage.keyboard.press('ControlOrMeta+z') - await orcaPage.screenshot({ path: testInfo.outputPath('remote-after-undo.png') }) - await expect.poll(valueTail).toBe('REMOTE_MARKER') - await orcaPage.keyboard.press('ControlOrMeta+End') - await orcaPage.keyboard.type('?') - await expect.poll(valueTail).toBe('REMOTE_MARKER?') - await orcaPage.keyboard.press('ControlOrMeta+z') - await expect.poll(valueTail).toBe('REMOTE_MARKER') - await orcaPage.evaluate((id) => window.__store?.getState().setActiveFile(id), filePath) - await expect.poll(valueTail).toBe('LOCAL_MARKER!') - await orcaPage.screenshot({ path: testInfo.outputPath('local-retained-edit.png') }) + // Each workspace keeps its own editor mounted; a runtime-owned model gets a host-keyed URI instead of file:. + const localEditor = page.locator('.monaco-editor[data-uri^="file:"]').filter({ visible: true }) + const remoteEditor = page + .locator('.monaco-editor[data-uri^="vscode-remote:"]') + .filter({ visible: true }) + const localText = localEditor.locator('.view-lines') + const remoteText = remoteEditor.locator('.view-lines') + await openWorkspaceFile(page, local) + await expect(localText).toHaveText('MARKER', { timeout: 30_000 }) + await localEditor.click() + await page.keyboard.press('ControlOrMeta+End') + await page.keyboard.type('!') + await expect(localText).toHaveText('MARKER!') + + await openWorkspaceFile(page, remote) + await expect(remoteText).toHaveText('MARKER', { timeout: 30_000 }) + await page.screenshot({ path: testInfo.outputPath('remote-before-undo.png') }) + await remoteEditor.click() + await page.keyboard.press('ControlOrMeta+z') + await expect(remoteText).toHaveText('MARKER') + await page.keyboard.press('ControlOrMeta+End') + await page.keyboard.type('?') + await expect(remoteText).toHaveText('MARKER?') + await page.keyboard.press('ControlOrMeta+z') + await expect(remoteText).toHaveText('MARKER') + + await openWorkspaceFile(page, local) + await expect(localText).toHaveText('MARKER!') + await page.screenshot({ path: testInfo.outputPath('local-retained-edit.png') }) + } finally { + await client?.dispose() + } })