forked from starknet-io/starknet.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Test setup refactor & remove RPC sequencer (starknet-io#1044)
* test: test setup refactor & remove rpc sequencer * fix: enable sequencer tests to run on goerli * chore: format markdowns * fix: remove unnecessary goerli describeIf comment * chore: remove setup verifier & simplify envs check logic * test: naming improvements * test: update test setup data log for testnet * test: change from goerli to sepolia * test: rename from rpc testnet to testnet * test: remove `TEST_PROVIDER_BASE_URL` --------- Co-authored-by: Luka Saric <[email protected]> Co-authored-by: Ivan Pavičić <[email protected]>
- Loading branch information
1 parent
d396275
commit d5f0b75
Showing
17 changed files
with
230 additions
and
614 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
/* Default test config based on run `starknet-devnet --seed 0` */ | ||
export const GS_DEFAULT_TEST_PROVIDER_URL = 'http://127.0.0.1:5050/'; | ||
|
||
export const LOCAL_DEVNET_NOT_RUNNING_MESSAGE = ` | ||
Local devnet is not running. In order to properly run it you need to do the following: \n | ||
- Go to the: https://hub.docker.com/r/shardlabs/starknet-devnet-rs/tags | ||
- Find the latest tag and copy the "docker pull" command | ||
- Run Docker on your machine | ||
- Run the command: "docker pull shardlabs/starknet-devnet-rs:latest" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* eslint-disable no-console */ | ||
import { GS_DEFAULT_TEST_PROVIDER_URL } from '../constants'; | ||
|
||
class AccountResolver { | ||
get providedUrl() { | ||
return process.env.TEST_RPC_URL; | ||
} | ||
|
||
get hasAllAccountEnvs() { | ||
return process.env.TEST_ACCOUNT_ADDRESS && process.env.TEST_ACCOUNT_PRIVATE_KEY; | ||
} | ||
|
||
get hasPartialAccountEnvs() { | ||
return process.env.TEST_ACCOUNT_ADDRESS || process.env.TEST_ACCOUNT_PRIVATE_KEY; | ||
} | ||
|
||
private async fetchAccount(url: string) { | ||
const response = await fetch(`${url}predeployed_accounts`); | ||
const [account] = await response.json(); | ||
const { address, private_key, initial_balance } = account; | ||
process.env.TEST_ACCOUNT_ADDRESS = address; | ||
process.env.TEST_ACCOUNT_PRIVATE_KEY = private_key; | ||
process.env.INITIAL_BALANCE = initial_balance; | ||
} | ||
|
||
private async isAccountSet(isDevnet: boolean): Promise<boolean> { | ||
if (this.hasAllAccountEnvs) { | ||
return true; | ||
} | ||
if (this.hasPartialAccountEnvs) { | ||
throw new Error( | ||
'If you are providing one of you need to provide both: TEST_ACCOUNT_ADDRESS & TEST_ACCOUNT_PRIVATE_KEY' | ||
); | ||
} | ||
if (isDevnet) { | ||
// get account from devnet | ||
try { | ||
await this.fetchAccount(GS_DEFAULT_TEST_PROVIDER_URL); | ||
return true; | ||
} catch (error) { | ||
console.error('Fetching account from devnet failed'); | ||
} | ||
} else if (this.providedUrl) { | ||
// try to get it from remote devnet | ||
try { | ||
await this.fetchAccount(this.providedUrl); | ||
return true; | ||
} catch (error) { | ||
console.error(`Fetching account from provided url ${this.providedUrl} failed`); | ||
} | ||
} | ||
|
||
throw new Error( | ||
'Setting Account using all known strategies failed, provide basic test parameters' | ||
); | ||
} | ||
|
||
async execute(isDevnet: boolean): Promise<void> { | ||
const isAccountSet = await this.isAccountSet(isDevnet); | ||
if (isAccountSet) console.log('Detected Account'); | ||
} | ||
} | ||
|
||
export default new AccountResolver(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* eslint-disable no-console */ | ||
import accountResolver from './accountResolver'; | ||
import { GS_DEFAULT_TEST_PROVIDER_URL, LOCAL_DEVNET_NOT_RUNNING_MESSAGE } from '../constants'; | ||
import { setIfNullish } from './env'; | ||
import { BaseUrl } from '../../../src/constants'; | ||
|
||
class StrategyResolver { | ||
private isDevnet = false; | ||
|
||
private isRpcNode = false; | ||
|
||
get isRpcDevnet() { | ||
return this.isDevnet || !!process.env.TEST_RPC_URL; | ||
} | ||
|
||
get isTestnet() { | ||
return process.env.TEST_RPC_URL?.includes(BaseUrl.SN_SEPOLIA); | ||
} | ||
|
||
get hasAllAccountEnvs() { | ||
const { TEST_ACCOUNT_ADDRESS, TEST_ACCOUNT_PRIVATE_KEY } = process.env; | ||
return !!(TEST_ACCOUNT_PRIVATE_KEY && TEST_ACCOUNT_ADDRESS); | ||
} | ||
|
||
private async isRsDevnet(): Promise<boolean> { | ||
const response = await fetch(GS_DEFAULT_TEST_PROVIDER_URL, { | ||
method: 'POST', | ||
headers: { Accept: 'application/json', 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'starknet_syncing' }), | ||
}); | ||
const { jsonrpc } = await response.json(); | ||
return jsonrpc === '2.0'; | ||
} | ||
|
||
async detectDevnet(): Promise<void> { | ||
// if on base url RPC endpoint work it is devnet-rs else it devnet-py | ||
try { | ||
this.isDevnet = await this.isRsDevnet(); | ||
if (this.isDevnet) console.log('Detected Devnet-RS'); | ||
} catch (error) { | ||
console.log('\x1b[36m%s\x1b[0m', LOCAL_DEVNET_NOT_RUNNING_MESSAGE); | ||
throw new Error( | ||
'Local RS devnet is not Running. Please follow the devnet setup instructions.' | ||
); | ||
} | ||
|
||
setIfNullish('IS_DEVNET', this.isRpcDevnet); | ||
} | ||
|
||
resolveRpc(): void { | ||
const hasRpcUrl = !!process.env.TEST_RPC_URL; | ||
|
||
this.isRpcNode = hasRpcUrl || this.isDevnet; | ||
|
||
if (!hasRpcUrl && this.isDevnet) { | ||
process.env.TEST_RPC_URL = GS_DEFAULT_TEST_PROVIDER_URL; | ||
} | ||
|
||
setIfNullish('IS_RPC', this.isRpcNode); | ||
setIfNullish('IS_TESTNET', this.isTestnet); | ||
|
||
console.log('Detected RPC'); | ||
} | ||
|
||
private logConfigInfo(): void { | ||
console.table({ | ||
TEST_ACCOUNT_ADDRESS: process.env.TEST_ACCOUNT_ADDRESS, | ||
TEST_ACCOUNT_PRIVATE_KEY: '****', | ||
INITIAL_BALANCE: process.env.INITIAL_BALANCE, | ||
TEST_RPC_URL: process.env.TEST_RPC_URL, | ||
TX_VERSION: process.env.TX_VERSION === 'v3' ? 'v3' : 'v2', | ||
}); | ||
|
||
console.table({ | ||
IS_DEVNET: process.env.IS_DEVNET, | ||
IS_RPC: process.env.IS_RPC, | ||
IS_TESTNET: process.env.IS_TESTNET, | ||
}); | ||
|
||
console.log('Global Test Environment is Ready'); | ||
} | ||
|
||
private verifyAccountData(shouldThrow?: boolean): void { | ||
const { TEST_ACCOUNT_ADDRESS, TEST_ACCOUNT_PRIVATE_KEY } = process.env; | ||
if (!TEST_ACCOUNT_ADDRESS) { | ||
if (shouldThrow) throw new Error('TEST_ACCOUNT_ADDRESS env is not provided'); | ||
console.log('\x1b[33m', 'TEST_ACCOUNT_ADDRESS env is not provided!'); | ||
delete process.env.TEST_ACCOUNT_ADDRESS; | ||
} | ||
if (!TEST_ACCOUNT_PRIVATE_KEY) { | ||
if (shouldThrow) throw new Error('TEST_ACCOUNT_PRIVATE_KEY env is not provided'); | ||
console.log('\x1b[33m', 'TEST_ACCOUNT_PRIVATE_KEY env is not provided!', '\x1b[0m'); | ||
delete process.env.TEST_ACCOUNT_PRIVATE_KEY; | ||
} | ||
} | ||
|
||
private useProvidedSetup(): void { | ||
setIfNullish('IS_DEVNET', false); | ||
setIfNullish('IS_RPC', !!process.env.TEST_RPC_URL); | ||
setIfNullish('IS_TESTNET', this.isTestnet); | ||
|
||
this.logConfigInfo(); | ||
|
||
console.log('Using Provided Test Setup'); | ||
} | ||
|
||
async execute(): Promise<void> { | ||
// 1. Assume setup is provided and ready; | ||
console.log('Global Test Setup Started'); | ||
this.verifyAccountData(); | ||
|
||
if (this.hasAllAccountEnvs) { | ||
this.useProvidedSetup(); | ||
return; | ||
} | ||
|
||
// 2. Try to detect devnet setup | ||
console.log('Basic test parameters are missing, Auto Setup Started'); | ||
|
||
await this.detectDevnet(); | ||
this.resolveRpc(); | ||
await accountResolver.execute(this.isDevnet); | ||
|
||
this.verifyAccountData(true); | ||
if (!this.hasAllAccountEnvs) console.error('Test Setup Environment is NOT Ready'); | ||
|
||
this.logConfigInfo(); | ||
} | ||
} | ||
|
||
export default new StrategyResolver(); |
Oops, something went wrong.