From 1bc6117045685433ead56f7a7b7f9b58f54772c5 Mon Sep 17 00:00:00 2001 From: Andras Schmelczer Date: Fri, 4 Jul 2025 02:23:32 +0100 Subject: [PATCH] Add JS tests --- reconcile-js/src/index.test.ts | 70 ++++++++++++++++++++++++++++++++++ reconcile-js/src/index.ts | 11 ++++++ scripts/test.sh | 12 ++++-- 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 reconcile-js/src/index.test.ts diff --git a/reconcile-js/src/index.test.ts b/reconcile-js/src/index.test.ts new file mode 100644 index 0000000..9fac9d0 --- /dev/null +++ b/reconcile-js/src/index.test.ts @@ -0,0 +1,70 @@ +import { init, reconcile, reconcileWithHistory } from "./index"; +import * as fs from "fs"; + +describe("reconcile", () => { + it("tries calling functions without init", () => { + expect(() => reconcile("Hello", "Hello world", "Hi world")).toThrow( + /call init()/ + ); + + expect(() => + reconcileWithHistory("Hello", "Hello world", "Hi world") + ).toThrow(/call init()/); + }); + + it("call reconcile without cursors", async () => { + await initWasm(); + + expect(reconcile("Hello", "Hello world", "Hi world").text).toEqual( + "Hi world" + ); + }); + + it("call reconcile with cursors", async () => { + await initWasm(); + + const result = reconcile( + "Hello", + { + text: "Hello world", + cursors: [ + { + id: 3, + position: 2, + }, + ], + }, + { + text: "Hi world", + cursors: [ + { + id: 4, + position: 0, + }, + { id: 5, position: 3 }, + ], + } + ); + + expect(result.text).toEqual("Hi world"); + expect(result.cursors).toEqual([ + { id: 3, position: 0 }, + { id: 4, position: 0 }, + { id: 5, position: 3 }, + ]); + }); + + it("call reconcileWithHistory", async () => { + await initWasm(); + + const result = reconcileWithHistory("Hello", "Hello world", "Hi world"); + + expect(result.text).toEqual("Hi world"); + expect(result.history.length).toBeGreaterThan(0); + }); +}); + +async function initWasm() { + const wasmBin = fs.readFileSync("../pkg/reconcile_bg.wasm"); + await init({ module_or_path: wasmBin }); +} diff --git a/reconcile-js/src/index.ts b/reconcile-js/src/index.ts index 8419be2..e9c769b 100644 --- a/reconcile-js/src/index.ts +++ b/reconcile-js/src/index.ts @@ -49,6 +49,9 @@ export type Tokenizer = "word" | "character"; let isInitialised = false; +const UNINITIALISED_MODULE_ERROR = + "Reconcile module has not been initialized. Please call init() before using any other functions."; + /** * Initializes the WASM module for text reconciliation. * Must be called before using any other functions. @@ -83,6 +86,10 @@ export function reconcile( right: string | TextWithCursors, tokenizer: BuiltinTokenizer = "Word" ): TextWithCursors { + if (!isInitialised) { + throw new Error(UNINITIALISED_MODULE_ERROR); + } + const leftCursor = toWasmTextWithCursors(left); const rightCursor = toWasmTextWithCursors(right); @@ -114,6 +121,10 @@ export function reconcileWithHistory( right: string | TextWithCursors, tokenizer: BuiltinTokenizer = "Word" ): TextWithCursorsAndHistory { + if (!isInitialised) { + throw new Error(UNINITIALISED_MODULE_ERROR); + } + const leftCursor = toWasmTextWithCursors(left); const rightCursor = toWasmTextWithCursors(right); diff --git a/scripts/test.sh b/scripts/test.sh index 236717c..9c556df 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -2,10 +2,14 @@ set -e -wasm-pack build --target web --features wasm +wasm-pack build --target web --features wasm,wee_alloc cargo test --verbose cargo test --features serde -cargo test --features wasm -wasm-pack test --node --features wasm +cargo test --features wasm,wee_alloc +wasm-pack test --node --features wasm,wee_alloc -echo "Success!" \ No newline at end of file +cd reconcile-js +npm run test +cd - + +echo "Success!"