wasm-hit-test.test.mjs 1.8 KB

12345678910111213141516171819202122232425262728293031323334
  1. import test from "node:test";
  2. import assert from "node:assert/strict";
  3. import {readFile} from "node:fs/promises";
  4. import {BoardWasmAccelerator} from "../../tools/board-viewer/web/wasm-engine.mjs";
  5. test("wide body edges remain pickable beyond the WASM center grid", async () => {
  6. const accelerator = new BoardWasmAccelerator();
  7. const bytes = await readFile(new URL("../../tools/board-viewer/web/board-accel.wasm", import.meta.url));
  8. assert.equal(await accelerator.init(bytes), true);
  9. const connector = {
  10. reference: "CON_WIDE", category: "CON", side: "both",
  11. view_bounds: {x: 165, y: 25, width: 70, height: 10}, pins: [],
  12. };
  13. const data = {board: {view_box: {width: 430, height: 75}}, touchpoints: [connector], resistors: []};
  14. accelerator.buildIndex(data);
  15. // This exact shipped binary omits an interior edge outside its nine cells.
  16. assert.equal(accelerator.wasm.wasm_hit_test(231, 30, 0, 1, 0, 0.35), -1);
  17. const pick = (x, y, side = "front", categories = new Set(["CON"]), shield = false) =>
  18. accelerator.hitTest(x, y, side, categories, shield, 1);
  19. assert.equal(pick(200, 30)?.touchpoint, connector);
  20. assert.equal(pick(231, 30)?.touchpoint, connector);
  21. assert.equal(pick(199, 30, "back")?.touchpoint, connector);
  22. assert.equal(pick(237, 30), null);
  23. assert.equal(pick(231, 30, "front", new Set()), null);
  24. connector.pins.push({number: "1", view_position: {x: 231, y: 30}, size: {width: 1.4, height: 1.4}, shape: "circle", pad_role: "contact"});
  25. accelerator.buildIndex(data);
  26. assert.equal(pick(231, 30)?.pin, connector.pins[0]);
  27. connector.pins[0].number = "SH1";
  28. accelerator.buildIndex(data);
  29. assert.equal(pick(231, 30)?.kind, "touchpoint");
  30. assert.equal(pick(231, 30, "front", new Set(["CON"]), true)?.kind, "pin");
  31. accelerator.ready = false;
  32. assert.equal(pick(231, 30)?.touchpoint, connector);
  33. });