#!/usr/bin/env node const assert = require("assert"); const cp = require("child_process"); const fs = require("fs"); const path = require("path"); const repo = path.resolve(__dirname, ".."); const wasmTarget = path.join( repo, "target", "wasm32-unknown-unknown", "release", "launch_build_demo.wasm" ); cp.execFileSync( "cargo", [ "build", "--release", "-p", "launch-build-demo", "--target", "wasm32-unknown-unknown", ], { cwd: repo, stdio: "inherit", env: { ...process.env, CARGO_PROFILE_RELEASE_OPT_LEVEL: "z", CARGO_PROFILE_RELEASE_LTO: "thin", CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "1", }, } ); assert(fs.existsSync(wasmTarget), `missing compiled Wasm module at ${wasmTarget}`); const output = cp.execFileSync( "cargo", [ "run", "-q", "-p", "clusterflux-node", "--bin", "clusterflux-wasmtime-smoke", "--", wasmTarget, "task_add_one", "41", "42", ], { cwd: repo, encoding: "utf8" } ); const report = JSON.parse(output); assert.strictEqual(report.type, "wasmtime_task_smoke"); assert.strictEqual(report.export, "task_add_one"); assert.strictEqual(report.arg, 41); assert.strictEqual(report.result, 42); const debugOutput = cp.execFileSync( "cargo", [ "run", "-q", "-p", "clusterflux-node", "--bin", "clusterflux-wasmtime-smoke", "--", "--debug-freeze-resume", wasmTarget, "task_add_one", "41", "42", ], { cwd: repo, encoding: "utf8" } ); const debugReport = JSON.parse(debugOutput); assert.strictEqual(debugReport.type, "wasmtime_debug_freeze_resume_smoke"); assert.strictEqual(debugReport.export, "task_add_one"); assert.strictEqual(debugReport.task, "task_add_one"); assert.strictEqual(debugReport.frozen_state, "Frozen"); assert.strictEqual(debugReport.resumed_state, "Running"); assert(debugReport.stack_frames.some((frame) => String(frame).includes("task_add_one"))); assert( debugReport.local_values.some( ([name, value]) => name === "wasm_local_0" && String(value).includes("41") ), "Wasmtime debug snapshot must expose the real i32 argument as a frame local" ); assert.strictEqual(debugReport.node_runtime_captured_wasm_locals, true); assert.strictEqual(debugReport.result, 42); assert.strictEqual(debugReport.node_runtime_reached_wasm_task, true); const hostCommandOutput = cp.execFileSync( "cargo", [ "run", "-q", "-p", "clusterflux-node", "--bin", "clusterflux-wasmtime-smoke", "--", "--host-command", wasmTarget, "compile_linux", ], { cwd: repo, encoding: "utf8" } ); const hostCommandReport = JSON.parse(hostCommandOutput); assert.strictEqual(hostCommandReport.type, "wasmtime_host_command_smoke"); assert.strictEqual(hostCommandReport.task, "compile_linux"); assert.match(hostCommandReport.export, /^clusterflux_task_v1_[0-9a-f]{64}$/); assert.strictEqual(hostCommandReport.program, "cc"); assert.deepStrictEqual(hostCommandReport.args, [ "-Os", "-static", "-s", "fixture/hello-clusterflux.c", "-o", "/clusterflux/output/hello-clusterflux", ]); assert.strictEqual(hostCommandReport.working_directory, "/workspace"); assert.deepStrictEqual(hostCommandReport.environment_variables, { SOURCE_DATE_EPOCH: "0", }); assert.strictEqual(hostCommandReport.timeout_ms, 180000); assert.strictEqual(hostCommandReport.network, "disabled"); assert.strictEqual(hostCommandReport.status_code, 0); assert.strictEqual(hostCommandReport.stdout, ""); assert.strictEqual(hostCommandReport.artifact_name, "hello-clusterflux"); assert.strictEqual(hostCommandReport.artifact_size_bytes, "hello-clusterflux".length); assert.match(hostCommandReport.artifact_digest, /^sha256:[0-9a-f]{64}$/); assert.strictEqual(hostCommandReport.node_host_import, "clusterflux.command_run_v1"); assert.strictEqual(hostCommandReport.artifact_host_import, "clusterflux.vfs_operation_v1"); assert.strictEqual(hostCommandReport.flagship_linux_build_task, true); assert.strictEqual(hostCommandReport.node_executed_host_command, true); assert.strictEqual(hostCommandReport.hosted_control_plane_ran_command, false); const artifactOutput = cp.execFileSync( "cargo", [ "run", "-q", "-p", "clusterflux-node", "--bin", "clusterflux-wasmtime-smoke", "--", "--task-artifact", wasmTarget, "package_release", ], { cwd: repo, encoding: "utf8" } ); const artifactReport = JSON.parse(artifactOutput); assert.strictEqual(artifactReport.type, "wasmtime_task_artifact_smoke"); assert.strictEqual(artifactReport.task, "package_release"); assert.strictEqual(artifactReport.artifact_name, "release.tar"); assert.strictEqual(artifactReport.artifact_size_bytes, "release.tar".length); assert.match(artifactReport.artifact_digest, /^sha256:[0-9a-f]{64}$/); assert.strictEqual(artifactReport.host_import, "clusterflux.vfs_operation_v1"); assert.strictEqual(artifactReport.host_issued_handle_returned, true); assert.ok(artifactReport.artifact.id.endsWith(artifactReport.artifact_digest.slice("sha256:".length))); console.log("Wasmtime node smoke passed");