From f8f745692cdf035af683685e8bb5e6b84f8400e9 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Fri, 30 May 2025 18:25:06 -0600 Subject: [PATCH] refactor: simplify error handling in suggestDownloadTool and enhance test setup for CLI --- codex/slots/proofs/proverfactory.nim | 4 +- tests/integration/1_minute/testcli.nim | 79 +++++++++++++++++++------- 2 files changed, 61 insertions(+), 22 deletions(-) diff --git a/codex/slots/proofs/proverfactory.nim b/codex/slots/proofs/proverfactory.nim index 9924c294..419b0fa5 100644 --- a/codex/slots/proofs/proverfactory.nim +++ b/codex/slots/proofs/proverfactory.nim @@ -71,9 +71,7 @@ proc getZkeyFile*(config: CodexConf): ?!string = proc suggestDownloadTool(config: CodexConf) = without address =? config.marketplaceAddress: - raise (ref Defect)( - msg: "Proving backend initializing while marketplace address not set." - ) + raiseAssert("Proving backend initializing while marketplace address not set.") let tokens = ["cirdl", "\"" & $config.circuitDir & "\"", config.ethProvider, $address] diff --git a/tests/integration/1_minute/testcli.nim b/tests/integration/1_minute/testcli.nim index 778608b8..3cd576a7 100644 --- a/tests/integration/1_minute/testcli.nim +++ b/tests/integration/1_minute/testcli.nim @@ -1,4 +1,6 @@ import std/tempfiles +import std/appdirs +import std/paths import codex/conf import codex/utils/fileutils import ../../asynctest @@ -10,51 +12,90 @@ import ../../examples asyncchecksuite "Command line interface": let key = "4242424242424242424242424242424242424242424242424242424242424242" + var tmpDataDir: string + setup: + # Ensure the key file is created with safe permissions + tmpDataDir = createTempDir(prefix = "testcli_", suffix = "", dir = $getTempDir()) + + teardown: + # Remove the temporary data directory after tests + discard removeDir(tmpDataDir) + proc startCodex(args: seq[string]): Future[CodexProcess] {.async.} = - return await CodexProcess.startNode(args, false, "cli-test-node") + var args = args + if not args.anyIt(it.contains("--data-dir")): + args.add("--data-dir=" & tmpDataDir) + + return await CodexProcess.startNode(args, debug = false, "cli-test-node") test "complains when persistence is enabled without ethereum account": let node = await startCodex(@["persistence"]) + + defer: + await node.stop() + await node.waitUntilOutput("Persistence enabled, but no Ethereum account was set") - await node.stop() test "complains when ethereum private key file has wrong permissions": let unsafeKeyFile = genTempPath("", "") discard unsafeKeyFile.writeFile(key, 0o666) let node = await startCodex(@["persistence", "--eth-private-key=" & unsafeKeyFile]) + + defer: + await node.stop() + discard removeFile(unsafeKeyFile) + await node.waitUntilOutput( "Ethereum private key file does not have safe file permissions" ) - await node.stop() - discard removeFile(unsafeKeyFile) - let - marketplaceArg = "--marketplace-address=" & $EthAddress.example - expectedDownloadInstruction = - "Proving circuit files are not found. Please run the following to download them:" + let expectedDownloadInstruction = + "Proving circuit files are not found. Please run the following to download them:" test "suggests downloading of circuit files when persistence is enabled without accessible r1cs file": - let node = await startCodex(@["persistence", "prover", marketplaceArg]) - await node.waitUntilOutput(expectedDownloadInstruction) - await node.stop() - - test "suggests downloading of circuit files when persistence is enabled without accessible wasm file": let node = await startCodex( @[ - "persistence", "prover", marketplaceArg, - "--circom-r1cs=tests/circuits/fixtures/proof_main.r1cs", + "persistence", + "prover", + "--marketplace-address=" & $EthAddress.example, + "--prover-backend=nimgroth16", ] ) + + defer: + await node.stop() + await node.waitUntilOutput(expectedDownloadInstruction) - await node.stop() test "suggests downloading of circuit files when persistence is enabled without accessible zkey file": let node = await startCodex( @[ - "persistence", "prover", marketplaceArg, + "persistence", + "prover", + "--marketplace-address=" & $EthAddress.example, + "--prover-backend=nimgroth16", "--circom-r1cs=tests/circuits/fixtures/proof_main.r1cs", - "--circom-wasm=tests/circuits/fixtures/proof_main.wasm", ] ) + + defer: + await node.stop() + + await node.waitUntilOutput(expectedDownloadInstruction) + + test "suggests downloading of circuit files when persistence is enabled without accessible graph file": + let node = await startCodex( + @[ + "persistence", + "prover", + "--marketplace-address=" & $EthAddress.example, + "--prover-backend=nimgroth16", + "--circom-r1cs=tests/circuits/fixtures/proof_main.r1cs", + "--circom-zkey=tests/circuits/fixtures/proof_main.zkey", + ] + ) + + defer: + await node.stop() + await node.waitUntilOutput(expectedDownloadInstruction) - await node.stop()