Skip to content

Commit

Permalink
test(world): add tests for systems writing to the world
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Feb 17, 2023
1 parent e3b5be7 commit 9950bab
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions packages/world/test/World.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity >=0.8.0;

import "forge-std/Test.sol";
import { IStoreHook } from "store/IStore.sol";
import { StoreCore } from "store/StoreCore.sol";
import { StoreSwitch } from "store/StoreSwitch.sol";

import { World, _isRoute, _isSingleLevelRoute } from "../src/World.sol";
import { System } from "../src/System.sol";
Expand All @@ -19,6 +21,7 @@ struct WorldTestSystemReturn {

contract WorldTestSystem is System {
error WorldTestSystemError(string err);
event WorldTestSystemLog(string log);

function msgSender() public pure returns (address) {
return _msgSender();
Expand All @@ -44,6 +47,30 @@ contract WorldTestSystem is System {
}
return returndata;
}

function writeData(
string calldata accessRoute,
string calldata subRoute,
bool data
) public {
bytes32[] memory key = new bytes32[](1);
key[0] = "testKey";

if (StoreSwitch.isDelegateCall()) {
uint256 tableId = uint256(keccak256(abi.encodePacked(accessRoute, subRoute)));
StoreCore.setRecord(tableId, key, abi.encodePacked(data));
} else {
World(msg.sender).setRecord(accessRoute, subRoute, key, abi.encodePacked(data));
}
}

function emitCallType() public {
if (StoreSwitch.isDelegateCall()) {
emit WorldTestSystemLog("delegatecall");
} else {
emit WorldTestSystemLog("call");
}
}
}

contract WorldTestTableHook is IStoreHook {
Expand Down Expand Up @@ -73,6 +100,8 @@ contract WorldTestTableHook is IStoreHook {

contract WorldTest is Test {
event HookCalled(bytes data);
event WorldTestSystemLog(string log);

World world;

function setUp() public {
Expand Down Expand Up @@ -442,6 +471,66 @@ contract WorldTest is Test {
// TODO
}

function testWriteRootSystem() public {
// Register a new table
uint256 tableId = world.registerTable("", "/testTable", BoolSchemaLib.getSchema());

// Register a new system
WorldTestSystem system = new WorldTestSystem();
world.registerSystem("", "/testSystem", system, false);

// Call a system function that write data to the World
world.call("/testSystem", abi.encodeWithSelector(WorldTestSystem.writeData.selector, "", "/testTable", true));

// Expect the data to be written
assertTrue(BoolSchemaLib.get(tableId, world, "testKey"));
}

function testWriteAutonomousSystem() public {
// Register a new subroute
world.registerRoute("", "/testRoute");

// Register a new table
uint256 tableId = world.registerTable("/testRoute", "/testTable", BoolSchemaLib.getSchema());

// Register a new system
WorldTestSystem system = new WorldTestSystem();
world.registerSystem("/testRoute", "/testSystem", system, false);

// Call a system function that writes data to the World
world.call(
"/testRoute/testSystem",
abi.encodeWithSelector(WorldTestSystem.writeData.selector, "/testRoute", "/testTable", true)
);

// Expect the data to be written
assertTrue(BoolSchemaLib.get(tableId, world, "testKey"));
}

function testDelegatecallRootSystem() public {
// Register a new root system
WorldTestSystem system = new WorldTestSystem();
world.registerSystem("", "/testSystem", system, false);

// Call the root sysyem
vm.expectEmit(true, true, true, true);
emit WorldTestSystemLog("delegatecall");
world.call("/testSystem", abi.encodeWithSelector(WorldTestSystem.emitCallType.selector));
}

function testCallAutonomousSystem() public {
world.registerRoute("", "/testRoute");

// Register a new non-root system
WorldTestSystem system = new WorldTestSystem();
world.registerSystem("/testRoute", "/testSystem", system, false);

// Call the root sysyem
vm.expectEmit(true, true, true, true);
emit WorldTestSystemLog("call");
world.call("/testRoute/testSystem", abi.encodeWithSelector(WorldTestSystem.emitCallType.selector));
}

// TODO: add a test for systems writing to tables via the World

function testHashEquality() public {
Expand Down

0 comments on commit 9950bab

Please sign in to comment.