-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
687 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Log } from "viem"; | ||
|
||
export function logSort(a: Log, b: Log): number { | ||
if (a.blockNumber === b.blockNumber) { | ||
if (a.logIndex === b.logIndex) return 0; | ||
if (a.logIndex == null) return 1; | ||
if (b.logIndex == null) return -1; | ||
return a.logIndex - b.logIndex; | ||
} | ||
|
||
if (a.blockNumber == null) return 1; | ||
if (b.blockNumber == null) return -1; | ||
if (a.blockNumber > b.blockNumber) return 1; | ||
if (a.blockNumber < b.blockNumber) return -1; | ||
return 0; | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
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
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,16 @@ | ||
import { createTestClient, http } from "viem"; | ||
|
||
export const anvilHost = "127.0.0.1"; | ||
export const anvilPort = 8555; | ||
|
||
// ID of the current test worker. Used by the `@viem/anvil` proxy server. | ||
export const poolId = Number(process.env.VITEST_POOL_ID ?? 1); | ||
|
||
export const anvilRpcUrl = `http://${anvilHost}:${anvilPort}/${poolId}`; | ||
|
||
export const testClient = createTestClient({ | ||
mode: "anvil", | ||
// TODO: if tests get slow, try switching to websockets? | ||
transport: http(anvilRpcUrl), | ||
pollingInterval: 10, | ||
}); |
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,19 @@ | ||
import { startProxy as startAnvilProxy } from "@viem/anvil"; | ||
import { anvilHost, anvilPort } from "./common"; | ||
import { execa } from "execa"; | ||
|
||
export default async function globalSetup(): Promise<() => Promise<void>> { | ||
console.log("building mock game"); | ||
await execa("pnpm", ["run", "build"], { | ||
cwd: `${__dirname}/../../../../test/mock-game-contracts`, | ||
}); | ||
|
||
const shutdownAnvilProxy = await startAnvilProxy({ | ||
host: anvilHost, | ||
port: anvilPort, | ||
}); | ||
|
||
return async () => { | ||
await shutdownAnvilProxy(); | ||
}; | ||
} |
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,35 @@ | ||
import { execa } from "execa"; | ||
import { anvilRpcUrl } from "./common"; | ||
import { Hex, isHex } from "viem"; | ||
import config from "mock-game-contracts/mud.config"; | ||
import worldAbi from "mock-game-contracts/out/IWorld.sol/IWorld.abi.json"; | ||
|
||
export { config, worldAbi }; | ||
|
||
export async function deployMockGame(): Promise<Hex> { | ||
console.log("deploying mock game to", anvilRpcUrl); | ||
const { stdout, stderr } = await execa( | ||
"pnpm", | ||
// skip build because its slow and we do it in global setup | ||
// if we don't skip build here, it regenerates ABIs which cause the tests to re-run (because we import the ABI here), which re-runs this deploy... | ||
["mud", "deploy", "--rpc", anvilRpcUrl, "--saveDeployment", "false", "--skipBuild"], | ||
{ | ||
cwd: `${__dirname}/../../../../test/mock-game-contracts`, | ||
env: { | ||
// anvil default account | ||
PRIVATE_KEY: "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80", | ||
DEBUG: "mud:*", | ||
}, | ||
}, | ||
); | ||
if (stderr) console.error(stderr); | ||
if (stdout) console.log(stdout); | ||
|
||
const [, worldAddress] = stdout.match(/worldAddress: '(0x[0-9a-f]+)'/i) ?? []; | ||
if (!isHex(worldAddress)) { | ||
throw new Error("world address not found in output, did the deploy fail?"); | ||
} | ||
console.log("deployed mock game", worldAddress); | ||
|
||
return worldAddress; | ||
} |
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,18 @@ | ||
import { beforeAll, beforeEach } from "vitest"; | ||
import { testClient } from "./common"; | ||
|
||
// Some test suites deploy contracts in a `beforeAll` handler, so we restore chain state here. | ||
beforeAll(async () => { | ||
const state = await testClient.dumpState(); | ||
return async (): Promise<void> => { | ||
await testClient.loadState({ state }); | ||
}; | ||
}); | ||
|
||
// Some tests execute transactions, so we restore chain state here. | ||
beforeEach(async () => { | ||
const state = await testClient.dumpState(); | ||
return async (): Promise<void> => { | ||
await testClient.loadState({ state }); | ||
}; | ||
}); |
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,12 @@ | ||
import { logSort, resourceToLabel, hexToResource } from "@latticexyz/common"; | ||
import { StoreLog } from "../storeLog"; | ||
|
||
export function summarizeLogs(logs: StoreLog[]) { | ||
return logs | ||
.slice() | ||
.sort(logSort) | ||
.map( | ||
({ eventName, args: { tableId, keyTuple } }) => | ||
`${eventName} ${resourceToLabel(hexToResource(tableId))} (${keyTuple})`, | ||
); | ||
} |
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
Oops, something went wrong.