mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
merge: resolve conflict with main branch in README.md
This commit is contained in:
@@ -69,11 +69,38 @@ describe("needsCliDecode", () => {
|
||||
it("returns false for unknown format", () => {
|
||||
expect(needsCliDecode("xyz")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
// ==========================================================================
|
||||
// decodeToSharpCompat — routing logic (default passthrough)
|
||||
// ==========================================================================
|
||||
it("returns true for jp2 format", () => {
|
||||
expect(needsCliDecode("jp2")).toBe(true);
|
||||
});
|
||||
it("returns true for eps format", () => {
|
||||
expect(needsCliDecode("eps")).toBe(true);
|
||||
});
|
||||
it("returns true for dds format", () => {
|
||||
expect(needsCliDecode("dds")).toBe(true);
|
||||
});
|
||||
it("returns true for cur format", () => {
|
||||
expect(needsCliDecode("cur")).toBe(true);
|
||||
});
|
||||
it("returns true for dpx format", () => {
|
||||
expect(needsCliDecode("dpx")).toBe(true);
|
||||
});
|
||||
it("returns true for fits format", () => {
|
||||
expect(needsCliDecode("fits")).toBe(true);
|
||||
});
|
||||
it("returns true for qoi format", () => {
|
||||
expect(needsCliDecode("qoi")).toBe(true);
|
||||
});
|
||||
it("returns true for ppm format", () => {
|
||||
expect(needsCliDecode("ppm")).toBe(true);
|
||||
});
|
||||
it("returns true for pgm format", () => {
|
||||
expect(needsCliDecode("pgm")).toBe(true);
|
||||
});
|
||||
it("returns true for pbm format", () => {
|
||||
expect(needsCliDecode("pbm")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("decodeToSharpCompat", () => {
|
||||
it("returns buffer unchanged for unknown/native formats", async () => {
|
||||
|
||||
@@ -238,5 +238,60 @@ describe("detectFormat", () => {
|
||||
const format = await detectFormat(Buffer.from([0x89]));
|
||||
expect(format).toBe("unknown");
|
||||
});
|
||||
|
||||
it("detects DDS magic bytes", async () => {
|
||||
const buf = Buffer.from([0x44, 0x44, 0x53, 0x20, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
expect(await detectFormat(buf)).toBe("dds");
|
||||
});
|
||||
|
||||
it("detects CUR magic bytes", async () => {
|
||||
const buf = Buffer.from([0x00, 0x00, 0x02, 0x00, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
expect(await detectFormat(buf)).toBe("cur");
|
||||
});
|
||||
|
||||
it("detects DPX forward magic bytes", async () => {
|
||||
const buf = Buffer.from([0x53, 0x44, 0x50, 0x58, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
expect(await detectFormat(buf)).toBe("dpx");
|
||||
});
|
||||
|
||||
it("detects FITS magic bytes", async () => {
|
||||
const buf = Buffer.from([0x53, 0x49, 0x4d, 0x50, 0x4c, 0x45, 0, 0, 0, 0, 0, 0]);
|
||||
expect(await detectFormat(buf)).toBe("fits");
|
||||
});
|
||||
|
||||
it("detects EPS ASCII magic bytes", async () => {
|
||||
const buf = Buffer.from([0x25, 0x21, 0x50, 0x53, 0x2d, 0x41, 0x64, 0x6f, 0x62, 0x65, 0, 0]);
|
||||
expect(await detectFormat(buf)).toBe("eps");
|
||||
});
|
||||
|
||||
it("detects EPS DOS binary magic bytes", async () => {
|
||||
const buf = Buffer.from([0xc5, 0xd0, 0xd3, 0xc6, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
expect(await detectFormat(buf)).toBe("eps");
|
||||
});
|
||||
|
||||
it("detects QOI magic bytes", async () => {
|
||||
const buf = Buffer.from([0x71, 0x6f, 0x69, 0x66, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||
expect(await detectFormat(buf)).toBe("qoi");
|
||||
});
|
||||
});
|
||||
|
||||
describe("real fixture format detection", () => {
|
||||
const fixtures: Array<{ file: string; expected: string[] }> = [
|
||||
{ file: "sample.dds", expected: ["dds", "unknown"] },
|
||||
{ file: "sample.cur", expected: ["cur", "ico", "unknown"] },
|
||||
{ file: "sample.dpx", expected: ["dpx", "unknown"] },
|
||||
{ file: "sample.fits", expected: ["fits", "unknown"] },
|
||||
{ file: "sample.eps", expected: ["eps", "unknown"] },
|
||||
{ file: "sample.qoi", expected: ["qoi", "unknown"] },
|
||||
{ file: "sample.apng", expected: ["png"] },
|
||||
];
|
||||
|
||||
for (const { file, expected } of fixtures) {
|
||||
it(`detects ${file}`, async () => {
|
||||
const buffer = readFileSync(path.join(FORMATS_DIR, file));
|
||||
const format = await detectFormat(buffer);
|
||||
expect(expected).toContain(format);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { qoiDecode, qoiEncode } from "@snapotter/image-engine";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const FORMATS_DIR = path.resolve(__dirname, "../../fixtures/formats");
|
||||
|
||||
describe("QOI codec", () => {
|
||||
it("round-trips RGBA pixel data", () => {
|
||||
const w = 4,
|
||||
h = 4;
|
||||
const pixels = new Uint8Array(w * h * 4);
|
||||
for (let i = 0; i < w * h; i++) {
|
||||
pixels[i * 4] = (i * 17) & 0xff;
|
||||
pixels[i * 4 + 1] = (i * 31) & 0xff;
|
||||
pixels[i * 4 + 2] = (i * 53) & 0xff;
|
||||
pixels[i * 4 + 3] = 255;
|
||||
}
|
||||
const encoded = qoiEncode(pixels, w, h, 4);
|
||||
const { header, pixels: decoded } = qoiDecode(encoded);
|
||||
expect(header.width).toBe(w);
|
||||
expect(header.height).toBe(h);
|
||||
for (let i = 0; i < w * h * 4; i++) expect(decoded[i]).toBe(pixels[i]);
|
||||
});
|
||||
|
||||
it("encodes 3-channel data", () => {
|
||||
const w = 3,
|
||||
h = 3;
|
||||
const pixels = new Uint8Array(w * h * 3);
|
||||
for (let i = 0; i < pixels.length; i++) pixels[i] = (i * 41) & 0xff;
|
||||
const encoded = qoiEncode(pixels, w, h, 3);
|
||||
const { header, pixels: decoded } = qoiDecode(encoded);
|
||||
expect(header.channels).toBe(3);
|
||||
for (let i = 0; i < w * h; i++) {
|
||||
expect(decoded[i * 4 + 3]).toBe(255);
|
||||
}
|
||||
});
|
||||
|
||||
it("writes correct header magic", () => {
|
||||
const encoded = qoiEncode(new Uint8Array(4), 1, 1, 4);
|
||||
const view = new DataView(encoded.buffer, encoded.byteOffset);
|
||||
expect(view.getUint32(0)).toBe(0x716f6966);
|
||||
});
|
||||
|
||||
it("writes correct header dimensions", () => {
|
||||
const encoded = qoiEncode(new Uint8Array(20 * 15 * 4), 20, 15, 4);
|
||||
const view = new DataView(encoded.buffer, encoded.byteOffset);
|
||||
expect(view.getUint32(4)).toBe(20);
|
||||
expect(view.getUint32(8)).toBe(15);
|
||||
expect(encoded[12]).toBe(4);
|
||||
});
|
||||
|
||||
it("round-trips 1x1 image", () => {
|
||||
const pixels = new Uint8Array([42, 128, 200, 255]);
|
||||
const { pixels: decoded } = qoiDecode(qoiEncode(pixels, 1, 1, 4));
|
||||
expect(decoded[0]).toBe(42);
|
||||
expect(decoded[3]).toBe(255);
|
||||
});
|
||||
|
||||
it("round-trips solid color image", () => {
|
||||
const w = 8,
|
||||
h = 8;
|
||||
const pixels = new Uint8Array(w * h * 4);
|
||||
for (let i = 0; i < w * h; i++) {
|
||||
pixels[i * 4] = 100;
|
||||
pixels[i * 4 + 1] = 150;
|
||||
pixels[i * 4 + 2] = 200;
|
||||
pixels[i * 4 + 3] = 255;
|
||||
}
|
||||
const { pixels: decoded } = qoiDecode(qoiEncode(pixels, w, h, 4));
|
||||
for (let i = 0; i < pixels.length; i++) expect(decoded[i]).toBe(pixels[i]);
|
||||
});
|
||||
|
||||
it("compresses solid color efficiently", () => {
|
||||
const w = 100,
|
||||
h = 100;
|
||||
const pixels = new Uint8Array(w * h * 4);
|
||||
for (let i = 0; i < w * h; i++) {
|
||||
pixels[i * 4] = 50;
|
||||
pixels[i * 4 + 1] = 100;
|
||||
pixels[i * 4 + 2] = 150;
|
||||
pixels[i * 4 + 3] = 255;
|
||||
}
|
||||
const encoded = qoiEncode(pixels, w, h, 4);
|
||||
expect(encoded.length).toBeLessThan(pixels.length / 10);
|
||||
});
|
||||
|
||||
it("round-trips gradient", () => {
|
||||
const w = 16,
|
||||
h = 1;
|
||||
const pixels = new Uint8Array(w * 4);
|
||||
for (let i = 0; i < w; i++) {
|
||||
const v = Math.round((i / (w - 1)) * 255);
|
||||
pixels[i * 4] = v;
|
||||
pixels[i * 4 + 1] = v;
|
||||
pixels[i * 4 + 2] = v;
|
||||
pixels[i * 4 + 3] = 255;
|
||||
}
|
||||
const { pixels: decoded } = qoiDecode(qoiEncode(pixels, w, h, 4));
|
||||
for (let i = 0; i < pixels.length; i++) expect(decoded[i]).toBe(pixels[i]);
|
||||
});
|
||||
|
||||
it("round-trips random-ish data", () => {
|
||||
const w = 10,
|
||||
h = 10;
|
||||
const pixels = new Uint8Array(w * h * 4);
|
||||
for (let i = 0; i < pixels.length; i++) pixels[i] = (i * 97 + 53) & 0xff;
|
||||
const { pixels: decoded } = qoiDecode(qoiEncode(pixels, w, h, 4));
|
||||
for (let i = 0; i < pixels.length; i++) expect(decoded[i]).toBe(pixels[i]);
|
||||
});
|
||||
|
||||
it("round-trips varying alpha", () => {
|
||||
const pixels = new Uint8Array([
|
||||
100, 150, 200, 0, 100, 150, 200, 128, 100, 150, 200, 255, 0, 0, 0, 0,
|
||||
]);
|
||||
const { pixels: decoded } = qoiDecode(qoiEncode(pixels, 4, 1, 4));
|
||||
for (let i = 0; i < pixels.length; i++) expect(decoded[i]).toBe(pixels[i]);
|
||||
});
|
||||
|
||||
it("throws on invalid magic", () => {
|
||||
expect(() => qoiDecode(new Uint8Array(20))).toThrow();
|
||||
});
|
||||
|
||||
it("throws on zero width", () => {
|
||||
const buf = new Uint8Array(14);
|
||||
const v = new DataView(buf.buffer);
|
||||
v.setUint32(0, 0x716f6966);
|
||||
v.setUint32(4, 0);
|
||||
v.setUint32(8, 1);
|
||||
buf[12] = 4;
|
||||
expect(() => qoiDecode(buf)).toThrow();
|
||||
});
|
||||
|
||||
it("throws on zero height", () => {
|
||||
const buf = new Uint8Array(14);
|
||||
const v = new DataView(buf.buffer);
|
||||
v.setUint32(0, 0x716f6966);
|
||||
v.setUint32(4, 1);
|
||||
v.setUint32(8, 0);
|
||||
buf[12] = 4;
|
||||
expect(() => qoiDecode(buf)).toThrow();
|
||||
});
|
||||
|
||||
it("throws on invalid channels", () => {
|
||||
const buf = new Uint8Array(14);
|
||||
const v = new DataView(buf.buffer);
|
||||
v.setUint32(0, 0x716f6966);
|
||||
v.setUint32(4, 1);
|
||||
v.setUint32(8, 1);
|
||||
buf[12] = 5;
|
||||
expect(() => qoiDecode(buf)).toThrow();
|
||||
});
|
||||
|
||||
it("ends with correct end marker", () => {
|
||||
const encoded = qoiEncode(new Uint8Array([1, 2, 3, 255]), 1, 1, 4);
|
||||
expect(Array.from(encoded.slice(-8))).toEqual([0, 0, 0, 0, 0, 0, 0, 1]);
|
||||
});
|
||||
|
||||
it("decodes real fixture with correct header", () => {
|
||||
const data = readFileSync(path.join(FORMATS_DIR, "sample.qoi"));
|
||||
const { header } = qoiDecode(new Uint8Array(data));
|
||||
expect(header.width).toBe(10);
|
||||
expect(header.height).toBe(10);
|
||||
expect(header.channels).toBe(4);
|
||||
});
|
||||
|
||||
it("re-encodes real fixture with matching pixels", () => {
|
||||
const data = readFileSync(path.join(FORMATS_DIR, "sample.qoi"));
|
||||
const { header, pixels } = qoiDecode(new Uint8Array(data));
|
||||
const reEncoded = qoiEncode(pixels, header.width, header.height, 4);
|
||||
const { pixels: reDecoded } = qoiDecode(reEncoded);
|
||||
for (let i = 0; i < pixels.length; i++) expect(reDecoded[i]).toBe(pixels[i]);
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,168 @@
|
||||
// @vitest-environment jsdom
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type {
|
||||
CanvasObject,
|
||||
EllipseAttrs,
|
||||
LineAttrs,
|
||||
RectAttrs,
|
||||
TextAttrs,
|
||||
ToolType,
|
||||
} from "@/types/editor";
|
||||
|
||||
describe("CanvasObject discriminated union", () => {
|
||||
it("narrows to line type with LineAttrs", () => {
|
||||
const obj: CanvasObject = {
|
||||
id: "l1",
|
||||
type: "line",
|
||||
layerId: "layer-1",
|
||||
attrs: {
|
||||
points: [0, 0, 100, 100],
|
||||
stroke: "#000",
|
||||
strokeWidth: 2,
|
||||
tension: 0,
|
||||
lineCap: "round",
|
||||
lineJoin: "round",
|
||||
opacity: 1,
|
||||
globalCompositeOperation: "source-over",
|
||||
},
|
||||
};
|
||||
if (obj.type === "line") {
|
||||
const attrs: LineAttrs = obj.attrs;
|
||||
expect(attrs.points).toEqual([0, 0, 100, 100]);
|
||||
expect(attrs.stroke).toBe("#000");
|
||||
expect(attrs.strokeWidth).toBe(2);
|
||||
} else {
|
||||
expect.unreachable("should have narrowed to line");
|
||||
}
|
||||
});
|
||||
|
||||
it("narrows to rect type with RectAttrs", () => {
|
||||
const obj: CanvasObject = {
|
||||
id: "r1",
|
||||
type: "rect",
|
||||
layerId: "layer-1",
|
||||
attrs: {
|
||||
x: 10,
|
||||
y: 20,
|
||||
width: 100,
|
||||
height: 50,
|
||||
fill: "#ff0000",
|
||||
stroke: "#000",
|
||||
strokeWidth: 1,
|
||||
cornerRadius: 5,
|
||||
rotation: 0,
|
||||
opacity: 1,
|
||||
},
|
||||
};
|
||||
if (obj.type === "rect") {
|
||||
const attrs: RectAttrs = obj.attrs;
|
||||
expect(attrs.x).toBe(10);
|
||||
expect(attrs.y).toBe(20);
|
||||
expect(attrs.cornerRadius).toBe(5);
|
||||
} else {
|
||||
expect.unreachable("should have narrowed to rect");
|
||||
}
|
||||
});
|
||||
|
||||
it("narrows to text type with TextAttrs", () => {
|
||||
const obj: CanvasObject = {
|
||||
id: "t1",
|
||||
type: "text",
|
||||
layerId: "layer-1",
|
||||
attrs: {
|
||||
x: 50,
|
||||
y: 60,
|
||||
text: "Hello",
|
||||
fontFamily: "Arial",
|
||||
fontSize: 16,
|
||||
fontStyle: "normal",
|
||||
fontVariant: "normal",
|
||||
textDecoration: "",
|
||||
align: "left",
|
||||
fill: "#000",
|
||||
lineHeight: 1.2,
|
||||
letterSpacing: 0,
|
||||
rotation: 0,
|
||||
opacity: 1,
|
||||
},
|
||||
};
|
||||
if (obj.type === "text") {
|
||||
const attrs: TextAttrs = obj.attrs;
|
||||
expect(attrs.text).toBe("Hello");
|
||||
expect(attrs.fontFamily).toBe("Arial");
|
||||
expect(attrs.fontSize).toBe(16);
|
||||
} else {
|
||||
expect.unreachable("should have narrowed to text");
|
||||
}
|
||||
});
|
||||
|
||||
it("narrows to ellipse type with EllipseAttrs", () => {
|
||||
const obj: CanvasObject = {
|
||||
id: "e1",
|
||||
type: "ellipse",
|
||||
layerId: "layer-1",
|
||||
attrs: {
|
||||
x: 100,
|
||||
y: 100,
|
||||
radiusX: 50,
|
||||
radiusY: 30,
|
||||
fill: "#00ff00",
|
||||
stroke: "#000",
|
||||
strokeWidth: 1,
|
||||
rotation: 0,
|
||||
opacity: 1,
|
||||
},
|
||||
};
|
||||
if (obj.type === "ellipse") {
|
||||
const attrs: EllipseAttrs = obj.attrs;
|
||||
expect(attrs.radiusX).toBe(50);
|
||||
expect(attrs.radiusY).toBe(30);
|
||||
} else {
|
||||
expect.unreachable("should have narrowed to ellipse");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("ToolType", () => {
|
||||
it("includes expected tool variants", () => {
|
||||
const tools: ToolType[] = [
|
||||
"move",
|
||||
"marquee-rect",
|
||||
"marquee-ellipse",
|
||||
"lasso-free",
|
||||
"lasso-poly",
|
||||
"magic-wand",
|
||||
"crop",
|
||||
"eyedropper",
|
||||
"brush",
|
||||
"eraser",
|
||||
"pencil",
|
||||
"clone-stamp",
|
||||
"dodge",
|
||||
"burn",
|
||||
"sponge",
|
||||
"blur-brush",
|
||||
"sharpen-brush",
|
||||
"smudge",
|
||||
"fill",
|
||||
"gradient",
|
||||
"shape-rect",
|
||||
"shape-ellipse",
|
||||
"shape-line",
|
||||
"shape-arrow",
|
||||
"shape-polygon",
|
||||
"shape-star",
|
||||
"text",
|
||||
"hand",
|
||||
"zoom",
|
||||
"transform",
|
||||
];
|
||||
// If this compiles without error, the union accepts all these values
|
||||
expect(tools).toHaveLength(30);
|
||||
});
|
||||
|
||||
it("each variant is a string", () => {
|
||||
const tool: ToolType = "brush";
|
||||
expect(typeof tool).toBe("string");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user