blurhash-c-wasm/spec/librarySpec.js

106 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

2023-08-15 21:30:13 +00:00
// @ts-check
import * as blurhash from "../dist/blurhash-c-wasm.js";
describe("blurhash-c-wasm tests", () => {
/** @type {import("../dist/blurhash-c-wasm").BlurhashInstanceType} */
let instance;
2023-08-15 21:30:13 +00:00
beforeAll(async () => {
instance =
/** @type {import("../dist/blurhash-c-wasm").BlurhashInstanceType} */ (
/** @type {unknown} */ await blurhash.initWasm()
);
2023-08-15 21:30:13 +00:00
});
it("detects valid blurhash", () => {
expect(
blurhash.isValidBlurhash(instance, "LEHLk~WB2yk8pyo0adR*.7kCMdnj")
).toBe(true);
expect(
blurhash.isValidBlurhash(instance, "LGF5]+Yk^6#M@-5c,1J5@[or[Q6.")
).toBe(true);
expect(
blurhash.isValidBlurhash(instance, "L6PZfSjE.AyE_3t7t7R**0o#DgR4")
).toBe(true);
expect(
blurhash.isValidBlurhash(instance, "LKO2:N%2Tw=w]~RBVZRi};RPxuwH")
).toBe(true);
2023-08-15 21:30:13 +00:00
});
it("generates valid 1:1 blurhash images", () => {
expect(
blurhash.decode(instance, "LEHLk~WB2yk8pyo0adR*.7kCMdnj", 1, 1)
).toEqual(Uint8ClampedArray.of(134, 164, 176, 255));
expect(
blurhash.decode(instance, "LGF5]+Yk^6#M@-5c,1J5@[or[Q6.", 1, 1)
).toEqual(Uint8ClampedArray.of(176, 118, 163, 255));
expect(
blurhash.decode(instance, "L6PZfSjE.AyE_3t7t7R**0o#DgR4", 1, 1)
).toEqual(Uint8ClampedArray.of(229, 228, 226, 255));
expect(
blurhash.decode(instance, "LKO2:N%2Tw=w]~RBVZRi};RPxuwH", 1, 1)
).toEqual(Uint8ClampedArray.of(251, 192, 161, 255));
});
it("generates valid non 1:1 blurhash images", () => {
expect(
blurhash.decode(instance, "LEHLk~WB2yk8pyo0adR*.7kCMdnj", 1, 2)
).toEqual(Uint8ClampedArray.of(134, 164, 176, 255, 119, 148, 161, 255));
expect(
blurhash.decode(instance, "LGF5]+Yk^6#M@-5c,1J5@[or[Q6.", 1, 2)
).toEqual(Uint8ClampedArray.of(176, 118, 163, 255, 137, 155, 177, 255));
expect(
blurhash.decode(instance, "L6PZfSjE.AyE_3t7t7R**0o#DgR4", 1, 2)
).toEqual(Uint8ClampedArray.of(229, 228, 226, 255, 226, 223, 223, 255));
expect(
blurhash.decode(instance, "LKO2:N%2Tw=w]~RBVZRi};RPxuwH", 1, 2)
).toEqual(Uint8ClampedArray.of(251, 192, 161, 255, 200, 187, 180, 255));
});
it("detects invalid blurhash", () => {
expect(blurhash.isValidBlurhash(instance, "1234")).toBe(false);
expect(blurhash.isValidBlurhash(instance, "sus")).toBe(false);
});
it("fails on invalid blurhash", () => {
const decodingHasFailed = "Decoding the Blurhash string has failed.";
expect(() => blurhash.decode(instance, "1234", 1, 1)).toThrow(
new Error(decodingHasFailed)
2023-08-15 21:30:13 +00:00
);
expect(() => blurhash.decode(instance, "sus", 1, 1)).toThrow(
new Error(decodingHasFailed)
2023-08-15 21:30:13 +00:00
);
});
it("don't accept less then the required parameters in the isValidBlurhash function", () => {
// @ts-expect-error
expect(() => blurhash.isValidBlurhash(undefined)).toThrow(
new Error("You are required to give the WASM Module.")
);
// @ts-expect-error
expect(() => blurhash.isValidBlurhash(instance, undefined)).toThrow(
new Error("You are required to give the Blurhash string.")
);
2023-08-15 21:30:13 +00:00
});
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 WASM Module.")
);
// @ts-expect-error
expect(() => blurhash.decode(instance, undefined)).toThrow(
2023-08-15 21:30:13 +00:00
new Error("You are required to give the Blurhash string.")
);
// @ts-expect-error
expect(() => blurhash.decode(instance, "test")).toThrow(
2023-08-15 21:30:13 +00:00
new Error("You are required to give the width.")
);
// @ts-expect-error
expect(() => blurhash.decode(instance, "test", 32)).toThrow(
2023-08-15 21:30:13 +00:00
new Error("You are required to give the height.")
);
});
});