plugin-install-approve.e2e.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // A real Host/Remote/browser composition with a controllable package-manager process:
  2. // pnpm leaves an install script undecided, the dialog offers approval, and the retry runs with it.
  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('offers approval for blocked install scripts and installs once they are allowed', async () => {
  12. const scratch = await mkdtemp(join(tmpdir(), 'dsh-install-approve-'))
  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 policyPath = join(profile, 'pnpm-workspace.yaml')
  22. // Node stands in for pnpm: `view` answers the check, and `add` first leaves the script undecided in the
  23. // profile policy the way pnpm 11 does and fails; once the policy allows it, the same `add` installs the package.
  24. await writeFile(join(profile, 'view'), 'console.log(JSON.stringify({ name: "native-package", version: "1.0.0", dsh: { bundle: { patch: "./cordis.patch.yml" } } }))\n')
  25. await writeFile(join(profile, 'add'), `
  26. import('node:fs').then(fs => {
  27. const policy = fs.readFileSync('pnpm-workspace.yaml', 'utf8');
  28. if (!/native-package: true/.test(policy)) {
  29. const pending = ' native-package: set this to true or false\\n';
  30. fs.writeFileSync('pnpm-workspace.yaml', /^allowBuilds:/m.test(policy)
  31. ? policy.replace(/^allowBuilds:\\n/m, 'allowBuilds:\\n' + pending)
  32. : policy + 'allowBuilds:\\n' + pending);
  33. console.error('ERR_PNPM_IGNORED_BUILDS Ignored build scripts: native-package');
  34. process.exitCode = 1;
  35. return;
  36. }
  37. fs.mkdirSync('node_modules/native-package', { recursive: true });
  38. fs.writeFileSync('node_modules/native-package/package.json', JSON.stringify({ name: 'native-package', version: '1.0.0', dsh: { bundle: { patch: './cordis.patch.yml' } } }));
  39. fs.writeFileSync('node_modules/native-package/cordis.patch.yml', '[]\\n');
  40. fs.writeFileSync('package.json', JSON.stringify({ ...JSON.parse(fs.readFileSync('package.json', 'utf8')), dependencies: { 'native-package': '1.0.0' } }));
  41. console.log('Built native-package');
  42. });
  43. `)
  44. const page = await browser.newPage({ viewport: { width: 1440, height: 1000 }, locale: ZH_BROWSER_LOCALE })
  45. const tripwire = watchConsole(page)
  46. await page.goto(scaffold.authenticatedUrl)
  47. await page.waitForSelector('[class*="frame"]')
  48. if (await page.getByRole('dialog', { name: '设置' }).count() > 0) await page.keyboard.press('Escape')
  49. await page.getByRole('navigation', { name: '全局面板' }).getByRole('button', { name: '插件', exact: true }).click()
  50. const panel = page.locator('[data-plugin-panel]')
  51. await panel.getByRole('button', { name: '添加插件', exact: true }).click()
  52. const dialog = page.getByRole('dialog')
  53. await dialog.getByRole('textbox').fill('native-package')
  54. await dialog.getByRole('button', { name: '安装', exact: true }).click()
  55. // pnpm's refusal becomes the approval block, naming the package whose script waits; plain retry is not offered.
  56. const approval = dialog.getByRole('group', { name: '需要允许安装脚本' })
  57. await approval.waitFor({ timeout: 20_000 })
  58. await expect.poll(() => approval.getByText('native-package', { exact: true }).count()).toBe(1)
  59. expect(await dialog.getByRole('button', { name: '重试', exact: true }).count()).toBe(0)
  60. expect(await readFile(policyPath, 'utf8')).toContain('native-package: set this to true or false')
  61. const snapshot = (await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd))
  62. .split(process.execPath).join('{{node}}')
  63. .split(scaffold.harnessHome).join('{{harnessHome}}')
  64. await compareOrRefreshGolden(fileURLToPath(new URL('./expected/plugin-install-approve/blocked.expected.md', import.meta.url)), snapshot, webSnapshotMode())
  65. await approval.getByRole('button', { name: '允许这些脚本并重试', exact: true }).click()
  66. // The Host saved the permission before running pnpm again, and the installed screen says so.
  67. await dialog.getByRole('button', { name: '立即启用', exact: true }).waitFor({ timeout: 20_000 })
  68. await dialog.getByText('已允许运行安装脚本:native-package', { exact: true }).waitFor()
  69. expect(await readFile(policyPath, 'utf8')).toMatch(/native-package: true/)
  70. expect(JSON.parse(await readFile(join(profile, 'package.json'), 'utf8'))).toMatchObject({ dependencies: { 'native-package': '1.0.0' } })
  71. expect(tripwire.pageErrors).toEqual([])
  72. } finally { await browser.close() }
  73. } finally {
  74. await scaffold?.close()
  75. await rm(scratch, { recursive: true, force: true })
  76. }
  77. })