Files
tribes-supertest/tests/test-dev-branch-script.test.ts
self 63af948463 refactor: align provider configuration model
Drop provider account ids from runtime config and node-create CLI calls. Update sanitized Legion state handling for the providers field and keep helper tests portable on macOS temp paths.
2026-06-12 13:32:17 +02:00

122 lines
3.8 KiB
TypeScript

import assert from "node:assert/strict"
import { spawnSync } from "node:child_process"
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join, resolve } from "node:path"
import test from "node:test"
const script = resolve("scripts/test-dev-branch")
const supertestLegionScript = resolve("scripts/supertest-legion")
function normalizeMacosTempPath(path: string): string {
return path.replace(/^\/private\/var\//, "/var/")
}
function normalizeEnvPaths(env: Record<string, string>): Record<string, string> {
return Object.fromEntries(
Object.entries(env).map(([key, value]) => [key, normalizeMacosTempPath(value)])
)
}
test("test-dev-branch helper is valid Perl", () => {
const result = spawnSync("perl", ["-c", script], {
encoding: "utf8"
})
assert.equal(result.status, 0, result.stderr)
assert.match(result.stderr, /syntax OK/)
})
test("test-dev-branch helper documents core commands", () => {
const result = spawnSync(script, ["--help"], {
encoding: "utf8"
})
assert.equal(result.status, 0, result.stderr)
assert.match(result.stdout, /prepare/)
assert.match(result.stdout, /reset/)
assert.match(result.stdout, /ssh/)
assert.match(result.stdout, /rpc/)
assert.match(result.stdout, /--plugin NAME/)
assert.match(result.stdout, /self-signed/)
})
test("supertest-legion helper is valid bash", () => {
const result = spawnSync("bash", ["-n", supertestLegionScript], {
encoding: "utf8"
})
assert.equal(result.status, 0, result.stderr)
})
test("supertest-legion helper documents ssh and rpc commands", () => {
const result = spawnSync(supertestLegionScript, ["--help"], {
encoding: "utf8"
})
assert.equal(result.status, 0, result.stderr)
assert.match(result.stdout, /node list --json/)
assert.match(result.stdout, /ssh <node>/)
assert.match(result.stdout, /rpc <node>/)
assert.match(result.stdout, /latest\.json/)
})
test("supertest-legion helper reuses latest run metadata", () => {
const root = mkdtempSync(join(tmpdir(), "supertest-legion-"))
const legionRepo = join(root, "legion")
const latestPath = join(root, "latest.json")
try {
mkdirSync(legionRepo)
writeFileSync(
join(root, "latest.json"),
JSON.stringify({
legionRepo,
legionCliEntry: "cli-main.ts",
legionStateDir: join(root, "state"),
legionCacheDir: join(root, "cache"),
certMode: "self-signed"
})
)
writeFileSync(
join(root, "legion", "cli-main.ts"),
`console.log(JSON.stringify({
cwd: process.cwd(),
args: process.argv.slice(2),
env: {
LEGION_STATE_DIR: process.env.LEGION_STATE_DIR,
LEGION_CACHE_DIR: process.env.LEGION_CACHE_DIR,
LEGION_APP_ROOT: process.env.LEGION_APP_ROOT,
LEGION_TEST_CERT_MODE: process.env.LEGION_TEST_CERT_MODE
}
}))\n`
)
const result = spawnSync(supertestLegionScript, ["rpc", "node-a", "1 + 1"], {
encoding: "utf8",
env: {
...process.env,
LEGION_UNLOCK_PASSWORD: "unlock-secret",
SUPERTEST_LATEST: latestPath
}
})
assert.equal(result.status, 0, result.stderr)
const output = JSON.parse(result.stdout) as {
cwd: string
args: string[]
env: Record<string, string>
}
assert.equal(normalizeMacosTempPath(output.cwd), normalizeMacosTempPath(legionRepo))
assert.deepEqual(output.args, ["node", "rpc", "node-a", "--", "1 + 1"])
assert.deepEqual(normalizeEnvPaths(output.env), {
LEGION_STATE_DIR: normalizeMacosTempPath(join(root, "state")),
LEGION_CACHE_DIR: normalizeMacosTempPath(join(root, "cache")),
LEGION_APP_ROOT: normalizeMacosTempPath(legionRepo),
LEGION_TEST_CERT_MODE: "self-signed"
})
} finally {
rmSync(root, { recursive: true, force: true })
}
})