diff --git a/tests/globalSetup.ts b/tests/globalSetup.ts index f76d774a..e9840f64 100644 --- a/tests/globalSetup.ts +++ b/tests/globalSetup.ts @@ -1,7 +1,7 @@ -import { type ChainConfig, initChain, killAllNetworks } from "./test.utils" +import { type NetworkConfig, initNetwork, killAllNetworks } from "./test.utils" export async function setup({ provide }) { - const network = await initChain() + const network = await initNetwork() const { bundlerInstance, instance, ...serializeableConfig } = network provide("globalNetwork", serializeableConfig) } @@ -12,6 +12,6 @@ export async function teardown() { declare module "vitest" { export interface ProvidedContext { - globalNetwork: ChainConfig + globalNetwork: NetworkConfig } } diff --git a/tests/instances/account.test.ts b/tests/instances/account.test.ts index 7c21d541..3576eef6 100644 --- a/tests/instances/account.test.ts +++ b/tests/instances/account.test.ts @@ -18,13 +18,17 @@ import { killNetwork, toTestClient } from "../test.utils" -import type { ChainConfig, MasterClient } from "../test.utils" -import { type TestFileNetworkType, toNetwork } from "../testSetup" +import type { + MasterClient, + NetworkConfig, + NetworkConfigWithBundler +} from "../test.utils" +import { type TestFileNetworkType, aaTest, toNetwork } from "../testSetup" const NETWORK_TYPE: TestFileNetworkType = "LOCAL" describe("account", () => { - let network: ChainConfig + let network: NetworkConfig let chain: Chain let bundlerUrl: string let testClient: MasterClient @@ -103,7 +107,7 @@ describe("account", () => { ]) }) - test("should check bytecode at Counter contract", async () => { + test("should", async () => { const counterAddress = network.deployment.counterAddress const byteCode = await testClient.getBytecode({ address: counterAddress }) expect(byteCode).toBeTruthy() diff --git a/tests/instances/bundler.test.ts b/tests/instances/bundler.test.ts index 0ed93054..0e4f52d7 100644 --- a/tests/instances/bundler.test.ts +++ b/tests/instances/bundler.test.ts @@ -18,13 +18,13 @@ import { killNetwork, toTestClient } from "../test.utils" -import type { ChainConfig, MasterClient } from "../test.utils" +import type { MasterClient, NetworkConfig } from "../test.utils" import { type TestFileNetworkType, toNetwork } from "../testSetup" const NETWORK_TYPE: TestFileNetworkType = "LOCAL" describe("bundler", () => { - let network: ChainConfig + let network: NetworkConfig let chain: Chain let bundlerUrl: string let testClient: MasterClient diff --git a/tests/instances/hook.module.test.ts b/tests/instances/hook.module.test.ts index 65a3800a..42a76f32 100644 --- a/tests/instances/hook.module.test.ts +++ b/tests/instances/hook.module.test.ts @@ -18,13 +18,13 @@ import { killNetwork, toTestClient } from "../test.utils" -import type { ChainConfig, MasterClient } from "../test.utils" +import type { MasterClient, NetworkConfig } from "../test.utils" import { type TestFileNetworkType, toNetwork } from "../testSetup" const NETWORK_TYPE: TestFileNetworkType = "LOCAL" describe("hook.module", () => { - let network: ChainConfig + let network: NetworkConfig let chain: Chain let bundlerUrl: string let testClient: MasterClient diff --git a/tests/instances/modules.test.ts b/tests/instances/modules.test.ts index 26cf59e9..d75a6a70 100644 --- a/tests/instances/modules.test.ts +++ b/tests/instances/modules.test.ts @@ -18,13 +18,13 @@ import { killNetwork, toTestClient } from "../test.utils" -import type { ChainConfig, MasterClient } from "../test.utils" +import type { MasterClient, NetworkConfig } from "../test.utils" import { type TestFileNetworkType, toNetwork } from "../testSetup" const NETWORK_TYPE: TestFileNetworkType = "LOCAL" describe("modules", () => { - let network: ChainConfig + let network: NetworkConfig let chain: Chain let bundlerUrl: string let testClient: MasterClient diff --git a/tests/test.utils.ts b/tests/test.utils.ts index 4bd510f0..77b7fa39 100644 --- a/tests/test.utils.ts +++ b/tests/test.utils.ts @@ -9,6 +9,7 @@ import { type Hex, type SetCodeParameters, createTestClient, + createWalletClient, encodeAbiParameters, parseAbi, parseAbiParameters, @@ -17,7 +18,12 @@ import { } from "viem" import { mnemonicToAccount } from "viem/accounts" import { anvil as anvilChain } from "viem/chains" -import type { EIP712DomainReturn, NexusSmartAccount } from "../src" +import { + type EIP712DomainReturn, + type NexusSmartAccount, + type NexusSmartAccountConfig, + createSmartAccountClient +} from "../src" import { getCustomChain } from "../src/account/utils" import { Logger } from "../src/account/utils/Logger" import { ENTRYPOINT_ADDRESS, ENTRYPOINT_SIMULATIONS } from "../src/contracts" @@ -55,9 +61,9 @@ export type AnvilDto = { instance: AnvilInstance deployment: Deployment } -export type ChainConfigWithBundler = AnvilDto & BundlerDto -export type ChainConfig = Omit< - ChainConfigWithBundler, +export type NetworkConfigWithBundler = AnvilDto & BundlerDto +export type NetworkConfig = Omit< + NetworkConfigWithBundler, "instance" | "bundlerInstance" > export const pKey = @@ -91,7 +97,7 @@ export const killNetwork = (ids: number[]) => }) ) -export const initChain = async (): Promise => { +export const initNetwork = async (): Promise => { const configuredChain = await initAnvilPayload() const bundlerConfig = await initBundlerInstance({ rpcUrl: configuredChain.rpcUrl @@ -197,6 +203,66 @@ export const nonZeroBalance = async ( ) } +export type FundedTestClients = Awaited> +export const toFundedTestClients = async ( + network: NetworkConfigWithBundler +) => { + const testConfig: Partial = { + factoryAddress: network.deployment.k1FactoryAddress, + k1ValidatorAddress: network.deployment.k1ValidatorAddress + } + + const chain = network.chain + const bundlerUrl = network.bundlerUrl + + const account = getTestAccount(2) + const recipientAccount = getTestAccount(3) + + const walletClient = createWalletClient({ + account, + chain, + transport: http() + }) + + const recipientWalletClient = createWalletClient({ + account: recipientAccount, + chain, + transport: http() + }) + + const testClient = toTestClient(chain, getTestAccount()) + + const smartAccount = await createSmartAccountClient({ + signer: walletClient, + bundlerUrl, + chain, + ...testConfig + }) + + const recipientSmartAccount = await createSmartAccountClient({ + signer: recipientWalletClient, + bundlerUrl, + chain, + ...testConfig + }) + + const smartAccountAddress = await smartAccount.getAddress() + const recipientSmartAccountAddress = await recipientSmartAccount.getAddress() + await fundAndDeploy(testClient, [smartAccount, recipientSmartAccount]) + + return { + account, + recipientAccount, + walletClient, + recipientWalletClient, + testClient, + smartAccount, + recipientSmartAccount, + smartAccountAddress, + recipientSmartAccountAddress + } +} + export const fundAndDeploy = async ( testClient: MasterClient, smartAccounts: NexusSmartAccount[] diff --git a/tests/testSetup.ts b/tests/testSetup.ts index 0b0baeed..2227f0bd 100644 --- a/tests/testSetup.ts +++ b/tests/testSetup.ts @@ -1,16 +1,26 @@ import { inject, test } from "vitest" -import { type ChainConfigWithBundler, initChain } from "./test.utils" +import { + type FundedTestClients, + type NetworkConfigWithBundler, + initNetwork, + toFundedTestClients +} from "./test.utils" -export const testWithBundler = test.extend<{ - rpc: ChainConfigWithBundler +export type NetworkConfigWithTestClients = NetworkConfigWithBundler & { + fundedTestClients: FundedTestClients +} + +export const aaTest = test.extend<{ + config: NetworkConfigWithTestClients }>({ // biome-ignore lint/correctness/noEmptyPattern: Needed in vitest :/ - rpc: async ({}, use) => { - const testChain = await initChain() - await use({ ...testChain }) + config: async ({}, use) => { + const testNetwork = await initNetwork() + const fundedTestClients = await toFundedTestClients(testNetwork) + await use({ ...testNetwork, fundedTestClients }) await Promise.all([ - testChain.instance.stop(), - testChain.bundlerInstance.stop() + testNetwork.instance.stop(), + testNetwork.bundlerInstance.stop() ]) } }) @@ -18,4 +28,4 @@ export const testWithBundler = test.extend<{ export type TestFileNetworkType = "LOCAL" | "GLOBAL" export const toNetwork = async (networkType: TestFileNetworkType) => // @ts-ignore - await (networkType === "GLOBAL" ? inject("globalNetwork") : initChain()) + await (networkType === "GLOBAL" ? inject("globalNetwork") : initNetwork())