import fs from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' import { createRequire } from 'node:module' import assert from 'node:assert/strict' const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..') const require = createRequire(path.join(root, 'ai_person/ape2e/package.json')) const { chromium, expect } = require('@playwright/test') const report = path.join(root, 'unreal_tran/ute2e/reports/open-platform-pdf-20260909') fs.mkdirSync(report, { recursive: true }) const browser = await chromium.launch({ channel: 'msedge', headless: true }) try { const page = await browser.newPage({ viewport: { width: 1600, height: 1000 } }) const errors = [] page.on('pageerror', error => errors.push(error.message)) await page.goto('http://127.0.0.1:8003/developer') await expect(page.locator('.endpoint-section')).toHaveCount(13) const button = page.getByRole('button', { name: '导出 PDF', exact: true }) const downloadPromise = page.waitForEvent('download') await button.click() const download = await downloadPromise assert.equal(download.suggestedFilename(), 'digital-human-open-api.pdf') await download.saveAs(path.join(report, 'digital-human-open-api.pdf')) assert.equal(fs.readFileSync(path.join(report, 'digital-human-open-api.pdf')).subarray(0, 5).toString(), '%PDF-') await expect(button).toBeEnabled() await page.screenshot({ path: path.join(report, '01-pdf-download.png'), animations: 'disabled' }) await page.route('**/open-api/v1/documentation.pdf', route => route.fulfill({ status: 503, contentType: 'application/json', body: JSON.stringify({ code: 50000, message: '导出服务暂不可用', data: null }) })) await button.click() await expect(page.locator('.el-message--error')).toBeVisible() await expect(button).toBeEnabled() await page.unroute('**/open-api/v1/documentation.pdf') const retry = page.waitForEvent('download') await button.click() assert.equal((await retry).suggestedFilename(), 'digital-human-open-api.pdf') await page.locator('#endpoint-9').evaluate(el => window.scrollTo({ top: window.scrollY + el.getBoundingClientRect().top - 20, behavior: 'instant' })) await expect(page.locator('.docs-nav a.active')).toHaveAttribute('href', '#endpoint-9') assert.deepEqual(errors, []) fs.writeFileSync(path.join(report, 'results.json'), JSON.stringify({ passed: true, endpoints: 13, backendPdfDownload: true, errorMessage: true, retry: true, scrollSync: true, scriptErrors: errors }, null, 2)) console.log('PDF download, error message, retry and scroll sync passed') } finally { await browser.close() }