// @ts-check import * as blurhash from "../dist/blurhash-c-wasm.js"; describe("blurhash-c-wasm tests", () => { beforeAll(async () => { await blurhash.initWasm(); }); it("detects valid blurhash", () => { expect(blurhash.isValidBlurhash("LEHLk~WB2yk8pyo0adR*.7kCMdnj")).toBe(true); expect(blurhash.isValidBlurhash("LGF5]+Yk^6#M@-5c,1J5@[or[Q6.")).toBe(true); expect(blurhash.isValidBlurhash("L6PZfSjE.AyE_3t7t7R**0o#DgR4")).toBe(true); expect(blurhash.isValidBlurhash("LKO2:N%2Tw=w]~RBVZRi};RPxuwH")).toBe(true); }); it("generates valid blurhash images", () => { expect(blurhash.decode("LEHLk~WB2yk8pyo0adR*.7kCMdnj", 1, 1)).toEqual( Uint8ClampedArray.of(134, 164, 176, 255) ); expect(blurhash.decode("LGF5]+Yk^6#M@-5c,1J5@[or[Q6.", 1, 1)).toEqual( Uint8ClampedArray.of(176, 118, 163, 255) ); expect(blurhash.decode("L6PZfSjE.AyE_3t7t7R**0o#DgR4", 1, 1)).toEqual( Uint8ClampedArray.of(229, 228, 226, 255) ); expect(blurhash.decode("LKO2:N%2Tw=w]~RBVZRi};RPxuwH", 1, 1)).toEqual( Uint8ClampedArray.of(251, 192, 161, 255) ); }); it("detects invalid blurhash", () => { expect(blurhash.isValidBlurhash("1234")).toBe(false); expect(blurhash.isValidBlurhash("sus")).toBe(false); }); it("don't accept less then the required paramaters in the decode function", () => { // @ts-expect-error expect(() => blurhash.decode(undefined)).toThrow( new Error("You are required to give the Blurhash string.") ); // @ts-expect-error expect(() => blurhash.decode("test")).toThrow( new Error("You are required to give the width.") ); // @ts-expect-error expect(() => blurhash.decode("test", 32)).toThrow( new Error("You are required to give the height.") ); }); });