const assert = require("assert"); const cp = require("child_process"); const fs = require("fs"); const path = require("path"); const repo = path.resolve(__dirname, ".."); const hello = path.join(repo, "examples/hello-build"); const recovery = path.join(repo, "examples/recovery-build"); const conformance = path.join(repo, "tests/fixtures/runtime-conformance"); const source = fs.readFileSync(path.join(hello, "src/lib.rs"), "utf8"); const nonblank = source.split(/\r?\n/).filter((line) => line.trim()).length; assert(nonblank <= 80, "hello-build source must stay below 80 nonblank lines"); for (const forbidden of [ "#[cfg", "unsafe", "extern \"C\"", "no_mangle", "sha256:", ".task_id(", "#[test]", "unwrap()", "expect(", ]) { assert(!source.includes(forbidden), "hello-build leaked implementation detail: " + forbidden); } assert.strictEqual((source.match(/#\[clusterflux::task/g) || []).length, 1); assert.strictEqual((source.match(/#\[clusterflux::main/g) || []).length, 1); assert(source.includes("source::current_project().snapshot().await?")); assert(source.includes("clusterflux::spawn!(compile(source))")); assert(source.includes(".on(clusterflux::env!(\"linux\"))")); assert(source.includes(".run()")); assert(source.includes("fs::publish")); assert(fs.existsSync(path.join(hello, "fixture/hello-clusterflux.c"))); assert(fs.existsSync(path.join(hello, "envs/linux/Containerfile"))); const recoverySource = fs.readFileSync(path.join(recovery, "src/lib.rs"), "utf8"); assert.strictEqual((recoverySource.match(/spawn!\(build_lane/g) || []).length, 2); assert(recoverySource.includes("TaskFailurePolicy::AwaitOperator")); assert(recoverySource.includes("\"exit 23\"")); for (const forbidden of ["#[cfg", "unsafe", "extern \"C\"", "no_mangle", ".task_id("]) { assert(!recoverySource.includes(forbidden), "recovery-build leaked " + forbidden); } const fixtureSource = fs.readFileSync(path.join(conformance, "src/lib.rs"), "utf8"); assert(fixtureSource.includes("task_trap")); assert(fixtureSource.includes("cooperative_cancellation_probe")); assert(!fs.existsSync(path.join(repo, "examples", "launch-" + "build-demo"))); assert(!fs.existsSync(path.join(hello, "src/bin/sdk-product-runtime.rs"))); const args = [ "run", "-q", "-p", "clusterflux-cli", "--bin", "clusterflux", "--", "build", "--project", hello, "--json", ]; const report = JSON.parse(cp.execFileSync("cargo", args, { cwd: repo, env: process.env, encoding: "utf8", })); assert(report.bundle_artifact); assert(report.bundle.metadata.selected_inputs.some((input) => input.path === "src/lib.rs")); assert(report.bundle.metadata.task_metadata.entrypoints.includes("build")); const tasks = JSON.parse( fs.readFileSync( path.resolve(repo, report.bundle_artifact.directory, "task-descriptors.json"), "utf8" ) ); assert(tasks.some((task) => task.name === "compile")); assert(tasks.some((task) => task.name === "snapshot_current_project")); console.log("primary and recovery example smoke passed");