1
0
Files
nex/frontend/e2e/fixtures.ts
lanyuanxiaoyao 5e7267db07 fix(e2e): 修复 10 个被 skip 的 E2E 测试
- 将 playwright.config.ts 的 mkdtemp 替换为固定路径,解决主进程/worker 临时目录不一致问题
- 交换后端 WAL 与迁移执行顺序,确保 sql.js 能读取到完整 schema
- 修复 models.spec.ts 断言使用 exact:true 避免统一模型 ID 列干扰
- 移除全部 10 个 test.skip,26 个 E2E 测试全部通过
2026-04-22 14:32:12 +08:00

103 lines
2.5 KiB
TypeScript

import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import initSqlite from 'sql.js'
export const API_BASE = `http://localhost:${process.env.NEX_BACKEND_PORT || '19026'}`
export interface SeedProviderInput {
id: string
name: string
apiKey: string
baseUrl: string
protocol: 'openai' | 'anthropic'
enabled: boolean
}
export interface SeedModelInput {
providerId: string
modelName: string
enabled: boolean
}
export interface SeedStatsInput {
providerId: string
modelName: string
requestCount: number
date: string
}
export async function clearDatabase(
request: import('@playwright/test').APIRequestContext,
) {
const providers = await request.get(`${API_BASE}/api/providers`)
if (providers.ok()) {
const data = await providers.json()
for (const p of data) {
await request.delete(`${API_BASE}/api/providers/${p.id}`)
}
}
}
export async function seedProvider(
request: import('@playwright/test').APIRequestContext,
data: SeedProviderInput,
) {
const resp = await request.post(`${API_BASE}/api/providers`, {
data: {
id: data.id,
name: data.name,
api_key: data.apiKey,
base_url: data.baseUrl,
protocol: data.protocol,
enabled: data.enabled,
},
})
if (!resp.ok()) {
throw new Error(`seedProvider failed: ${resp.status()} ${await resp.text()}`)
}
return resp.json()
}
export async function seedModel(
request: import('@playwright/test').APIRequestContext,
data: SeedModelInput,
) {
const resp = await request.post(`${API_BASE}/api/models`, {
data: {
provider_id: data.providerId,
model_name: data.modelName,
enabled: data.enabled,
},
})
if (!resp.ok()) {
throw new Error(`seedModel failed: ${resp.status()} ${await resp.text()}`)
}
return resp.json()
}
export async function seedUsageStats(statsData: SeedStatsInput[]) {
const tempDir = path.join(os.tmpdir(), 'nex-e2e')
const dbPath = path.join(tempDir, 'test.db')
if (!fs.existsSync(dbPath)) {
throw new Error(`Database file not found at ${dbPath}. Backend may not have created it yet.`)
}
const SQL = await initSqlite()
const buf = fs.readFileSync(dbPath)
const db = new SQL.Database(buf)
for (const row of statsData) {
db.run(
'INSERT OR REPLACE INTO usage_stats (provider_id, model_name, request_count, date) VALUES (?, ?, ?, ?)',
[row.providerId, row.modelName, row.requestCount, row.date],
)
}
const data = db.export()
fs.writeFileSync(dbPath, Buffer.from(data))
db.close()
}