Skip to content

Commit

Permalink
add storage adapter test
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 18, 2024
1 parent 6907380 commit c0ab8a9
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
95 changes: 95 additions & 0 deletions packages/store-sync/src/stash/createStorageAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { beforeAll, describe, expect, it } from "vitest";
import { storeEventsAbi } from "@latticexyz/store";
import { createStorageAdapter } from "./createStorageAdapter";
import { config, deployMockGame } from "../../test/mockGame";
import { fetchAndStoreLogs } from "../fetchAndStoreLogs";
import { testClient } from "../../test/common";
import { getBlockNumber } from "viem/actions";
import { Address } from "viem";
import { createStash } from "@latticexyz/stash/internal";

describe("createStorageAdapter", async () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let worldAddress: Address;
beforeAll(async () => {
worldAddress = await deployMockGame();
});

it("sets component values from logs", async () => {
const stash = createStash(config);
const storageAdapter = createStorageAdapter({ config, stash });

console.log("fetching blocks");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const block of fetchAndStoreLogs({
storageAdapter,
publicClient: testClient,
events: storeEventsAbi,
fromBlock: 0n,
toBlock: await getBlockNumber(testClient),
})) {
// console.log("got block", block.blockNumber);
}

expect(stash.get().records).toMatchInlineSnapshot(`
{
"": {
"Health": {
"0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb": {
"health": 0n,
"player": "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb",
},
"0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e": {
"health": 5n,
"player": "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e",
},
"0x328809Bc894f92807417D2dAD6b7C998c1aFdac6": {
"health": 5n,
"player": "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6",
},
},
"Inventory": {},
"Position": {
"0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb": {
"player": "0x078cf0753dd50f7C56F20B3Ae02719EA199BE2eb",
"x": 3,
"y": 5,
},
"0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e": {
"player": "0x1D96F2f6BeF1202E4Ce1Ff6Dad0c2CB002861d3e",
"x": 1,
"y": -1,
},
"0x328809Bc894f92807417D2dAD6b7C998c1aFdac6": {
"player": "0x328809Bc894f92807417D2dAD6b7C998c1aFdac6",
"x": 3,
"y": 5,
},
"0xdBa86119a787422C593ceF119E40887f396024E2": {
"player": "0xdBa86119a787422C593ceF119E40887f396024E2",
"x": 100,
"y": 100,
},
},
"Score": {},
"Terrain": {
"3|5": {
"terrainType": 2,
"x": 3,
"y": 5,
},
},
"Winner": {},
},
}
`);

expect(stash.getRecord({ table: config.tables.Terrain, key: { x: 3, y: 5 } })).toMatchInlineSnapshot(`
{
"terrainType": 2,
"x": 3,
"y": 5,
}
`);
});
});
2 changes: 1 addition & 1 deletion packages/store-sync/src/stash/createStorageAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { size } from "viem";

export type CreateStorageAdapterOptions<config extends World> = {
config: config;
stash: CreateStoreResult;
stash: CreateStoreResult<config>;
};

const emptyValueArgs = {
Expand Down
5 changes: 0 additions & 5 deletions packages/world/ts/config/v2/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import { DynamicResolution, ValueWithType } from "./dynamicResolution";
import { Hex } from "viem";

export type Module = {
/**
* The name of the module
* @deprecated
*/
readonly name: string;
/** Should this module be installed as a root module? */
readonly root: boolean;
/** Arguments to be passed to the module's install method */
Expand Down
4 changes: 3 additions & 1 deletion packages/world/ts/config/v2/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export type resolveNamespaceMode<input> = "namespaces" extends keyof input
>;
};

type resolveModules<input> = { [key in keyof input]: mergeIfUndefined<input[key], MODULE_DEFAULTS> };

export type resolveWorld<input> = resolveNamespaceMode<input> &
Omit<resolveStore<input>, "multipleNamespaces" | "namespace" | "namespaces" | "tables"> & {
readonly tables: flattenNamespacedTables<resolveNamespaceMode<input>>;
Expand All @@ -88,7 +90,7 @@ export type resolveWorld<input> = resolveNamespaceMode<input> &
readonly excludeSystems: "excludeSystems" extends keyof input
? input["excludeSystems"]
: CONFIG_DEFAULTS["excludeSystems"];
readonly modules: "modules" extends keyof input ? input["modules"] : CONFIG_DEFAULTS["modules"];
readonly modules: "modules" extends keyof input ? resolveModules<input["modules"]> : CONFIG_DEFAULTS["modules"];
readonly codegen: show<resolveCodegen<"codegen" extends keyof input ? input["codegen"] : {}>>;
readonly deploy: show<resolveDeploy<"deploy" extends keyof input ? input["deploy"] : {}>>;
};
Expand Down

0 comments on commit c0ab8a9

Please sign in to comment.