BareGit
const test = require("node:test");
const assert = require("node:assert/strict");

const preview_math = require("../static/foil/card_preview_math.js");

test("scaleDimensions preserves aspect ratio", function()
{
    assert.deepEqual(
        preview_math.scaleDimensions(5, 7, 256),
        {width: 183, height: 256});
    assert.deepEqual(
        preview_math.scaleDimensions(8, 4, 100),
        {width: 100, height: 50});
});

test("findAlphaBounds finds nontransparent pixels", function()
{
    const pixels = new Uint8Array(3 * 2 * 4);
    pixels[(0 * 3 + 1) * 4 + 3] = 255;
    pixels[(1 * 3 + 2) * 4 + 3] = 1;

    assert.deepEqual(
        preview_math.findAlphaBounds(pixels, 3, 2),
        {min_x: 1, min_y: 0, max_x: 2, max_y: 1});
    assert.equal(
        preview_math.findAlphaBounds(new Uint8Array(8), 2, 1),
        null);
});

test("cropWebGLPixels crops and flips rows", function()
{
    const pixels = new Uint8Array([
        1, 0, 0, 255, 2, 0, 0, 255,
        3, 0, 0, 255, 4, 0, 0, 255,
    ]);
    const cropped = preview_math.cropWebGLPixels(
        pixels,
        2,
        {min_x: 0, min_y: 0, max_x: 1, max_y: 1});

    assert.equal(cropped.width, 2);
    assert.equal(cropped.height, 2);
    assert.deepEqual(
        Array.from(cropped.data),
        [
            3, 0, 0, 255, 4, 0, 0, 255,
            1, 0, 0, 255, 2, 0, 0, 255,
        ]);
});