Files
orca/tests/e2e/diff-context-copy.spec.ts
T

69 lines
3.0 KiB
TypeScript

import { rmSync, writeFileSync } from 'node:fs'
import { test, expect } from './helpers/orca-app'
import { waitForSessionReady } from './helpers/store'
import { addAndActivateRepo } from './helpers/isolated-repo-activation'
import { createIsolatedLargeDiffRepo } from './large-diff-repro-fixtures'
test.use({ seedTestRepo: false })
test('copies backwards selections with file and line context from each diff side', async ({
orcaPage,
electronApp,
registerPostElectronShutdownCleanup
}) => {
const original = 'export const one = 1\nexport const two = 2\nexport const three = 3\n'
const fixture = createIsolatedLargeDiffRepo(original)
registerPostElectronShutdownCleanup(async () =>
rmSync(fixture.repoPath, { recursive: true, force: true })
)
const modified = original.replaceAll('= ', '= 9')
writeFileSync(fixture.absolutePath, modified)
await waitForSessionReady(orcaPage)
await addAndActivateRepo(orcaPage, fixture.repoPath)
await orcaPage.evaluate(() =>
window.__store!.getState().updateSettings({ diffDefaultView: 'side-by-side' })
)
await orcaPage.getByRole('button', { name: /^Source Control/ }).click()
await orcaPage.locator('[data-testid="source-control-entry"]').first().click()
await expect(orcaPage.locator('diffs-container [data-content] [data-line]').first()).toBeVisible({
timeout: 20_000
})
const previousClipboard = await electronApp.evaluate(({ clipboard }) => clipboard.readText())
let copied = ''
try {
for (const side of ['deletions', 'additions']) {
const coordinates = await orcaPage.locator('diffs-container').evaluate((host, side) => {
const rows = [
...host.shadowRoot!.querySelectorAll(`[data-${side}] [data-content] [data-line]`)
]
const rect = (row: Element) => {
const range = document.createRange()
range.selectNodeContents(row)
const bounds = range.getBoundingClientRect()
return { left: bounds.left, right: bounds.right, y: bounds.top + bounds.height / 2 }
}
return { start: rect(rows[0]), end: rect(rows.at(-1)) }
}, side)
await orcaPage.mouse.move(coordinates.end.right, coordinates.end.y)
await orcaPage.mouse.down()
await orcaPage.mouse.move(coordinates.start.left, coordinates.start.y, { steps: 8 })
await orcaPage.mouse.up()
await orcaPage.keyboard.press('ControlOrMeta+Alt+c')
const contents = side === 'deletions' ? original : modified
copied = `File: ${fixture.relativePath}\nLines: 1-3\n\n\`\`\`ts\n${contents}\`\`\``
await expect
.poll(() => electronApp.evaluate(({ clipboard }) => clipboard.readText()))
.toBe(copied)
await expect(orcaPage.getByText('Context copied', { exact: true }).first()).toBeVisible()
}
} finally {
await electronApp.evaluate(
({ clipboard }, { copied, previousClipboard }) => {
if (clipboard.readText() === copied) {
clipboard.writeText(previousClipboard)
}
},
{ copied, previousClipboard }
)
}
})