World Model Testing Pt 1.

There were some challenges here, specifically visibility. I have about 10 ADRs (Architecture Decision Records) that I'll share that address complex issues like lighting. We implemented a baseline standard isDark lighting system and this can be replaced with future extensions.

PASS tests/unit/world/visibility-behavior.test.ts (18.625 s)
  VisibilityBehavior
    canSee
      ✓ should always see self (372 ms)
      ✓ should see entities in same room (1 ms)
      ✓ should not see entities in different room (1 ms)
      ✓ should see the room observer is in
      ✓ should not see invisible entities
      ✓ should see entities in transparent containers
      ✓ should see entities in open opaque containers (9 ms)
      ✓ should not see entities in closed opaque containers (1 ms)
      ✓ should handle nested containers
      ✓ should block sight through any closed container in path (1 ms)
    dark rooms
      ✓ should not see anything in dark room without light
      ✓ should only see lit light sources in dark room (1 ms)
      ✓ should see everything when carrying lit lamp
      ✓ should not benefit from light in closed container
      ✓ should handle room lighting toggle
    getVisible
      ✓ should return all visible entities
      ✓ should include carried items (1 ms)
      ✓ should handle empty room
      ✓ should handle observer not in room (1 ms)
    isVisible
      ✓ should return true for uncontained entities
      ✓ should return false for invisible scenery
      ✓ should return true for entity in transparent container
      ✓ should return true for entity in open opaque container
      ✓ should return false for entity in closed opaque container
      ✓ should handle opaque container without openable trait
    complex scenarios
      ✓ should handle deeply nested visibility
      ✓ should handle supporter visibility
      ✓ should handle visibility in nested containers (1 ms)
      ✓ should handle circular containment gracefully
    edge cases
      ✓ should handle missing entities gracefully (1 ms)
      ✓ should handle entities with no location
      ✓ should handle max depth in containment path

PASS tests/unit/world/world-model.test.ts (19.921 s)
  WorldModel
    initialization
      ✓ should create empty world model (415 ms)
      ✓ should accept configuration
    entity management
      ✓ should create entity (1 ms)
      ✓ should throw on duplicate entity id (163 ms)
      ✓ should get entity by id (1 ms)
      ✓ should return undefined for missing entity (1 ms)
      ✓ should check entity existence
      ✓ should remove entity
      ✓ should return false when removing non-existent entity
      ✓ should get all entities
      ✓ should update entity (1 ms)
      ✓ should handle updating non-existent entity
      ✓ should throw in strict mode when updating non-existent entity
    spatial management
      ✓ should get entity location (1 ms)
      ✓ should get container contents (1 ms)
      ✓ should move entity
      ✓ should remove entity from world
      ✓ should check if move is valid
      ✓ should prevent moving to non-container
      ✓ should prevent containment loops
      ✓ should get containing room (1 ms)
      ✓ should get all contents recursively
      ✓ should handle max depth limit
    world state management
      ✓ should get and set state (1 ms)
      ✓ should get and set state values
      ✓ should handle nested state values
    query operations
      ✓ should find entities by trait (1 ms)
      ✓ should find entities by type
      ✓ should find entities with predicate
      ✓ should handle find options (1 ms)
    visibility and scope
      ✓ should get entities in scope
      ✓ should include carried items in scope
      ✓ should check visibility (1 ms)
    relationships
      ✓ should add relationship
      ✓ should get related entities
      ✓ should remove relationship
      ✓ should handle multiple relationship types
      ✓ should handle non-existent entities in non-strict mode
      ✓ should throw in strict mode for non-existent entities (1 ms)
    utility methods
      ✓ should calculate total weight
      ✓ should detect containment loops
      ✓ should find path between rooms
      ✓ should get and set player
      ✓ should throw when setting non-existent player (1 ms)
    persistence
      ✓ should serialize to JSON (1 ms)
      ✓ should load from JSON (1 ms)
      ✓ should clear world
    event sourcing
      ✓ should register and apply event handler (2 ms)
      ✓ should validate events (1 ms)
      ✓ should throw when applying invalid event
      ✓ should preview event changes
      ✓ should track event history
      ✓ should get events since timestamp (1 ms)
      ✓ should clear event history
      ✓ should unregister event handler
      ✓ should handle unregistered events silently
    edge cases
      ✓ should handle empty world operations (1 ms)
      ✓ should handle removing entity with contents
      ✓ should handle circular references in toJSON

PASS tests/unit/world/spatial-index.test.ts (62.272 s)
  SpatialIndex
    basic operations
      ✓ should add child to parent (788 ms)
      ✓ should add multiple children to parent (1 ms)
      ✓ should remove child from parent (1 ms)
      ✓ should move child to new parent
      ✓ should handle non-existent parent (1 ms)
      ✓ should handle non-existent child
    remove operations
      ✓ should remove entity and its relationships
      ✓ should remove only specified child
      ✓ should handle removing non-existent child
      ✓ should clean up empty parent sets
    hasChildren
      ✓ should return true for parent with children
      ✓ should return false for parent without children
      ✓ should return false after removing all children
    getAllDescendants
      ✓ should get all descendants
      ✓ should respect max depth (1 ms)
      ✓ should handle entity with no descendants
      ✓ should handle circular references
      ✓ should collect all descendants up to max depth
    getAncestors
      ✓ should get all ancestors (1 ms)
      ✓ should get ancestors up to depth
      ✓ should handle entity with no ancestors
      ✓ should handle missing entity
    clear
      ✓ should clear all relationships
    persistence
      ✓ should serialize to JSON (1 ms)
      ✓ should load from JSON
      ✓ should handle empty JSON
      ✓ should clear before loading (1 ms)
    edge cases
      ✓ should handle adding same child multiple times
      ✓ should handle removing child from wrong parent
      ✓ should handle self-parenting
      ✓ should handle very deep hierarchies
      ✓ should maintain consistency when moving entities (1 ms)

Test Suites: 3 passed, 3 total
Tests:       123 passed, 123 total
Snapshots:   0 total
Time:        84.38 s

Subscribe to My So Called Interactive Fiction Life

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe