Contributing to Mineflayer Tests as an LLM
This guide explains how to add and modify tests in Mineflayer, based on the experience of working with the time-related functionality. It provides a structured approach for LLMs to help with test development and debugging.
Test Structure
Location
Tests are located in
test/externalTests/
Each test file corresponds to a specific functionality
Test files follow the naming convention of the feature they test (e.g.,
time.js
for time-related tests)
Basic Test Template
Writing Tests
1. Property Testing
Define expected properties and their types
Use
assert.strictEqual
for type checkingVerify value ranges where applicable
Example:
2. Helper Functions
Create reusable helper functions for common operations
Include functions for waiting and state verification
Use descriptive names that explain their purpose
Example:
3. Test Cases
Organize test cases in arrays for better maintainability
Include descriptive names and expected outcomes
Group related tests together
Example:
Running Tests
Basic Test Execution
Version-Specific Testing
Test against multiple Minecraft versions
Common versions to test: 1.14.4, 1.20.4, 1.21.3
Example:
Debugging Tests
1. Adding Debug Logs
Use
console.log
for debugging (remove before final commit)Log important state changes and values
Example:
2. Common Issues
Timing issues: Adjust wait times if needed (default 200ms)
Version compatibility: Check packet formats across versions
State synchronization: Ensure proper event handling
3. Test Output
Watch for server startup messages
Monitor bot commands and responses
Check for any error messages or warnings
Best Practices
Test Organization
Group related tests together
Use descriptive test names
Keep tests focused and atomic
Error Handling
Include clear error messages
Test edge cases
Verify state after each operation
Performance
Minimize wait times
Clean up resources
Avoid redundant tests
Documentation
Comment complex logic
Explain test purposes
Document version-specific behavior
Common Commands
Server Commands
Bot Operations
Version Compatibility
Test against multiple Minecraft versions
Handle version-specific packet formats
Consider backward compatibility
Document version-specific behavior
Adding a New Test
When adding a new test, follow these steps:
Create a new test file in the
test/externalTests
directory. For example,experience.js
.Write the test logic using async/await. Avoid using the
done
callback if possible.Handle version differences if necessary. For example, the experience command syntax differs between Minecraft versions:
For versions older than 1.13, use
/xp <amount> [player]
.For versions 1.13 and newer, use
/xp add <player> <amount> points
or/xp add <player> <amount> levels
.
Add event listeners for debugging if needed, and ensure they are removed at the end of the test to prevent memory leaks.
Use
bot.chat
to issue commands directly instead ofbot.test.runCommand
.Run the test for different Minecraft versions to ensure compatibility.
Example test structure:
Specific Details from Recent Experience
Version-Specific Command Syntax: Always check the Minecraft Wiki or existing tests for the correct command syntax for each version. For example, the experience command syntax changed in 1.13.
Event Listener Cleanup: Always remove event listeners at the end of the test to prevent memory leaks. Use
bot.removeListener('eventName', listenerFunction)
.Use
bot.chat
: For issuing commands, usebot.chat
directly instead ofbot.test.runCommand
to ensure commands are sent correctly.Debugging: Use
console.log
for debugging, but remove these statements before finalizing the test.
Title Plugin Implementation Details
Version-Specific Title Handling
Title packets changed significantly between versions:
1.8.8 uses a single
title
packet with an action field1.14.4+ uses separate packets for different title operations
Use
bot.supportFeature('titleUsesLegacyPackets')
to detect versionHandle both JSON and plain text title formats
Title Testing Strategy
Title-Specific Best Practices
Event Handling
Listen for both legacy and modern title events
Handle title clear events separately
Parse JSON title text properly
Version Compatibility
Test title display, subtitle, and clear operations
Verify title timing settings work
Check title text parsing across versions
Error Prevention
Handle malformed JSON in title text
Provide fallbacks for unsupported operations
Log title-related errors for debugging
Conclusion
When adding or modifying tests:
Understand the feature being tested
Write clear, focused tests
Test across multiple versions
Include proper error handling
Clean up debug code before committing
Document any version-specific behavior
Remember to remove any debugging console.log
statements before finalizing the changes.