Files
deepseek-harness/apps/web/tests/plugin-install-cancel.e2e.ts
T

67 lines
4.3 KiB
TypeScript

// A real Host/Remote/browser composition with a controllable package-manager process.
// No model call is needed: installation state and profile files are the observable result.
import { mkdtemp, readFile, writeFile, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
import { chromium } from 'playwright'
import { expect, it } from 'vitest'
import { launchWebScaffold, captureStableAria, compareOrRefreshGolden, webSnapshotMode, watchConsole, type WebScaffold } from './scaffold.ts'
import { ZH_BROWSER_LOCALE } from './support.ts'
it('cancels installation through the UI, restores files, and accepts a retry', async () => {
const scratch = await mkdtemp(join(tmpdir(), 'dsh-install-cancel-'))
const overlay = join(scratch, 'cordis.patch.yml')
await writeFile(overlay, `- id: plugin-manager\n config: ${JSON.stringify({ pnpmCommand: process.execPath, installTimeoutMs: 60_000, installKillGraceMs: 50 })}\n`)
let scaffold: WebScaffold | undefined
try {
scaffold = await launchWebScaffold({ profileRuntime: { packages: [] }, extraOverlayPath: overlay })
const browser = await chromium.launch()
try {
const profile = join(scaffold.harnessHome, 'profiles', 'scaffold')
const manifestPath = join(profile, 'package.json')
const manifest = await readFile(manifestPath, 'utf8')
const lockPath = join(profile, 'pnpm-lock.yaml')
await writeFile(lockPath, 'original lockfile\n')
// Node stands in for the pnpm executable; the same installer owns and stops this real child.
await writeFile(join(profile, 'add'), `
import('node:fs').then(fs => {
fs.writeFileSync('package.json', JSON.stringify({ ...JSON.parse(fs.readFileSync('package.json', 'utf8')), dependencies: { partial: '1.0.0' } }));
fs.writeFileSync('pnpm-lock.yaml', 'partial lockfile');
console.log('Waiting for package download');
setInterval(() => {}, 1000);
});
`)
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 }, locale: ZH_BROWSER_LOCALE })
const tripwire = watchConsole(page)
await page.goto(scaffold.authenticatedUrl)
await page.waitForSelector('[class*="frame"]')
if (await page.getByRole('dialog', { name: '设置' }).count() > 0) await page.keyboard.press('Escape')
await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
const panel = page.locator('[data-plugin-panel]')
await panel.getByRole('button', { name: '添加插件', exact: true }).click()
const dialog = page.getByRole('dialog', { name: '添加插件' })
await dialog.getByRole('textbox').fill('slow-package')
await dialog.getByRole('button', { name: '安装', exact: true }).click()
await dialog.getByText('Waiting for package download', { exact: true }).waitFor()
await dialog.getByRole('button', { name: '取消安装', exact: true }).click()
await dialog.getByText('已取消安装,本次未继续启用插件。下载缓存或已解包文件可能保留,需要时可重新安装。', { exact: true }).waitFor()
expect(await readFile(manifestPath, 'utf8')).toBe(manifest)
expect(await readFile(lockPath, 'utf8')).toBe('original lockfile\n')
expect(await dialog.getByRole('textbox').inputValue()).toBe('slow-package')
const snapshot = (await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd))
.split(process.execPath).join('{{node}}')
.split(scaffold.harnessHome).join('{{harnessHome}}')
await compareOrRefreshGolden(fileURLToPath(new URL('./expected/plugin-install-cancel/cancelled.expected.md', import.meta.url)), snapshot, webSnapshotMode())
await writeFile(join(profile, 'add'), 'console.log("Retry completed")\n')
await dialog.getByRole('button', { name: '重试', exact: true }).click()
await dialog.getByText('Retry completed', { exact: true }).waitFor()
await expect.poll(() => dialog.getByRole('button', { name: '完成', exact: true }).count()).toBeGreaterThan(0)
expect(tripwire.pageErrors).toEqual([])
} finally { await browser.close() }
} finally {
await scaffold?.close()
await rm(scratch, { recursive: true, force: true })
}
})