- Add spatial search module for finding users/objects by name - Add js/spatial-search.js with search functionality - Add tests (9 tests, all passing) - Add script to index.html Features: 1. Search by name with autocomplete 2. Distance calculation 3. Direction indicator (N/S/E/W/NE/SE/SW/NW) 4. Pathfinding arrow on HUD 5. Keyboard shortcut (Ctrl+F / Cmd+F) Addresses issue #1540: feat: spatial search — find nearest user/object by name Usage: - Open search: Ctrl+F or Cmd+F - Type name to search - Select result to see direction arrow - Arrow points to selected entity Tested: - Entity registration/unregistration - Position updates - Distance calculation - Direction calculation - Search functionality
223 lines
7.0 KiB
JavaScript
223 lines
7.0 KiB
JavaScript
/**
|
|
* Tests for Spatial Search
|
|
* Issue #1540: feat: spatial search — find nearest user/object by name
|
|
*/
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const ROOT = path.resolve(__dirname, '..');
|
|
|
|
// Mock document
|
|
const mockDocument = {
|
|
createElement: (tag) => {
|
|
const element = {
|
|
style: {},
|
|
innerHTML: '',
|
|
textContent: '',
|
|
placeholder: '',
|
|
title: '',
|
|
addEventListener: () => {},
|
|
appendChild: () => {},
|
|
querySelector: () => null,
|
|
querySelectorAll: () => [],
|
|
focus: () => {}
|
|
};
|
|
return element;
|
|
},
|
|
body: {
|
|
appendChild: () => {}
|
|
},
|
|
getElementById: () => null,
|
|
addEventListener: () => {}
|
|
};
|
|
|
|
// Mock console
|
|
const mockConsole = {
|
|
log: () => {},
|
|
warn: () => {},
|
|
error: () => {}
|
|
};
|
|
|
|
// Load spatial-search.js
|
|
const spatialSearchPath = path.join(ROOT, 'js', 'spatial-search.js');
|
|
const spatialSearchCode = fs.readFileSync(spatialSearchPath, 'utf8');
|
|
|
|
// Create VM context
|
|
const context = {
|
|
module: { exports: {} },
|
|
exports: {},
|
|
console: mockConsole,
|
|
document: mockDocument,
|
|
window: {
|
|
addEventListener: () => {},
|
|
location: { protocol: 'http:', hostname: 'localhost' }
|
|
},
|
|
Math: Math,
|
|
setTimeout: () => {},
|
|
clearTimeout: () => {}
|
|
};
|
|
|
|
// Execute spatial-search.js in context
|
|
const vm = require('node:vm');
|
|
vm.runInNewContext(spatialSearchCode, context);
|
|
|
|
// Get SpatialSearch class
|
|
const SpatialSearch = context.window.SpatialSearch || context.module.exports;
|
|
|
|
test('SpatialSearch loads correctly', () => {
|
|
assert.ok(SpatialSearch, 'SpatialSearch should be defined');
|
|
assert.ok(typeof SpatialSearch === 'function', 'SpatialSearch should be a constructor');
|
|
});
|
|
|
|
test('SpatialSearch can be instantiated', () => {
|
|
const search = new SpatialSearch();
|
|
assert.ok(search, 'SpatialSearch instance should be created');
|
|
assert.equal(search.maxDistance, 1000, 'Should have default max distance');
|
|
assert.equal(search.searchDelay, 300, 'Should have default search delay');
|
|
assert.ok(search.entities, 'Should have entities Map');
|
|
});
|
|
|
|
test('SpatialSearch can register entities', () => {
|
|
const search = new SpatialSearch();
|
|
|
|
search.registerEntity('user1', {
|
|
name: 'Alice',
|
|
type: 'user',
|
|
position: { x: 10, y: 0, z: 5 }
|
|
});
|
|
|
|
search.registerEntity('user2', {
|
|
name: 'Bob',
|
|
type: 'user',
|
|
position: { x: 20, y: 0, z: 10 }
|
|
});
|
|
|
|
assert.equal(search.entities.size, 2, 'Should have 2 entities');
|
|
assert.ok(search.entities.get('user1'), 'Should have user1');
|
|
assert.ok(search.entities.get('user2'), 'Should have user2');
|
|
});
|
|
|
|
test('SpatialSearch can unregister entities', () => {
|
|
const search = new SpatialSearch();
|
|
|
|
search.registerEntity('user1', { name: 'Alice', type: 'user' });
|
|
search.registerEntity('user2', { name: 'Bob', type: 'user' });
|
|
|
|
assert.equal(search.entities.size, 2, 'Should have 2 entities');
|
|
|
|
search.unregisterEntity('user1');
|
|
assert.equal(search.entities.size, 1, 'Should have 1 entity after unregister');
|
|
assert.ok(!search.entities.get('user1'), 'Should not have user1');
|
|
});
|
|
|
|
test('SpatialSearch can update entity position', () => {
|
|
const search = new SpatialSearch();
|
|
|
|
search.registerEntity('user1', {
|
|
name: 'Alice',
|
|
type: 'user',
|
|
position: { x: 10, y: 0, z: 5 }
|
|
});
|
|
|
|
const newPos = { x: 15, y: 0, z: 10 };
|
|
search.updateEntityPosition('user1', newPos);
|
|
|
|
const entity = search.entities.get('user1');
|
|
assert.deepEqual(entity.position, newPos, 'Should update position');
|
|
});
|
|
|
|
test('SpatialSearch calculates distance correctly', () => {
|
|
const search = new SpatialSearch();
|
|
|
|
// Mock getLocalPlayerPosition
|
|
search.getLocalPlayerPosition = () => ({ x: 0, y: 0, z: 0 });
|
|
|
|
const pos1 = { x: 3, y: 0, z: 4 };
|
|
const distance1 = search.calculateDistance(pos1);
|
|
assert.equal(distance1, 5, 'Should calculate 3-4-5 triangle correctly');
|
|
|
|
const pos2 = { x: 0, y: 0, z: 0 };
|
|
const distance2 = search.calculateDistance(pos2);
|
|
assert.equal(distance2, 0, 'Should be 0 at same position');
|
|
});
|
|
|
|
test('SpatialSearch calculates direction correctly', () => {
|
|
const search = new SpatialSearch();
|
|
|
|
// Mock getLocalPlayerPosition
|
|
search.getLocalPlayerPosition = () => ({ x: 0, y: 0, z: 0 });
|
|
|
|
// Test different directions
|
|
assert.equal(search.calculateDirection({ x: 10, y: 0, z: 0 }), 'E', 'Should be East');
|
|
assert.equal(search.calculateDirection({ x: 0, y: 0, z: 10 }), 'S', 'Should be South');
|
|
assert.equal(search.calculateDirection({ x: -10, y: 0, z: 0 }), 'W', 'Should be West');
|
|
assert.equal(search.calculateDirection({ x: 0, y: 0, z: -10 }), 'N', 'Should be North');
|
|
});
|
|
|
|
test('SpatialSearch searches entities correctly', () => {
|
|
const search = new SpatialSearch();
|
|
|
|
// Mock getLocalPlayerPosition
|
|
search.getLocalPlayerPosition = () => ({ x: 0, y: 0, z: 0 });
|
|
|
|
// Register entities
|
|
search.registerEntity('user1', {
|
|
name: 'Alice',
|
|
type: 'user',
|
|
position: { x: 10, y: 0, z: 0 }
|
|
});
|
|
|
|
search.registerEntity('user2', {
|
|
name: 'Bob',
|
|
type: 'user',
|
|
position: { x: 20, y: 0, z: 0 }
|
|
});
|
|
|
|
search.registerEntity('obj1', {
|
|
name: 'Apple',
|
|
type: 'object',
|
|
position: { x: 5, y: 0, z: 0 }
|
|
});
|
|
|
|
// Search for 'ali'
|
|
const results1 = search.searchEntities('ali');
|
|
assert.equal(results1.length, 1, 'Should find 1 result for "ali"');
|
|
assert.equal(results1[0].name, 'Alice', 'Should find Alice');
|
|
|
|
// Search for 'bob'
|
|
const results2 = search.searchEntities('bob');
|
|
assert.equal(results2.length, 1, 'Should find 1 result for "bob"');
|
|
assert.equal(results2[0].name, 'Bob', 'Should find Bob');
|
|
|
|
// Search for 'user' (type)
|
|
const results3 = search.searchEntities('user');
|
|
assert.equal(results3.length, 2, 'Should find 2 results for "user"');
|
|
|
|
// Search for 'apple'
|
|
const results4 = search.searchEntities('apple');
|
|
assert.equal(results4.length, 1, 'Should find 1 result for "apple"');
|
|
assert.equal(results4[0].name, 'Apple', 'Should find Apple');
|
|
|
|
// Search for non-existent
|
|
const results5 = search.searchEntities('xyz');
|
|
assert.equal(results5.length, 0, 'Should find 0 results for "xyz"');
|
|
});
|
|
|
|
test('SpatialSearch gets status', () => {
|
|
const search = new SpatialSearch();
|
|
|
|
search.registerEntity('user1', { name: 'Alice', type: 'user' });
|
|
search.registerEntity('user2', { name: 'Bob', type: 'user' });
|
|
|
|
const status = search.getStatus();
|
|
|
|
assert.ok(status, 'Should return status object');
|
|
assert.equal(status.entityCount, 2, 'Should have 2 entities');
|
|
assert.equal(status.maxDistance, 1000, 'Should have correct max distance');
|
|
assert.equal(status.searchDelay, 300, 'Should have correct search delay');
|
|
});
|
|
|
|
console.log('All SpatialSearch tests passed!'); |