A failed upload was dropping the local preview, and Revelar waited on the server before opening. The preview now stays on screen, and the next photo does not wait for the upload.
159 lines
6.2 KiB
TypeScript
159 lines
6.2 KiB
TypeScript
import type { DevelopState, EditCommand, EditLogEntry, PostEdits } from "@photoreport/contracts";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { DevelopSession, type OutboxStore, type PendingOp, type SettingsEngine, type StateStore } from "./session.js";
|
|
|
|
/** Settings as a flat map of control values; enough to check ordering, undo and sync. */
|
|
const engine: SettingsEngine = {
|
|
defaultSettings: () => "{}",
|
|
apply(settings, command) {
|
|
const s = JSON.parse(settings) as Record<string, unknown>;
|
|
const c = JSON.parse(command) as EditCommand;
|
|
if (c.command === "develop.set") return JSON.stringify({ ...s, ...c.params.values });
|
|
if (c.command === "develop.reset") return "{}";
|
|
if (c.command === "develop.merge") return JSON.stringify(c.params.partial);
|
|
return JSON.stringify({ ...s, crop: c.params });
|
|
},
|
|
value: (settings, id) => ((JSON.parse(settings) as Record<string, number>)[id] ?? 0),
|
|
};
|
|
|
|
/** An in-memory server: one op log, every accepted op fanned out to the other sessions. */
|
|
class FakeServer {
|
|
log: EditLogEntry[] = [];
|
|
down = false;
|
|
sessions: DevelopSession[] = [];
|
|
async developState(_photoId: string, since?: number): Promise<DevelopState> {
|
|
const from = since ?? 0;
|
|
return { photoId: "p", developSeq: this.log.length, snapshotSeq: from, snapshot: null, ops: this.log.filter((o) => o.seq > from) };
|
|
}
|
|
async postEdits(_photoId: string, body: PostEdits) {
|
|
if (this.down) throw Object.assign(new Error("offline"), { status: 0 });
|
|
const out: EditLogEntry[] = [];
|
|
const fresh: EditLogEntry[] = [];
|
|
for (const op of body.ops) {
|
|
const known = this.log.find((o) => o.opId === op.opId);
|
|
if (known) {
|
|
out.push(known);
|
|
continue;
|
|
}
|
|
const entry = { seq: this.log.length + 1, opId: op.opId, clientId: body.clientId, command: op.command, userId: null, at: "" };
|
|
this.log.push(entry);
|
|
out.push(entry);
|
|
fresh.push(entry);
|
|
}
|
|
for (const s of this.sessions) void s.remote({ clientId: body.clientId, ops: fresh });
|
|
return { developSeq: this.log.length, ops: out };
|
|
}
|
|
}
|
|
|
|
const set = (values: Record<string, number>): EditCommand => ({ command: "develop.set", params: { values } });
|
|
|
|
describe("DevelopSession", () => {
|
|
let server: FakeServer;
|
|
const open = async (clientId: string, outbox?: OutboxStore) => {
|
|
const s = new DevelopSession("p", { engine, api: server, clientId, outbox });
|
|
server.sessions.push(s);
|
|
await s.load();
|
|
return s;
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.useFakeTimers();
|
|
server = new FakeServer();
|
|
});
|
|
afterEach(() => vi.useRealTimers());
|
|
|
|
it("shows edits at once and sends a drag as few ops with one undo step", async () => {
|
|
const web = await open("web");
|
|
web.edit(set({ "light.exposure": 0.1 }), "light.exposure");
|
|
web.edit(set({ "light.exposure": 0.2 }), "light.exposure");
|
|
expect(web.value("light.exposure")).toBe(0.2);
|
|
await vi.advanceTimersByTimeAsync(200);
|
|
web.edit(set({ "light.exposure": 0.6 }), "light.exposure");
|
|
web.endGesture();
|
|
await vi.advanceTimersByTimeAsync(200);
|
|
expect(server.log.map((o) => (o.command as { params: { values: Record<string, number> } }).params.values["light.exposure"])).toEqual([0.2, 0.6]);
|
|
expect(web.seq).toBe(2);
|
|
expect(web.hasPending).toBe(false);
|
|
|
|
web.undo();
|
|
expect(web.value("light.exposure")).toBe(0);
|
|
web.redo();
|
|
expect(web.value("light.exposure")).toBe(0.6);
|
|
});
|
|
|
|
it("keeps two windows in step, last writer winning per control", async () => {
|
|
const web = await open("web");
|
|
const desktop = await open("desktop");
|
|
web.edit(set({ "light.exposure": 1 }));
|
|
desktop.edit(set({ "effects.clarity": 30 }));
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
desktop.edit(set({ "light.exposure": -0.5 }));
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
for (const s of [web, desktop]) {
|
|
expect(s.value("light.exposure")).toBe(-0.5);
|
|
expect(s.value("effects.clarity")).toBe(30);
|
|
expect(s.seq).toBe(3);
|
|
}
|
|
});
|
|
|
|
it("queues edits while offline, survives a reload, and stores a retried op once", async () => {
|
|
const saved = new Map<string, PendingOp[]>();
|
|
const outbox: OutboxStore = { load: (id) => saved.get(id) ?? [], save: (id, ops) => void saved.set(id, ops) };
|
|
server.down = true;
|
|
const web = await open("web", outbox);
|
|
web.edit(set({ "light.exposure": 0.7 }));
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
expect(web.offline).toBe(true);
|
|
expect(saved.get("p")).toHaveLength(1);
|
|
web.close();
|
|
|
|
server.down = false;
|
|
const again = await open("web", outbox);
|
|
expect(again.value("light.exposure")).toBe(0.7);
|
|
await vi.advanceTimersByTimeAsync(50);
|
|
expect(server.log).toHaveLength(1);
|
|
expect(saved.get("p")).toEqual([]);
|
|
await again.flush();
|
|
expect(server.log).toHaveLength(1);
|
|
});
|
|
|
|
it("catches up when it missed ops", async () => {
|
|
const web = await open("web");
|
|
server.sessions = []; // messages to web are lost
|
|
const other = new DevelopSession("p", { engine, api: server, clientId: "other" });
|
|
await other.load();
|
|
other.edit(set({ "light.contrast": 10 }));
|
|
other.edit(set({ "light.contrast": 20 }));
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
server.sessions = [web];
|
|
other.edit(set({ "light.whites": 5 }));
|
|
await vi.advanceTimersByTimeAsync(300);
|
|
expect(web.value("light.contrast")).toBe(20);
|
|
expect(web.value("light.whites")).toBe(5);
|
|
expect(web.seq).toBe(3);
|
|
});
|
|
|
|
it("opens on the last state when the server does not answer", async () => {
|
|
server.developState = () => new Promise(() => {});
|
|
const states: StateStore = {
|
|
get: async () => ({ settings: JSON.stringify({ "light.exposure": 0.4 }), seq: 2 }),
|
|
set: () => {},
|
|
};
|
|
const s = new DevelopSession("p", {
|
|
engine,
|
|
api: server,
|
|
clientId: "web",
|
|
states,
|
|
isOffline: (err) => err instanceof Error && /timeout|offline/i.test(err.message),
|
|
});
|
|
const loading = s.load();
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(s.loaded).toBe(true);
|
|
expect(s.value("light.exposure")).toBe(0.4);
|
|
await vi.advanceTimersByTimeAsync(8_000);
|
|
await loading;
|
|
expect(s.offline).toBe(true);
|
|
expect(s.value("light.exposure")).toBe(0.4);
|
|
});
|
|
});
|