core.test.js
1.5 kB · javascript · 35 lines
1import { expect, test } from "bun:test";2import * as core from "./core.js";34const bytes = await Bun.file(new URL("./pkg/core/mrlyjs_core_bg.wasm", import.meta.url)).arrayBuffer();5core.initSync({ module: bytes });6const rows = await Bun.file(new URL("../mrlyrs/fixtures/core.json", import.meta.url)).json();7const row = (fn) => rows.find((r) => r.fn === fn);8const sha256 = (bytes) => new Bun.CryptoHasher("sha256").update(bytes).digest("hex");910test("core::Tensor::rot90", () => {11 const r = row("core::Tensor::rot90");12 const turned = core.tensor.rot90({ shape: r.in.shape, data: Uint8Array.from(r.in.data) }, r.in.k, r.in.axes);13 expect(turned.shape).toEqual(r.out.shape);14 expect(Array.from(turned.data)).toEqual(r.out.data);15});1617test("core::Color::from_hex", () => {18 const r = row("core::Color::from_hex");19 const color = core.colors.from_hex(r.in.hex);20 expect(color).toEqual(r.out.rgba);21 expect(core.colors.to_hex(color)).toBe(r.out.hex);22});2324test("core::png", () => {25 const r = row("core::png");26 expect(sha256(core.codec.png(r.in.colors, r.in.width, r.in.height, r.in.scale))).toBe(r.out);27});2829test("core::Colorizer::color", () => {30 const r = row("core::Colorizer::color");31 const ramp = r.in.ramp.map(core.colors.from_hex);32 const colorizer = core.Colorizer.gradient_bins(core.colors.from_hex(r.in.background), ramp, r.in.shades);33 const hexes = r.in.values.map((value) => core.colors.to_hex(core.ramp.color(colorizer, value, r.in.max)));34 expect(hexes).toEqual(r.out);35});