UNPKG

@moonwall/cli

Version:

Testing framework for the Moon family of projects

1,447 lines (1,417 loc) 176 kB
var __defProp = Object.defineProperty; var __getOwnPropNames = Object.getOwnPropertyNames; var __esm = (fn, res) => function __init() { return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; // src/lib/configReader.ts var configReader_exports = {}; __export(configReader_exports, { cacheConfig: () => cacheConfig, configExists: () => configExists, configSetup: () => configSetup, getEnvironmentFromConfig: () => getEnvironmentFromConfig, importAsyncConfig: () => importAsyncConfig, importConfig: () => importConfig, importJsonConfig: () => importJsonConfig, isEthereumDevConfig: () => isEthereumDevConfig, isEthereumZombieConfig: () => isEthereumZombieConfig, isOptionSet: () => isOptionSet, loadEnvVars: () => loadEnvVars, parseZombieConfigForBins: () => parseZombieConfigForBins }); import { readFile, access } from "fs/promises"; import { readFileSync, existsSync, constants } from "fs"; import JSONC from "jsonc-parser"; import path, { extname } from "path"; async function configExists() { try { await access(process.env.MOON_CONFIG_PATH || "", constants.R_OK); return true; } catch { return false; } } function configSetup(args) { if (args.includes("--configFile") || process.argv.includes("-c")) { const index = process.argv.indexOf("--configFile") !== -1 ? process.argv.indexOf("--configFile") : process.argv.indexOf("-c") !== -1 ? process.argv.indexOf("-c") : 0; if (index === 0) { throw new Error("Invalid configFile argument"); } const configFile = process.argv[index + 1]; if (!existsSync(configFile)) { throw new Error(`Config file not found at "${configFile}"`); } process.env.MOON_CONFIG_PATH = configFile; } if (!process.env.MOON_CONFIG_PATH) { process.env.MOON_CONFIG_PATH = "moonwall.config.json"; } } async function parseConfig(filePath) { let result; const file = await readFile(filePath, "utf8"); switch (extname(filePath)) { case ".json": result = JSON.parse(file); break; case ".config": result = JSONC.parse(file); break; default: result = void 0; break; } return result; } function parseConfigSync(filePath) { let result; const file = readFileSync(filePath, "utf8"); switch (extname(filePath)) { case ".json": result = JSON.parse(file); break; case ".config": result = JSONC.parse(file); break; default: result = void 0; break; } return result; } async function importConfig(configPath) { return await import(configPath); } function isOptionSet(option) { const env = getEnvironmentFromConfig(); const optionValue = traverseConfig(env, option); return optionValue !== void 0; } function isEthereumZombieConfig() { const env = getEnvironmentFromConfig(); return env.foundation.type === "zombie" && !env.foundation.zombieSpec.disableDefaultEthProviders; } function isEthereumDevConfig() { const env = getEnvironmentFromConfig(); return env.foundation.type === "dev" && !env.foundation.launchSpec[0].disableDefaultEthProviders; } async function cacheConfig() { const configPath = process.env.MOON_CONFIG_PATH; if (!configPath) { throw new Error(`Environment ${process.env.MOON_TEST_ENV} not found in config`); } const filePath = path.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath); try { const config = parseConfigSync(filePath); const replacedConfig = replaceEnvVars(config); cachedConfig = replacedConfig; } catch (e) { console.error(e); throw new Error(`Error import config at ${filePath}`); } } function getEnvironmentFromConfig() { const globalConfig = importJsonConfig(); const config = globalConfig.environments.find(({ name }) => name === process.env.MOON_TEST_ENV); if (!config) { throw new Error(`Environment ${process.env.MOON_TEST_ENV} not found in config`); } return config; } function importJsonConfig() { if (cachedConfig) { return cachedConfig; } const configPath = process.env.MOON_CONFIG_PATH; if (!configPath) { throw new Error("No moonwall config path set. This is a defect, please raise it."); } const filePath = path.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath); try { const config = parseConfigSync(filePath); const replacedConfig = replaceEnvVars(config); cachedConfig = replacedConfig; return cachedConfig; } catch (e) { console.error(e); throw new Error(`Error import config at ${filePath}`); } } async function importAsyncConfig() { if (cachedConfig) { return cachedConfig; } const configPath = process.env.MOON_CONFIG_PATH; if (!configPath) { throw new Error("No moonwall config path set. This is a defect, please raise it."); } const filePath = path.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath); try { const config = await parseConfig(filePath); const replacedConfig = replaceEnvVars(config); cachedConfig = replacedConfig; return cachedConfig; } catch (e) { console.error(e); throw new Error(`Error import config at ${filePath}`); } } function loadEnvVars() { const env = getEnvironmentFromConfig(); for (const envVar of env.envVars || []) { const [key, value] = envVar.split("="); process.env[key] = value; } } function replaceEnvVars(value) { if (typeof value === "string") { return value.replace(/\$\{([^}]+)\}/g, (match, group) => { const envVarValue = process.env[group]; return envVarValue || match; }); } if (Array.isArray(value)) { return value.map(replaceEnvVars); } if (typeof value === "object" && value !== null) { return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, replaceEnvVars(v)])); } return value; } function traverseConfig(configObj, option) { if (typeof configObj !== "object" || !configObj) return void 0; if (Object.hasOwn(configObj, option)) { return configObj[option]; } for (const key in configObj) { const result = traverseConfig(configObj[key], option); if (result !== void 0) { return result; } } return void 0; } function parseZombieConfigForBins(zombieConfigPath) { const config = JSON.parse(readFileSync(zombieConfigPath, "utf8")); const commands = []; if (config.relaychain?.default_command) { commands.push(path.basename(config.relaychain.default_command)); } if (config.parachains) { for (const parachain of config.parachains) { if (parachain.collator?.command) { commands.push(path.basename(parachain.collator.command)); } } } return [...new Set(commands)].sort(); } var cachedConfig; var init_configReader = __esm({ "src/lib/configReader.ts"() { "use strict"; } }); // src/internal/logging.ts var originalWrite = process.stderr.write.bind(process.stderr); var blockList = [ "has multiple versions, ensure that there is only one installed", "Unable to map [u8; 32] to a lookup index", "Either remove and explicitly install matching versions or dedupe using your package manager." ]; process.stderr.write = (chunk, encodingOrCallback, callback) => { let shouldWrite = true; if (typeof chunk === "string") { shouldWrite = !blockList.some((phrase) => chunk.includes(phrase)); } if (shouldWrite) { if (typeof encodingOrCallback === "function") { return originalWrite.call(process.stderr, chunk, void 0, encodingOrCallback); } return originalWrite.call(process.stderr, chunk, encodingOrCallback, callback); } const cb = typeof encodingOrCallback === "function" ? encodingOrCallback : callback; if (cb) cb(null); return true; }; // src/internal/cmdFunctions/downloader.ts import { SingleBar, Presets } from "cli-progress"; import fs from "fs"; import { Readable } from "stream"; async function downloader(url, outputPath) { const tempPath = `${outputPath}.tmp`; const writeStream = fs.createWriteStream(tempPath); let transferredBytes = 0; if (url.startsWith("ws")) { console.log("You've passed a WebSocket URL to fetch. Is this intended?"); } const headers = {}; if (process.env.GITHUB_TOKEN) { headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`; } const response = await fetch(url, { headers }); if (!response.body) { throw new Error("No response body"); } const readStream = Readable.fromWeb(response.body); const contentLength = Number.parseInt(response.headers.get("Content-Length") || "0", 10); const progressBar = initializeProgressBar(); progressBar.start(contentLength, 0); readStream.pipe(writeStream); await new Promise((resolve, reject) => { readStream.on("data", (chunk) => { transferredBytes += chunk.length; progressBar.update(transferredBytes); }); readStream.on("end", () => { writeStream.end(); progressBar.stop(); process.stdout.write(" \u{1F4BE} Saving binary artifact..."); writeStream.close(() => resolve()); }); readStream.on("error", (error) => { reject(error); }); }); fs.writeFileSync(outputPath, fs.readFileSync(tempPath)); fs.rmSync(tempPath); } function initializeProgressBar() { const options = { etaAsynchronousUpdate: true, etaBuffer: 40, format: "Downloading: [{bar}] {percentage}% | ETA: {eta_formatted} | {value}/{total}" }; return new SingleBar(options, Presets.shades_classic); } // src/internal/cmdFunctions/fetchArtifact.ts import fs2 from "fs/promises"; import path2 from "path"; import semver from "semver"; import chalk from "chalk"; // src/internal/processHelpers.ts import child_process from "child_process"; import { promisify } from "util"; import { createLogger } from "@moonwall/util"; var logger = createLogger({ name: "actions:runner" }); var debug = logger.debug.bind(logger); var execAsync = promisify(child_process.exec); var withTimeout = (promise, ms) => { return Promise.race([ promise, new Promise( (_, reject) => setTimeout(() => reject(new Error("Operation timed out")), ms) ) ]); }; async function runTask(cmd, { cwd, env } = { cwd: process.cwd() }, title) { debug(`${title ? `Title: ${title} ` : ""}Running task on directory ${cwd}: ${cmd} `); try { const result = await execAsync(cmd, { cwd, env }); return result.stdout; } catch (error) { const status = error.status ? `[${error.status}]` : "[Unknown Status]"; const message = error.message ? `${error.message}` : "No Error Message"; debug(`Caught exception in command execution. Error[${status}] ${message}`); throw error; } } async function spawnTask(cmd, { cwd, env } = { cwd: process.cwd() }, title) { debug(`${title ? `Title: ${title} ` : ""}Running task on directory ${process.cwd()}: ${cmd} `); try { const process2 = child_process.spawn( cmd.split(" ")[0], cmd.split(" ").slice(1).filter((a) => a.length > 0), { cwd, env } ); return process2; } catch (error) { const status = error.status ? `[${error.status}]` : "[Unknown Status]"; const message = error.message ? `${error.message}` : "No Error Message"; debug(`Caught exception in command execution. Error[${status}] ${message} `); throw error; } } // src/internal/cmdFunctions/fetchArtifact.ts import { minimatch } from "minimatch"; // src/lib/repoDefinitions/moonbeam.ts var repo = { name: "moonbeam", binaries: [ { name: "moonbeam", defaultArgs: [ "--no-hardware-benchmarks", "--no-telemetry", "--reserved-only", "--rpc-cors=all", "--unsafe-rpc-external", "--unsafe-force-node-key-generation", "--no-grandpa", "--sealing=manual", "--force-authoring", "--database=paritydb", "--no-prometheus", "--alice", "--chain=moonbase-dev", "--tmp" ] }, { name: "moonbase-runtime" }, { name: "moonbeam-runtime" }, { name: "moonriver-runtime" } ], ghAuthor: "moonbeam-foundation", ghRepo: "moonbeam" }; var moonbeam_default = repo; // src/lib/repoDefinitions/polkadot.ts var repo2 = { name: "polkadot", binaries: [ { name: "polkadot" }, { name: "polkadot-prepare-worker" }, { name: "polkadot-execute-worker" } ], ghAuthor: "paritytech", ghRepo: "polkadot-sdk" }; var polkadot_default = repo2; // src/lib/repoDefinitions/tanssi.ts var repo3 = { name: "tanssi", binaries: [ { name: "tanssi-node", defaultArgs: ["--dev", "--sealing=manual", "--no-hardware-benchmarks"] }, { name: "container-chain-template-simple-node" }, { name: "container-chain-template-frontier-node" } ], ghAuthor: "moondance-labs", ghRepo: "tanssi" }; var tanssi_default = repo3; // src/lib/repoDefinitions/index.ts init_configReader(); async function allReposAsync() { const defaultRepos = [moonbeam_default, polkadot_default, tanssi_default]; const globalConfig = await importAsyncConfig(); const importedRepos = globalConfig.additionalRepos || []; return [...defaultRepos, ...importedRepos]; } function standardRepos() { const defaultRepos = [moonbeam_default, polkadot_default, tanssi_default]; return [...defaultRepos]; } // src/internal/cmdFunctions/fetchArtifact.ts init_configReader(); import { execSync } from "child_process"; import { Octokit } from "@octokit/rest"; import { confirm } from "@inquirer/prompts"; var octokit = new Octokit({ baseUrl: "https://api.github.com", log: { debug: () => { }, info: () => { }, warn: console.warn, error: console.error } }); async function fetchArtifact(args) { if (args.path && await fs2.access(args.path).catch(() => true)) { console.log("Folder not exists, creating"); fs2.mkdir(args.path); } const checkOverwrite = async (path13) => { try { await fs2.access(path13, fs2.constants.R_OK); if (args.overwrite) { console.log("File exists, overwriting ..."); } else { const cont = await confirm({ message: "File exists, do you want to overwrite?" }); if (!cont) { return false; } } } catch { console.log("File does not exist, creating ..."); } return true; }; const binary = args.bin; const repos = await configExists() ? await allReposAsync() : standardRepos(); const repo4 = repos.find((network) => network.binaries.find((bin) => bin.name === binary)); if (!repo4) { throw new Error(`Downloading ${binary} unsupported`); } const enteredPath = args.path ? args.path : "tmp/"; const releases = await octokit.rest.repos.listReleases({ owner: repo4.ghAuthor, repo: repo4.ghRepo }); if (releases.status !== 200 || releases.data.length === 0) { throw new Error(`No releases found for ${repo4.ghAuthor}.${repo4.ghRepo}, try again later.`); } const release = binary.includes("-runtime") ? releases.data.find((release2) => { if (args.ver === "latest") { return release2.assets.find((asset2) => asset2.name.includes(binary)); } return release2.assets.find((asset2) => asset2.name === `${binary}-${args.ver}.wasm`); }) : args.ver === "latest" ? releases.data.find((release2) => release2.assets.find((asset2) => asset2.name === binary)) : releases.data.filter((release2) => release2.tag_name.includes(args.ver || "")).find((release2) => release2.assets.find((asset2) => minimatch(asset2.name, binary))); if (!release) { throw new Error(`Release not found for ${args.ver}`); } const asset = binary.includes("-runtime") ? release.assets.find((asset2) => asset2.name.includes(binary) && asset2.name.includes("wasm")) : release.assets.find((asset2) => minimatch(asset2.name, binary)); if (!asset) { throw new Error(`Asset not found for ${binary}`); } if (!binary.includes("-runtime")) { const url = asset.browser_download_url; const filename = path2.basename(url); const binPath = args.outputName ? args.outputName : path2.join("./", enteredPath, filename); if (await checkOverwrite(binPath) === false) { console.log("User chose not to overwrite existing file, exiting."); return; } await downloader(url, binPath); await fs2.chmod(binPath, "755"); if (filename.endsWith(".tar.gz")) { const outputBuffer = execSync(`tar -xzvf ${binPath}`); const cleaned = outputBuffer.toString().split("\n")[0].split("/")[0]; const version2 = (await runTask(`./${cleaned} --version`)).trim(); process.stdout.write(` ${chalk.green(version2.trim())} \u2713 `); return; } const version = (await runTask(`./${binPath} --version`)).trim(); process.stdout.write(`${path2.basename(binPath)} ${chalk.green(version.trim())} \u2713 `); return; } const binaryPath = args.outputName ? args.outputName : path2.join("./", args.path || "", `${args.bin}-${args.ver}.wasm`); if (await checkOverwrite(binaryPath) === false) { console.log("User chose not to overwrite existing file, exiting."); return; } await downloader(asset.browser_download_url, binaryPath); await fs2.chmod(binaryPath, "755"); process.stdout.write(` ${chalk.green("done")} \u2713 `); return; } async function getVersions(name, runtime = false) { const repos = await configExists() ? await allReposAsync() : standardRepos(); const repo4 = repos.find((network) => network.binaries.find((bin) => bin.name === name)); if (!repo4) { throw new Error(`Network not found for ${name}`); } const releases = await octokit.rest.repos.listReleases({ owner: repo4.ghAuthor, repo: repo4.ghRepo }); if (releases.status !== 200 || releases.data.length === 0) { throw new Error(`No releases found for ${repo4.ghAuthor}.${repo4.ghRepo}, try again later.`); } const versions = releases.data.map((release) => { let tag = release.tag_name; if (release.tag_name.includes("v")) { tag = tag.split("v")[1]; } if (tag.includes("-rc")) { tag = tag.split("-rc")[0]; } return tag; }).filter( (version) => runtime && version.includes("runtime") || !runtime && !version.includes("runtime") ).map((version) => version.replace("runtime-", "")); const set = new Set(versions); return runtime ? [...set] : [...set].sort( (a, b) => semver.valid(a) && semver.valid(b) ? semver.rcompare(a, b) : a ); } // src/internal/cmdFunctions/initialisation.ts import fs3 from "fs/promises"; import { confirm as confirm2, input, number } from "@inquirer/prompts"; async function createFolders() { await fs3.mkdir("scripts").catch(() => "scripts folder already exists, skipping"); await fs3.mkdir("tests").catch(() => "tests folder already exists, skipping"); await fs3.mkdir("tmp").catch(() => "tmp folder already exists, skipping"); } async function generateConfig(argv) { let answers; try { await fs3.access("moonwall.config.json"); console.log("\u2139\uFE0F Config file already exists at this location. Quitting."); return; } catch (_) { } if (argv.acceptAllDefaults) { answers = { label: "moonwall_config", timeout: 3e4, environmentName: "default_env", foundation: "dev", testDir: "tests/default/" }; } else { while (true) { answers = { label: await input({ message: "Provide a label for the config file", default: "moonwall_config" }), timeout: await number({ message: "Provide a global timeout value", default: 3e4 }) ?? 3e4, environmentName: await input({ message: "Provide a name for this environment", default: "default_env" }), foundation: "dev", testDir: await input({ message: "Provide the path for where tests for this environment are kept", default: "tests/default/" }) }; const proceed = await confirm2({ message: "Would you like to generate this config? (no to restart from beginning)" }); if (proceed) { break; } console.log("Restarting the configuration process..."); } } const config = createSampleConfig({ label: answers.label, timeout: answers.timeout, environmentName: answers.environmentName, foundation: answers.foundation, testDir: answers.testDir }); const JSONBlob = JSON.stringify(config, null, 3); await fs3.writeFile("moonwall.config.json", JSONBlob, "utf-8"); process.env.MOON_CONFIG_PATH = "./moonwall.config.json"; await createSampleTest(answers.testDir); console.log("Test directory created at: ", answers.testDir); console.log( `You can now add tests to this directory and run them with 'pnpm moonwall test ${answers.environmentName}'` ); console.log("Goodbye! \u{1F44B}"); } function createConfig(options) { return { label: options.label, defaultTestTimeout: options.timeout, environments: [ { name: options.environmentName, testFileDir: [options.testDir], foundation: { type: options.foundation } } ] }; } function createSampleConfig(options) { return { $schema: "https://raw.githubusercontent.com/Moonsong-Labs/moonwall/main/packages/types/config_schema.json", label: options.label, defaultTestTimeout: options.timeout, environments: [ { name: options.environmentName, testFileDir: [options.testDir], multiThreads: false, foundation: { type: "dev", launchSpec: [ { name: "moonbeam", useDocker: true, newRpcBehaviour: true, binPath: "moonbeamfoundation/moonbeam" } ] } } ] }; } async function createSampleTest(directory) { await fs3.mkdir(directory, { recursive: true }); await fs3.writeFile(`${directory}/sample.test.ts`, sampleTest, "utf-8"); } var sampleTest = `import { describeSuite, expect } from "@moonwall/cli"; describeSuite({ id: "B01", title: "Sample test suite for moonbeam network", foundationMethods: "dev", testCases: ({ context, it }) => { const ALITH_ADDRESS = "0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac" it({ id: "T01", title: "Test that API is connected correctly", test: async () => { const chainName = context.pjsApi.consts.system.version.specName.toString(); const specVersion = context.pjsApi.consts.system.version.specVersion.toNumber(); expect(chainName.length).toBeGreaterThan(0) expect(chainName).toBe("moonbase") expect(specVersion).toBeGreaterThan(0) }, }); it({ id: "T02", title: "Test that chain queries can be made", test: async () => { const balance = (await context.pjsApi.query.system.account(ALITH_ADDRESS)).data.free expect(balance.toBigInt()).toBeGreaterThan(0n) }, }); }, }); `; // src/internal/cmdFunctions/tempLogs.ts import path3 from "path"; import fs4 from "fs"; function clearNodeLogs(silent = true) { const dirPath = path3.join(process.cwd(), "tmp", "node_logs"); if (!fs4.existsSync(dirPath)) { fs4.mkdirSync(dirPath, { recursive: true }); } const files = fs4.readdirSync(dirPath); for (const file of files) { !silent && console.log(`Deleting log: ${file}`); if (file.endsWith(".log")) { fs4.unlinkSync(path3.join(dirPath, file)); } } } function reportLogLocation(silent = false) { const dirPath = path3.join(process.cwd(), "tmp", "node_logs"); if (!fs4.existsSync(dirPath)) { fs4.mkdirSync(dirPath, { recursive: true }); } const result = fs4.readdirSync(dirPath); let consoleMessage = ""; let filePath = ""; try { filePath = process.env.MOON_ZOMBIE_DIR ? process.env.MOON_ZOMBIE_DIR : process.env.MOON_LOG_LOCATION ? process.env.MOON_LOG_LOCATION : path3.join( dirPath, result.find((file) => path3.extname(file) === ".log") || "no_logs_found" ); consoleMessage = ` \u{1FAB5} Log location: ${filePath}`; } catch (e) { console.error(e); } !silent && console.log(consoleMessage); return filePath.trim(); } // src/internal/commandParsers.ts import { createLogger as createLogger3 } from "@moonwall/util"; import path4 from "path"; import net from "net"; import { Effect as Effect3 } from "effect"; // src/lib/shardManager.ts var ShardManager = class _ShardManager { static instance = null; _shardInfo = null; constructor() { } static getInstance() { if (!_ShardManager.instance) { _ShardManager.instance = new _ShardManager(); } return _ShardManager.instance; } /** * Initialize shard configuration from command line argument or environment * @param shardArg Optional shard argument from CLI (format: "current/total") */ initializeSharding(shardArg) { if (shardArg) { this._shardInfo = this.parseShardString(shardArg); process.env.MOONWALL_TEST_SHARD = shardArg; } else if (process.env.MOONWALL_TEST_SHARD) { this._shardInfo = this.parseShardString(process.env.MOONWALL_TEST_SHARD); } else { this._shardInfo = { current: 1, total: 1, isSharded: false }; } } /** * Get current shard information */ getShardInfo() { if (!this._shardInfo) { this.initializeSharding(); } return this._shardInfo; } /** * Get shard index (0-based) for port calculations */ getShardIndex() { return this.getShardInfo().current - 1; } /** * Get total number of shards */ getTotalShards() { return this.getShardInfo().total; } /** * Check if sharding is enabled */ isSharded() { return this.getShardInfo().isSharded; } /** * Reset shard configuration (mainly for testing) */ reset() { this._shardInfo = null; delete process.env.MOONWALL_TEST_SHARD; } parseShardString(shardString) { if (!shardString.includes("/")) { throw new Error( `Invalid shard format: "${shardString}". Expected format: "current/total" (e.g., "1/3")` ); } const [currentStr, totalStr] = shardString.split("/"); const current = parseInt(currentStr, 10); const total = parseInt(totalStr, 10); if (Number.isNaN(current) || Number.isNaN(total) || current < 1 || total < 1) { throw new Error( `Invalid shard numbers in "${shardString}". Both current and total must be positive integers.` ); } if (current > total) { throw new Error( `Invalid shard configuration: current shard ${current} cannot be greater than total ${total}` ); } const isSharded = total > 1; return { current, total, isSharded }; } }; var shardManager = ShardManager.getInstance(); // src/internal/commandParsers.ts import invariant from "tiny-invariant"; // src/internal/effect/StartupCacheService.ts import { Command, FileSystem as FileSystem2, Path } from "@effect/platform"; import { NodeContext } from "@effect/platform-node"; import { createLogger as createLogger2 } from "@moonwall/util"; import { Context, Duration as Duration2, Effect as Effect2, Layer, Option, Stream } from "effect"; import * as crypto from "crypto"; // src/internal/effect/errors.ts import { Data } from "effect"; var PortDiscoveryError = class extends Data.TaggedError("PortDiscoveryError") { }; var NodeLaunchError = class extends Data.TaggedError("NodeLaunchError") { }; var NodeReadinessError = class extends Data.TaggedError("NodeReadinessError") { }; var ProcessError = class extends Data.TaggedError("ProcessError") { }; var StartupCacheError = class extends Data.TaggedError("StartupCacheError") { }; var FileLockError = class extends Data.TaggedError("FileLockError") { }; // src/internal/effect/FileLock.ts import { FileSystem } from "@effect/platform"; import { Duration, Effect, Schedule } from "effect"; import * as os from "os"; var LOCK_MAX_AGE = Duration.minutes(2); var LOCK_POLL_INTERVAL = Duration.millis(500); var isProcessAlive = (pid) => Effect.try(() => { process.kill(pid, 0); return true; }).pipe(Effect.orElseSucceed(() => false)); var isLockStale = (info) => Effect.gen(function* () { const isTimedOut = Date.now() - info.timestamp > Duration.toMillis(LOCK_MAX_AGE); if (isTimedOut) return true; const isSameHost = info.hostname === os.hostname(); if (!isSameHost) return false; const alive = yield* isProcessAlive(info.pid); return !alive; }); var cleanupStaleLock = (lockPath) => Effect.gen(function* () { const fs16 = yield* FileSystem.FileSystem; const infoPath = `${lockPath}/lock.json`; const exists = yield* fs16.exists(infoPath).pipe(Effect.orElseSucceed(() => false)); if (!exists) return; const content = yield* fs16.readFileString(infoPath).pipe(Effect.orElseSucceed(() => "")); const info = yield* Effect.try(() => JSON.parse(content)).pipe( Effect.orElseSucceed(() => null) ); if (!info) return; const stale = yield* isLockStale(info); if (stale) { yield* fs16.remove(lockPath, { recursive: true }).pipe(Effect.ignore); } }); var writeLockInfo = (lockPath) => Effect.gen(function* () { const fs16 = yield* FileSystem.FileSystem; const info = { pid: process.pid, timestamp: Date.now(), hostname: os.hostname() }; yield* fs16.writeFileString(`${lockPath}/lock.json`, JSON.stringify(info)).pipe(Effect.ignore); }); var tryAcquireLock = (lockPath) => Effect.gen(function* () { const fs16 = yield* FileSystem.FileSystem; yield* cleanupStaleLock(lockPath); yield* fs16.makeDirectory(lockPath).pipe(Effect.mapError(() => new FileLockError({ reason: "acquisition_failed", lockPath }))); yield* writeLockInfo(lockPath); }); var acquireFileLock = (lockPath, timeout = Duration.minutes(2)) => tryAcquireLock(lockPath).pipe( Effect.retry(Schedule.fixed(LOCK_POLL_INTERVAL).pipe(Schedule.upTo(timeout))), Effect.catchAll(() => Effect.fail(new FileLockError({ reason: "timeout", lockPath }))) ); var releaseFileLock = (lockPath) => Effect.gen(function* () { const fs16 = yield* FileSystem.FileSystem; yield* fs16.remove(lockPath, { recursive: true }).pipe(Effect.ignore); }); var withFileLock = (lockPath, effect, timeout = Duration.minutes(2)) => Effect.acquireUseRelease( acquireFileLock(lockPath, timeout), () => effect, () => releaseFileLock(lockPath) ); // src/internal/effect/StartupCacheService.ts var logger2 = createLogger2({ name: "StartupCacheService" }); var StartupCacheService = class extends Context.Tag("StartupCacheService")() { }; var hashFile = (filePath) => Effect2.gen(function* () { const fs16 = yield* FileSystem2.FileSystem; const hash = crypto.createHash("sha256"); yield* fs16.stream(filePath).pipe(Stream.runForEach((chunk) => Effect2.sync(() => hash.update(chunk)))); return hash.digest("hex"); }).pipe(Effect2.mapError((cause) => new StartupCacheError({ cause, operation: "hash" }))); var findPrecompiledWasm = (dir) => Effect2.gen(function* () { const fs16 = yield* FileSystem2.FileSystem; const pathService = yield* Path.Path; const exists = yield* fs16.exists(dir); if (!exists) return Option.none(); const files = yield* fs16.readDirectory(dir).pipe(Effect2.orElseSucceed(() => [])); const wasmFile = files.find( (f) => f.startsWith("precompiled_wasm_") || f.endsWith(".cwasm") || f.endsWith(".wasm") ); return wasmFile ? Option.some(pathService.join(dir, wasmFile)) : Option.none(); }).pipe(Effect2.catchAll(() => Effect2.succeed(Option.none()))); var checkCache = (cacheDir, hashPath, expectedHash) => Effect2.gen(function* () { const fs16 = yield* FileSystem2.FileSystem; const savedHash = yield* fs16.readFileString(hashPath).pipe(Effect2.orElseSucceed(() => "")); if (savedHash.trim() !== expectedHash) return Option.none(); const wasmPath = yield* findPrecompiledWasm(cacheDir); if (Option.isNone(wasmPath)) return Option.none(); const accessible = yield* fs16.access(wasmPath.value).pipe( Effect2.as(true), Effect2.orElseSucceed(() => false) ); return accessible ? wasmPath : Option.none(); }); var runPrecompile = (binPath, chainArg, outputDir) => Effect2.gen(function* () { const fs16 = yield* FileSystem2.FileSystem; const pathService = yield* Path.Path; const args = chainArg ? ["precompile-wasm", chainArg, outputDir] : ["precompile-wasm", outputDir]; logger2.debug(`Precompiling: ${binPath} ${args.join(" ")}`); const startTime = Date.now(); const exitCode = yield* Command.exitCode(Command.make(binPath, ...args)).pipe( Effect2.mapError( (e) => new StartupCacheError({ cause: e, operation: "precompile" }) ) ); const files = yield* fs16.readDirectory(outputDir).pipe(Effect2.mapError((e) => new StartupCacheError({ cause: e, operation: "precompile" }))); const wasmFile = files.find( (f) => f.startsWith("precompiled_wasm_") || f.endsWith(".cwasm") || f.endsWith(".wasm") ); if (!wasmFile) { return yield* Effect2.fail( new StartupCacheError({ cause: `precompile-wasm failed (code ${exitCode}): no WASM file generated`, operation: "precompile" }) ); } const wasmPath = pathService.join(outputDir, wasmFile); logger2.debug(`Precompiled in ${Date.now() - startTime}ms: ${wasmPath}`); return wasmPath; }); var generateRawChainSpec = (binPath, chainName, outputPath) => Effect2.gen(function* () { const fs16 = yield* FileSystem2.FileSystem; const args = chainName === "dev" || chainName === "default" ? ["build-spec", "--dev", "--raw"] : ["build-spec", `--chain=${chainName}`, "--raw"]; logger2.debug(`Generating raw chain spec: ${binPath} ${args.join(" ")}`); const stdout = yield* Command.string(Command.make(binPath, ...args)).pipe( Effect2.mapError( (e) => new StartupCacheError({ cause: e, operation: "chainspec" }) ) ); if (!stdout.length) { return yield* Effect2.fail( new StartupCacheError({ cause: "build-spec produced no output", operation: "chainspec" }) ); } yield* fs16.writeFileString(outputPath, stdout).pipe(Effect2.mapError((e) => new StartupCacheError({ cause: e, operation: "chainspec" }))); return outputPath; }); var maybeGetRawChainSpec = (binPath, chainName, cacheSubDir, shouldGenerate) => Effect2.gen(function* () { if (!shouldGenerate) return Option.none(); const fs16 = yield* FileSystem2.FileSystem; const pathService = yield* Path.Path; const rawSpecPath = pathService.join(cacheSubDir, `${chainName}-raw.json`); const exists = yield* fs16.exists(rawSpecPath).pipe(Effect2.orElseSucceed(() => false)); if (exists) return Option.some(rawSpecPath); return yield* generateRawChainSpec(binPath, chainName, rawSpecPath).pipe( Effect2.map(Option.some), Effect2.catchAll(() => Effect2.succeed(Option.none())) ); }); var getCachedArtifactsImpl = (config) => Effect2.gen(function* () { const fs16 = yield* FileSystem2.FileSystem; const pathService = yield* Path.Path; const binaryHash = yield* hashFile(config.binPath); const shortHash = binaryHash.substring(0, 12); const chainName = config.isDevMode ? "dev" : config.chainArg?.match(/--chain[=\s]?(\S+)/)?.[1] || "default"; const binName = pathService.basename(config.binPath); const cacheSubDir = pathService.join(config.cacheDir, `${binName}-${chainName}-${shortHash}`); const hashPath = pathService.join(cacheSubDir, "binary.hash"); const lockPath = pathService.join(config.cacheDir, `${binName}-${chainName}.lock`); yield* fs16.makeDirectory(cacheSubDir, { recursive: true }).pipe(Effect2.mapError((e) => new StartupCacheError({ cause: e, operation: "cache" }))); const cached = yield* checkCache(cacheSubDir, hashPath, binaryHash); if (Option.isSome(cached)) { logger2.debug(`Using cached precompiled WASM: ${cached.value}`); const rawChainSpecPath = yield* maybeGetRawChainSpec( config.binPath, chainName, cacheSubDir, config.generateRawChainSpec ?? false ); return { precompiledPath: cached.value, fromCache: true, rawChainSpecPath: Option.getOrUndefined(rawChainSpecPath) }; } return yield* withFileLock( lockPath, Effect2.gen(function* () { const nowCached = yield* checkCache(cacheSubDir, hashPath, binaryHash); if (Option.isSome(nowCached)) { logger2.debug( `Using cached precompiled WASM (created by another process): ${nowCached.value}` ); const rawChainSpecPath2 = yield* maybeGetRawChainSpec( config.binPath, chainName, cacheSubDir, config.generateRawChainSpec ?? false ); return { precompiledPath: nowCached.value, fromCache: true, rawChainSpecPath: Option.getOrUndefined(rawChainSpecPath2) }; } logger2.debug("Precompiling WASM (this may take a moment)..."); const wasmPath = yield* runPrecompile(config.binPath, config.chainArg, cacheSubDir); yield* fs16.writeFileString(hashPath, binaryHash).pipe(Effect2.mapError((e) => new StartupCacheError({ cause: e, operation: "cache" }))); const rawChainSpecPath = yield* maybeGetRawChainSpec( config.binPath, chainName, cacheSubDir, config.generateRawChainSpec ?? false ); return { precompiledPath: wasmPath, fromCache: false, rawChainSpecPath: Option.getOrUndefined(rawChainSpecPath) }; }), Duration2.minutes(2) ); }); var StartupCacheServiceLive = Layer.succeed(StartupCacheService, { getCachedArtifacts: (config) => getCachedArtifactsImpl(config).pipe( Effect2.mapError( (e) => e._tag === "FileLockError" ? new StartupCacheError({ cause: e, operation: "lock" }) : e ), Effect2.provide(NodeContext.layer) ) }); var StartupCacheServiceTestable = Layer.succeed(StartupCacheService, { getCachedArtifacts: (config) => getCachedArtifactsImpl(config).pipe( Effect2.mapError( (e) => e._tag === "FileLockError" ? new StartupCacheError({ cause: e, operation: "lock" }) : e ) ) }); // src/internal/commandParsers.ts var logger3 = createLogger3({ name: "commandParsers" }); function parseZombieCmd(launchSpec) { if (launchSpec) { return { cmd: launchSpec.configPath }; } throw new Error( "No ZombieSpec found in config. Are you sure your moonwall.config.json file has the correct 'configPath' in zombieSpec?" ); } function fetchDefaultArgs(binName, additionalRepos = []) { let defaultArgs; const repos = [...standardRepos(), ...additionalRepos]; for (const repo4 of repos) { const foundBin = repo4.binaries.find((bin) => bin.name === binName); if (foundBin) { defaultArgs = foundBin.defaultArgs; break; } } if (!defaultArgs) { defaultArgs = ["--dev"]; } return defaultArgs; } var LaunchCommandParser = class _LaunchCommandParser { args; cmd; launch; launchSpec; launchOverrides; constructor(options) { const { launchSpec, additionalRepos, launchOverrides } = options; this.launchSpec = launchSpec; this.launchOverrides = launchOverrides; this.launch = !launchSpec.running ? true : launchSpec.running; this.cmd = launchSpec.binPath; this.args = launchSpec.options ? [...launchSpec.options] : fetchDefaultArgs(path4.basename(launchSpec.binPath), additionalRepos); } overrideArg(newArg) { const newArgKey = newArg.split("=")[0]; const existingIndex = this.args.findIndex((arg) => arg.startsWith(`${newArgKey}=`)); if (existingIndex !== -1) { this.args[existingIndex] = newArg; } else { this.args.push(newArg); } } async withPorts() { if (process.env.MOON_RECYCLE === "true") { const existingPort = process.env.MOONWALL_RPC_PORT; if (existingPort) { this.overrideArg(`--rpc-port=${existingPort}`); } return this; } if (this.launchSpec.ports) { const ports = this.launchSpec.ports; if (ports.p2pPort) { this.overrideArg(`--port=${ports.p2pPort}`); } if (ports.wsPort) { this.overrideArg(`--ws-port=${ports.wsPort}`); } if (ports.rpcPort) { this.overrideArg(`--rpc-port=${ports.rpcPort}`); } } else { const freePort = (await getFreePort()).toString(); process.env.MOONWALL_RPC_PORT = freePort; this.overrideArg(`--rpc-port=${freePort}`); } return this; } withDefaultForkConfig() { const forkOptions = this.launchSpec.defaultForkConfig; if (forkOptions) { this.applyForkOptions(forkOptions); } return this; } withLaunchOverrides() { if (this.launchOverrides?.forkConfig) { this.applyForkOptions(this.launchOverrides.forkConfig); } return this; } print() { logger3.debug(`Command to run: ${this.cmd}`); logger3.debug(`Arguments: ${this.args.join(" ")}`); return this; } applyForkOptions(forkOptions) { if (forkOptions.url) { invariant(forkOptions.url.startsWith("http"), "Fork URL must start with http:// or https://"); this.overrideArg(`--fork-chain-from-rpc=${forkOptions.url}`); } if (forkOptions.blockHash) { this.overrideArg(`--block=${forkOptions.blockHash}`); } if (forkOptions.stateOverridePath) { this.overrideArg(`--fork-state-overrides=${forkOptions.stateOverridePath}`); } if (forkOptions.verbose) { this.overrideArg("-llazy-loading=trace"); } } /** * Cache startup artifacts if enabled in launchSpec. * This uses an Effect-based service that caches artifacts by binary hash. * * When cacheStartupArtifacts is enabled, this generates: * 1. Precompiled WASM for the runtime * 2. Raw chain spec to skip genesis WASM compilation * * This reduces startup from ~3s to ~200ms (~10x improvement). */ async withStartupCache() { if (!this.launchSpec.cacheStartupArtifacts) { return this; } if (this.launchSpec.useDocker) { logger3.warn("Startup caching is not supported for Docker images, skipping"); return this; } const chainArg = this.args.find((arg) => arg.startsWith("--chain")); const hasDevFlag = this.args.includes("--dev"); const existingChainName = chainArg?.match(/--chain[=\s]?(\S+)/)?.[1]; const canGenerateRawSpec = hasDevFlag || !!existingChainName; const cacheDir = this.launchSpec.startupCacheDir || path4.join(process.cwd(), "tmp", "startup-cache"); const program = StartupCacheService.pipe( Effect3.flatMap( (service) => service.getCachedArtifacts({ binPath: this.launchSpec.binPath, chainArg, cacheDir, // Generate raw chain spec for faster startup (works for both --dev and --chain=XXX) generateRawChainSpec: canGenerateRawSpec, // Pass dev mode flag for proper chain name detection isDevMode: hasDevFlag }) ), Effect3.provide(StartupCacheServiceLive) ); try { const result = await Effect3.runPromise(program); const precompiledDir = path4.dirname(result.precompiledPath); this.overrideArg(`--wasmtime-precompiled=${precompiledDir}`); if (result.rawChainSpecPath) { if (hasDevFlag) { this.args = this.args.filter((arg) => arg !== "--dev"); this.overrideArg(`--chain=${result.rawChainSpecPath}`); this.overrideArg("--alice"); this.overrideArg("--force-authoring"); this.overrideArg("--rpc-cors=all"); this.overrideArg( "--node-key=0000000000000000000000000000000000000000000000000000000000000001" ); } else if (existingChainName) { this.overrideArg(`--chain=${result.rawChainSpecPath}`); } logger3.debug(`Using raw chain spec for ~10x faster startup: ${result.rawChainSpecPath}`); } process.env.MOONWALL_CACHE_DIR = precompiledDir; logger3.debug( result.fromCache ? `Using cached precompiled WASM: ${result.precompiledPath}` : `Precompiled WASM created: ${result.precompiledPath}` ); } catch (error) { logger3.warn(`WASM precompilation failed, continuing without: ${error}`); } return this; } build() { return { cmd: this.cmd, args: this.args, launch: this.launch }; } static async create(options) { const parser = new _LaunchCommandParser(options); const parsed = await parser.withPorts().then((p) => p.withDefaultForkConfig().withLaunchOverrides()).then((p) => p.withStartupCache()); if (options.verbose) { parsed.print(); } return parsed.build(); } }; function parseChopsticksRunCmd(launchSpecs) { const launch = !launchSpecs[0].running ? true : launchSpecs[0].running; if (launchSpecs.length === 1) { const chopsticksCmd2 = "node"; const chopsticksArgs2 = [ "node_modules/@acala-network/chopsticks/chopsticks.cjs", `--config=${launchSpecs[0].configPath}`, `--host=${launchSpecs[0].address ?? "127.0.0.1"}` ]; const mode = launchSpecs[0].buildBlockMode ? launchSpecs[0].buildBlockMode : "manual"; const num = mode === "batch" ? "Batch" : mode === "instant" ? "Instant" : "Manual"; chopsticksArgs2.push(`--build-block-mode=${num}`); if (launchSpecs[0].wsPort) { chopsticksArgs2.push(`--port=${launchSpecs[0].wsPort}`); } if (launchSpecs[0].wasmOverride) { chopsticksArgs2.push(`--wasm-override=${launchSpecs[0].wasmOverride}`); } if (launchSpecs[0].allowUnresolvedImports) { chopsticksArgs2.push("--allow-unresolved-imports"); } return { cmd: chopsticksCmd2, args: chopsticksArgs2, launch }; } const chopsticksCmd = "node"; const chopsticksArgs = ["node_modules/@acala-network/chopsticks/chopsticks.cjs", "xcm"]; for (const spec of launchSpecs) { const type = spec.type ? spec.type : "parachain"; switch (type) { case "parachain": chopsticksArgs.push(`--parachain=${spec.configPath}`); break; case "relaychain": chopsticksArgs.push(`--relaychain=${spec.configPath}`); } } return { cmd: chopsticksCmd, args: chopsticksArgs, launch }; } var isPortAvailable = async (port) => { return new Promise((resolve) => { const server = net.createServer(); server.listen(port, () => { server.once("close", () => resolve(true)); server.close(); }); server.on("error", () => resolve(false)); }); }; var getNextAvailablePort = async (startPort) => { let port = startPort; while (port <= 65535) { if (await isPortAvailable(port)) { return port; } port++; } throw new Error(`No available ports found starting from ${startPort}`); }; var getFreePort = async () => { const shardIndex = shardManager.getShardIndex(); const totalShards = shardManager.getTotalShards(); const poolId = parseInt(process.env.VITEST_POOL_ID || "0", 10); const basePort = 1e4; const shardOffset = shardIndex * 1e3; const poolOffset = poolId * 100; const processOffset = process.pid % 50; const calculatedPort = basePort + shardOffset + poolOffset + processOffset; const startPort = Math.min(calculatedPort, 6e4 + shardIndex * 100 + poolId); logger3.debug( `Port calculation: shard=${shardIndex + 1}/${totalShards}, pool=${poolId}, final=${startPort}` ); return getNextAvailablePort(startPort); }; // src/internal/deriveTestIds.ts import chalk2 from "chalk"; import fs5 from "fs"; import { confirm as confirm3 } from "@inquirer/prompts"; import path5 from "path"; // src/internal/testIdParser.ts import { Lang, parse, findInFiles } from "@ast-grep/napi"; function findIdValueNode(objNode) { for (const child of objNode.children()) { if (child.kind() === "pair") { const key = child.field("key"); const value = child.field("value"); if (key?.text() === "id" && value) { return value; } } } return void 0; } function replaceSuiteId(fileContent, newId) { const ast = parse(Lang.TypeScript, fileContent); const root = ast.root(); const describeSuiteCall = root.find("describeSuite($OPTS)"); if (!describeSuiteCall) { return void 0; } const optsNode = describeSuiteCall.getMatch("OPTS"); if (!optsNode || optsNode.kind() !== "object") { return void 0; } const idValueNode = findIdValueNode(optsNode); if (!idValueNode) { return void 0; } const originalText = idValueNode.text(); const quoteChar = originalText[0]; const edit = idValueNode.replace(`${quoteChar}${newId}${quoteChar}`); return root.commitEdits([edit]); } function hasSuiteDefinition(fileContent) { const ast = parse(Lang.TypeScript, fileContent); const root = ast.root(); return root.find("describeSuite($$$)") !== null; } // src/internal/deriveTestIds.ts async function deriveTestIds(params) { const usedPrefixes = /* @__PURE__ */ new Set(); const { rootDir, singlePrefix } = params; try { await fs5.promises.access(rootDir, fs5.constants.R_OK); } catch (_error) { console.error( `\u{1F534} Error accessing directory ${chalk2.bold(`/${rootDir}`)}, please make sure this exists` ); process.exitCode = 1; return; } console.log(`\u{1F7E2} Processing ${rootDir} ...`); const topLevelDirs = getTopLevelDirs(rootDir); const foldersToRename = []; if (singlePrefix) { const prefix = generatePrefix(rootDir, usedPrefixes, params.prefixPhrase); foldersToRename.push({ prefix, dir: "." }); } else { for (const dir of topLevelDirs) { const prefix = generatePrefix(dir, usedPrefixes, params.prefixPhrase); foldersToRename.push({ prefix, dir }); } } const result = await confirm3({ message: `This will rename ${foldersToRename.length} suites IDs in ${rootDir}, continue?` }); if (!result) { console.log("\u{1F534} Aborted"); return; } for (const folder of foldersT