Skip to content

Commit

Permalink
feat: state fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ponderingdemocritus committed Oct 2, 2024
1 parent 94bddf6 commit 02b6567
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 201 deletions.
8 changes: 3 additions & 5 deletions examples/example-vite-react-app-recs/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useComponentValue, useQuerySync } from "@dojoengine/react";
import { Entity } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";

import { Direction } from "./dojo/typescript/models.gen";
import { useDojo } from "./dojo/useDojo";

enum DirectionEnum {
Expand Down Expand Up @@ -44,10 +43,6 @@ function App() {
const moves = useComponentValue(Moves, entityId);
const directions = useComponentValue(DirectionsAvailable, entityId);

const moved = useComponentValue(Moved, entityId);

console.log("moved", moved);

const handleRestoreBurners = async () => {
try {
await account?.applyFromClipboard();
Expand Down Expand Up @@ -78,14 +73,17 @@ function App() {
<button onClick={() => account?.create()}>
{account?.isDeploying ? "deploying burner" : "create burner"}
</button>

{account && account?.list().length > 0 && (
<button onClick={async () => await account?.copyToClipboard()}>
Save Burners to Clipboard
</button>
)}

<button onClick={handleRestoreBurners}>
Restore Burners from Clipboard
</button>

{clipboardStatus.message && (
<div className={clipboardStatus.isError ? "error" : "success"}>
{clipboardStatus.message}
Expand Down
Empty file.
6 changes: 2 additions & 4 deletions packages/state/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@dojoengine/state",
"version": "1.0.0-alpha.16",
"description": "dojo: State syncing for dojo games. Currently supports RECS.",
"description": "dojo: State syncing for dojo games",
"author": "dojo",
"license": "MIT",
"main": "dist/index.js",
Expand All @@ -26,8 +26,6 @@
"dependencies": {
"@dojoengine/recs": "2.0.13",
"@dojoengine/torii-client": "workspace:*",
"@latticexyz/utils": "^2.2.8",
"vitest": "^1.6.0",
"zustand": "^4.5.5"
"vitest": "^1.6.0"
}
}
213 changes: 207 additions & 6 deletions packages/state/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Type as RecsType } from "@dojoengine/recs";
import { describe, expect, it } from "vitest";
import { Type as RecsType, Schema } from "@dojoengine/recs";
import { describe, expect, it, beforeEach, vi } from "vitest";

import { convertValues } from "../utils";

describe("convertValues", () => {
// ... existing tests ...
// Mock console.warn to suppress warnings during tests
beforeEach(() => {
vi.spyOn(console, "warn").mockImplementation(() => {});
});

describe("huge numbers", () => {
it("should correctly convert huge BigInt values", () => {
Expand All @@ -22,7 +25,7 @@ describe("convertValues", () => {
);
});

it("should correctly convert huge BigInt values", () => {
it("should correctly convert huge hexadecimal BigInt values", () => {
const schema = { hugeNumber: RecsType.BigInt };
const values = {
hugeNumber: {
Expand Down Expand Up @@ -55,6 +58,7 @@ describe("convertValues", () => {
const schema = { hugeArray: RecsType.StringArray };
const values = {
hugeArray: {
type: "array",
value: [
{ value: "12345678901234567890" },
{ value: "98765432109876543210" },
Expand All @@ -67,6 +71,28 @@ describe("convertValues", () => {
98765432109876543210n,
]);
});

it("should handle empty StringArray", () => {
const schema = { tags: RecsType.StringArray };
const values = { tags: { type: "array", value: [] } };
const result = convertValues(schema, values);
expect(result.tags).toEqual([]);
});

it("should correctly convert StringArray with enum types", () => {
const schema = { statuses: RecsType.StringArray };
const values = {
statuses: {
type: "array",
value: [
{ type: "enum", value: { option: "ACTIVE" } },
{ type: "enum", value: { option: "INACTIVE" } },
],
},
};
const result = convertValues(schema, values);
expect(result.statuses).toEqual(["ACTIVE", "INACTIVE"]);
});
});

describe("additional test cases", () => {
Expand Down Expand Up @@ -101,11 +127,186 @@ describe("convertValues", () => {
it("should correctly convert nested schema values", () => {
const schema = { nested: { innerField: RecsType.Number } };
const values = {
nested: { value: { innerField: { value: "42" } } },
nested: {
type: "struct",
value: { innerField: { value: "42" } },
},
};
const result = convertValues(schema, values);

expect(result.nested).toEqual({ innerField: 42 });
});
});

describe("enum type handling", () => {
it("should correctly convert enum values", () => {
const schema = { status: RecsType.T };
const values = {
status: { type: "enum", value: { option: "ACTIVE" } },
};
const result = convertValues(schema, values);
expect(result.status).toBe("ACTIVE");
});
});

describe("BigInt conversion fallback", () => {
it("should fallback to hexadecimal conversion for invalid BigInt strings", () => {
const schema = { invalidBigInt: RecsType.BigInt };
const values = { invalidBigInt: { value: "invalid_bigint" } };
const result = convertValues(schema, values);
// Since "invalid_bigint" is not a valid decimal or hexadecimal BigInt, expect undefined.
expect(result.invalidBigInt).toBeUndefined();
});
});

describe("array of structs", () => {
it("should correctly convert array of structs", () => {
const schema = {
users: [
{
name: RecsType.String,
age: RecsType.Number,
},
],
};
const values = {
users: {
type: "array",
value: [
{
type: "struct",
value: {
name: { value: "Alice" },
age: { value: "30" },
},
},
{
type: "struct",
value: {
name: { value: "Bob" },
age: { value: "25" },
},
},
],
},
};
const result = convertValues(schema as any, values);
expect(result.users).toEqual([
{ name: "Alice", age: 30 },
{ name: "Bob", age: 25 },
]);
});
});

describe("default case handling", () => {
it("should assign value directly for unhandled schema types", () => {
const schema = { miscellaneous: RecsType.T };
const values = { miscellaneous: { value: { random: "data" } } };
const result = convertValues(schema, values);
expect(result.miscellaneous).toEqual({ random: "data" });
});

expect(result.nested).toEqual({ innerField: { value: "42" } });
it("should handle struct with Map as value", () => {
const schema = {
config: {
settingA: RecsType.String,
settingB: RecsType.Number,
},
};
const values = {
config: {
type: "struct",
value: new Map([
["settingA", { value: "Enabled" }],
["settingB", { value: "100" }],
]),
},
};
const result = convertValues(schema, values);
expect(result.config).toEqual({
settingA: "Enabled",
settingB: 100,
});
});

it("should handle nested arrays of structs", () => {
const schema = {
departments: [
{
name: RecsType.String,
employees: [
{
name: RecsType.String,
role: RecsType.String,
},
],
},
],
};
const values = {
departments: {
type: "array",
value: [
{
type: "struct",
value: {
name: { value: "Engineering" },
employees: {
type: "array",
value: [
{
type: "struct",
value: {
name: { value: "Alice" },
role: { value: "Developer" },
},
},
{
type: "struct",
value: {
name: { value: "Bob" },
role: { value: "Designer" },
},
},
],
},
},
},
{
type: "struct",
value: {
name: { value: "Marketing" },
employees: {
type: "array",
value: [
{
type: "struct",
value: {
name: { value: "Charlie" },
role: { value: "Manager" },
},
},
],
},
},
},
],
},
};
const result = convertValues(schema as any, values);
expect(result.departments).toEqual([
{
name: "Engineering",
employees: [
{ name: "Alice", role: "Developer" },
{ name: "Bob", role: "Designer" },
],
},
{
name: "Marketing",
employees: [{ name: "Charlie", role: "Manager" }],
},
]);
});
});
});
64 changes: 0 additions & 64 deletions packages/state/src/__tests__/zustand.test.ts

This file was deleted.

Loading

0 comments on commit 02b6567

Please sign in to comment.