harb/tests/setup/stack.ts

102 lines
3.3 KiB
TypeScript
Raw Normal View History

feat: Add functional health checks for test prerequisites (#65) Replaces basic "service is up" checks with functional verification that tests can actually use the services. ## Changes ### New Health Checks - **RPC Proxy**: Verifies eth_call works and deployed contracts are accessible - **GraphQL**: Confirms Ponder has indexed data with non-zero stats - **Web App**: Validates endpoint accessibility ### Improvements - Clear error messages explain what failed and how to fix it - Checks verify actual functionality, not just HTTP 200 responses - Fails fast before tests run with cryptic errors ### Files - `tests/setup/health-checks.ts` - Core health check functions - `tests/setup/stack.ts` - Integration with waitForStackReady() - `tests/HEALTH_CHECKS.md` - Documentation and troubleshooting guide ## Error Message Example Before: ``` RPC health check failed with status 404 ``` After: ``` ❌ Stack health check failed Failed services: • RPC Proxy: RPC proxy returned HTTP 404 Expected 200, got 404. Check if Anvil is running and RPC_URL is correct. • GraphQL Indexer: GraphQL has no indexed data yet Ponder is running but has not indexed contract events. Troubleshooting: 1. Check stack logs: tail tests/.stack.log 2. Verify services are running: ./scripts/dev.sh status 3. Restart stack: ./scripts/dev.sh restart --full ``` ## Benefits - ✅ Tests fail fast with clear error messages - ✅ Catches configuration issues before tests run - ✅ Verifies services are actually usable, not just running resolves #61 Co-authored-by: johba <johba@harb.eth> Reviewed-on: https://codeberg.org/johba/harb/pulls/65
2025-10-05 20:03:07 +02:00
import {
runAllHealthChecks,
formatHealthCheckError,
} from './health-checks.js';
refactor: migrate kraiken-lib to explicit subpath imports (BREAKING CHANGE) (#89) Removes the barrel export pattern in favor of explicit subpath imports for better tree-shaking and clearer dependencies. ## Breaking Changes - Removed `src/helpers.ts` barrel export - Removed `./helpers` from package.json exports - Root `kraiken-lib` import now raises build errors - Consumers MUST use explicit subpaths: - `kraiken-lib/abis` - Contract ABIs - `kraiken-lib/staking` - Staking helpers - `kraiken-lib/snatch` - Snatch selection - `kraiken-lib/ids` - Position ID utilities - `kraiken-lib/subgraph` - Byte conversion utilities - `kraiken-lib/taxRates` - Tax rate constants - `kraiken-lib/version` - Version validation ## Changes - kraiken-lib: - Bumped version to 1.0.0 (breaking change) - Updated src/index.ts to raise build errors - Added backward-compatible ABI aliases (KraikenAbi, StakeAbi) - Updated all test files to use .js extensions and new imports - Updated documentation (README, AGENTS.md) - Consumer updates: - services/ponder: Updated ponder.config.ts to use kraiken-lib/abis - web-app: Updated all imports to use subpaths - composables/usePositions.ts: kraiken-lib/subgraph - contracts/harb.ts: kraiken-lib/abis - contracts/stake.ts: kraiken-lib/abis ## Migration Guide ```typescript // OLD import { getSnatchList } from 'kraiken-lib/helpers'; import { KraikenAbi } from 'kraiken-lib'; // NEW import { getSnatchList } from 'kraiken-lib/snatch'; import { KraikenAbi } from 'kraiken-lib/abis'; ``` Fixes #86 Co-authored-by: openhands <openhands@all-hands.dev> Reviewed-on: https://codeberg.org/johba/harb/pulls/89
2025-11-20 18:54:53 +01:00
import { readFileSync } from 'fs';
import { join } from 'path';
2025-10-11 10:55:49 +00:00
const DEFAULT_RPC_URL = 'http://localhost:8081/api/rpc';
2025-10-07 21:57:32 +00:00
const DEFAULT_WEBAPP_URL = 'http://localhost:8081';
2025-10-11 10:55:49 +00:00
const DEFAULT_GRAPHQL_URL = 'http://localhost:8081/api/graphql';
refactor: migrate kraiken-lib to explicit subpath imports (BREAKING CHANGE) (#89) Removes the barrel export pattern in favor of explicit subpath imports for better tree-shaking and clearer dependencies. ## Breaking Changes - Removed `src/helpers.ts` barrel export - Removed `./helpers` from package.json exports - Root `kraiken-lib` import now raises build errors - Consumers MUST use explicit subpaths: - `kraiken-lib/abis` - Contract ABIs - `kraiken-lib/staking` - Staking helpers - `kraiken-lib/snatch` - Snatch selection - `kraiken-lib/ids` - Position ID utilities - `kraiken-lib/subgraph` - Byte conversion utilities - `kraiken-lib/taxRates` - Tax rate constants - `kraiken-lib/version` - Version validation ## Changes - kraiken-lib: - Bumped version to 1.0.0 (breaking change) - Updated src/index.ts to raise build errors - Added backward-compatible ABI aliases (KraikenAbi, StakeAbi) - Updated all test files to use .js extensions and new imports - Updated documentation (README, AGENTS.md) - Consumer updates: - services/ponder: Updated ponder.config.ts to use kraiken-lib/abis - web-app: Updated all imports to use subpaths - composables/usePositions.ts: kraiken-lib/subgraph - contracts/harb.ts: kraiken-lib/abis - contracts/stake.ts: kraiken-lib/abis ## Migration Guide ```typescript // OLD import { getSnatchList } from 'kraiken-lib/helpers'; import { KraikenAbi } from 'kraiken-lib'; // NEW import { getSnatchList } from 'kraiken-lib/snatch'; import { KraikenAbi } from 'kraiken-lib/abis'; ``` Fixes #86 Co-authored-by: openhands <openhands@all-hands.dev> Reviewed-on: https://codeberg.org/johba/harb/pulls/89
2025-11-20 18:54:53 +01:00
export interface ContractAddresses {
Kraiken: string;
Stake: string;
LiquidityManager: string;
OptimizerProxy?: string;
refactor: migrate kraiken-lib to explicit subpath imports (BREAKING CHANGE) (#89) Removes the barrel export pattern in favor of explicit subpath imports for better tree-shaking and clearer dependencies. ## Breaking Changes - Removed `src/helpers.ts` barrel export - Removed `./helpers` from package.json exports - Root `kraiken-lib` import now raises build errors - Consumers MUST use explicit subpaths: - `kraiken-lib/abis` - Contract ABIs - `kraiken-lib/staking` - Staking helpers - `kraiken-lib/snatch` - Snatch selection - `kraiken-lib/ids` - Position ID utilities - `kraiken-lib/subgraph` - Byte conversion utilities - `kraiken-lib/taxRates` - Tax rate constants - `kraiken-lib/version` - Version validation ## Changes - kraiken-lib: - Bumped version to 1.0.0 (breaking change) - Updated src/index.ts to raise build errors - Added backward-compatible ABI aliases (KraikenAbi, StakeAbi) - Updated all test files to use .js extensions and new imports - Updated documentation (README, AGENTS.md) - Consumer updates: - services/ponder: Updated ponder.config.ts to use kraiken-lib/abis - web-app: Updated all imports to use subpaths - composables/usePositions.ts: kraiken-lib/subgraph - contracts/harb.ts: kraiken-lib/abis - contracts/stake.ts: kraiken-lib/abis ## Migration Guide ```typescript // OLD import { getSnatchList } from 'kraiken-lib/helpers'; import { KraikenAbi } from 'kraiken-lib'; // NEW import { getSnatchList } from 'kraiken-lib/snatch'; import { KraikenAbi } from 'kraiken-lib/abis'; ``` Fixes #86 Co-authored-by: openhands <openhands@all-hands.dev> Reviewed-on: https://codeberg.org/johba/harb/pulls/89
2025-11-20 18:54:53 +01:00
}
2025-10-07 21:57:32 +00:00
export interface StackConfig {
rpcUrl: string;
webAppUrl: string;
graphqlUrl: string;
refactor: migrate kraiken-lib to explicit subpath imports (BREAKING CHANGE) (#89) Removes the barrel export pattern in favor of explicit subpath imports for better tree-shaking and clearer dependencies. ## Breaking Changes - Removed `src/helpers.ts` barrel export - Removed `./helpers` from package.json exports - Root `kraiken-lib` import now raises build errors - Consumers MUST use explicit subpaths: - `kraiken-lib/abis` - Contract ABIs - `kraiken-lib/staking` - Staking helpers - `kraiken-lib/snatch` - Snatch selection - `kraiken-lib/ids` - Position ID utilities - `kraiken-lib/subgraph` - Byte conversion utilities - `kraiken-lib/taxRates` - Tax rate constants - `kraiken-lib/version` - Version validation ## Changes - kraiken-lib: - Bumped version to 1.0.0 (breaking change) - Updated src/index.ts to raise build errors - Added backward-compatible ABI aliases (KraikenAbi, StakeAbi) - Updated all test files to use .js extensions and new imports - Updated documentation (README, AGENTS.md) - Consumer updates: - services/ponder: Updated ponder.config.ts to use kraiken-lib/abis - web-app: Updated all imports to use subpaths - composables/usePositions.ts: kraiken-lib/subgraph - contracts/harb.ts: kraiken-lib/abis - contracts/stake.ts: kraiken-lib/abis ## Migration Guide ```typescript // OLD import { getSnatchList } from 'kraiken-lib/helpers'; import { KraikenAbi } from 'kraiken-lib'; // NEW import { getSnatchList } from 'kraiken-lib/snatch'; import { KraikenAbi } from 'kraiken-lib/abis'; ``` Fixes #86 Co-authored-by: openhands <openhands@all-hands.dev> Reviewed-on: https://codeberg.org/johba/harb/pulls/89
2025-11-20 18:54:53 +01:00
contracts: ContractAddresses;
}
/**
* Load contract addresses, preferring env var overrides over the deployments file.
* Env vars: STACK_KRAIKEN_ADDRESS, STACK_STAKE_ADDRESS, STACK_LM_ADDRESS.
* Falls back to onchain/deployments-local.json when env vars are absent.
refactor: migrate kraiken-lib to explicit subpath imports (BREAKING CHANGE) (#89) Removes the barrel export pattern in favor of explicit subpath imports for better tree-shaking and clearer dependencies. ## Breaking Changes - Removed `src/helpers.ts` barrel export - Removed `./helpers` from package.json exports - Root `kraiken-lib` import now raises build errors - Consumers MUST use explicit subpaths: - `kraiken-lib/abis` - Contract ABIs - `kraiken-lib/staking` - Staking helpers - `kraiken-lib/snatch` - Snatch selection - `kraiken-lib/ids` - Position ID utilities - `kraiken-lib/subgraph` - Byte conversion utilities - `kraiken-lib/taxRates` - Tax rate constants - `kraiken-lib/version` - Version validation ## Changes - kraiken-lib: - Bumped version to 1.0.0 (breaking change) - Updated src/index.ts to raise build errors - Added backward-compatible ABI aliases (KraikenAbi, StakeAbi) - Updated all test files to use .js extensions and new imports - Updated documentation (README, AGENTS.md) - Consumer updates: - services/ponder: Updated ponder.config.ts to use kraiken-lib/abis - web-app: Updated all imports to use subpaths - composables/usePositions.ts: kraiken-lib/subgraph - contracts/harb.ts: kraiken-lib/abis - contracts/stake.ts: kraiken-lib/abis ## Migration Guide ```typescript // OLD import { getSnatchList } from 'kraiken-lib/helpers'; import { KraikenAbi } from 'kraiken-lib'; // NEW import { getSnatchList } from 'kraiken-lib/snatch'; import { KraikenAbi } from 'kraiken-lib/abis'; ``` Fixes #86 Co-authored-by: openhands <openhands@all-hands.dev> Reviewed-on: https://codeberg.org/johba/harb/pulls/89
2025-11-20 18:54:53 +01:00
*/
function loadContractAddresses(): ContractAddresses {
const envKraiken = process.env.STACK_KRAIKEN_ADDRESS;
const envStake = process.env.STACK_STAKE_ADDRESS;
const envLm = process.env.STACK_LM_ADDRESS;
if (envKraiken && envStake && envLm) {
const envOptimizerProxy = process.env.STACK_OPTIMIZER_PROXY_ADDRESS;
return {
Kraiken: envKraiken,
Stake: envStake,
LiquidityManager: envLm,
...(envOptimizerProxy !== undefined ? { OptimizerProxy: envOptimizerProxy } : {}),
};
}
let fileContracts!: ContractAddresses;
refactor: migrate kraiken-lib to explicit subpath imports (BREAKING CHANGE) (#89) Removes the barrel export pattern in favor of explicit subpath imports for better tree-shaking and clearer dependencies. ## Breaking Changes - Removed `src/helpers.ts` barrel export - Removed `./helpers` from package.json exports - Root `kraiken-lib` import now raises build errors - Consumers MUST use explicit subpaths: - `kraiken-lib/abis` - Contract ABIs - `kraiken-lib/staking` - Staking helpers - `kraiken-lib/snatch` - Snatch selection - `kraiken-lib/ids` - Position ID utilities - `kraiken-lib/subgraph` - Byte conversion utilities - `kraiken-lib/taxRates` - Tax rate constants - `kraiken-lib/version` - Version validation ## Changes - kraiken-lib: - Bumped version to 1.0.0 (breaking change) - Updated src/index.ts to raise build errors - Added backward-compatible ABI aliases (KraikenAbi, StakeAbi) - Updated all test files to use .js extensions and new imports - Updated documentation (README, AGENTS.md) - Consumer updates: - services/ponder: Updated ponder.config.ts to use kraiken-lib/abis - web-app: Updated all imports to use subpaths - composables/usePositions.ts: kraiken-lib/subgraph - contracts/harb.ts: kraiken-lib/abis - contracts/stake.ts: kraiken-lib/abis ## Migration Guide ```typescript // OLD import { getSnatchList } from 'kraiken-lib/helpers'; import { KraikenAbi } from 'kraiken-lib'; // NEW import { getSnatchList } from 'kraiken-lib/snatch'; import { KraikenAbi } from 'kraiken-lib/abis'; ``` Fixes #86 Co-authored-by: openhands <openhands@all-hands.dev> Reviewed-on: https://codeberg.org/johba/harb/pulls/89
2025-11-20 18:54:53 +01:00
try {
const deploymentsPath = join(process.cwd(), 'onchain', 'deployments-local.json');
const deploymentsJson = readFileSync(deploymentsPath, 'utf-8');
const deployments = JSON.parse(deploymentsJson);
fileContracts = deployments.contracts;
refactor: migrate kraiken-lib to explicit subpath imports (BREAKING CHANGE) (#89) Removes the barrel export pattern in favor of explicit subpath imports for better tree-shaking and clearer dependencies. ## Breaking Changes - Removed `src/helpers.ts` barrel export - Removed `./helpers` from package.json exports - Root `kraiken-lib` import now raises build errors - Consumers MUST use explicit subpaths: - `kraiken-lib/abis` - Contract ABIs - `kraiken-lib/staking` - Staking helpers - `kraiken-lib/snatch` - Snatch selection - `kraiken-lib/ids` - Position ID utilities - `kraiken-lib/subgraph` - Byte conversion utilities - `kraiken-lib/taxRates` - Tax rate constants - `kraiken-lib/version` - Version validation ## Changes - kraiken-lib: - Bumped version to 1.0.0 (breaking change) - Updated src/index.ts to raise build errors - Added backward-compatible ABI aliases (KraikenAbi, StakeAbi) - Updated all test files to use .js extensions and new imports - Updated documentation (README, AGENTS.md) - Consumer updates: - services/ponder: Updated ponder.config.ts to use kraiken-lib/abis - web-app: Updated all imports to use subpaths - composables/usePositions.ts: kraiken-lib/subgraph - contracts/harb.ts: kraiken-lib/abis - contracts/stake.ts: kraiken-lib/abis ## Migration Guide ```typescript // OLD import { getSnatchList } from 'kraiken-lib/helpers'; import { KraikenAbi } from 'kraiken-lib'; // NEW import { getSnatchList } from 'kraiken-lib/snatch'; import { KraikenAbi } from 'kraiken-lib/abis'; ``` Fixes #86 Co-authored-by: openhands <openhands@all-hands.dev> Reviewed-on: https://codeberg.org/johba/harb/pulls/89
2025-11-20 18:54:53 +01:00
} catch (error) {
console.error('Failed to load contract addresses from deployments-local.json:', error);
throw new Error('Cannot run tests without deployed contract addresses');
}
return {
Kraiken: envKraiken ?? fileContracts.Kraiken,
Stake: envStake ?? fileContracts.Stake,
LiquidityManager: envLm ?? fileContracts.LiquidityManager,
...(fileContracts.OptimizerProxy !== undefined
? { OptimizerProxy: fileContracts.OptimizerProxy }
: {}),
};
}
2025-10-07 21:57:32 +00:00
/**
* Get stack configuration from environment variables.
* Tests should NOT start their own stack - they require a pre-existing healthy stack.
*/
export function getStackConfig(): StackConfig {
return {
rpcUrl: process.env.STACK_RPC_URL ?? DEFAULT_RPC_URL,
webAppUrl: process.env.STACK_WEBAPP_URL ?? DEFAULT_WEBAPP_URL,
graphqlUrl: process.env.STACK_GRAPHQL_URL ?? DEFAULT_GRAPHQL_URL,
refactor: migrate kraiken-lib to explicit subpath imports (BREAKING CHANGE) (#89) Removes the barrel export pattern in favor of explicit subpath imports for better tree-shaking and clearer dependencies. ## Breaking Changes - Removed `src/helpers.ts` barrel export - Removed `./helpers` from package.json exports - Root `kraiken-lib` import now raises build errors - Consumers MUST use explicit subpaths: - `kraiken-lib/abis` - Contract ABIs - `kraiken-lib/staking` - Staking helpers - `kraiken-lib/snatch` - Snatch selection - `kraiken-lib/ids` - Position ID utilities - `kraiken-lib/subgraph` - Byte conversion utilities - `kraiken-lib/taxRates` - Tax rate constants - `kraiken-lib/version` - Version validation ## Changes - kraiken-lib: - Bumped version to 1.0.0 (breaking change) - Updated src/index.ts to raise build errors - Added backward-compatible ABI aliases (KraikenAbi, StakeAbi) - Updated all test files to use .js extensions and new imports - Updated documentation (README, AGENTS.md) - Consumer updates: - services/ponder: Updated ponder.config.ts to use kraiken-lib/abis - web-app: Updated all imports to use subpaths - composables/usePositions.ts: kraiken-lib/subgraph - contracts/harb.ts: kraiken-lib/abis - contracts/stake.ts: kraiken-lib/abis ## Migration Guide ```typescript // OLD import { getSnatchList } from 'kraiken-lib/helpers'; import { KraikenAbi } from 'kraiken-lib'; // NEW import { getSnatchList } from 'kraiken-lib/snatch'; import { KraikenAbi } from 'kraiken-lib/abis'; ``` Fixes #86 Co-authored-by: openhands <openhands@all-hands.dev> Reviewed-on: https://codeberg.org/johba/harb/pulls/89
2025-11-20 18:54:53 +01:00
contracts: loadContractAddresses(),
2025-10-07 21:57:32 +00:00
};
}
feat: Add functional health checks for test prerequisites (#65) Replaces basic "service is up" checks with functional verification that tests can actually use the services. ## Changes ### New Health Checks - **RPC Proxy**: Verifies eth_call works and deployed contracts are accessible - **GraphQL**: Confirms Ponder has indexed data with non-zero stats - **Web App**: Validates endpoint accessibility ### Improvements - Clear error messages explain what failed and how to fix it - Checks verify actual functionality, not just HTTP 200 responses - Fails fast before tests run with cryptic errors ### Files - `tests/setup/health-checks.ts` - Core health check functions - `tests/setup/stack.ts` - Integration with waitForStackReady() - `tests/HEALTH_CHECKS.md` - Documentation and troubleshooting guide ## Error Message Example Before: ``` RPC health check failed with status 404 ``` After: ``` ❌ Stack health check failed Failed services: • RPC Proxy: RPC proxy returned HTTP 404 Expected 200, got 404. Check if Anvil is running and RPC_URL is correct. • GraphQL Indexer: GraphQL has no indexed data yet Ponder is running but has not indexed contract events. Troubleshooting: 1. Check stack logs: tail tests/.stack.log 2. Verify services are running: ./scripts/dev.sh status 3. Restart stack: ./scripts/dev.sh restart --full ``` ## Benefits - ✅ Tests fail fast with clear error messages - ✅ Catches configuration issues before tests run - ✅ Verifies services are actually usable, not just running resolves #61 Co-authored-by: johba <johba@harb.eth> Reviewed-on: https://codeberg.org/johba/harb/pulls/65
2025-10-05 20:03:07 +02:00
/**
2025-10-07 21:57:32 +00:00
* Validate that a healthy stack exists and is functional.
* If validation fails, the test run should exit immediately.
*
* Tests do NOT manage stack lifecycle - stack must be started externally via:
* ./scripts/dev.sh start
feat: Add functional health checks for test prerequisites (#65) Replaces basic "service is up" checks with functional verification that tests can actually use the services. ## Changes ### New Health Checks - **RPC Proxy**: Verifies eth_call works and deployed contracts are accessible - **GraphQL**: Confirms Ponder has indexed data with non-zero stats - **Web App**: Validates endpoint accessibility ### Improvements - Clear error messages explain what failed and how to fix it - Checks verify actual functionality, not just HTTP 200 responses - Fails fast before tests run with cryptic errors ### Files - `tests/setup/health-checks.ts` - Core health check functions - `tests/setup/stack.ts` - Integration with waitForStackReady() - `tests/HEALTH_CHECKS.md` - Documentation and troubleshooting guide ## Error Message Example Before: ``` RPC health check failed with status 404 ``` After: ``` ❌ Stack health check failed Failed services: • RPC Proxy: RPC proxy returned HTTP 404 Expected 200, got 404. Check if Anvil is running and RPC_URL is correct. • GraphQL Indexer: GraphQL has no indexed data yet Ponder is running but has not indexed contract events. Troubleshooting: 1. Check stack logs: tail tests/.stack.log 2. Verify services are running: ./scripts/dev.sh status 3. Restart stack: ./scripts/dev.sh restart --full ``` ## Benefits - ✅ Tests fail fast with clear error messages - ✅ Catches configuration issues before tests run - ✅ Verifies services are actually usable, not just running resolves #61 Co-authored-by: johba <johba@harb.eth> Reviewed-on: https://codeberg.org/johba/harb/pulls/65
2025-10-05 20:03:07 +02:00
*/
2025-10-07 21:57:32 +00:00
export async function validateStackHealthy(config: StackConfig): Promise<void> {
const results = await runAllHealthChecks(config);
feat: Add functional health checks for test prerequisites (#65) Replaces basic "service is up" checks with functional verification that tests can actually use the services. ## Changes ### New Health Checks - **RPC Proxy**: Verifies eth_call works and deployed contracts are accessible - **GraphQL**: Confirms Ponder has indexed data with non-zero stats - **Web App**: Validates endpoint accessibility ### Improvements - Clear error messages explain what failed and how to fix it - Checks verify actual functionality, not just HTTP 200 responses - Fails fast before tests run with cryptic errors ### Files - `tests/setup/health-checks.ts` - Core health check functions - `tests/setup/stack.ts` - Integration with waitForStackReady() - `tests/HEALTH_CHECKS.md` - Documentation and troubleshooting guide ## Error Message Example Before: ``` RPC health check failed with status 404 ``` After: ``` ❌ Stack health check failed Failed services: • RPC Proxy: RPC proxy returned HTTP 404 Expected 200, got 404. Check if Anvil is running and RPC_URL is correct. • GraphQL Indexer: GraphQL has no indexed data yet Ponder is running but has not indexed contract events. Troubleshooting: 1. Check stack logs: tail tests/.stack.log 2. Verify services are running: ./scripts/dev.sh status 3. Restart stack: ./scripts/dev.sh restart --full ``` ## Benefits - ✅ Tests fail fast with clear error messages - ✅ Catches configuration issues before tests run - ✅ Verifies services are actually usable, not just running resolves #61 Co-authored-by: johba <johba@harb.eth> Reviewed-on: https://codeberg.org/johba/harb/pulls/65
2025-10-05 20:03:07 +02:00
const failures = results.filter(r => !r.success);
if (failures.length > 0) {
2025-10-07 21:57:32 +00:00
const errorMessage = formatHealthCheckError(results);
console.error('\n❌ Stack health validation failed');
console.error('Tests require a pre-existing healthy stack.');
console.error('Start the stack with: ./scripts/dev.sh start\n');
console.error(errorMessage);
process.exit(1);
}
2025-10-07 21:57:32 +00:00
console.log('✅ Stack health validation passed');
}