♠️
RACE Protocol
NFT PresaleDiscord
  • ❤️Official Docs & Links
  • ⏳Progress Overview
  • RACE Protocol
    • 🏗️System Architecture
      • Components
      • On-chain Accounts
      • Synchronization
      • Randomization
      • Payment
    • 🎲Game Development
      • Game Handler
      • Effect
      • Event Handling
      • Race API
        • Arweave Integration
      • Race JS SDK
        • Installation and Setup
        • Key Components
          • AppClient
          • SubClient
          • Events
          • Game Context
          • Decryption Cache
        • Advanced Features
          • Getting Revealed Knowledge
          • Exiting and Detaching
          • Submitting Messages
          • Sub-game Interaction
        • Best Practices
        • API Reference
        • Troubleshooting
    • 🧪Testing
      • Race Test Kit
      • Unit Testing
      • Integration Testing
      • Additional Considerations
    • 🧱Modules & Features
      • Encryption Methods
      • Command-line Tools
      • Configuration Options
      • Blockchain Transport Implementations
    • 📃Smart Contract
      • Solana Program
    • 🔦Examples and Use Cases
      • Draw Card Game
      • Raffle Game
      • Other Examples
  • RACE Games
    • ♠️RACE Poker app
      • 🎮Start playing in minutes
        • 💰Cash and sit-n-go
        • 🏆Tournaments
      • 🎨Workshop
        • 🏆Create cash games
        • 🏨Create tourneys
      • 💡Concept introduction
      • 🏗️System architecture
      • 👾Game Flow
      • 🎲Cards shuffling
      • ☎️Communication
      • 🔐Key sharing/decryption
      • 💱Cash flow structure
    • ⚡Solfast
      • 🎲Game modes
  • RACE RESEARCH
    • 👾No-Code Development
      • Brainstorming
      • Implementation Approach
      • Project Status
    • 0️⃣Zero-Knowledge Proofs
      • Brainstorming
      • Integration steps
        • Verifiable Randomness Generation
        • Private Game State Updates
        • Verifiable Settlements
        • Private Player Actions
      • Project Status
    • 🛡️Security Audit Report
      • Executive summary
        • Introduction to Race Protocol
        • Audit Methodology
      • Findings
        • Smart Contract Security
        • WebAssembly Security
        • Client-side Security (Race SDK)
        • Server-side Security
        • Randomization and Encryption
        • On-chain Account Management
        • Synchronization Mechanisms
        • Payment Handling
      • Recommendations
      • Conclusion
  • RACE DAO
    • 😎About
    • 🫂Community
    • 🖼️NFT Collection [!]
Powered by GitBook
On this page
  • Examples
  • WASM Bundle (Rust)
  • Client-Side (TypeScript)
  1. RACE Protocol
  2. Testing

Unit Testing

Unit Testing with Race Test Kit

The Race test kit makes unit testing straightforward. You can:

  1. Create a test client: Instantiate a TestClient object to simulate a player or server.

// Rust example
use race_test::TestClient;
use race_core::types::ClientMode;

// Create a test client to simulate a player
let mut player = TestClient::player("Alice");

// Create a test client to simulate a transactor server
let mut transactor = TestClient::transactor("Foo");
  1. Build a test game account: Use the TestGameAccountBuilder to create a mock game account with the desired initial state and configuration.

// Rust example
use race_test::TestGameAccountBuilder;

// Define your game's account data structure
#[derive(BorshSerialize, BorshDeserialize)]
struct MyGameData {
    // ... game-specific data fields
}

// Create a test game account with initial data and configuration
let game_account = TestGameAccountBuilder::default()
    .add_player(&mut player, 1000) // Add a player with initial balance
    .set_transactor(&mut transactor) // Set the transactor server
    .with_data(MyGameData { /* ... */ }) // Provide initial game data
    .build();
  1. Initialize the game context and handler: Create a GameContext object based on the test game account and initialize your game handler using the TestHandler.

// Rust example
use race_core::context::GameContext;
use race_test::TestHandler;

// Create the game context
let mut ctx = GameContext::try_new(&game_account).unwrap();

// Initialize your game handler (replace MyGameHandler with your actual handler)
let mut handler = TestHandler::<MyGameHandler>::init_state(&mut ctx, &game_account).unwrap();
  1. Send events and observe outcomes: Use the test client to send custom and built-in events to the game handler. Assert that the game state and generated effects match your expectations.

// Rust example
use race_api::event::Event;

// Create a custom game event (replace MyCustomEvent with your event type)
let custom_event = Event::custom(player.id(), &MyCustomEvent { /* ... */ });

// Send the event to the game handler
handler.handle_event(&mut ctx, &custom_event).unwrap();

// Assert that the game state and effects are as expected
assert_eq!(ctx.get_status(), GameStatus::Running); // Example assertion
// ... more assertions

Remember:

  • Replace MyGameHandler and MyCustomEvent with your actual game handler and custom event types.

  • Write specific assertions to verify the expected behavior of your game logic after handling events.

By following these steps, you can effectively unit test your Race Protocol game bundles using the Race test kit.

Examples

WASM Bundle (Rust)

Here's an example of unit testing a custom event handler within the WASM bundle using the Race test kit in Rust:

use race_test::{TestClient, TestGameAccountBuilder, TestHandler};

let game_account = TestGameAccountBuilder::new().build();
let context = GameContext::new(game_account);
let handler = TestHandler::new(game_bundle, encryptor, client, decryption_cache);

let mut test_client = TestClient::new(context, handler);

// Send a custom event and assert the expected state update
test_client.submit_event(CustomEvent { ... });

let updated_state = test_client.context().handler_state();

assert_eq!(updated_state, expected_state);

This code snippet demonstrates how to use the test kit in Rust to create a test client, build a mock game account, and send a custom event to the game handler. The test then asserts that the resulting game state matches the expected outcome.

Client-Side (TypeScript)

import { TestClient, TestGameAccountBuilder, TestHandler } from '@race-foundation/sdk-core';

const gameAccount = new TestGameAccountBuilder().build();
const context = new GameContext(gameAccount);
const handler = new TestHandler(gameBundle, encryptor, client, decryptionCache);

const testClient = new TestClient(context, handler);

// Send a custom event and assert the expected state update
await testClient.submitEvent({ type: 'playerAction', ... });

const updatedState = context.handlerState;

assert.deepEqual(updatedState, expectedState);

This code snippet demonstrates how to use the test kit to create a test client, build a mock game account, and send a custom event to the game handler. The test then asserts that the resulting game state matches the expected outcome.

PreviousRace Test KitNextIntegration Testing

Last updated 1 year ago

Here's an example of unit testing in TypeScript using

🧪
Race JS SDK