onboarding-deepseek-config.e2e.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. acknowledgeReloadConnectionLoss, assertFixtureInventory, captureStableAria, compareOrRefreshGolden,
  13. launchWebScaffold, watchConsole, webSnapshotMode, type WebScaffold,
  14. } from './scaffold.ts'
  15. import { ZH_BROWSER_LOCALE, connectFreshWorkspaceZh, saveFailureShot } from './support.ts'
  16. import { settingsNamespace } from '@deepseek-ai/dsh-settings'
  17. import {
  18. WELCOME_NOTICE_ACK_FIELD, WELCOME_NOTICE_COPY, WELCOME_NOTICE_SETTINGS_NAMESPACE,
  19. WELCOME_NOTICE_VERSION,
  20. } from '@deepseek-ai/dsh-client-ui-settings-general'
  21. const SNAPSHOT_DIR = fileURLToPath(new URL('./snapshots/onboarding-deepseek-config', import.meta.url))
  22. const WELCOME_EXPECTED = join(SNAPSHOT_DIR, 'welcome.expected.md')
  23. const MISSING_EXPECTED = join(SNAPSHOT_DIR, 'missing.expected.md')
  24. const MODELS_EXPECTED = join(SNAPSHOT_DIR, 'models.expected.md')
  25. const MODE = webSnapshotMode()
  26. describe.skipIf(MODE === 'record')('web e2e: first-run DeepSeek credential setup', () => {
  27. let scaffold: WebScaffold
  28. let browser: Browser
  29. let page: Page
  30. let tripwire: ReturnType<typeof watchConsole>
  31. const browserConsole: string[] = []
  32. beforeAll(async () => {
  33. scaffold = await launchWebScaffold({ deepSeekMissingCredential: true, welcomeNoticePending: true })
  34. browser = await chromium.launch()
  35. // The scenario asserts the shipped Chinese copy, so the browser asks for it.
  36. page = await browser.newPage({ viewport: { width: 1440, height: 960 }, locale: ZH_BROWSER_LOCALE })
  37. tripwire = watchConsole(page)
  38. page.on('console', message => browserConsole.push(message.text()))
  39. await page.goto(scaffold.baseUrl, { waitUntil: 'load' })
  40. await page.waitForSelector('[class*="frame"]', { timeout: 30_000 })
  41. }, 120_000)
  42. afterAll(async () => {
  43. await browser?.close()
  44. await scaffold?.close()
  45. })
  46. it('stores a key write-only and observes configured state without restarting', async () => {
  47. onTestFailed(() => saveFailureShot(page, 'web-e2e-onboarding-deepseek-config'))
  48. const welcome = page.getByRole('region', { name: WELCOME_NOTICE_COPY.zh.title })
  49. await welcome.waitFor({ timeout: 15_000 })
  50. expect(await page.locator('#root').evaluate(root => (root as HTMLElement).inert)).toBe(true)
  51. const welcomeAria = await captureStableAria(page, '[role="region"]', scaffold.workspaceCwd)
  52. await compareOrRefreshGolden(WELCOME_EXPECTED, welcomeAria, MODE)
  53. expect(await welcome.getByRole('button').allTextContents()).toEqual([WELCOME_NOTICE_COPY.zh.continueLabel])
  54. expect(await welcome.locator('button').count()).toBe(1)
  55. const mask = page.locator('[class*="onboardingMask"]')
  56. expect(await mask.count()).toBe(1)
  57. const maskStyles = await mask.evaluate((mask) => {
  58. const style = getComputedStyle(mask)
  59. const rect = mask.getBoundingClientRect()
  60. return {
  61. position: style.position,
  62. left: style.left,
  63. right: style.right,
  64. top: style.top,
  65. bottom: style.bottom,
  66. background: style.backgroundColor,
  67. backdropFilter: style.backdropFilter,
  68. rect: { left: rect.left, top: rect.top, right: rect.right, bottom: rect.bottom },
  69. }
  70. })
  71. expect(maskStyles).toEqual({
  72. position: 'absolute',
  73. left: '0px',
  74. right: '0px',
  75. top: '80px',
  76. bottom: '0px',
  77. background: 'rgba(0, 0, 0, 0.24)',
  78. backdropFilter: 'blur(2px)',
  79. rect: { left: 0, top: 80, right: 1440, bottom: 960 },
  80. })
  81. // Closing the process/page before acknowledgement writes nothing, so the
  82. // same durable profile presents the notice again after reload.
  83. const firstReloadWarnings = tripwire.warnings.length
  84. await page.reload({ waitUntil: 'load' })
  85. acknowledgeReloadConnectionLoss(tripwire, firstReloadWarnings)
  86. await welcome.waitFor({ timeout: 15_000 })
  87. await welcome.getByRole('button', { name: WELCOME_NOTICE_COPY.zh.continueLabel }).click()
  88. await welcome.waitFor({ state: 'detached', timeout: 15_000 })
  89. const credentialStep = page.getByRole('region', { name: '添加一个 API Key 开始使用' })
  90. await credentialStep.waitFor({ timeout: 15_000 })
  91. expect(await credentialStep.getByRole('textbox').count()).toBe(0)
  92. const initial = await captureStableAria(page, '[role="region"]', scaffold.workspaceCwd)
  93. await compareOrRefreshGolden(MISSING_EXPECTED, initial, MODE)
  94. await credentialStep.getByRole('button', { name: '前往配置' }).click()
  95. await credentialStep.waitFor({ state: 'detached', timeout: 15_000 })
  96. const settings = page.getByRole('dialog', { name: '设置' })
  97. await settings.waitFor({ timeout: 10_000 })
  98. expect(await page.locator('#root').evaluate(root => (root as HTMLElement).inert)).toBe(false)
  99. const keyInput = settings.getByLabel('API 密钥', { exact: true })
  100. await keyInput.waitFor({ timeout: 10_000 })
  101. const secret = `dsh_onboarding_${randomBytes(12).toString('hex')}`
  102. await keyInput.fill(secret)
  103. await settings.getByRole('button', { name: '保存', exact: true }).click()
  104. await keyInput.waitFor({ state: 'detached', timeout: 15_000 })
  105. const stored = await readFile(join(scaffold.harnessHome, '.env'), 'utf8')
  106. expect(stored.includes(`DEEPSEEK_API_KEY=${secret}`)).toBe(true)
  107. expect((await page.content()).includes(secret)).toBe(false)
  108. expect((await page.locator('body').ariaSnapshot()).includes(secret)).toBe(false)
  109. expect(browserConsole.some(line => line.includes(secret))).toBe(false)
  110. // The same open Models surface reuses the refreshed join and exposes the
  111. // configured write-only placeholder without a reload.
  112. const deepSeekRow = settings.getByText('DeepSeek', { exact: true }).first()
  113. await deepSeekRow.waitFor({ timeout: 10_000 })
  114. await deepSeekRow.locator('xpath=ancestor::li').getByRole('button', { name: '编辑' }).click()
  115. const configuredInput = settings.getByLabel('API 密钥', { exact: true })
  116. await configuredInput.waitFor({ timeout: 10_000 })
  117. await expect.poll(
  118. () => configuredInput.getAttribute('placeholder'),
  119. { timeout: 10_000 },
  120. ).toBe('已配置——输入新值可替换')
  121. const acknowledgedSettings = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  122. expect(acknowledgedSettings).toContain(`${WELCOME_NOTICE_ACK_FIELD}: ${WELCOME_NOTICE_VERSION}`)
  123. const secondReloadWarnings = tripwire.warnings.length
  124. await page.reload({ waitUntil: 'load' })
  125. acknowledgeReloadConnectionLoss(tripwire, secondReloadWarnings)
  126. await page.waitForSelector('[class*="frame"]', { timeout: 15_000 })
  127. expect(await page.getByRole('region', { name: WELCOME_NOTICE_COPY.zh.title }).count()).toBe(0)
  128. expect(await page.getByRole('region', { name: '添加一个 API Key 开始使用' }).count()).toBe(0)
  129. // A different stored copy version represents an intentional version bump:
  130. // the welcome step returns even though the credential is already ready.
  131. await scaffold.ctx.settings.mutate(settingsNamespace(WELCOME_NOTICE_SETTINGS_NAMESPACE), [{
  132. op: 'set', path: [WELCOME_NOTICE_ACK_FIELD], value: 'previous-copy-version',
  133. }])
  134. const thirdReloadWarnings = tripwire.warnings.length
  135. await page.reload({ waitUntil: 'load' })
  136. acknowledgeReloadConnectionLoss(tripwire, thirdReloadWarnings)
  137. await welcome.waitFor({ timeout: 15_000 })
  138. await welcome.getByRole('button', { name: WELCOME_NOTICE_COPY.zh.continueLabel }).click()
  139. await welcome.waitFor({ state: 'detached', timeout: 15_000 })
  140. expect(await page.getByRole('region', { name: '添加一个 API Key 开始使用' }).count()).toBe(0)
  141. expect((await page.content()).includes(secret)).toBe(false)
  142. expect((await page.locator('body').ariaSnapshot()).includes(secret)).toBe(false)
  143. expect(browserConsole.some(line => line.includes(secret))).toBe(false)
  144. expect(tripwire.warnings).toEqual([])
  145. expect(tripwire.pageErrors).toEqual([])
  146. }, 60_000)
  147. it('configures arbitrary DeepSeek models and prompts after the selected model is removed', async () => {
  148. onTestFailed(() => saveFailureShot(page, 'web-e2e-onboarding-deepseek-models'))
  149. // Opened here rather than inherited: the credential test reloads the page
  150. // to exercise the welcome step, so nothing carries an open dialog across.
  151. await page.getByRole('button', { name: '设置', exact: true }).click()
  152. const settings = page.getByRole('dialog', { name: '设置' })
  153. await settings.waitFor({ timeout: 10_000 })
  154. await settings.getByRole('button', { name: '模型' }).click()
  155. const deepSeek = settings.getByText('DeepSeek', { exact: true }).first()
  156. await deepSeek.waitFor({ timeout: 10_000 })
  157. await deepSeek.locator('xpath=ancestor::li').getByRole('button', { name: '编辑' }).click()
  158. await settings.getByText('自定义设置').click()
  159. await settings.getByRole('button', { name: /删除模型/ }).first().click()
  160. await settings.getByRole('button', { name: '添加模型' }).click()
  161. const customModelId = settings.getByLabel('模型 ID 2')
  162. await customModelId.fill('private-preview')
  163. await settings.getByLabel('显示名称 2').fill('Private Preview')
  164. // Capacities live behind the row's own disclosure, as in the pi-ai form.
  165. await settings.getByRole('button', { name: '容量 2' }).click()
  166. await settings.getByLabel('上下文窗口 2').fill('131072')
  167. await settings.getByLabel('最大输出 token 数 2').fill('64K')
  168. const modelEditor = await captureStableAria(page, '[role="dialog"]', scaffold.workspaceCwd)
  169. await compareOrRefreshGolden(MODELS_EXPECTED, modelEditor, MODE)
  170. await settings.getByRole('button', { name: '保存', exact: true }).click()
  171. await customModelId.waitFor({ state: 'detached', timeout: 15_000 })
  172. const document = await readFile(join(scaffold.harnessHome, 'settings.yaml'), 'utf8')
  173. expect(document).toContain('id: deepseek-v4-pro')
  174. expect(document).toContain('id: private-preview')
  175. expect(document).toContain('name: Private Preview')
  176. expect(document).toContain('contextWindow: 131072')
  177. expect(document).toContain('maxTokens: 64000')
  178. expect(document).not.toContain('id: deepseek-v4-flash')
  179. await page.keyboard.press('Escape')
  180. // A connected Workspace is what puts a live composer — and its model
  181. // trigger — on the page; the scaffold boots without one.
  182. await connectFreshWorkspaceZh(page, scaffold.workspaceCwd, 'model-fallback-e2e')
  183. const modelTrigger = page.getByRole('button', { name: '选择模型', exact: true })
  184. await modelTrigger.waitFor({ timeout: 10_000 })
  185. await modelTrigger.click()
  186. await page.getByRole('menuitem', { name: /模型/ }).click()
  187. expect(await page.getByText('deepseek-v4-flash', { exact: true }).count()).toBe(0)
  188. await page.getByRole('menuitemradio', { name: 'Private Preview' }).waitFor({ timeout: 10_000 })
  189. expect(tripwire.warnings).toEqual([])
  190. expect(tripwire.pageErrors).toEqual([])
  191. }, 60_000)
  192. it('keeps the fixture inventory closed', async () => {
  193. await assertFixtureInventory(
  194. SNAPSHOT_DIR,
  195. ['missing.expected.md', 'models.expected.md', 'welcome.expected.md'],
  196. )
  197. })
  198. })