plugin-install-cancel.e2e.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // A real Host/Remote/browser composition with a controllable package-manager process.
  2. // No model call is needed: installation state and profile files are the observable result.
  3. import { mkdtemp, readFile, writeFile, rm } from 'node:fs/promises'
  4. import { tmpdir } from 'node:os'
  5. import { join } from 'node:path'
  6. import { fileURLToPath } from 'node:url'
  7. import { chromium } from 'playwright'
  8. import { expect, it } from 'vitest'
  9. import { launchWebScaffold, captureStableAria, compareOrRefreshGolden, webSnapshotMode, watchConsole, type WebScaffold } from './scaffold.ts'
  10. import { ZH_BROWSER_LOCALE } from './support.ts'
  11. it('cancels installation through the UI, restores files, and offers the spec again', async () => {
  12. const scratch = await mkdtemp(join(tmpdir(), 'dsh-install-cancel-'))
  13. const overlay = join(scratch, 'cordis.patch.yml')
  14. await writeFile(overlay, `- id: plugin-manager\n config: ${JSON.stringify({ pnpmCommand: process.execPath })}\n`)
  15. let scaffold: WebScaffold | undefined
  16. try {
  17. scaffold = await launchWebScaffold({ profile: { packages: [] }, extraOverlayPath: overlay })
  18. const browser = await chromium.launch()
  19. try {
  20. const profile = join(scaffold.harnessHome, 'profiles', 'scaffold')
  21. const manifestPath = join(profile, 'package.json')
  22. const manifest = await readFile(manifestPath, 'utf8')
  23. const lockPath = join(profile, 'pnpm-lock.yaml')
  24. await writeFile(lockPath, 'original lockfile\n')
  25. // Node stands in for the pnpm executable: `view` answers the check that precedes the run,
  26. // and the same installer owns and stops the real `add` child.
  27. await writeFile(join(profile, 'view'), 'console.log(JSON.stringify({ name: "slow-package", version: "1.0.0", dsh: { bundle: { patch: "./cordis.patch.yml" } } }))\n')
  28. await writeFile(join(profile, 'add'), `
  29. import('node:fs').then(fs => {
  30. fs.writeFileSync('package.json', JSON.stringify({ ...JSON.parse(fs.readFileSync('package.json', 'utf8')), dependencies: { partial: '1.0.0' } }));
  31. fs.writeFileSync('pnpm-lock.yaml', 'partial lockfile');
  32. console.log('Waiting for package download');
  33. setInterval(() => {}, 1000);
  34. });
  35. `)
  36. const page = await browser.newPage({ viewport: { width: 1440, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  37. const tripwire = watchConsole(page)
  38. await page.goto(scaffold.authenticatedUrl)
  39. await page.waitForSelector('[class*="frame"]')
  40. if (await page.getByRole('dialog', { name: '设置' }).count() > 0) await page.keyboard.press('Escape')
  41. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  42. const panel = page.locator('[data-plugin-panel]')
  43. await panel.getByRole('button', { name: '添加插件', exact: true }).click()
  44. // The dialog is named after its current screen, so it is found by role alone.
  45. const dialog = page.getByRole('dialog')
  46. await dialog.getByRole('textbox').fill('slow-package')
  47. await dialog.getByRole('button', { name: '安装', exact: true }).click()
  48. // The check passed: the running screen names the package and folds pnpm's output behind the details.
  49. await dialog.getByText('版本 1.0.0', { exact: true }).waitFor()
  50. await dialog.getByRole('button', { name: '查看安装详情', exact: true }).click()
  51. await dialog.getByText('Waiting for package download', { exact: true }).waitFor()
  52. await dialog.getByRole('button', { name: '取消安装', exact: true }).click()
  53. // The Host's confirmation returns the dialog to the spec and says so in a toast.
  54. await page.getByText('已取消安装,插件未启用,下载的文件可能保留', { exact: true }).waitFor()
  55. expect(await readFile(manifestPath, 'utf8')).toBe(manifest)
  56. expect(await readFile(lockPath, 'utf8')).toBe('original lockfile\n')
  57. expect(await dialog.getByRole('textbox').inputValue()).toBe('slow-package')
  58. const snapshot = (await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd))
  59. .split(process.execPath).join('{{node}}')
  60. .split(scaffold.harnessHome).join('{{harnessHome}}')
  61. await compareOrRefreshGolden(fileURLToPath(new URL('./expected/plugin-install-cancel/cancelled.expected.md', import.meta.url)), snapshot, webSnapshotMode())
  62. // The second run installs a bundle the way pnpm leaves one: the dependency in the manifest and the package under node_modules.
  63. await writeFile(join(profile, 'add'), `
  64. import('node:fs').then(fs => {
  65. fs.mkdirSync('node_modules/slow-package', { recursive: true });
  66. fs.writeFileSync('node_modules/slow-package/package.json', JSON.stringify({ name: 'slow-package', version: '1.0.0', dsh: { bundle: { patch: './cordis.patch.yml' } } }));
  67. fs.writeFileSync('node_modules/slow-package/cordis.patch.yml', '[]\\n');
  68. fs.writeFileSync('package.json', JSON.stringify({ ...JSON.parse(fs.readFileSync('package.json', 'utf8')), dependencies: { 'slow-package': '1.0.0' } }));
  69. console.log('Retry completed');
  70. });
  71. `)
  72. await dialog.getByRole('button', { name: '安装', exact: true }).click()
  73. await dialog.getByRole('button', { name: '立即启用', exact: true }).waitFor()
  74. await dialog.getByRole('button', { name: '查看安装详情', exact: true }).click()
  75. await dialog.getByText('Retry completed', { exact: true }).waitFor()
  76. expect(JSON.parse(await readFile(manifestPath, 'utf8'))).toMatchObject({ dependencies: { 'slow-package': '1.0.0' } })
  77. expect(tripwire.pageErrors).toEqual([])
  78. } finally { await browser.close() }
  79. } finally {
  80. await scaffold?.close()
  81. await rm(scratch, { recursive: true, force: true })
  82. }
  83. })