|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { resolve } from "node:path"; |
| 4 | + |
| 5 | +const probe = resolve(import.meta.dir, "helpers/probe-runtime.ts"); |
| 6 | + |
| 7 | +function runProbe(extraEnv: Record<string, string> = {}) { |
| 8 | + const r = spawnSync("bun", ["run", probe], { |
| 9 | + env: { ...process.env, ...extraEnv }, |
| 10 | + encoding: "utf8", |
| 11 | + }); |
| 12 | + if (r.status !== 0) { |
| 13 | + throw new Error( |
| 14 | + `probe exited with ${r.status}\nstdout: ${r.stdout}\nstderr: ${r.stderr}`, |
| 15 | + ); |
| 16 | + } |
| 17 | + const lines = r.stdout.trim().split("\n"); |
| 18 | + return JSON.parse(lines[lines.length - 1]); |
| 19 | +} |
| 20 | + |
| 21 | +describe("SIMD / non-SIMD initialization paths", () => { |
| 22 | + test("default path uses SIMD wasm and exposes working bridge", () => { |
| 23 | + const out = runProbe(); |
| 24 | + expect(out.simdActive).toBe(true); |
| 25 | + expect(out.encodedLen).toBeGreaterThan(0); |
| 26 | + expect(out.decodedTag).toBe("iq"); |
| 27 | + expect(out.decodedAttrsOk).toBe(true); |
| 28 | + expect(out.md5Hex).toBe("900150983cd24fb0d6963f7d28e17f72"); |
| 29 | + expect(out.hkdfLen).toBe(32); |
| 30 | + }); |
| 31 | + |
| 32 | + test("forced non-SIMD path falls back to nosimd wasm with identical output", () => { |
| 33 | + const out = runProbe({ WHATSAPP_RUST_BRIDGE_FORCE_NOSIMD: "1" }); |
| 34 | + expect(out.simdActive).toBe(false); |
| 35 | + expect(out.encodedLen).toBeGreaterThan(0); |
| 36 | + expect(out.decodedTag).toBe("iq"); |
| 37 | + expect(out.decodedAttrsOk).toBe(true); |
| 38 | + expect(out.md5Hex).toBe("900150983cd24fb0d6963f7d28e17f72"); |
| 39 | + expect(out.hkdfLen).toBe(32); |
| 40 | + }); |
| 41 | + |
| 42 | + test("SIMD and non-SIMD paths produce byte-identical encode output", () => { |
| 43 | + const probeBytes = resolve(import.meta.dir, "helpers/probe-encode-bytes.ts"); |
| 44 | + const simd = spawnSync("bun", ["run", probeBytes], { |
| 45 | + env: { ...process.env }, |
| 46 | + encoding: "utf8", |
| 47 | + }); |
| 48 | + const noSimd = spawnSync("bun", ["run", probeBytes], { |
| 49 | + env: { ...process.env, WHATSAPP_RUST_BRIDGE_FORCE_NOSIMD: "1" }, |
| 50 | + encoding: "utf8", |
| 51 | + }); |
| 52 | + expect(simd.status).toBe(0); |
| 53 | + expect(noSimd.status).toBe(0); |
| 54 | + |
| 55 | + const simdLines = simd.stdout.trim().split("\n"); |
| 56 | + const noSimdLines = noSimd.stdout.trim().split("\n"); |
| 57 | + const simdOut = JSON.parse(simdLines[simdLines.length - 1]); |
| 58 | + const noSimdOut = JSON.parse(noSimdLines[noSimdLines.length - 1]); |
| 59 | + expect(simdOut.simd).toBe(true); |
| 60 | + expect(noSimdOut.simd).toBe(false); |
| 61 | + // Byte-identical wire output and structurally identical decoded form. |
| 62 | + expect(simdOut.encHex).toBe(noSimdOut.encHex); |
| 63 | + expect(simdOut.decoded).toEqual(noSimdOut.decoded); |
| 64 | + }); |
| 65 | +}); |
0 commit comments