engine/core renders with the LightCraft pipeline; engine/wasm runs it in the browser; workers/render replays op logs and renders originals natively. Op log with live fan-out and offline outbox; edited thumbnails and deliveries rendered from the original. Revelar page with Basic and optional modules, crop with aspect presets and straighten, before/after, undo/redo, copy/paste settings. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01HT6iXLYUUp9pJz5Kza9xCW
161 lines
8.6 KiB
TypeScript
161 lines
8.6 KiB
TypeScript
import { copyFile, mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import sharp from "sharp";
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
import type { RenderEngine, RenderJob, Settings } from "../src/develop/engine.js";
|
|
import { DevelopService, RENDER_QUIET_MS } from "../src/develop/service.js";
|
|
import { IngestService } from "../src/ingest/service.js";
|
|
import { MemoryBroadcaster } from "../src/realtime.js";
|
|
import { LocalStorage, UrlSigner } from "../src/storage/storage.js";
|
|
import { bearer, createTestApp, login, resetDb, seedUser, type TestContext } from "./helpers.js";
|
|
|
|
const quiet = { info: () => {}, warn: () => {} };
|
|
|
|
/** Stands in for workers/render: "settings" are the merged `develop.set` values; render copies the file. */
|
|
class FakeEngine implements RenderEngine {
|
|
renders: RenderJob[] = [];
|
|
async replay(base: Settings | null, commands: unknown[]) {
|
|
let s: Record<string, number> = { ...((base as Record<string, number> | null) ?? {}) };
|
|
const skipped: number[] = [];
|
|
commands.forEach((c, i) => {
|
|
const cmd = c as { command: string; params?: { values?: Record<string, number> } };
|
|
if (cmd.command === "develop.set") s = { ...s, ...cmd.params!.values };
|
|
else if (cmd.command === "develop.reset") s = {};
|
|
else skipped.push(i);
|
|
});
|
|
const unedited = Object.values(s).every((v) => v === 0);
|
|
return { settings: s, skipped, unedited };
|
|
}
|
|
async render(job: RenderJob) {
|
|
this.renders.push(job);
|
|
await copyFile(job.input, job.output);
|
|
return { width: 600, height: 400 };
|
|
}
|
|
async close() {}
|
|
}
|
|
|
|
const set = (opId: string, values: Record<string, number>) => ({ opId, command: { command: "develop.set", params: { values } } });
|
|
|
|
describe("develop op log", () => {
|
|
let ctx: TestContext;
|
|
let dir: string;
|
|
let ingestRoot: string;
|
|
let ingest: IngestService;
|
|
let develop: DevelopService;
|
|
let engine: FakeEngine;
|
|
const broadcaster = new MemoryBroadcaster();
|
|
|
|
beforeAll(async () => {
|
|
dir = await mkdtemp(path.join(tmpdir(), "pr-develop-"));
|
|
ingestRoot = path.join(dir, "ingest");
|
|
ctx = await createTestApp({ STORAGE_ROOT: path.join(dir, "storage"), INGEST_ROOT: ingestRoot }, broadcaster);
|
|
const storage = new LocalStorage(ctx.config.STORAGE_ROOT);
|
|
const signer = new UrlSigner(ctx.config.JWT_SECRET, ctx.config.FILE_URL_TTL_SEC);
|
|
engine = new FakeEngine();
|
|
ingest = new IngestService({ db: ctx.db, storage, signer, broadcaster, ingestRoot, log: quiet });
|
|
develop = new DevelopService({ db: ctx.db, storage, signer, broadcaster, log: quiet, engine });
|
|
});
|
|
afterAll(async () => {
|
|
await ctx.app.close();
|
|
await ctx.db.$disconnect();
|
|
await rm(dir, { recursive: true, force: true });
|
|
});
|
|
beforeEach(async () => {
|
|
await resetDb(ctx.db);
|
|
broadcaster.sent.length = 0;
|
|
engine.renders = [];
|
|
});
|
|
|
|
let n = 0;
|
|
async function setup() {
|
|
const { org, user } = await seedUser(ctx.db, "[email protected]", { org: "fpa", role: "EDITOR" });
|
|
await seedUser(ctx.db, "[email protected]", { org: "fpa", role: "PHOTOGRAPHER" });
|
|
await ctx.db.ftpAccount.create({ data: { organizationId: org!.id, userId: user.id, login: "cam" } });
|
|
const data = await sharp({ create: { width: 600, height: 400, channels: 3, background: { r: ++n, g: 90, b: 40 } } }).jpeg().toBuffer();
|
|
const file = path.join(ingestRoot, "cam", `IMG_${n}.jpg`);
|
|
await mkdir(path.dirname(file), { recursive: true });
|
|
await writeFile(file, data);
|
|
const out = await ingest.ingestFile(file);
|
|
if (out.status !== "ingested") throw new Error(JSON.stringify(out));
|
|
return {
|
|
orgId: org!.id,
|
|
photoId: out.photoId,
|
|
editor: (await login(ctx.app, "[email protected]")).body.accessToken,
|
|
photographer: (await login(ctx.app, "[email protected]")).body.accessToken,
|
|
};
|
|
}
|
|
|
|
const post = (token: string, photoId: string, ops: unknown[], clientId = "web-1") =>
|
|
ctx.app.inject({ method: "POST", url: `/v1/photos/${photoId}/edits`, headers: bearer(token), payload: { clientId, ops } });
|
|
|
|
it("orders edits per photo, stores a retried op once, and fans them out", async () => {
|
|
const { photoId, editor } = await setup();
|
|
const first = await post(editor, photoId, [set("a", { "light.exposure": 0.5 }), set("b", { "effects.clarity": 20 })]);
|
|
expect(first.statusCode).toBe(200);
|
|
expect(first.json()).toMatchObject({ developSeq: 2, ops: [{ seq: 1, opId: "a" }, { seq: 2, opId: "b" }] });
|
|
// The offline queue sends "b" again together with a new op.
|
|
const retry = await post(editor, photoId, [set("b", { "effects.clarity": 20 }), set("c", { "light.exposure": 0.7 })]);
|
|
expect(retry.json()).toMatchObject({ developSeq: 3, ops: [{ seq: 2, opId: "b" }, { seq: 3, opId: "c" }] });
|
|
const edited = broadcaster.sent.filter((s) => s.message.type === "photo.edited").map((s) => s.message as { ops: { seq: number }[]; clientId: string });
|
|
expect(edited.map((m) => m.ops.map((o) => o.seq))).toEqual([[1, 2], [3]]);
|
|
expect(edited[0]!.clientId).toBe("web-1");
|
|
|
|
const state = (await ctx.app.inject({ method: "GET", url: `/v1/photos/${photoId}/develop`, headers: bearer(editor) })).json();
|
|
expect(state).toMatchObject({ developSeq: 3, snapshotSeq: 0, snapshot: null });
|
|
expect(state.ops).toHaveLength(3);
|
|
const since = (await ctx.app.inject({ method: "GET", url: `/v1/photos/${photoId}/develop?since=2`, headers: bearer(editor) })).json();
|
|
expect(since.ops.map((o: { seq: number }) => o.seq)).toEqual([3]);
|
|
});
|
|
|
|
it("refuses controls the engine does not have, values out of range, and other photographers", async () => {
|
|
const { photoId, editor, photographer } = await setup();
|
|
expect((await post(editor, photoId, [set("x", { "light.nope": 1 })])).json()).toMatchObject({ error: "unknown_control" });
|
|
expect((await post(editor, photoId, [set("x", { "light.exposure": 99 })])).json()).toMatchObject({ error: "value_out_of_range" });
|
|
expect((await post(photographer, photoId, [set("x", { "light.exposure": 1 })])).statusCode).toBe(403);
|
|
expect((await ctx.db.photo.findUniqueOrThrow({ where: { id: photoId } })).developSeq).toBe(0);
|
|
});
|
|
|
|
it("renders edited thumbnails once edits pause, and keeps the unedited source for the editor", async () => {
|
|
const { photoId, editor } = await setup();
|
|
await post(editor, photoId, [set("a", { "light.exposure": 0.5 })]);
|
|
expect(await develop.claimRenders(5)).toEqual([]); // still editing
|
|
const later = new Date(Date.now() + RENDER_QUIET_MS + 100);
|
|
const [claimed] = await develop.claimRenders(5, later);
|
|
expect(claimed).toBe(photoId);
|
|
await develop.render(photoId);
|
|
|
|
const photo = await ctx.db.photo.findUniqueOrThrow({ where: { id: photoId } });
|
|
expect(photo).toMatchObject({ renderedSeq: 1, snapshotSeq: 1, renderStatus: "DONE", developSnapshot: { "light.exposure": 0.5 } });
|
|
expect(engine.renders.map((r) => [r.maxEdge, r.settings])).toEqual([
|
|
[2048, { "light.exposure": 0.5 }],
|
|
[480, { "light.exposure": 0.5 }],
|
|
]);
|
|
const rendered = broadcaster.sent.find((s) => s.message.type === "photo.rendered")!.message as { urls: { thumb: string } };
|
|
expect(rendered.urls.thumb).toContain("&v=1");
|
|
expect((await ctx.app.inject({ method: "GET", url: rendered.urls.thumb })).statusCode).toBe(200);
|
|
|
|
const list = (await ctx.app.inject({ method: "GET", url: `/v1/orgs/${photo.organizationId}/photos`, headers: bearer(editor) })).json() as {
|
|
photos: { developSeq: number; urls: { thumb: string; source: string } }[];
|
|
};
|
|
expect(list.photos[0]).toMatchObject({ developSeq: 1 });
|
|
expect(list.photos[0]!.urls.thumb).toContain("&v=1");
|
|
expect((await ctx.app.inject({ method: "GET", url: list.photos[0]!.urls.source })).statusCode).toBe(200);
|
|
|
|
// State now starts from the snapshot.
|
|
const state = (await ctx.app.inject({ method: "GET", url: `/v1/photos/${photoId}/develop`, headers: bearer(editor) })).json();
|
|
expect(state).toMatchObject({ developSeq: 1, snapshotSeq: 1, snapshot: { "light.exposure": 0.5 }, ops: [] });
|
|
});
|
|
|
|
it("goes back to the original thumbnails after a reset", async () => {
|
|
const { photoId, editor } = await setup();
|
|
await post(editor, photoId, [set("a", { "light.exposure": 0.5 })]);
|
|
await develop.render(photoId);
|
|
await post(editor, photoId, [{ opId: "r", command: { command: "develop.reset" } }]);
|
|
await develop.render(photoId);
|
|
const photo = await ctx.db.photo.findUniqueOrThrow({ where: { id: photoId } });
|
|
expect(photo).toMatchObject({ renderedSeq: 0, snapshotSeq: 2, developSnapshot: null });
|
|
expect(await develop.settings(photoId)).toEqual({ settings: null, seq: 2 });
|
|
});
|
|
});
|