-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from dojoengine/recs-work
feat: test cover state & provider
- Loading branch information
Showing
17 changed files
with
234 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"$schema": "node_modules/lerna/schemas/lerna-schema.json", | ||
"version": "0.7.8", | ||
"version": "0.7.9-alpha.1", | ||
"packages": ["packages/*", "examples/*"], | ||
"npmClient": "pnpm" | ||
} |
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
2 changes: 1 addition & 1 deletion
2
packages/core/test/config/config.test.ts → ...ges/core/src/_test_/config/config.test.ts
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
4 changes: 2 additions & 2 deletions
4
packages/core/test/utils/utils.test.ts → packages/core/src/_test_/utils/utils.test.ts
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
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
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
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,87 @@ | ||
import { describe, it, expect, vi, beforeEach } from "vitest"; | ||
import { | ||
getSyncEntities, | ||
getEntities, | ||
syncEntities, | ||
setEntities, | ||
} from "../recs"; | ||
import { Component, setComponent } from "@dojoengine/recs"; | ||
import { Client } from "@dojoengine/torii-client"; | ||
import { convertValues } from "../utils"; | ||
|
||
// Mock dependencies | ||
vi.mock("@dojoengine/recs", () => ({ | ||
setComponent: vi.fn(), | ||
Component: vi.fn(), | ||
})); | ||
|
||
vi.mock("@dojoengine/torii-client", () => ({ | ||
Client: vi.fn(), | ||
})); | ||
|
||
vi.mock("../utils", () => ({ | ||
convertValues: vi.fn(), | ||
})); | ||
|
||
describe("RECS functions", () => { | ||
let mockClient: any; | ||
let mockComponents: any[]; | ||
|
||
beforeEach(() => { | ||
mockClient = { | ||
getAllEntities: vi.fn(), | ||
onEntityUpdated: vi.fn(), | ||
}; | ||
mockComponents = [{ schema: {} }, { schema: {} }]; | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
describe("getSyncEntities", () => { | ||
it("should call getEntities and syncEntities", async () => { | ||
const entities = [{ id: 1 }, { id: 2 }]; | ||
// Mock getAllEntities to return an object | ||
mockClient.getAllEntities.mockResolvedValue({ 1: {}, 2: {} }); | ||
|
||
await getSyncEntities(mockClient, mockComponents, entities); | ||
|
||
expect(mockClient.getAllEntities).toHaveBeenCalled(); | ||
expect(mockClient.onEntityUpdated).toHaveBeenCalledWith( | ||
entities, | ||
expect.any(Function) | ||
); | ||
}); | ||
}); | ||
|
||
describe("syncEntities", () => { | ||
it("should set up entity update listener", async () => { | ||
const entities = [{ id: 1 }, { id: 2 }]; | ||
|
||
await syncEntities(mockClient, mockComponents, entities); | ||
|
||
expect(mockClient.onEntityUpdated).toHaveBeenCalledWith( | ||
entities, | ||
expect.any(Function) | ||
); | ||
}); | ||
}); | ||
|
||
describe("setEntities", () => { | ||
it("should set components for each entity", async () => { | ||
const entities = { | ||
1: { comp1: { value: 10 } }, | ||
2: { comp2: { value: 20 } }, | ||
}; | ||
const components = { | ||
comp1: { schema: {} }, | ||
comp2: { schema: {} }, | ||
}; | ||
|
||
(convertValues as any).mockReturnValue({ value: "converted" }); | ||
|
||
await setEntities(entities as any, components as any); | ||
|
||
expect(setComponent).toHaveBeenCalledTimes(2); | ||
expect(convertValues).toHaveBeenCalledTimes(2); | ||
}); | ||
}); | ||
}); |
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,109 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { convertValues } from "../utils"; | ||
import { Type as RecsType } from "@dojoengine/recs"; | ||
|
||
describe("convertValues", () => { | ||
// ... existing tests ... | ||
|
||
describe("huge numbers", () => { | ||
it("should correctly convert huge BigInt values", () => { | ||
const schema = { hugeNumber: RecsType.BigInt }; | ||
const values = { | ||
hugeNumber: { | ||
value: "1000000000000000000000000000000000000000000000000000000000000001", | ||
}, | ||
}; | ||
const result = convertValues(schema, values); | ||
expect(result.hugeNumber).toBe( | ||
BigInt( | ||
"1000000000000000000000000000000000000000000000000000000000000001" | ||
) | ||
); | ||
}); | ||
|
||
it("should correctly convert huge BigInt values", () => { | ||
const schema = { hugeNumber: RecsType.BigInt }; | ||
const values = { | ||
hugeNumber: { | ||
value: "7f000000000000000000000000000000000000000000000000000000103fffff", | ||
}, | ||
}; | ||
const result = convertValues(schema, values); | ||
expect(result.hugeNumber).toBe( | ||
BigInt( | ||
"0x7f000000000000000000000000000000000000000000000000000000103fffff" | ||
) | ||
); | ||
}); | ||
|
||
it("should correctly convert huge Number values", () => { | ||
const schema = { hugeNumber: RecsType.Number }; | ||
const values = { hugeNumber: { value: "1e+308" } }; // Max safe double | ||
const result = convertValues(schema, values); | ||
expect(result.hugeNumber).toBe(1e308); | ||
}); | ||
|
||
it("should handle overflow for Number type", () => { | ||
const schema = { overflowNumber: RecsType.Number }; | ||
const values = { overflowNumber: { value: "1e+309" } }; // Beyond max safe double | ||
const result = convertValues(schema, values); | ||
expect(result.overflowNumber).toBe(Infinity); | ||
}); | ||
|
||
it("should correctly convert huge numbers in StringArray", () => { | ||
const schema = { hugeArray: RecsType.StringArray }; | ||
const values = { | ||
hugeArray: { | ||
value: [ | ||
{ value: "12345678901234567890" }, | ||
{ value: "98765432109876543210" }, | ||
], | ||
}, | ||
}; | ||
const result = convertValues(schema, values); | ||
expect(result.hugeArray).toEqual([ | ||
12345678901234567890n, | ||
98765432109876543210n, | ||
]); | ||
}); | ||
}); | ||
|
||
describe("additional test cases", () => { | ||
it("should correctly convert boolean values", () => { | ||
const schema = { isActive: RecsType.Boolean }; | ||
const values = { isActive: { value: true } }; | ||
const result = convertValues(schema, values); | ||
expect(result.isActive).toBe(true); | ||
}); | ||
|
||
it("should correctly convert string values", () => { | ||
const schema = { name: RecsType.String }; | ||
const values = { name: { value: "Test Name" } }; | ||
const result = convertValues(schema, values); | ||
expect(result.name).toBe("Test Name"); | ||
}); | ||
|
||
it("should handle null values", () => { | ||
const schema = { nullableField: RecsType.String }; | ||
const values = { nullableField: { value: null } }; | ||
const result = convertValues(schema, values); | ||
expect(result.nullableField).toBeNull(); | ||
}); | ||
|
||
it("should handle undefined values", () => { | ||
const schema = { undefinedField: RecsType.String }; | ||
const values = { undefinedField: { value: undefined } }; | ||
const result = convertValues(schema, values); | ||
expect(result.undefinedField).toBeUndefined(); | ||
}); | ||
|
||
it("should correctly convert nested schema values", () => { | ||
const schema = { nested: { innerField: RecsType.Number } }; | ||
const values = { | ||
nested: { value: { innerField: { value: "42" } } }, | ||
}; | ||
const result = convertValues(schema, values); | ||
expect(result.nested.innerField).toBe(42); | ||
}); | ||
}); | ||
}); |
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.