datahaven/test/utils/parameters.ts
Facundo Farall 9b311e00ef
test: 🏗️ Setup e2e testing framework (#104)
## Implement E2E Testing Framework with Isolated Networks

### Summary
Refactors the existing E2E testing infrastructure to provide isolated
test environments with parallel execution support. Each test suite now
runs in its own network namespace, preventing resource conflicts.

### Key Changes
- **New Testing Framework** (`test/framework/`): Base classes for test
lifecycle management with automatic setup/teardown
- **Launcher Module** (`test/launcher/`): Extracted network
orchestration logic from CLI handlers for reusability
- **Parallel Execution**: Added `test-parallel.ts` script with
concurrency limits to prevent resource exhaustion
- **Test Isolation**: Each suite gets unique network IDs (format:
`suiteName-timestamp`) and Docker networks
- **Improved Test Organization**: Migrated tests to new framework,
deprecated old test structure

### Test Improvements
- Added 4 new test suites demonstrating framework usage. :
  - `contracts.test.ts` - Smart contract deployment/interaction
  - `datahaven-substrate.test.ts` - Substrate API operations  
  - `cross-chain.test.ts` - Snowbridge cross-chain messaging
  - `ethereum-basic.test.ts` - Ethereum network operations

> [!WARNING]
The test suites themselves are bad and shouldn't be consider examples of
good tests. They were AI generated just to test the concurrency of test
runners

### Documentation
- Added comprehensive framework overview (`E2E_FRAMEWORK_OVERVIEW.md`)
- Updated README with parallel testing commands
- Added test patterns and best practices

### Breaking Changes
- Old test suites moved to `e2e - DEPRECATED/` directory
- Test execution now requires extending `BaseTestSuite` class

### Testing
Run tests with: `bun test:e2e` or `bun test:e2e:parallel` (with
concurrency limits)

### TODO
- [ ] Implement good test examples.
- [ ] Implement useful test utils (like waiting for an event to show up
in DataHaven or Ethereum).
- [ ] Enforce tests with CI (currently cannot be done due to
intermittent error when sending a transaction with PAPI).

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: undercover-cactus <lola@moonsonglabs.com>
2025-07-16 18:51:07 +02:00

86 lines
3.1 KiB
TypeScript

import path from "node:path";
import { $ } from "bun";
import { logger } from "./logger";
import type { ParsedDataHavenParameter } from "./types";
// Constants for paths
export const PARAMETERS_TEMPLATE_PATH = "configs/parameters/datahaven-parameters.json";
export const PARAMETERS_OUTPUT_DIR = "tmp/configs";
export const PARAMETERS_OUTPUT_FILE = "datahaven-parameters.json";
export const PARAMETERS_OUTPUT_PATH = path.join(PARAMETERS_OUTPUT_DIR, PARAMETERS_OUTPUT_FILE);
/**
* A collection of parameters to be set in the DataHaven runtime.
* This class is used to collect parameters from different steps of the launch process
* and then generate a JSON file to be used by the setDataHavenParameters script.
*/
export class ParameterCollection {
private parameters: ParsedDataHavenParameter[] = [];
/**
* Adds a parameter to the collection
* @param param The parameter to add
*/
public addParameter(param: ParsedDataHavenParameter): void {
// Check if parameter with same name already exists
const existingIndex = this.parameters.findIndex((p) => p.name === param.name);
if (existingIndex !== -1) {
// Replace existing parameter
this.parameters[existingIndex] = param;
logger.debug(`Updated parameter: ${String(param.name)} = ${JSON.stringify(param.value)}`);
} else {
// Add new parameter
this.parameters.push(param);
logger.debug(`Added parameter: ${String(param.name)} = ${JSON.stringify(param.value)}`);
}
}
/**
* Returns the current parameters
*/
public getParameters(): ParsedDataHavenParameter[] {
return [...this.parameters];
}
/**
* Generates a JSON file with the parameters collected so far
*/
public async generateParametersFile(): Promise<string> {
logger.debug(`Ensuring output directory exists: ${PARAMETERS_OUTPUT_DIR}`);
await $`mkdir -p ${PARAMETERS_OUTPUT_DIR}`.quiet();
// If we have no parameters, load the template to get the structure
if (this.parameters.length === 0) {
logger.debug(`No parameters collected, loading template from ${PARAMETERS_TEMPLATE_PATH}`);
const templateFile = Bun.file(PARAMETERS_TEMPLATE_PATH);
if (!(await templateFile.exists())) {
throw new Error(`Template file ${PARAMETERS_TEMPLATE_PATH} does not exist`);
}
this.parameters = await templateFile.json();
}
// Write the parameters to a file
logger.debug(`Writing parameters to ${PARAMETERS_OUTPUT_PATH}`);
await Bun.write(PARAMETERS_OUTPUT_PATH, JSON.stringify(this.parameters, null, 2));
logger.debug(`Parameters file generated at ${PARAMETERS_OUTPUT_PATH}`);
return PARAMETERS_OUTPUT_PATH;
}
}
/**
* Creates a new ParameterCollection, pre-loaded with template parameters if available
*/
export const createParameterCollection = async (): Promise<ParameterCollection> => {
const collection = new ParameterCollection();
const templateFile = Bun.file(PARAMETERS_TEMPLATE_PATH);
if (await templateFile.exists()) {
const templateParams = await templateFile.json();
for (const param of templateParams) {
collection.addParameter(param);
}
}
return collection;
};