Throw JS errors

This commit is contained in:
Andras Schmelczer 2026-10-28 23:20:32 +00:00
commit 80c2ef142d
2 changed files with 29 additions and 8 deletions

View file

@ -254,6 +254,7 @@ export interface ReconcileApi {
* @param diff - The compact diff array (inserts as strings, deletes as negative integers, retained spans as positive integers)
* @param tokenizer - The tokenisation strategy, which is the same as used in `reconcile`.
* @returns The reconstructed changed text as a string.
* @throws {Error} If the diff contains a non-integer, or spans more characters than `original` has.
*/
undiff(
original: string,

View file

@ -91,6 +91,27 @@ describe.each(backends)('reconcile [$name]', ({ api }) => {
expect(withBigints.some((item) => typeof item === 'bigint')).toBe(true);
expect(undiff(original, withBigints)).toEqual(changed);
});
// A trap would also be an `Error` (`RuntimeError: unreachable`), so check the message
it.each([
{ invalidDiff: [10], message: 'Invalid diff' },
{ invalidDiff: [2 ** 32], message: 'Invalid diff' },
{ invalidDiff: [-(2 ** 40)], message: 'Invalid diff' },
{ invalidDiff: [2n ** 40n], message: 'Invalid diff' },
{ invalidDiff: [1.5], message: 'must be an integer' },
{ invalidDiff: [-0.4, 'X', 3], message: 'must be an integer' },
{ invalidDiff: [NaN], message: 'NaN' },
])('undiff rejects $invalidDiff with an Error', ({ invalidDiff, message }) => {
let thrown: unknown;
try {
undiff('abc', invalidDiff);
} catch (error) {
thrown = error;
}
expect(thrown).toBeInstanceOf(Error);
expect((thrown as Error).message).toContain(message);
});
});
describe.each(backends)('WASM cleanup after exceptions [$name]', ({ api, bindings }) => {