Skip to content

Commit e4d0093

Browse files
committed
Fix WebGPU async constructor, add entity buffer tests
1 parent e13f8c8 commit e4d0093

3 files changed

Lines changed: 73 additions & 3 deletions

File tree

src/simulation/tests.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,3 +381,74 @@ fn test_drift_direction_analysis() {
381381
}
382382
}
383383
}
384+
385+
#[test]
386+
fn test_entity_data_format() {
387+
let sim = Simulation::new(100.0);
388+
let entities = sim.get_entities();
389+
390+
// Each entity should have 6 components: x, y, radius, r, g, b
391+
for (x, y, radius, r, g, b) in &entities {
392+
// Position should be within world bounds
393+
assert!(*x >= -50.0 && *x <= 50.0, "x={} out of bounds", x);
394+
assert!(*y >= -50.0 && *y <= 50.0, "y={} out of bounds", y);
395+
396+
// Radius should be positive
397+
assert!(*radius > 0.0, "radius should be positive");
398+
399+
// Colors should be in 0-1 range
400+
assert!(*r >= 0.0 && *r <= 1.0, "r={} out of color range", r);
401+
assert!(*g >= 0.0 && *g <= 1.0, "g={} out of color range", g);
402+
assert!(*b >= 0.0 && *b <= 1.0, "b={} out of color range", b);
403+
}
404+
}
405+
406+
#[test]
407+
fn test_entity_buffer_conversion() {
408+
// Test the buffer format used by WebGPU renderer
409+
// Simulates what update_entity_buffer does in lib.rs
410+
let sim = Simulation::new(100.0);
411+
let entities = sim.get_entities();
412+
413+
// Convert to flat buffer (same as update_entity_buffer)
414+
let mut buffer: Vec<f32> = Vec::with_capacity(entities.len() * 6);
415+
for (x, y, radius, r, g, b) in entities.iter() {
416+
buffer.push(*x);
417+
buffer.push(*y);
418+
buffer.push(*radius);
419+
buffer.push(*r);
420+
buffer.push(*g);
421+
buffer.push(*b);
422+
}
423+
424+
// Buffer length should be 6 * entity count
425+
assert_eq!(buffer.len(), entities.len() * 6);
426+
427+
// Entity count calculation should match
428+
let entity_count = buffer.len() / 6;
429+
assert_eq!(entity_count, entities.len());
430+
431+
// Verify data integrity by reading back
432+
for (i, (x, y, radius, r, g, b)) in entities.iter().enumerate() {
433+
let base = i * 6;
434+
assert_eq!(buffer[base], *x);
435+
assert_eq!(buffer[base + 1], *y);
436+
assert_eq!(buffer[base + 2], *radius);
437+
assert_eq!(buffer[base + 3], *r);
438+
assert_eq!(buffer[base + 4], *g);
439+
assert_eq!(buffer[base + 5], *b);
440+
}
441+
}
442+
443+
#[test]
444+
fn test_update_config() {
445+
let mut sim = Simulation::new(100.0);
446+
let original_velocity = sim.config.physics.max_velocity;
447+
448+
let mut new_config = sim.config.clone();
449+
new_config.physics.max_velocity = 5.0;
450+
sim.update_config(new_config);
451+
452+
assert_ne!(sim.config.physics.max_velocity, original_velocity);
453+
assert_eq!(sim.config.physics.max_velocity, 5.0);
454+
}

src/web/webgpu.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ pub struct WebGpuRenderer {
2626

2727
#[wasm_bindgen]
2828
impl WebGpuRenderer {
29-
#[wasm_bindgen(constructor)]
30-
pub async fn new(canvas_id: &str) -> Result<WebGpuRenderer, JsValue> {
29+
pub async fn create(canvas_id: &str) -> Result<WebGpuRenderer, JsValue> {
3130
let window = web_sys::window().ok_or("No window")?;
3231
let document = window.document().ok_or("No document")?;
3332
let canvas = document

web/js/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class EvolutionApp {
7878
try {
7979
if (navigator.gpu) {
8080
console.log("WebGPU available, initializing WebGPU renderer...");
81-
this.renderer = await WebGpuRenderer.new("simulation-canvas");
81+
this.renderer = await WebGpuRenderer.create("simulation-canvas");
8282
this.useWebGPU = true;
8383
console.log("WebGPU renderer initialized successfully!");
8484
} else {

0 commit comments

Comments
 (0)