Files
Ncurator/scripts/test/validate-e2e-contract.mjs
hy 6e5332670a refactor: change launchMode to serviceMode in e2e tests
- Updated multiple e2e test files to replace 'launchMode: real' with 'serviceMode: real' for consistency in service mode configuration.
- Refactored the electronApp.ts to introduce E2EServiceMode and related types for better clarity and maintainability.
- Enhanced run-e2e-case.ts to accommodate the new service mode structure, ensuring proper handling of test cases.
- Created workspaceFixture.ts to manage temporary workspaces more efficiently during tests.
2026-07-20 18:10:50 +08:00

53 lines
2.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFile, readdir } from 'node:fs/promises'
import path from 'node:path'
const ROOT = process.cwd()
const CASE_ROOT = path.join(ROOT, 'tests', 'case')
const walk = async (directory) => {
const files = []
for (const entry of await readdir(directory, { withFileTypes: true })) {
const entryPath = path.join(directory, entry.name)
if (entry.isDirectory()) files.push(...(await walk(entryPath)))
else if (entry.isFile() && entry.name.endsWith('.e2e.test.ts')) files.push(entryPath)
}
return files
}
const files = await walk(CASE_ROOT)
const failures = []
let realCases = 0
let fixtureCases = 0
for (const filePath of files) {
const source = await readFile(filePath, 'utf8')
const relativePath = path.relative(ROOT, filePath)
const caseCount = (source.match(/\brunE2ECase(?:<[^>]+>)?\s*\(/gu) || []).length
const realCount = (source.match(/\bserviceMode\s*:\s*['"]real['"]/gu) || []).length
const fixtureCount = (source.match(/\bserviceMode\s*:\s*['"]fixture['"]/gu) || []).length
const reasonCount = (source.match(/\bfixtureReason\s*:\s*['"][^'"]+['"]/gu) || []).length
if (source.includes('launchMode:')) failures.push(`${relativePath}: 不允许继续使用 launchMode`)
if (caseCount === 0) failures.push(`${relativePath}: 未找到 caseId`)
if (realCount + fixtureCount !== caseCount) {
failures.push(
`${relativePath}: 每个 caseId 必须对应一个显式 serviceMode,case=${caseCount} mode=${realCount + fixtureCount}`
)
}
if (reasonCount !== fixtureCount) {
failures.push(
`${relativePath}: 每个 fixture 必须提供 fixtureReason,fixture=${fixtureCount} reason=${reasonCount}`
)
}
realCases += realCount
fixtureCases += fixtureCount
}
if (failures.length > 0) {
throw new Error(`E2E 契约校验失败:\n${failures.map((item) => `- ${item}`).join('\n')}`)
}
process.stdout.write(
`[e2e-contract] ${String(files.length)} files, ${String(realCases)} real, ${String(fixtureCases)} fixture\n`
)