Skip to content

Free open-source image editor for the browser

6.3 KB
Raw Blame
import { expect, test } from "bun:test";
import { createImageLayer, createLayer, createMask } from "@/app/editor/layers";
import { createCameraRawXmpLoader } from "@/app/loaders/camera-raw-xmp";
import { createLoaderRegistry, type FileLoader } from "@/app/loaders/registry";
import { createWorkspace } from "@/app/workspace";
import { createDocument } from "@/core/document";
import { canDecode } from "@/core/image/decode";
import { imageFrame } from "@/core/image/frame";
import { setAdjustments } from "@/features/adjustments/edits";
import { defaultAdjustments } from "@/features/adjustments/model";
import { addLayer } from "@/features/layers/edits";
import { defaultGradient } from "@/features/layers/gradient";

function settings(attributes: string, name = "photo.xmp") {
  return new File(
    [
      `<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/" ${attributes} /></rdf:RDF>`,
    ],
    name,
  );
}

test("file batches preserve ordering, group imports, recover from failures, and discard stale settings", async () => {
  for (const name of ["mamiya.MEF", "epson.erf", "fuji.raf", "iphone.dng"]) {
    expect(canDecode(new File([], name))).toBe(true);
  }
  const workspace = createWorkspace();
  // Substitute decoding only; the registry, workspace, XMP parser, and documents are real.
  const image: FileLoader = {
    kind: "document",
    accepts: (file) => file.name.endsWith(".png"),
    load: (file) =>
      workspace.open(file.name, async () => {
        if (file.name === "broken.png") throw new Error("Decode failed");
        return createDocument({
          frame: imageFrame([32, 32]),
          layers: [{ ...createImageLayer(file.name, "Photo"), id: "base" }],
        });
      }),
  };
  const xmp = createCameraRawXmpLoader(workspace);
  const registry = createLoaderRegistry(
    [xmp, image],
    () => workspace.state.getState().status === "ready",
  );
  const layers = () => workspace.getDocument().scene.getState().layers;
  const adjustments = () => layers()[0].adjustments;
  const photo = new File([], "photo.png");
  const broken = new File([], "broken.png");
  const exposure = settings('crs:Exposure2012="1.25"');
  await registry.openFiles([new File([], "notes.txt"), exposure]);
  expect(workspace.state.getState()).toEqual({ status: "empty" });
  const imported = {
    ...defaultAdjustments,
    exposure: 1.25,
    contrast: -20,
    highlights: -50,
    shadows: 50,
    whites: 25,
    blacks: -25,
    vibrance: 30,
    saturation: -10,
  };
  const all =
    'crs:Exposure2012="1.25" crs:Contrast2012="-20" crs:Highlights2012="-50" crs:Shadows2012="50" crs:Whites2012="25" crs:Blacks2012="-25" crs:Vibrance="30" crs:Saturation="-10"';
  // Settings wait for the batch's first image, before or after them.
  for (const batch of [
    [settings(all, "photo.xml"), photo, broken],
    [photo, broken, settings(all)],
  ]) {
    await registry.openFiles(batch);
    expect(workspace.state.getState().file).toBe("photo.png");
    expect(adjustments()).toEqual(imported);
  }
  const document = workspace.getDocument();
  // Each settings file is its own undo step, and an import commits an open gesture first.
  await registry.openFiles([
    settings('crs:Exposure2012="3"'),
    settings('crs:Exposure2012="-1"'),
  ]);
  document.history.undo();
  expect(adjustments().exposure).toBe(3);
  document.history.redo();
  document.history.begin();
  setAdjustments(document, { contrast: 20 });
  setAdjustments(document, { contrast: 30 });
  await registry.openFiles([settings('crs:Highlights2012="0"')]);
  expect(adjustments().highlights).toBe(0);
  document.history.undo();
  expect(adjustments()).toEqual({ ...imported, exposure: -1, contrast: 30 });
  document.history.undo();
  expect(adjustments().contrast).toBe(-20);
  const maskId = addLayer(document, createMask(defaultGradient([32, 32])));
  addLayer(document, createLayer("details"), { inside: maskId });
  const before = document.scene.getState();
  // Clarity goes to a root Details layer, leaving the mask's own untouched.
  await xmp.load(settings('crs:Exposure2012="0.5" crs:Clarity2012="35"'));
  expect(layers()[2]).toMatchObject({ details: { clarity: 35 } });
  expect(adjustments().exposure).toBe(0.5);
  expect(layers()[1]).toBe(before.layers[1]);
  document.history.undo();
  expect(document.scene.getState()).toEqual(before);
  document.history.redo();
  await xmp.load(settings('crs:Clarity2012="0"'));
  expect(layers()).toHaveLength(3);
  expect(layers()[2]).toMatchObject({ details: { clarity: 0 } });
  await Promise.all([
    registry.openFiles([exposure, new File([], "first.png")]),
    registry.openFiles([
      new File([], "second.png"),
      settings('crs:Contrast2012="20"'),
    ]),
  ]);
  expect(workspace.state.getState().file).toBe("second.png");
  expect(adjustments()).toEqual({ ...defaultAdjustments, contrast: 20 });
  // A failed replacement keeps the open document and skips the settings meant for the failed file.
  const kept = workspace.getDocument();
  const keptAdjustments = adjustments();
  await registry.openFiles([exposure, broken]);
  expect(workspace.getDocument()).toBe(kept);
  expect(workspace.state.getState()).toMatchObject({
    status: "ready",
    file: "second.png",
    failure: { file: "broken.png", error: "Error: Decode failed" },
  });
  expect(adjustments()).toBe(keptAdjustments);
  workspace.dismissFailure();
  expect(workspace.state.getState()).not.toHaveProperty("failure");
  await registry.openFiles([exposure, photo]);
  const recovered = workspace.getDocument();
  await expect(
    registry.openFiles([settings('crs:Exposure2012="99"')]),
  ).rejects.toThrow("Invalid adjustment");
  await registry.openFiles([
    settings('crs:Contrast2012="10" crs:Exposure2012="NaN"'),
  ]);
  expect(adjustments()).toMatchObject({ exposure: 1.25, contrast: 10 });

  // Hold file reading so replacement happens before the old import completes.
  const text = Promise.withResolvers<string>();
  const delayed = settings('crs:Exposure2012="4"');
  delayed.text = () => text.promise;
  const pending = xmp.load(delayed);
  await registry.loadFile(image, photo);
  text.resolve(await exposure.text());
  await pending;
  expect(workspace.getDocument()).not.toBe(recovered);
  expect(adjustments()).toEqual(defaultAdjustments);
  workspace.dispose();
});