Skip to content

fix: tests, improvements #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2024
Merged

fix: tests, improvements #302

merged 1 commit into from
Oct 12, 2024

Conversation

ponderingdemocritus
Copy link
Contributor

@ponderingdemocritus ponderingdemocritus commented Oct 12, 2024

  • updates

Summary by CodeRabbit

  • New Features

    • Enhanced synchronization mechanism in the Dojo application with the addition of a new asynchronous call.
  • Bug Fixes

    • Improved robustness of the convertValues function with expanded test coverage for various input scenarios.
  • Refactor

    • Simplified the convertValues function by extracting complex logic into dedicated helper functions.
  • Documentation

    • Updated dependencies in the package.json for better version management.

Copy link

coderabbitai bot commented Oct 12, 2024

Walkthrough

The changes in this pull request involve modifications to several files primarily within the @dojoengine/state package. A new asynchronous function call to getSyncEntities has been added to the setup.ts file, enhancing synchronization in the Dojo application. Additionally, the package.json for the state package has been updated to a specific version for a dependency. The test suite for the convertValues function has been expanded to cover more scenarios, and several functions have undergone parameter adjustments and refactoring to improve logging and maintainability.

Changes

File Path Change Summary
examples/example-vite-react-app-recs/src/dojo/setup.ts Added asynchronous call to getSyncEntities and included it in the return object of the setup function.
packages/state/package.json Updated @dojoengine/torii-client dependency from workspace:* to 1.0.0-alpha.20.
packages/state/src/tests/utils.test.ts Expanded tests for convertValues to handle various input types and scenarios.
packages/state/src/recs/index.ts Changed default logging parameter from true to false for several functions and added logging in setEntities.
packages/state/src/utils/index.ts Refactored convertValues by extracting logic into handleStringArray, handleBigInt, and handleDefault.

Possibly related PRs

  • feat: subscription clauses #235: The changes in this PR involve modifications to the getSyncEntities function, which is directly related to the new asynchronous call to getSyncEntities in the main PR's setup.ts file.
  • fix: utils - change way to compute selector hash #250: This PR also modifies the getEntities and setEntities functions in the same index.ts file, which may relate to the synchronization mechanisms enhanced in the main PR.
  • fix: entity sync #252: This PR updates the syncEntities function, which is relevant to the synchronization improvements made in the main PR.
  • fix: recs tests #301: This PR addresses modifications to the test suite for the convertValues function, which may indirectly relate to the changes in the main PR regarding synchronization and data handling.

🐰 In the land of code where rabbits play,
New sync calls hop in, brightening the day!
With tests that leap and functions refined,
Our Dojo's now stronger, more beautifully aligned.
So let’s celebrate with joy and delight,
For every small change makes our app feel just right! 🎉


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

🧹 Outside diff range and nitpick comments (5)
examples/example-vite-react-app-recs/src/dojo/setup.ts (2)

91-91: LGTM: getSync added to return object.

The addition of getSync to the return object is correct and consistent with its initialization earlier in the function. This makes the new functionality available to consumers of the setup function.

Consider adding a brief comment above the getSync property in the return object to explain its purpose, similar to other properties like eventSync. This would improve code readability and maintainability.


Line range hint 1-93: Overall, the changes look good with some areas for improvement.

The introduction of getSyncEntities and the new getSync functionality appears to be a valuable addition to the setup process. However, there are a few points to address:

  1. Clarify the purpose and types of parameters passed to getSyncEntities.
  2. Consider adding comments to explain the new functionality and its significance in the overall setup process.
  3. Ensure consistent usage of getSyncEntities across the project.

These improvements will enhance the code's readability and maintainability.

As this change introduces new synchronization functionality, it might be worth considering how this impacts the overall architecture of the application. Does this change affect performance or introduce any new dependencies that need to be managed? It may be beneficial to update any relevant documentation or architecture diagrams to reflect this new capability.

packages/state/src/recs/index.ts (3)

52-52: LGTM. Consider updating the documentation.

The change to set logging default to false is good for reducing unnecessary output in production. However, ensure that the function's documentation is updated to reflect this change in default behavior.

Update the function's JSDoc comment to indicate the new default value for the logging parameter.


202-202: LGTM. Update the documentation for consistency.

The change to set logging default to false is consistent with the previous function and good for reducing unnecessary output. Ensure that the function's documentation is updated to reflect this change in default behavior.

Update the function's JSDoc comment to indicate the new default value for the logging parameter.


Line range hint 313-373: LGTM. Enhanced logging improves debugging capabilities.

The changes to the setEntities function are well-implemented:

  1. Setting logging default to false is consistent with other functions.
  2. The new logging statements provide valuable debugging information.
  3. The added error check for undefined or invalid conversion results is a good safeguard.

These improvements will make it easier to diagnose issues during development and testing.

Consider using a logging library or a custom logger function to handle these log statements. This would allow for easier management of log levels and output formatting in the future.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 820346a and 9353306.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (5)
  • examples/example-vite-react-app-recs/src/dojo/setup.ts (3 hunks)
  • packages/state/package.json (1 hunks)
  • packages/state/src/tests/utils.test.ts (2 hunks)
  • packages/state/src/recs/index.ts (5 hunks)
  • packages/state/src/utils/index.ts (3 hunks)
🧰 Additional context used
🔇 Additional comments (21)
examples/example-vite-react-app-recs/src/dojo/setup.ts (1)

3-3: LGTM: Import statement updated correctly.

The import statement has been properly updated to include getSyncEntities, which is used later in the file. This change is consistent with the new functionality being added.

packages/state/src/recs/index.ts (1)

Line range hint 1-377: Overall, these changes improve code quality and debugging capabilities.

The modifications in this file demonstrate a consistent approach to:

  1. Reduce default verbosity by setting logging to false by default.
  2. Enhance debugging capabilities, particularly in the setEntities function.
  3. Improve error handling and reporting during the entity update process.

These changes will likely result in cleaner production logs and easier troubleshooting during development and testing phases.

To further improve the codebase:

  1. Update the JSDoc comments for all modified functions to reflect the new default logging behavior.
  2. Consider implementing a centralized logging mechanism to manage log levels and formatting consistently across the entire module.
packages/state/src/utils/index.ts (5)

Line range hint 1-46: Refactoring improves readability and maintainability

The extraction of complex logic into helper functions (handleStringArray, handleBigInt, handleDefault) simplifies the convertValues function and enhances readability and maintainability.


8-10: Null check correctly handles null and undefined values

The use of value == null effectively checks for both null and undefined, ensuring that these cases are appropriately handled.


20-21: Integration of handleStringArray enhances modularity

By delegating string array handling to handleStringArray, the code becomes more modular and easier to maintain.


28-29: Integration of handleBigInt improves code clarity

Using handleBigInt centralizes BigInt conversion logic, making the code cleaner and more maintainable.


40-41: Use of handleDefault function enhances extensibility

Delegating to handleDefault in the default case allows for easier extension and handling of additional types in the future.

packages/state/src/__tests__/utils.test.ts (14)

1-2: Imports updated appropriately

The addition of Schema and vi to the imports ensures that all necessary dependencies are available for the extended test cases.


112-127: Test for handling null and undefined values is comprehensive

This test correctly verifies that convertValues can handle undefined values in the input, ensuring that optional fields are managed appropriately without causing errors.


128-139: Accurate conversion of enum types to strings

The test ensures that enum values are correctly converted to their string representations, validating the handling of enum types within convertValues.


141-152: Proper handling of empty arrays in RecsType.StringArray

This test confirms that convertValues correctly processes an empty array for RecsType.StringArray, which is important for cases where array fields may legitimately be empty.


154-171: Correct conversion of enum items within StringArray

The test verifies that convertValues can handle arrays containing enum values, converting each enum item to its string representation as expected.


195-214: Effective handling of BigInt conversion failures

The test appropriately mocks console.warn to suppress warnings during execution and verifies that a warning is issued when BigInt conversion fails, ensuring that such cases are properly logged.


216-227: Validation of RecsType.String conversion

This test confirms that string values are correctly returned by convertValues, ensuring basic string handling functions as expected.


229-240: Accurate conversion of valid BigInt values

The test ensures that convertValues successfully converts string representations of BigInt numbers into actual BigInt types, verifying the function's ability to handle large integers.


242-253: Correct handling of boolean values

This test verifies that convertValues processes boolean values accurately, confirming proper handling of RecsType.Boolean.


255-266: Proper conversion of numeric strings to numbers

The test ensures that numeric string values are converted to numbers correctly, validating the numeric conversion logic within convertValues.


268-295: Successful processing of nested structures

The test demonstrates that convertValues can handle nested schemas, converting nested values appropriately. This is crucial for complex data structures that contain multiple layers.


323-334: Appropriate fallback for unknown types

The test confirms that when convertValues encounters an unknown type, it falls back to returning the original string value. This ensures robustness against unexpected input types.


336-343: Correct behavior with empty schema

This test verifies that convertValues returns an empty object when provided with an empty schema, which is the expected behavior in such cases.


297-321: Verify handling of Map structures in convertValues

In this test case, values.config.value is a Map object. Ensure that convertValues is capable of processing Map structures. If convertValues expects plain objects, the test might not behave as intended, and the handling of Map may need to be adjusted.

Run the following script to verify if convertValues handles Map inputs:

@@ -25,7 +25,7 @@
},
"dependencies": {
"@dojoengine/recs": "2.0.13",
"@dojoengine/torii-client": "workspace:*",
"@dojoengine/torii-client": "1.0.0-alpha.20",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Inconsistent @dojoengine/torii-client Versions Detected

The packages/state/package.json is using version 1.0.0-alpha.20 for @dojoengine/torii-client, while other packages are referencing it as workspace:*. This discrepancy may lead to version conflicts and affect dependency management across the project.

  • Action Required:
    • Update all package.json files to use a consistent version for @dojoengine/torii-client.
    • Alternatively, revert packages/state/package.json to use workspace:* if the specific versioning is not necessary.
🔗 Analysis chain

LGTM! Consider the impact on development workflow.

The change from workspace:* to a specific version 1.0.0-alpha.20 for @dojoengine/torii-client is approved. This update aligns with the package version, which is a good practice for version consistency.

However, please consider the following:

  1. This change might impact the development workflow, especially if you were previously using a local workspace reference.
  2. Ensure that this version is consistent with other packages in the project that might depend on @dojoengine/torii-client.

To verify version consistency across the project, run the following script:

This script will help identify any inconsistencies in the usage of @dojoengine/torii-client across the project.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for inconsistencies in @dojoengine/torii-client versions across the project

# Search for @dojoengine/torii-client in all package.json files
echo "Searching for @dojoengine/torii-client in package.json files:"
rg --type json '"@dojoengine/torii-client"' -g 'package.json'

# Search for import statements of @dojoengine/torii-client in TypeScript/JavaScript files
echo -e "\nSearching for import statements of @dojoengine/torii-client:"
rg --type-add 'ts:*.{ts,tsx,js,jsx}' --type ts 'from\s+[''"]@dojoengine/torii-client[''"]'

Length of output: 3633

Comment on lines +27 to +34
const getSync = await getSyncEntities(
toriiClient,
contractComponents as any,
undefined,
[],
3000,
true
);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Clarification Needed on getSyncEntities Parameters

The initialization of getSync looks good, but there are a few points that need clarification:

  1. The contractComponents is cast to any. Is this necessary, or can we use a more specific type?
  2. Could you explain the purpose of the hardcoded values (undefined, empty array, 3000, true)?
  3. Consider adding comments to explain the significance of these parameters for better maintainability.
🔗 Analysis chain

Please clarify the parameters passed to getSyncEntities.

The initialization of getSync looks good, but there are a few points that need clarification:

  1. The contractComponents is cast to any. Is this necessary, or can we use a more specific type?
  2. Could you explain the purpose of the hardcoded values (undefined, empty array, 3000, true)?
  3. Consider adding comments to explain the significance of these parameters for better maintainability.

To ensure getSyncEntities is used consistently across the project, let's check its usage:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other usages of getSyncEntities
rg --type typescript 'getSyncEntities\(' -A 5

Length of output: 82


Script:

#!/bin/bash
# Search for other usages of getSyncEntities in TypeScript files
rg --type ts --type tsx 'getSyncEntities\(' -A 5

Length of output: 78


Script:

#!/bin/bash
# Search for other usages of getSyncEntities in .ts and .tsx files
rg 'getSyncEntities\(' --glob '*.ts' --glob '*.tsx' -A 5

Length of output: 4520

Comment on lines +48 to +65
function handleStringArray(value: any) {
if (value.type === "array" && value.value.length === 0) {
return [];
}
if (value.type === "array" && value.value[0]?.type === "enum") {
return value.value.map((item: any) => item.value.option);
}
return value.value.map((a: any) => {
try {
return BigInt(a.value);
} catch (error) {
console.warn(
`Failed to convert ${a.value} to BigInt. Using string value instead.`
);
return a.value;
}
});
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure consistent return types in handleStringArray

The handleStringArray function may return an array containing both BigInts and strings if BigInt conversion fails. This could lead to inconsistent data types and potential issues downstream.

Consider ensuring that the function returns an array of consistent types. You may decide to handle conversion failures differently or provide a default value.

Comment on lines +48 to +109
function handleStringArray(value: any) {
if (value.type === "array" && value.value.length === 0) {
return [];
}
if (value.type === "array" && value.value[0]?.type === "enum") {
return value.value.map((item: any) => item.value.option);
}
return value.value.map((a: any) => {
try {
return BigInt(a.value);
} catch (error) {
console.warn(
`Failed to convert ${a.value} to BigInt. Using string value instead.`
);
return a.value;
}
});
}

function handleBigInt(value: string | bigint) {
if (typeof value === "bigint") {
return value;
}
try {
return BigInt(value);
} catch (error) {
console.warn(
`Failed to convert ${value} to BigInt. Attempting hexadecimal conversion.`
);
try {
return BigInt(`0x${value}`);
} catch (hexError) {
console.warn(
`Failed to convert 0x${value} to BigInt. Using string value instead.`
);
return value;
}
}
}

function handleDefault(schemaType: any, value: any) {
if (typeof schemaType === "object" && value.type === "struct") {
if (value.value instanceof Map) {
const structValues = Object.fromEntries(value.value);
return convertValues(schemaType, structValues);
} else if (typeof value.value === "object") {
// Handle cases where value.value might already be a plain object
return convertValues(schemaType, value.value);
} else {
console.warn(
`Expected value.value to be a Map or object for struct type, got ${typeof value.value}.`
);
return value.value;
}
}
if (Array.isArray(schemaType) && value.type === "array") {
return value.value.map((item: any) =>
convertValues(schemaType[0], item)
);
}
return value.value;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance type safety by specifying parameter and return types

The helper functions use any for parameters and return types, which reduces the benefits of TypeScript's type checking.

Consider specifying more precise types for parameters and return values to improve type safety and catch potential errors at compile time.

Comment on lines +68 to +86
if (typeof value === "bigint") {
return value;
}
try {
return BigInt(value);
} catch (error) {
console.warn(
`Failed to convert ${value} to BigInt. Attempting hexadecimal conversion.`
);
try {
return BigInt(`0x${value}`);
} catch (hexError) {
console.warn(
`Failed to convert 0x${value} to BigInt. Using string value instead.`
);
return value;
}
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Simplify error handling in handleBigInt function

The nested try...catch blocks in handleBigInt can be flattened to improve readability.

Consider restructuring the error handling to eliminate nesting:

function handleBigInt(value: string | bigint) {
    if (typeof value === "bigint") {
        return value;
    }
    try {
        return BigInt(value);
    } catch (error) {
        try {
            return BigInt(`0x${value}`);
        } catch (hexError) {
            console.warn(
                `Failed to convert ${value} to BigInt. Using string value instead.`
            );
            return value;
        }
    }
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (typeof value === "bigint") {
return value;
}
try {
return BigInt(value);
} catch (error) {
console.warn(
`Failed to convert ${value} to BigInt. Attempting hexadecimal conversion.`
);
try {
return BigInt(`0x${value}`);
} catch (hexError) {
console.warn(
`Failed to convert 0x${value} to BigInt. Using string value instead.`
);
return value;
}
}
}
function handleBigInt(value: string | bigint) {
if (typeof value === "bigint") {
return value;
}
try {
return BigInt(value);
} catch (error) {
try {
return BigInt(`0x${value}`);
} catch (hexError) {
console.warn(
`Failed to convert ${value} to BigInt. Using string value instead.`
);
return value;
}
}
}

Comment on lines +89 to +102
if (typeof schemaType === "object" && value.type === "struct") {
if (value.value instanceof Map) {
const structValues = Object.fromEntries(value.value);
return convertValues(schemaType, structValues);
} else if (typeof value.value === "object") {
// Handle cases where value.value might already be a plain object
return convertValues(schemaType, value.value);
} else {
console.warn(
`Expected value.value to be a Map or object for struct type, got ${typeof value.value}.`
);
return value.value;
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add null checks for value.value in handleDefault

In handleDefault, accessing value.value without checking for null could lead to runtime errors. Since typeof null returns "object", the check typeof value.value === "object" may pass when value.value is actually null.

Consider adding a null check to ensure value.value is not null:

} else if (typeof value.value === "object" && value.value !== null) {
    // Handle cases where value.value might already be a plain object
    return convertValues(schemaType, value.value);
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (typeof schemaType === "object" && value.type === "struct") {
if (value.value instanceof Map) {
const structValues = Object.fromEntries(value.value);
return convertValues(schemaType, structValues);
} else if (typeof value.value === "object") {
// Handle cases where value.value might already be a plain object
return convertValues(schemaType, value.value);
} else {
console.warn(
`Expected value.value to be a Map or object for struct type, got ${typeof value.value}.`
);
return value.value;
}
}
if (typeof schemaType === "object" && value.type === "struct") {
if (value.value instanceof Map) {
const structValues = Object.fromEntries(value.value);
return convertValues(schemaType, structValues);
} else if (typeof value.value === "object" && value.value !== null) {
// Handle cases where value.value might already be a plain object
return convertValues(schemaType, value.value);
} else {
console.warn(
`Expected value.value to be a Map or object for struct type, got ${typeof value.value}.`
);
return value.value;
}
}

Comment on lines +173 to +193
it("should handle RecsType.StringArray with BigInt conversion", () => {
const schema: Schema = {
ids: RecsType.StringArray,
};
const values = {
ids: {
value: [
{ value: "12345678901234567890", type: "string" },
{ value: "98765432109876543210", type: "string" },
],
type: "array",
},
};
const expected = {
ids: [
BigInt("12345678901234567890"),
BigInt("98765432109876543210"),
],
};
expect(convertValues(schema, values)).toEqual(expected);
});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Potential inconsistency: RecsType.StringArray expected to return strings

In this test case, RecsType.StringArray is used in the schema, but the expected result contains an array of BigInt values. Since RecsType.StringArray implies an array of strings, converting items to BigInt may be inconsistent with the type definition. Consider using a type like RecsType.BigIntArray if available, or adjust the expected output to reflect an array of strings.

@ponderingdemocritus ponderingdemocritus merged commit e7d6f1f into main Oct 12, 2024
2 of 3 checks passed
@ponderingdemocritus ponderingdemocritus deleted the fix/recs branch October 12, 2024 04:51
This was referenced Oct 17, 2024
@coderabbitai coderabbitai bot mentioned this pull request Nov 1, 2024
@coderabbitai coderabbitai bot mentioned this pull request Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant