Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: fix setup
  • Loading branch information
0xisk committed Nov 18, 2025
commit 769a8600a020a39c310d7d40a54dded2f7b54f43
52 changes: 40 additions & 12 deletions packages/simulator/test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const ARTIFACTS_DIR = join(__dirname, 'fixtures', 'artifacts');

const CONTRACT_FILES = ['Simple.compact', 'Witness.compact', 'SampleZOwnable.compact'];

// Singleton pattern to ensure setup only runs once across all test workers
let setupPromise: Promise<void> | null = null;
let setupComplete = false;

async function compileContract(contractFile: string): Promise<void> {
const inputPath = join(SAMPLE_CONTRACTS_DIR, contractFile);
const contractName = contractFile.replace('.compact', '');
Expand All @@ -30,14 +34,15 @@ async function compileContract(contractFile: string): Promise<void> {
throw new Error(`Contract file not found: ${inputPath}`);
}

// Ensure output directory exists
// Ensure output directory and keys subdirectory exist
// compact compile requires the keys directory to exist
mkdirSync(outputDir, { recursive: true });

// Use Compact developer tools CLI to compile a single contract file
const command = `compact compile "${inputPath}" "${outputDir}"`;
mkdirSync(join(outputDir, 'keys'), { recursive: true });

try {
const command = `compact compile --skip-zk "${inputPath}" "${outputDir}"`;
const { stderr } = await execAsync(command);

if (stderr && !stderr.includes('warning')) {
console.log(`Warning for ${contractFile}: ${stderr}`);
}
Expand All @@ -49,21 +54,44 @@ async function compileContract(contractFile: string): Promise<void> {
}

async function setup(): Promise<void> {
console.log('🔨 Compiling sample contracts for tests...\n');

// Ensure artifacts directory exists
mkdirSync(ARTIFACTS_DIR, { recursive: true });
// If setup is already complete, return immediately
if (setupComplete) {
return;
}

// Compile each contract
for (const contractFile of CONTRACT_FILES) {
await compileContract(contractFile);
// If setup is in progress, wait for it
if (setupPromise) {
return setupPromise;
}

console.log('\n✅ Test artifacts compiled successfully!\n');
// Start setup
setupPromise = (async () => {
console.log('🔨 Compiling sample contracts for tests...\n');

// Ensure artifacts directory exists
mkdirSync(ARTIFACTS_DIR, { recursive: true });

// Compile each contract
for (const contractFile of CONTRACT_FILES) {
await compileContract(contractFile);
}

console.log('\n✅ Test artifacts compiled successfully!\n');
setupComplete = true;
})();

try {
await setupPromise;
} catch (error) {
// Reset promise on error so it can be retried
setupPromise = null;
throw error;
}
}

// Always run setup when this file is loaded by vitest
// Vitest's `setupFiles` loads (imports) this module, so execute on import.
// The singleton pattern ensures it only runs once even with parallel workers.
await setup().catch((error) => {
console.log(`❌ Setup failed: ${error}`);
process.exit(1);
Expand Down