onboarding-deepseek-config.e2e.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Keyless browser e2e: the shipped DeepSeek adapter stays mounted while its
  2. // credential is absent, onboarding routes to the real Models editor, and its
  3. // write lands in an isolated harness home without a reload or model call.
  4. import { randomBytes } from 'node:crypto'
  5. import { readFile } from 'node:fs/promises'
  6. import { fileURLToPath } from 'node:url'
  7. import { join } from 'node:path'
  8. import type { Browser, Page } from 'playwright'
  9. import { chromium } from 'playwright'
  10. import { afterAll, beforeAll, describe, expect, it, onTestFailed } from 'vitest'
  11. import {
  12. assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  13. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  14. } from './scaffold.ts'
  15. import { saveFailureShot } from './support.ts'
  16. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/onboarding-deepseek-config', import.meta.url))
  17. const MISSING_EXPECTED = join(SNAPSHOT_DIR, 'missing.expected.md')
  18. const MODELS_EXPECTED = join(SNAPSHOT_DIR, 'models.expected.md')
  19. const MODE = webSnapshotMode()
  20. describe.skipIf(MODE === 'record')('web e2e: first-run DeepSeek credential setup', () => {
  21. let scaffold: WebScaffold
  22. let browser: Browser
  23. let page: Page
  24. let tripwire: ReturnType<typeof watchConsole>
  25. const browserConsole: string[] = []
  26. beforeAll(async () => {
  27. scaffold = await launchWebScaffold({ deepSeekMissingCredential: true })
  28. browser = await chromium.launch()
  29. page = await browser.newPage({ viewport: { width: 1440, height: 960 } })
  30. tripwire = watchConsole(page)
  31. page.on('console', message => browserConsole.push(message.text()))
  32. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  33. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  34. }, 120_000)
  35. afterAll(async () => {
  36. await browser?.close()
  37. await scaffold?.close()
  38. })
  39. it('stores a key write-only and observes configured state without restarting', async () => {
  40. onTestFailed(() => saveFailureShot(page, 'web-e2e-onboarding-deepseek-config'))
  41. const dialog = page.getByRole('dialog', { name: '添加一个 API Key 开始使用' })
  42. await dialog.waitFor({ timeout: 15_000 })
  43. expect(await dialog.getByRole('textbox').count()).toBe(0)
  44. const initial = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  45. await compareOrRefreshGolden(MISSING_EXPECTED, initial, MODE)
  46. await dialog.getByRole('button', { name: '前往配置' }).click()
  47. await dialog.waitFor({ state: 'detached', timeout: 15_000 })
  48. const settings = page.getByRole('dialog', { name: '设置' })
  49. await settings.waitFor({ timeout: 10_000 })
  50. const keyInput = settings.getByLabel('API 密钥', { exact: true })
  51. await keyInput.waitFor({ timeout: 10_000 })
  52. const secret = `dsh_onboarding_${randomBytes(12).toString('hex')}`
  53. await keyInput.fill(secret)
  54. await settings.getByRole('button', { name: '保存', exact: true }).click()
  55. await keyInput.waitFor({ state: 'detached', timeout: 15_000 })
  56. const stored = await readFile(join(scaffold.harnessHome, '.env'), 'utf8')
  57. expect(stored.includes(`DEEPSEEK_API_KEY=${secret}`)).toBe(true)
  58. expect((await page.content()).includes(secret)).toBe(false)
  59. expect((await page.locator('body').ariaSnapshot()).includes(secret)).toBe(false)
  60. expect(browserConsole.some(line => line.includes(secret))).toBe(false)
  61. // The same open Models surface reuses the refreshed join and exposes the
  62. // configured write-only placeholder without a reload.
  63. const deepSeekRow = settings.getByText('DeepSeek', { exact: true }).first()
  64. await deepSeekRow.waitFor({ timeout: 10_000 })
  65. await deepSeekRow.locator('xpath=ancestor::li').getByRole('button', { name: '编辑' }).click()
  66. const configuredInput = settings.getByLabel('API 密钥', { exact: true })
  67. await configuredInput.waitFor({ timeout: 10_000 })
  68. await expect.poll(
  69. () => configuredInput.getAttribute('placeholder'),
  70. { timeout: 10_000 },
  71. ).toBe('已配置——输入新值可替换')
  72. expect((await page.content()).includes(secret)).toBe(false)
  73. expect((await page.locator('body').ariaSnapshot()).includes(secret)).toBe(false)
  74. expect(browserConsole.some(line => line.includes(secret))).toBe(false)
  75. expect(tripwire.warnings).toEqual([])
  76. expect(tripwire.pageErrors).toEqual([])
  77. }, 60_000)
  78. it('configures arbitrary DeepSeek models and prompts after the selected model is removed', async () => {
  79. onTestFailed(() => saveFailureShot(page, 'web-e2e-onboarding-deepseek-models'))
  80. const settings = page.getByRole('dialog', { name: '设置' })
  81. await settings.getByText('自定义设置').click()
  82. await settings.getByRole('button', { name: '删除模型' }).first().click()
  83. await settings.getByRole('button', { name: '添加模型' }).click()
  84. const customModelId = settings.getByLabel('模型 ID 2')
  85. await customModelId.fill('private-preview')
  86. await settings.getByLabel('显示名称 2').fill('Private Preview')
  87. await settings.getByLabel('上下文窗口 2').fill('131072')
  88. const modelEditor = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  89. await compareOrRefreshGolden(MODELS_EXPECTED, modelEditor, MODE)
  90. await settings.getByRole('button', { name: '保存', exact: true }).click()
  91. await customModelId.waitFor({ state: 'detached', timeout: 15_000 })
  92. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  93. expect(document).toContain('id: deepseek-v4-pro')
  94. expect(document).toContain('id: private-preview')
  95. expect(document).toContain('name: Private Preview')
  96. expect(document).toContain('contextWindow: 131072')
  97. expect(document).not.toContain('id: deepseek-v4-flash')
  98. await page.keyboard.press('Escape')
  99. await page.getByRole('button', { name: '创建工作区', exact: true }).click()
  100. await page.getByRole('menuitem', { name: '新建工作区', exact: true }).click()
  101. const workspaceDialog = page.getByRole('dialog', { name: '新建工作区' })
  102. await workspaceDialog.getByLabel('新工作区名称').fill('model-fallback-e2e')
  103. await workspaceDialog.getByRole('button', { name: '创建工作区', exact: true }).click()
  104. await workspaceDialog.waitFor({ state: 'detached', timeout: 10_000 })
  105. const modelTrigger = page.getByRole('button', { name: '选择模型', exact: true })
  106. await modelTrigger.waitFor({ timeout: 10_000 })
  107. await modelTrigger.click()
  108. await page.getByRole('menuitem', { name: /模型/ }).click()
  109. expect(await page.getByText('deepseek-v4-flash', { exact: true }).count()).toBe(0)
  110. await page.getByRole('menuitemradio', { name: 'Private Preview' }).waitFor({ timeout: 10_000 })
  111. expect(tripwire.warnings).toEqual([])
  112. expect(tripwire.pageErrors).toEqual([])
  113. }, 60_000)
  114. it('keeps the fixture inventory closed', async () => {
  115. await assertFixtureInventory(SNAPSHOT_DIR, ['missing.expected.md', 'models.expected.md'])
  116. })
  117. })