Poindexter/npm/poindexter-wasm/smoke.mjs

41 lines
1.3 KiB
JavaScript
Raw Normal View History

// Minimal Node smoke test for the WASM loader.
// Assumes npm-pack has prepared npm/poindexter-wasm with loader and dist assets.
import { init } from './loader.js';
(async function () {
feat(wasm): Improve WASM error handling and loader This commit introduces a comprehensive set of improvements to the error handling and loading mechanism of the WebAssembly (WASM) module. The key changes include: - **Structured Error Handling:** Replaced generic string-based errors with a structured `WasmError` type in the Go WASM wrapper. This provides standardized error codes (`bad_request`, `not_found`, `conflict`) and clear messages, allowing JavaScript clients to handle errors programmatically. - **Isomorphic WASM Loader:** Refactored the JavaScript loader (`loader.js`) to be isomorphic, enabling it to run seamlessly in both browser and Node.js environments. The loader now detects the environment and uses the appropriate mechanism for loading the WASM binary and `wasm_exec.js`. - **Type Conversion Fix:** Resolved a panic (`panic: ValueOf: invalid value`) that occurred when returning `[]float64` slices from Go to JavaScript. A new `pointToJS` helper function now correctly converts these slices to `[]any`, ensuring proper data marshalling. - **Improved Smoke Test:** Enhanced the WASM smoke test (`smoke.mjs`) to verify the new structured error handling and to correctly handle the API's response format. - **Configuration Updates:** Updated the `.golangci.yml` configuration to be compatible with the latest version of `golangci-lint`. In addition to these changes, this commit also includes a new `AUDIT-ERROR-HANDLING.md` file, which documents the findings of a thorough audit of the project's error handling and logging practices. Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
2026-02-02 01:27:55 +00:00
let px;
try {
feat(wasm): Improve WASM error handling and loader This commit introduces a comprehensive set of improvements to the error handling and loading mechanism of the WebAssembly (WASM) module. The key changes include: - **Structured Error Handling:** Replaced generic string-based errors with a structured `WasmError` type in the Go WASM wrapper. This provides standardized error codes (`bad_request`, `not_found`, `conflict`) and clear messages, allowing JavaScript clients to handle errors programmatically. - **Isomorphic WASM Loader:** Refactored the JavaScript loader (`loader.js`) to be isomorphic, enabling it to run seamlessly in both browser and Node.js environments. The loader now detects the environment and uses the appropriate mechanism for loading the WASM binary and `wasm_exec.js`. - **Type Conversion Fix:** Resolved a panic (`panic: ValueOf: invalid value`) that occurred when returning `[]float64` slices from Go to JavaScript. A new `pointToJS` helper function now correctly converts these slices to `[]any`, ensuring proper data marshalling. - **Improved Smoke Test:** Enhanced the WASM smoke test (`smoke.mjs`) to verify the new structured error handling and to correctly handle the API's response format. - **Configuration Updates:** Updated the `.golangci.yml` configuration to be compatible with the latest version of `golangci-lint`. In addition to these changes, this commit also includes a new `AUDIT-ERROR-HANDLING.md` file, which documents the findings of a thorough audit of the project's error handling and logging practices. Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
2026-02-02 01:27:55 +00:00
px = await init({
wasmURL: new URL('./dist/poindexter.wasm', import.meta.url).toString(),
wasmExecURL: new URL('./dist/wasm_exec.js', import.meta.url).toString(),
});
const ver = await px.version();
if (!ver || typeof ver !== 'string') throw new Error('version not string');
const tree = await px.newTree(2);
await tree.insert({ id: 'a', coords: [0, 0], value: 'A' });
await tree.insert({ id: 'b', coords: [1, 0], value: 'B' });
const nn = await tree.nearest([0.9, 0.1]);
feat(wasm): Improve WASM error handling and loader This commit introduces a comprehensive set of improvements to the error handling and loading mechanism of the WebAssembly (WASM) module. The key changes include: - **Structured Error Handling:** Replaced generic string-based errors with a structured `WasmError` type in the Go WASM wrapper. This provides standardized error codes (`bad_request`, `not_found`, `conflict`) and clear messages, allowing JavaScript clients to handle errors programmatically. - **Isomorphic WASM Loader:** Refactored the JavaScript loader (`loader.js`) to be isomorphic, enabling it to run seamlessly in both browser and Node.js environments. The loader now detects the environment and uses the appropriate mechanism for loading the WASM binary and `wasm_exec.js`. - **Type Conversion Fix:** Resolved a panic (`panic: ValueOf: invalid value`) that occurred when returning `[]float64` slices from Go to JavaScript. A new `pointToJS` helper function now correctly converts these slices to `[]any`, ensuring proper data marshalling. - **Improved Smoke Test:** Enhanced the WASM smoke test (`smoke.mjs`) to verify the new structured error handling and to correctly handle the API's response format. - **Configuration Updates:** Updated the `.golangci.yml` configuration to be compatible with the latest version of `golangci-lint`. In addition to these changes, this commit also includes a new `AUDIT-ERROR-HANDLING.md` file, which documents the findings of a thorough audit of the project's error handling and logging practices. Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
2026-02-02 01:27:55 +00:00
if (!nn || !nn.point || !nn.point.id) throw new Error('nearest failed');
console.log('WASM smoke ok:', ver, 'nearest.id=', nn.point.id);
} catch (err) {
console.error('WASM smoke failed:', err);
process.exit(1);
}
feat(wasm): Improve WASM error handling and loader This commit introduces a comprehensive set of improvements to the error handling and loading mechanism of the WebAssembly (WASM) module. The key changes include: - **Structured Error Handling:** Replaced generic string-based errors with a structured `WasmError` type in the Go WASM wrapper. This provides standardized error codes (`bad_request`, `not_found`, `conflict`) and clear messages, allowing JavaScript clients to handle errors programmatically. - **Isomorphic WASM Loader:** Refactored the JavaScript loader (`loader.js`) to be isomorphic, enabling it to run seamlessly in both browser and Node.js environments. The loader now detects the environment and uses the appropriate mechanism for loading the WASM binary and `wasm_exec.js`. - **Type Conversion Fix:** Resolved a panic (`panic: ValueOf: invalid value`) that occurred when returning `[]float64` slices from Go to JavaScript. A new `pointToJS` helper function now correctly converts these slices to `[]any`, ensuring proper data marshalling. - **Improved Smoke Test:** Enhanced the WASM smoke test (`smoke.mjs`) to verify the new structured error handling and to correctly handle the API's response format. - **Configuration Updates:** Updated the `.golangci.yml` configuration to be compatible with the latest version of `golangci-lint`. In addition to these changes, this commit also includes a new `AUDIT-ERROR-HANDLING.md` file, which documents the findings of a thorough audit of the project's error handling and logging practices. Co-authored-by: Snider <631881+Snider@users.noreply.github.com>
2026-02-02 01:27:55 +00:00
// Test error handling
try {
await px.newTree(0);
console.error('Expected error from newTree(0) but got none');
process.exit(1);
} catch (err) {
if (err.code === 'bad_request' && err.message.includes('dimension')) {
console.log('WASM error handling ok:', err.code);
} else {
console.error('WASM smoke failed: unexpected error format:', err);
process.exit(1);
}
}
})();