Files
baiyue-firstleg/scripts/controller-pipeline-contract.test.mjs
T

85 lines
4.8 KiB
JavaScript

import assert from 'node:assert/strict'
import { readFile } from 'node:fs/promises'
import test from 'node:test'
const pipeline = (await readFile(new URL('../Jenkinsfile', import.meta.url), 'utf8'))
.replaceAll('\r\n', '\n')
const servicePipeline = (await readFile(new URL('../Jenkinsfile.service', import.meta.url), 'utf8'))
.replaceAll('\r\n', '\n')
test('coordinator releases its builder while waiting for downstream jobs', () => {
assert.match(pipeline, /pipeline \{[\s\S]*?agent none/)
assert.match(pipeline, /stage\('确定构建范围'\) \{[\s\S]*?agent \{ label 'java-builder' \}/)
assert.match(pipeline, /agent \{ label 'java-builder' \}/)
assert.doesNotMatch(pipeline, /agent any/)
assert.doesNotMatch(pipeline, /cleanWs\(/)
assert.doesNotMatch(pipeline, /^\s*(?:&&|\|\|)/m)
})
test('coordinator runs selected tests once before parallel service packaging', () => {
const gateIndex = pipeline.indexOf("stage('统一 Maven 测试门禁')")
const dispatchIndex = pipeline.indexOf("stage('并行调度独立服务 Job')")
const staleReportCleanupIndex = pipeline.indexOf(
"find . -type f -path '*/target/surefire-reports/TEST-*.xml' -delete"
)
const selectedTestIndex = pipeline.indexOf('-pl ${selectedModules} -am test')
assert.ok(gateIndex >= 0, 'unified test gate must exist')
assert.ok(dispatchIndex > gateIndex, 'unified tests must finish before downstream dispatch')
assert.ok(staleReportCleanupIndex > gateIndex, 'stale Surefire reports must be removed inside the test gate')
assert.ok(selectedTestIndex > staleReportCleanupIndex, 'stale reports must be removed before selected tests run')
assert.match(pipeline, /-pl \$\{selectedModules\} -am test/)
assert.match(pipeline, /junit allowEmptyResults: true, testResults: '\*\*\/target\/surefire-reports\/TEST-\*\.xml'/)
assert.match(pipeline, /booleanParam\(name: 'SKIP_TESTS', value: true\)/)
assert.doesNotMatch(pipeline, /TESTED_COMMIT/)
})
test('AUTO detects database migrations without expanding them to all services', () => {
assert.match(pipeline, /def databaseMigrationPaths = \['deploy\/sql\/'\]/)
assert.match(pipeline, /List serviceRelevantChanges = changed\.findAll/)
assert.match(pipeline, /!databaseMigrationPaths\.any \{ prefix -> path\.startsWith\(prefix\) \}/)
assert.match(pipeline, /env\.DATABASE_CHANGE_DETECTED = databaseChanged \? 'true' : 'false'/)
})
test('database-only changes are reported as a separate migration action', () => {
assert.match(pipeline, /仅数据库迁移变更 \| 需单独审核执行/)
assert.match(pipeline, /统一 SQL 须独立审核并执行/)
})
test('coordinator cannot initialize test databases', () => {
assert.doesNotMatch(pipeline, /INITIALIZE_TEST_DATABASE/)
assert.doesNotMatch(pipeline, /initialize-test-databases\.sh/)
assert.doesNotMatch(pipeline, /测试数据库初始化/)
})
test('coordinator dispatches exactly the current TEST service map without retired regional units', async () => {
const serviceMap = await readFile(new URL('../deploy/service-map.yml', import.meta.url), 'utf8')
const expected = [...serviceMap.matchAll(/^ ([a-z][a-z0-9-]+):$/gm)].map(match => match[1]).sort()
const declaration = pipeline.match(/def serviceModules = \[([\s\S]*?)\]/)?.[1] || ''
const actual = [...declaration.matchAll(/'([^']+)'/g)].map(match => match[1]).sort()
assert.equal(expected.length, 13)
assert.deepEqual(actual, expected)
assert.doesNotMatch(pipeline, /EVENT_BRIDGE_REGION|gateway-overseas|event-bridge-service/)
})
test('TEST and gray jobs use fixed independent environment mappings and explicit DEPLOY', () => {
assert.match(pipeline, /booleanParam\(name: 'DEPLOY'/)
assert.match(pipeline, /case 'backend-java':[\s\S]*?return 'test'/)
assert.match(pipeline, /case 'backend-java-prod':[\s\S]*?return 'production'/)
assert.match(pipeline, /case 'backend-java-prod':[\s\S]*?return 'baiyue-firstleg-prod-'/)
assert.doesNotMatch(pipeline, /return 'development'|backend-java-dev/)
assert.match(pipeline, /booleanParam\(name: 'DEPLOY', value: params\.DEPLOY\)/)
assert.doesNotMatch(pipeline, /input message:/)
assert.doesNotMatch(pipeline, /\u786e\u8ba4\u90e8\u7f72/)
})
test('TEST coordinator records downstream identity and fails the aggregate on any unsuccessful service', () => {
assert.match(pipeline, /env\.SOURCE_COMMIT = sh\(script: 'git rev-parse HEAD'/)
assert.match(pipeline, /def downstreamBuildUrls = \[:\]/)
assert.match(pipeline, /downstreamBuildUrls\[service\] = downstreamBuild\.absoluteUrl/)
assert.match(pipeline, /downstreamBuildNumbers\[service\] = downstreamBuild\.number/)
assert.match(pipeline, /downstreamResults\.findAll \{ service, result -> result != 'SUCCESS' \}/)
assert.match(pipeline, /if \(failed\) \{[\s\S]*?unstable\(/)
assert.doesNotMatch(pipeline + servicePipeline, /wecom-prod-webhook|sendProductionWeComNotification/)
})