Files
baiyue-firstleg/scripts/agents-index-registry-contract.test.mjs
T

48 lines
2.8 KiB
JavaScript

import assert from 'node:assert/strict'
import { readFileSync } from 'node:fs'
import { spawnSync } from 'node:child_process'
import test from 'node:test'
const source = readFileSync(new URL('./agents-index-contract.ps1', import.meta.url), 'utf8')
const start = source.indexOf('$registeredTestRows =')
const end = source.indexOf("Assert-NotContains $testTaskRegistry '01a0194a", start)
assert.ok(start > 0 && end > start)
const registryContract = source.slice(start, end)
const project = 'c64729ff-6710-43c6-8ef2-d84e9666cc1e'
const root = '00000000-0000-4000-8000-000000000001'
const child = '00000000-0000-4000-8000-000000000002'
const enabled = `已重新启用测试环境总控调度\nparent_task_id project_id\n| \`${root}\` | \`${project}\` | 【总控】测试环境全流程 |\n| \`${child}\` | \`${root}\` | \`${project}\` | 【前端】专项 |`
function checkRegistry(text) {
const script = `
$ErrorActionPreference = 'Stop'
$failures = [System.Collections.Generic.List[string]]::new()
function Add-Failure([string]$message) { $script:failures.Add($message) }
function Assert-Contains([string]$text, [string]$needle, [string]$scope) { if (-not $text.Contains($needle)) { Add-Failure "$scope 缺少 $needle" } }
function Assert-Matches([string]$text, [string]$pattern, [string]$scope) { if ($text -notmatch $pattern) { Add-Failure "$scope 缺少结构" } }
$testTaskRegistry = [System.Text.Encoding]::UTF8.GetString([Convert]::FromBase64String('${Buffer.from(text).toString('base64')}'))
${registryContract}
Write-Output ('RESULT:' + (ConvertTo-Json -InputObject @($failures) -Compress))
`
const result = spawnSync('pwsh', ['-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(script, 'utf16le').toString('base64')], { encoding: 'utf8' })
assert.equal(result.status, 0, result.stderr)
const output = result.stdout.match(/RESULT:(\[[^\r\n]*\])/)
assert.ok(output, result.stdout)
return JSON.parse(output[1])
}
test('cleared test registry stays disabled and explicitly revokes inherited authorization', () => {
assert.deepEqual(checkRegistry('不启用总控调度\n当前无已登记总控或长期专项\n持续授权同步失效'), [])
assert.ok(checkRegistry('当前无已登记总控或长期专项').length > 0)
assert.ok(checkRegistry('').length > 0)
})
test('enabled registry requires explicit activation, one controller, unique tasks and its own project', () => {
assert.deepEqual(checkRegistry(enabled), [])
assert.ok(checkRegistry(enabled.replace('已重新启用测试环境总控调度', '')).length > 0)
assert.ok(checkRegistry(enabled.replace('parent_task_id', '')).length > 0)
assert.ok(checkRegistry(enabled.replace(child, root)).length > 0)
assert.ok(checkRegistry(enabled.replace('【前端】专项', '【总控】其他')).length > 0)
assert.ok(checkRegistry(enabled.replaceAll(project, '6f874ec3-d3b2-49bd-8625-327f4d685f98')).length > 0)
})