You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

329 lines
15 KiB

  1. import type { Page, Route } from '@playwright/test'
  2. import { captureScreenshot, expect, test } from './fixtures'
  3. import { loginAsAdmin } from './helpers'
  4. type JsonRecord = Record<string, unknown>
  5. const envelope = (data: unknown) => ({
  6. code: 0,
  7. message: '成功',
  8. data,
  9. timestamp: Date.now(),
  10. requestId: 'ape2e-zipvoice-controlled',
  11. })
  12. const fulfillJson = (route: Route, data: unknown, status = 200) => route.fulfill({
  13. status,
  14. contentType: 'application/json',
  15. body: JSON.stringify(envelope(data)),
  16. })
  17. const voiceRow = (page: Page, name: string) => page
  18. .locator('.voice-clone-table .el-table__row')
  19. .filter({ hasText: name })
  20. .first()
  21. const baseCapability = {
  22. dataVersion: 1,
  23. capabilityType: 'VOICE_CLONE',
  24. type: 'SHERPA_ZIPVOICE',
  25. description: '受控测试音色',
  26. reference: '参考录音.wav',
  27. referenceText: '进入检修区域前,请确认设备已停机、断电、卸压。',
  28. gender: 'MALE',
  29. enabled: true,
  30. builtIn: false,
  31. updatedAt: '2026-08-22T10:00:00+08:00',
  32. }
  33. test('ZipVoice 受控 UI:克隆契约、异步状态、重试与数字形象默认音色', async ({ page }, testInfo) => {
  34. test.setTimeout(120_000)
  35. await loginAsAdmin(page)
  36. const requests: { create?: JsonRecord; retry?: JsonRecord; speech?: JsonRecord; avatarUpdate?: JsonRecord; builtIn?: string | null } = {}
  37. let capabilities = [
  38. {
  39. ...baseCapability,
  40. id: 'builtin-voice-male-gentle',
  41. name: '温和男声',
  42. type: '平台内置音色',
  43. builtIn: true,
  44. voiceRuntimeMode: 'TTS_SPEAKER',
  45. validationStatus: 'AVAILABLE',
  46. status: 'ENABLED',
  47. mediaUrl: '',
  48. referenceText: null,
  49. },
  50. {
  51. ...baseCapability,
  52. id: 'clone-sample-only',
  53. name: '历史参考样音',
  54. voiceRuntimeMode: 'SAMPLE_ONLY',
  55. validationStatus: 'UNTESTED',
  56. status: 'ENABLED',
  57. mediaUrl: 'data:audio/wav;base64,UklGRgAAAABXQVZF',
  58. referenceText: null,
  59. gender: 'UNSPECIFIED',
  60. },
  61. {
  62. ...baseCapability,
  63. id: 'clone-processing',
  64. name: '排队中的男教员',
  65. voiceRuntimeMode: 'ZIPVOICE_PROCESSING',
  66. cloneStatus: 'PROCESSING',
  67. cloneProgress: 42,
  68. currentJobId: 'JOB-ZIPVOICE-42',
  69. validationStatus: 'UNTESTED',
  70. status: 'PROCESSING',
  71. },
  72. {
  73. ...baseCapability,
  74. id: 'clone-failed',
  75. name: '待重试音色',
  76. voiceRuntimeMode: 'ZIPVOICE_FAILED',
  77. cloneStatus: 'FAILED',
  78. currentJobId: 'JOB-ZIPVOICE-FAILED',
  79. validationStatus: 'UNAVAILABLE',
  80. status: 'FAILED',
  81. errorMessage: '参考音频有效人声时长不足',
  82. },
  83. {
  84. ...baseCapability,
  85. id: 'clone-ready',
  86. name: '可用克隆男声',
  87. voiceRuntimeMode: 'ZIPVOICE_READY',
  88. cloneStatus: 'READY',
  89. cloneProgress: 100,
  90. validationStatus: 'AVAILABLE',
  91. status: 'ENABLED',
  92. previewUrl: 'data:audio/wav;base64,UklGRgAAAABXQVZF',
  93. mediaUrl: 'data:audio/wav;base64,UklGRgAAAABXQVZF',
  94. },
  95. ]
  96. const ttsVoices = [
  97. { id: 'male-gentle', capabilityId: 'builtin-voice-male-gentle', name: '温和男声', speaker: 1, gender: 'male', language: 'zh-CN', runtimeMode: 'VITS' },
  98. { id: 'clone-ready', capabilityId: 'clone-ready', name: '可用克隆男声', speaker: null, gender: 'male', language: 'zh-CN', runtimeMode: 'ZIPVOICE' },
  99. ]
  100. const avatar = {
  101. id: 'avatar-controlled',
  102. dataVersion: 3,
  103. name: '受控维修教员',
  104. status: 'ready',
  105. bundleReady: true,
  106. owner: '系统管理员',
  107. ownerId: '1',
  108. ownerName: '系统管理员',
  109. sourceType: 'user',
  110. role: '维修教员',
  111. specialty: '装备检修',
  112. gender: '男',
  113. visibility: 'private',
  114. voiceCapabilityId: 'builtin-voice-male-gentle',
  115. voice: { capabilityId: 'builtin-voice-male-gentle', speaker: 1, name: '温和男声', speed: 0.8 },
  116. coverUrl: '/avatar-previews/mechanical-male.png',
  117. previewUrl: '/avatar-previews/mechanical-male.mp4',
  118. viewerUrl: '/human/viewer/avatar-controlled',
  119. updatedAt: '2026-08-22T10:00:00+08:00',
  120. }
  121. await page.route(/\/api\/v1\/tts(?:\/|\?|$)/, async (route) => {
  122. const url = new URL(route.request().url())
  123. if (route.request().method() === 'GET' && url.pathname.endsWith('/tts/voices')) {
  124. return fulfillJson(route, { ready: true, engine: 'sherpa-onnx', device: 'cpu', items: ttsVoices, error: null, missing: [] })
  125. }
  126. if (route.request().method() === 'POST' && url.pathname.endsWith('/tts')) {
  127. requests.speech = route.request().postDataJSON() as JsonRecord
  128. return fulfillJson(route, {
  129. status: 'succeeded', id: 'speech-controlled', audioUrl: 'data:audio/wav;base64,UklGRgAAAABXQVZF',
  130. mimeType: 'audio/wav', sampleRate: 24000, duration: 1, speaker: null,
  131. voiceCapabilityId: requests.speech.voiceCapabilityId, runtimeMode: 'ZIPVOICE', speed: 0.9,
  132. })
  133. }
  134. return route.fallback()
  135. })
  136. await page.route(/\/api\/v1\/files(?:\/|\?|$)/, async (route) => {
  137. const url = new URL(route.request().url())
  138. if (route.request().method() === 'POST' && url.pathname.endsWith('/files/signed-urls')) {
  139. return fulfillJson(route, { items: [{ id: 'file-controlled', fileCode: 'file-controlled', url: '/api/v1/files/file-controlled/content?expires=1999999999&signature=controlled', expiresAt: 1999999999, mediaType: 'audio/wav' }] })
  140. }
  141. if (route.request().method() === 'POST' && url.pathname.endsWith('/files')) {
  142. return fulfillJson(route, { id: 'file-controlled', databaseId: '91', originalName: 'reference.wav', mediaType: 'audio/wav', purposeCode: 'CAPABILITY_SOURCE', size: 1024, sha256: 'controlled', status: 'READY', downloadUrl: '' }, 201)
  143. }
  144. return route.fallback()
  145. })
  146. await page.route(/\/api\/v1\/capabilities(?:\/|\?|$)/, async (route) => {
  147. const request = route.request()
  148. const url = new URL(request.url())
  149. const retryMatch = url.pathname.match(/\/capabilities\/([^/]+)\/voice-clone\/retry$/)
  150. if (retryMatch && request.method() === 'POST') {
  151. requests.retry = request.postDataJSON() as JsonRecord
  152. await new Promise((resolve) => setTimeout(resolve, 350))
  153. capabilities = capabilities.map((item) => item.id === retryMatch[1]
  154. ? { ...item, dataVersion: 2, voiceRuntimeMode: 'ZIPVOICE_PROCESSING', cloneStatus: 'QUEUED', cloneProgress: 0, status: 'PROCESSING', errorMessage: '' }
  155. : item)
  156. return fulfillJson(route, capabilities.find((item) => item.id === retryMatch[1]))
  157. }
  158. const detailMatch = url.pathname.match(/\/capabilities\/([^/]+)$/)
  159. if (detailMatch && request.method() === 'GET') {
  160. return fulfillJson(route, capabilities.find((item) => item.id === detailMatch[1]))
  161. }
  162. if (url.pathname.endsWith('/capabilities') && request.method() === 'POST') {
  163. requests.create = request.postDataJSON() as JsonRecord
  164. const created = {
  165. ...baseCapability,
  166. ...requests.create,
  167. id: 'clone-new-processing',
  168. dataVersion: 1,
  169. name: String(requests.create.name || ''),
  170. mediaFileCode: 'file-controlled',
  171. mediaUrl: '',
  172. voiceRuntimeMode: 'ZIPVOICE_PROCESSING',
  173. cloneStatus: 'QUEUED',
  174. cloneProgress: 0,
  175. currentJobId: 'JOB-ZIPVOICE-NEW',
  176. validationStatus: 'UNTESTED',
  177. status: 'PROCESSING',
  178. }
  179. capabilities = [created, ...capabilities]
  180. return fulfillJson(route, created, 201)
  181. }
  182. if (url.pathname.endsWith('/capabilities') && request.method() === 'GET') {
  183. requests.builtIn = url.searchParams.get('builtIn')
  184. const visible = requests.builtIn == null
  185. ? capabilities
  186. : capabilities.filter((item) => item.builtIn === (requests.builtIn === 'true'))
  187. return fulfillJson(route, { items: visible, total: visible.length, page: 1, pageSize: 10, pages: 1 })
  188. }
  189. return route.fallback()
  190. })
  191. await page.route(/\/api\/v1\/avatars(?:\/|\?|$)/, async (route) => {
  192. const request = route.request()
  193. const url = new URL(request.url())
  194. if (request.method() === 'GET' && url.pathname.endsWith('/avatars')) {
  195. return fulfillJson(route, { items: [avatar], total: 1, page: 1, pageSize: 9, pages: 1 })
  196. }
  197. if (request.method() === 'PUT' && url.pathname.endsWith(`/avatars/${avatar.id}`)) {
  198. requests.avatarUpdate = request.postDataJSON() as JsonRecord
  199. Object.assign(avatar, {
  200. ...requests.avatarUpdate,
  201. dataVersion: 4,
  202. voiceCapabilityId: requests.avatarUpdate.voiceCapabilityId,
  203. voice: { capabilityId: requests.avatarUpdate.voiceCapabilityId, speaker: null, name: '可用克隆男声', speed: 0.8 },
  204. })
  205. return fulfillJson(route, avatar)
  206. }
  207. return route.fallback()
  208. })
  209. await page.route(/\/api\/v1\/avatar-preferences(?:\?|$)/, (route) => fulfillJson(route, {
  210. defaultAvatarId: '', builtInVisible: true, dataVersion: 1,
  211. }))
  212. await page.goto('/assets/voice-clones')
  213. await expect(page.getByRole('heading', { name: '声音克隆' })).toBeVisible()
  214. await expect(voiceRow(page, '历史参考样音')).toContainText('待开始克隆')
  215. await expect(voiceRow(page, '排队中的男教员')).toContainText('42%')
  216. const failedInitialRow = voiceRow(page, '待重试音色')
  217. await expect(failedInitialRow).not.toContainText('参考音频有效人声时长不足')
  218. await expect(voiceRow(page, '可用克隆男声')).toContainText('克隆音色可用')
  219. const builtinRow = voiceRow(page, '温和男声')
  220. await expect(builtinRow).toContainText('平台内置')
  221. await expect(builtinRow).toContainText('VITS')
  222. await expect(builtinRow.getByRole('button', { name: '试听', exact: true })).toBeVisible()
  223. await expect(builtinRow.getByRole('button', { name: '编辑', exact: true })).toHaveCount(0)
  224. await expect(builtinRow.getByRole('button', { name: '停用', exact: true })).toHaveCount(0)
  225. await expect(builtinRow.getByRole('button', { name: '删除', exact: true })).toHaveCount(0)
  226. await failedInitialRow.getByRole('button', { name: '详情', exact: true }).click()
  227. const failedDetailDialog = page.locator('.model-detail-dialog:visible').last()
  228. await expect(failedDetailDialog).toContainText('参考音频有效人声时长不足')
  229. await failedDetailDialog.getByRole('button', { name: '关闭', exact: true }).click()
  230. const readyDetailRow = voiceRow(page, '可用克隆男声')
  231. await readyDetailRow.getByRole('button', { name: '详情', exact: true }).click()
  232. const detailDialog = page.locator('.model-detail-dialog:visible').last()
  233. await expect(detailDialog.locator('.el-dialog__title')).toHaveText('音色详情')
  234. await expect(detailDialog).toContainText('可用克隆男声')
  235. await expect(detailDialog).toContainText('ZipVoice')
  236. await detailDialog.getByRole('button', { name: '关闭', exact: true }).click()
  237. await captureScreenshot(page, testInfo, '01-zipvoice-async-states')
  238. await page.locator('.voice-ownership-filter').click()
  239. await page.locator('.el-select-dropdown:visible .el-select-dropdown__item').filter({ hasText: '自定义音色' }).click()
  240. await page.getByRole('button', { name: '搜索', exact: true }).click()
  241. await expect.poll(() => requests.builtIn).toBe('false')
  242. await expect(voiceRow(page, '温和男声')).toHaveCount(0)
  243. const failedRow = voiceRow(page, '待重试音色')
  244. await failedRow.getByRole('button', { name: '重新克隆' }).click()
  245. await expect(failedRow.locator('.el-button.is-loading')).toBeVisible()
  246. await expect.poll(() => requests.retry?.dataVersion).toBe(1)
  247. await expect(voiceRow(page, '待重试音色')).toContainText('排队中')
  248. const readyRow = voiceRow(page, '可用克隆男声')
  249. await readyRow.getByRole('button', { name: '试听', exact: true }).click()
  250. await expect.poll(() => requests.speech?.voiceCapabilityId).toBe('clone-ready')
  251. await expect(readyRow.locator('.voice-table-preview audio')).toBeVisible()
  252. await page.getByRole('button', { name: /新建克隆音色/ }).click()
  253. const dialog = page.locator('.admin-form-dialog:visible').last()
  254. const genderField = dialog.locator('.voice-gender-field')
  255. const engineField = dialog.locator('.voice-engine-field')
  256. await expect(engineField).toContainText('Sherpa-ONNX ZipVoice')
  257. await expect(engineField).toContainText('CPU')
  258. const [genderBox, engineBox] = await Promise.all([genderField.boundingBox(), engineField.boundingBox()])
  259. expect(genderBox).not.toBeNull()
  260. expect(engineBox).not.toBeNull()
  261. expect(engineBox!.x).toBeGreaterThan(genderBox!.x)
  262. expect(genderBox!.x + genderBox!.width).toBeLessThanOrEqual(engineBox!.x)
  263. await dialog.locator('.capability-form > label').filter({ hasText: '名称' }).locator('input').fill('受控 ZipVoice 男教员')
  264. await dialog.locator('.capability-form > label').filter({ hasText: '性别' }).locator('.el-select').click()
  265. await page.locator('.el-select-dropdown:visible .el-select-dropdown__item').filter({ hasText: '男声' }).click()
  266. await dialog.locator('.capability-form > label').filter({ hasText: '说明' }).locator('textarea').fill('用于装备维修逐步讲解')
  267. await dialog.locator('.reference-transcript textarea').fill('进入检修区域前,请确认设备已停机、断电、卸压。')
  268. await dialog.locator('.el-dialog__body').evaluate((element) => { element.scrollTop = 0 })
  269. await captureScreenshot(page, testInfo, '02-zipvoice-required-fields')
  270. await dialog.locator('.voice-capture-panel input[type="file"]').setInputFiles({
  271. name: 'reference.wav',
  272. mimeType: 'audio/wav',
  273. buffer: Buffer.from('RIFF-controlled-wave'),
  274. })
  275. await expect(dialog.locator('.voice-consent .el-checkbox__label')).toHaveCSS('white-space', 'nowrap')
  276. await dialog.locator('.voice-consent').click()
  277. await captureScreenshot(page, testInfo, '03-zipvoice-source-and-consent')
  278. await dialog.getByRole('button', { name: '提交克隆任务' }).click()
  279. await expect(dialog.getByRole('button', { name: '提交克隆任务' })).toHaveClass(/is-loading/)
  280. await expect(dialog).toBeHidden()
  281. expect(requests.create).toMatchObject({
  282. capabilityType: 'VOICE_CLONE',
  283. type: 'SHERPA_ZIPVOICE',
  284. name: '受控 ZipVoice 男教员',
  285. gender: 'MALE',
  286. referenceText: '进入检修区域前,请确认设备已停机、断电、卸压。',
  287. mediaFileCode: 'file-controlled',
  288. consentConfirmed: true,
  289. })
  290. await expect(voiceRow(page, '受控 ZipVoice 男教员')).toContainText('排队中')
  291. await page.goto('/assets/avatars')
  292. await expect(page.getByRole('heading', { name: '数字形象' })).toBeVisible()
  293. const avatarCard = page.locator('.avatar-card').filter({ hasText: avatar.name })
  294. await avatarCard.getByRole('button', { name: '编辑' }).click()
  295. const avatarDialog = page.locator('.admin-form-dialog:visible').last()
  296. const voiceSelect = avatarDialog.locator('.el-form-item').filter({ hasText: '默认音色' }).locator('.el-select')
  297. await voiceSelect.click()
  298. const dropdown = page.locator('.el-select-dropdown:visible').last()
  299. await expect(dropdown).toContainText('温和男声(内置)')
  300. await expect(dropdown).toContainText('可用克隆男声(克隆)')
  301. await expect(dropdown).not.toContainText('排队中的男教员')
  302. await dropdown.locator('.el-select-dropdown__item').filter({ hasText: '可用克隆男声' }).click()
  303. await captureScreenshot(page, testInfo, '04-avatar-default-ready-voice-only')
  304. await avatarDialog.getByRole('button', { name: '保存修改' }).click()
  305. await expect.poll(() => requests.avatarUpdate?.voiceCapabilityId).toBe('clone-ready')
  306. expect(requests.avatarUpdate?.voiceSpeed).toBe(0.8)
  307. })