feat: platform backbone and field capture, first part (checkpoint)

Platform backbone (plan 2026-10-13-plattform-rueckgrat), reviewed tasks:
- run roles api/worker/job/all with shared startup checks (Task 1)
- schema and migration for DomainEvent, deliveries, JobRun, ThrottlerHit (Task 2)
- audit, notifications and domain events written in the same transaction (Task 3)
- dispatcher with SKIP LOCKED claims, retries and stale release (Task 4)
- worker building blocks: tick, LISTEN/NOTIFY wake-up, health (Task 5)
- worker process and entry points (Task 6)
- throttler counters in Postgres (Task 19)
- separate keys per purpose with key ring (Task 20)
- public origin page without exact location (Task 22)
- shop keys split into read and write (Task 23)
- Kustomize base, components, overlay encry, CI validation (Task 26)

Field capture T3 (plan 2026-10-13-t3-erfassung-am-stand), reviewed tasks:
- brood frames, read endpoints, apiary overview and offline pack
- network basics, offline queue v3, navigation sheets, form building blocks
- new inspection with last-time hints and weather, varroa per day,
  treatment prefill, apiary page with round
- inspection photo (Task 10) is implemented, review pending

Not rolled out. Remaining tasks follow in later commits.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
Nick SzaunigandClaude Opus 5.5 committed 2026-10-13 17:57:54 +02:00
1 parent 10cbe9bf2d
commit c01a38835a
340 files changed
+25695 -1808

No files matched your search

+389 -73
View File
@@ -1,111 +1,427 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { ApiError } from "@/lib/auth/api-client";
import { createInspection } from "@/lib/inspections/api";
import { getPendingInspections, removePendingInspection } from "./db";
import { syncPendingInspections } from "./sync";
import "fake-indexeddb/auto";
import { IDBFactory } from "fake-indexeddb";
import { openDB } from "idb";
import { beforeEach, describe, expect, it, vi, type MockedFunction } from "vitest";
vi.mock("@/lib/inspections/api", () => ({
createInspection: vi.fn(),
vi.mock("@/lib/inspections/api", () => ({ createInspection: vi.fn() }));
vi.mock("@/lib/treatments/api", () => ({ createTreatment: vi.fn() }));
vi.mock("@/lib/documents/api", () => ({ uploadDocument: vi.fn() }));
// What the inspection drain hands over to the photos, seen at that moment.
const photoHandOver = vi.hoisted(() => ({
calls: [] as { key: string; inspectionId: string; entryStillQueued: boolean }[],
failFor: null as string | null,
// Set per test: reads the queue of the test's fresh database.
isQueued: (() => Promise.resolve(false)) as (key: string) => Promise<boolean>,
}));
vi.mock("./db", () => ({
getPendingInspections: vi.fn(),
removePendingInspection: vi.fn(),
// The other five queues are exercised by their own dedicated tests
// elsewhere; here they just need to resolve empty so runSync()'s
// Promise.all doesn't touch their (unmocked, real) create functions.
getPendingTreatments: vi.fn().mockResolvedValue([]),
removePendingTreatment: vi.fn(),
getPendingFeedings: vi.fn().mockResolvedValue([]),
removePendingFeeding: vi.fn(),
getPendingVarroaMeasurements: vi.fn().mockResolvedValue([]),
removePendingVarroaMeasurement: vi.fn(),
getPendingSwarmControlChecks: vi.fn().mockResolvedValue([]),
removePendingSwarmControlCheck: vi.fn(),
getPendingHarvests: vi.fn().mockResolvedValue([]),
removePendingHarvest: vi.fn(),
}));
const mockedCreateInspection = vi.mocked(createInspection);
const mockedGetPending = vi.mocked(getPendingInspections);
const mockedRemovePending = vi.mocked(removePendingInspection);
function pendingEntry(id: number) {
vi.mock("@/lib/photos/store", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/photos/store")>();
return {
id,
input: { colonyId: `colony-${id}`, date: "2026-01-01T00:00:00.000Z" },
queuedAt: "2026-01-01T00:00:00.000Z",
...actual,
resolveQueuedInspection: async (key: string, inspectionId: string) => {
photoHandOver.calls.push({
key,
inspectionId,
entryStillQueued: await photoHandOver.isQueued(key),
});
if (photoHandOver.failFor === key) {
throw new Error("Speicher voll");
}
return actual.resolveQueuedInspection(key, inspectionId);
},
};
});
type OfflineDbModule = typeof import("./db");
type SyncModule = typeof import("./sync");
type PhotoStoreModule = typeof import("@/lib/photos/store");
type ApiErrorClass = typeof import("@/lib/auth/api-client").ApiError;
let db: OfflineDbModule;
let sync: SyncModule;
let photos: PhotoStoreModule;
let ApiError: ApiErrorClass;
let createInspection: MockedFunction<typeof import("@/lib/inspections/api").createInspection>;
let createTreatment: MockedFunction<typeof import("@/lib/treatments/api").createTreatment>;
let uploadDocument: MockedFunction<typeof import("@/lib/documents/api").uploadDocument>;
function inspection(colony: number) {
return { colonyId: `colony-${colony}`, date: "2026-10-01T00:00:00.000Z" };
}
describe("syncPendingInspections", () => {
beforeEach(() => {
vi.clearAllMocks();
/** A request that stays open until the test answers it. */
function openRequest() {
let answer: (saved: { id: string }) => void = () => undefined;
const promise = new Promise<{ id: string }>((resolve) => {
answer = resolve;
});
return { promise: promise as never, answer };
}
/** Lets every already-runnable promise callback run. */
function settle(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 0));
}
function sentColonies(): string[] {
return createInspection.mock.calls.map(([input]) => input.colonyId);
}
function takePhoto(sessionId: string) {
return photos.addDraftPhoto({
sessionId,
ownerUserId: "user-1",
blob: new Blob(["jpeg"], { type: "image/jpeg" }),
date: "2026-10-01",
});
}
describe("syncPendingEntries", () => {
beforeEach(async () => {
// A fresh database and fresh modules: nothing (no open connection, no
// in-flight run) carries over from the previous test.
globalThis.indexedDB = new IDBFactory();
vi.resetModules();
db = await import("./db");
sync = await import("./sync");
photos = await import("@/lib/photos/store");
ApiError = (await import("@/lib/auth/api-client")).ApiError;
createInspection = vi.mocked((await import("@/lib/inspections/api")).createInspection);
createTreatment = vi.mocked((await import("@/lib/treatments/api")).createTreatment);
uploadDocument = vi.mocked((await import("@/lib/documents/api")).uploadDocument);
// The mocks themselves outlive resetModules - start them from scratch too.
createInspection.mockReset();
createTreatment.mockReset();
uploadDocument.mockReset();
photoHandOver.calls.length = 0;
photoHandOver.failFor = null;
photoHandOver.isQueued = async (key) =>
(await db.getPendingEntries("pending-inspections")).some(
(entry) => entry.idempotencyKey === key,
);
});
it("syncs every queued entry and removes it from the queue", async () => {
mockedGetPending.mockResolvedValue([pendingEntry(1), pendingEntry(2)]);
mockedCreateInspection.mockResolvedValue({ id: "x" } as never);
await db.queueInspection(inspection(1), "key-1", "user-1");
await db.queueInspection(inspection(2), "key-2", "user-1");
createInspection.mockResolvedValue({ id: "x" } as never);
const result = await syncPendingInspections();
const result = await sync.syncPendingEntries("user-1");
expect(result).toEqual({ synced: 2, failed: 0 });
expect(mockedRemovePending).toHaveBeenCalledWith(1);
expect(mockedRemovePending).toHaveBeenCalledWith(2);
expect(result).toEqual({ synced: 2, failed: 0, skippedForeign: 0 });
expect(await db.getPendingEntries("pending-inspections")).toEqual([]);
});
it("resends a queued entry with the idempotency key it was queued with", async () => {
// The server may already have saved the first attempt; the same key lets
// it answer with that record instead of storing a duplicate.
mockedGetPending.mockResolvedValue([
{ ...pendingEntry(1), idempotencyKey: "key-from-the-form-0001" },
pendingEntry(2),
]);
mockedCreateInspection.mockResolvedValue({ id: "x" } as never);
await db.queueInspection(inspection(1), "key-from-the-form-0001", "user-1");
createInspection.mockResolvedValue({ id: "x" } as never);
await syncPendingInspections();
await sync.syncPendingEntries("user-1");
expect(mockedCreateInspection).toHaveBeenNthCalledWith(1, pendingEntry(1).input, {
expect(createInspection).toHaveBeenCalledWith(inspection(1), {
idempotencyKey: "key-from-the-form-0001",
});
// Entries queued before keys existed still sync, just without one.
expect(mockedCreateInspection).toHaveBeenNthCalledWith(2, pendingEntry(2).input, {
idempotencyKey: undefined,
});
});
it("keeps a rejected entry queued and continues with the rest on a server-side ApiError", async () => {
mockedGetPending.mockResolvedValue([pendingEntry(1), pendingEntry(2)]);
mockedCreateInspection
it("still sends entries queued before keys and owners existed", async () => {
await db.summarizeQueue("user-1"); // creates the database at the current version
const raw = await openDB("apicore-offline");
await raw.add("pending-inspections", {
input: inspection(1),
queuedAt: "2026-10-01T08:00:00.000Z",
});
raw.close();
createInspection.mockResolvedValue({ id: "x" } as never);
const result = await sync.syncPendingEntries("user-1");
expect(result).toEqual({ synced: 1, failed: 0, skippedForeign: 0 });
expect(createInspection).toHaveBeenCalledWith(inspection(1), { idempotencyKey: undefined });
});
it("stores the server rejection on the entry and keeps it", async () => {
await db.queueInspection(inspection(1), "key-1", "user-1");
const body = { statusCode: 400, message: "Volk V-03 ist beendet." };
createInspection.mockRejectedValue(new ApiError(400, "Volk V-03 ist beendet.", body));
const result = await sync.syncPendingEntries("user-1");
expect(result).toEqual({ synced: 0, failed: 1, skippedForeign: 0 });
const [kept] = await db.getPendingEntries("pending-inspections");
expect(kept.idempotencyKey).toBe("key-1");
expect(kept.lastError).toEqual({
status: 400,
message: "Volk V-03 ist beendet.",
details: body,
at: expect.any(String),
});
expect(kept.lastError?.status).toBe(400);
});
it("continues with the rest of the queue after a server rejection", async () => {
await db.queueInspection(inspection(1), "key-1", "user-1");
await db.queueInspection(inspection(2), "key-2", "user-1");
createInspection
.mockRejectedValueOnce(new ApiError(400, "invalid colony"))
.mockResolvedValueOnce({ id: "x" } as never);
const result = await syncPendingInspections();
const result = await sync.syncPendingEntries("user-1");
expect(result).toEqual({ synced: 1, failed: 1 });
expect(mockedRemovePending).not.toHaveBeenCalledWith(1);
expect(mockedRemovePending).toHaveBeenCalledWith(2);
expect(result).toEqual({ synced: 1, failed: 1, skippedForeign: 0 });
const remaining = await db.getPendingEntries("pending-inspections");
expect(remaining.map((entry) => entry.idempotencyKey)).toEqual(["key-1"]);
});
it("stops immediately on a network failure, leaving the rest queued", async () => {
mockedGetPending.mockResolvedValue([pendingEntry(1), pendingEntry(2)]);
mockedCreateInspection.mockRejectedValueOnce(new TypeError("Failed to fetch"));
it("stops immediately on a network failure, leaving the rest queued untouched", async () => {
await db.queueInspection(inspection(1), "key-1", "user-1");
await db.queueInspection(inspection(2), "key-2", "user-1");
createInspection.mockRejectedValueOnce(new TypeError("Failed to fetch"));
const result = await syncPendingInspections();
const result = await sync.syncPendingEntries("user-1");
expect(result).toEqual({ synced: 0, failed: 1 });
expect(mockedCreateInspection).toHaveBeenCalledTimes(1);
expect(mockedRemovePending).not.toHaveBeenCalled();
expect(result).toEqual({ synced: 0, failed: 1, skippedForeign: 0 });
expect(createInspection).toHaveBeenCalledTimes(1);
const remaining = await db.getPendingEntries("pending-inspections");
expect(remaining).toHaveLength(2);
expect(remaining.every((entry) => entry.lastError === undefined)).toBe(true);
});
it("never sends entries queued by another user", async () => {
await db.queueInspection(inspection(1), "key-1", "user-2");
createInspection.mockResolvedValue({ id: "x" } as never);
const result = await sync.syncPendingEntries("user-1");
expect(result).toEqual({ synced: 0, failed: 0, skippedForeign: 1 });
expect(createInspection).not.toHaveBeenCalled();
expect(await db.getPendingEntries("pending-inspections")).toHaveLength(1);
});
it("shares a single in-flight run across concurrent callers instead of double-processing the queue", async () => {
mockedGetPending.mockResolvedValue([pendingEntry(1)]);
mockedCreateInspection.mockResolvedValue({ id: "x" } as never);
await db.queueInspection(inspection(1), "key-1", "user-1");
createInspection.mockResolvedValue({ id: "x" } as never);
const [first, second] = await Promise.all([syncPendingInspections(), syncPendingInspections()]);
const [first, second] = await Promise.all([
sync.syncPendingEntries("user-1"),
sync.syncPendingEntries("user-1"),
]);
expect(first).toBe(second);
expect(mockedGetPending).toHaveBeenCalledTimes(1);
expect(mockedCreateInspection).toHaveBeenCalledTimes(1);
expect(createInspection).toHaveBeenCalledTimes(1);
});
it("never sends a user's entries after cancelSync() during a running sync", async () => {
await db.queueInspection(inspection(1), "key-1", "user-1");
await db.queueInspection(inspection(2), "key-2", "user-1");
const firstRequest = openRequest();
createInspection.mockReturnValueOnce(firstRequest.promise).mockResolvedValue({ id: "x" } as never);
const run = sync.syncPendingEntries("user-1");
await vi.waitFor(() => expect(createInspection).toHaveBeenCalledTimes(1));
// Logout: stop the run before the access token changes.
const cancelled = sync.cancelSync();
firstRequest.answer({ id: "saved-1" });
await cancelled;
// The request already on its way is finished properly, nothing after it is sent.
expect(await run).toEqual({ synced: 1, failed: 0, skippedForeign: 0 });
expect(sentColonies()).toEqual(["colony-1"]);
const remaining = await db.getPendingEntries("pending-inspections");
expect(remaining.map((entry) => entry.idempotencyKey)).toEqual(["key-2"]);
});
it("starts another user's run only after the current one finished", async () => {
await db.queueInspection(inspection(1), "key-a1", "user-a");
await db.queueInspection(inspection(2), "key-b1", "user-b");
const firstRequest = openRequest();
createInspection.mockReturnValueOnce(firstRequest.promise).mockResolvedValue({ id: "x" } as never);
const runA = sync.syncPendingEntries("user-a");
await vi.waitFor(() => expect(createInspection).toHaveBeenCalledTimes(1));
const runB = sync.syncPendingEntries("user-b");
await settle();
expect(createInspection).toHaveBeenCalledTimes(1);
firstRequest.answer({ id: "saved-a1" });
expect(await runA).toEqual({ synced: 1, failed: 0, skippedForeign: 1 });
expect(await runB).toEqual({ synced: 1, failed: 0, skippedForeign: 0 });
expect(sentColonies()).toEqual(["colony-1", "colony-2"]);
});
it("after a logout and the next login, never sends the first user's entries", async () => {
await db.queueInspection(inspection(1), "key-a1", "user-a");
await db.queueInspection(inspection(2), "key-a2", "user-a");
await db.queueInspection(inspection(3), "key-b1", "user-b");
const firstRequest = openRequest();
createInspection.mockReturnValueOnce(firstRequest.promise).mockResolvedValue({ id: "x" } as never);
const runA = sync.syncPendingEntries("user-a");
await vi.waitFor(() => expect(createInspection).toHaveBeenCalledTimes(1));
// A signs out (the cancel is awaited before the token changes) ...
const cancelled = sync.cancelSync();
firstRequest.answer({ id: "saved-a1" });
await cancelled;
// ... and B signs in on the same device: B's token is set, sync opens again.
sync.resumeSync();
const runB = await sync.syncPendingEntries("user-b");
expect(await runA).toEqual({ synced: 1, failed: 0, skippedForeign: 1 });
expect(runB).toEqual({ synced: 1, failed: 0, skippedForeign: 1 });
expect(sentColonies()).toEqual(["colony-1", "colony-3"]);
const remaining = await db.getPendingEntries("pending-inspections");
expect(remaining.map((entry) => entry.idempotencyKey)).toEqual(["key-a2"]);
});
it("sends nothing between cancelSync() and resumeSync(), not even for the same user", async () => {
await db.queueInspection(inspection(1), "key-1", "user-1");
await db.queueInspection(inspection(2), "key-2", "user-1");
const firstRequest = openRequest();
createInspection.mockReturnValueOnce(firstRequest.promise).mockResolvedValue({ id: "x" } as never);
void sync.syncPendingEntries("user-1");
await vi.waitFor(() => expect(createInspection).toHaveBeenCalledTimes(1));
const cancelled = sync.cancelSync();
// E.g. an `online` event while the logout still waits for the cancel.
const late = sync.syncPendingEntries("user-1");
firstRequest.answer({ id: "saved-1" });
await cancelled;
expect(await late).toEqual({ synced: 0, failed: 0, skippedForeign: 0 });
expect(await sync.syncPendingEntries("user-1")).toEqual({
synced: 0,
failed: 0,
skippedForeign: 0,
});
expect(sentColonies()).toEqual(["colony-1"]);
sync.resumeSync();
expect(await sync.syncPendingEntries("user-1")).toEqual({
synced: 1,
failed: 0,
skippedForeign: 0,
});
expect(sentColonies()).toEqual(["colony-1", "colony-2"]);
});
it("re-checks each entry right before sending it", async () => {
await db.queueInspection(inspection(1), "key-1", "user-1");
await db.queueInspection(inspection(2), "key-2", "user-1");
await db.queueInspection(inspection(3), "key-3", "user-1");
const firstRequest = openRequest();
createInspection.mockReturnValueOnce(firstRequest.promise).mockResolvedValue({ id: "x" } as never);
const run = sync.syncPendingEntries("user-1");
await vi.waitFor(() => expect(createInspection).toHaveBeenCalledTimes(1));
// While the first request is open: the second entry is discarded in the
// queue sheet, and the third no longer belongs to this user.
const [, second, third] = await db.getPendingEntries("pending-inspections");
await db.discardQueueItem({
store: "pending-inspections",
id: second.id,
label: "Durchsicht",
queuedAt: second.queuedAt,
});
const raw = await openDB("apicore-offline");
await raw.put("pending-inspections", { ...third, ownerUserId: "user-2" });
raw.close();
firstRequest.answer({ id: "saved-1" });
expect(await run).toEqual({ synced: 1, failed: 0, skippedForeign: 1 });
expect(sentColonies()).toEqual(["colony-1"]);
});
it("uploads photos only after their inspection synced in the same run", async () => {
await db.queueInspection(inspection(1), "inspection-key", "user-1");
await db.queueTreatment(
{ colonyId: "colony-1", date: "2026-10-01T00:00:00.000Z", product: "Oxuvar" },
"treatment-key",
"user-1",
);
const photo = await takePhoto("session-1");
await photos.attachSessionToQueuedInspection("session-1", "inspection-key");
createInspection.mockResolvedValue({ id: "srv-1" } as never);
const treatmentRequest = openRequest();
createTreatment.mockReturnValueOnce(treatmentRequest.promise);
uploadDocument.mockResolvedValue({ id: "doc-1" } as never);
const run = sync.syncPendingEntries("user-1");
await vi.waitFor(() => expect(createTreatment).toHaveBeenCalledTimes(1));
await vi.waitFor(async () =>
expect(await db.getPendingEntries("pending-inspections")).toEqual([]),
);
// The inspection is on the server, another record drain still runs: no photo yet.
await settle();
expect(uploadDocument).not.toHaveBeenCalled();
treatmentRequest.answer({ id: "srv-t1" });
expect(await run).toEqual({ synced: 3, failed: 0, skippedForeign: 0 });
// The photos got the server id while the inspection was still queued.
expect(photoHandOver.calls).toEqual([
{ key: "inspection-key", inspectionId: "srv-1", entryStillQueued: true },
]);
expect(uploadDocument).toHaveBeenCalledTimes(1);
expect(uploadDocument).toHaveBeenCalledWith("INSPECTION", "srv-1", expect.anything(), {
idempotencyKey: photo.uploadIdempotencyKey,
fileName: photo.fileName,
});
expect(await photos.listSessionPhotos("session-1")).toEqual([]);
});
it("keeps an inspection whose photo hand-over failed and still finishes the run", async () => {
await db.queueInspection(inspection(1), "key-1", "user-1");
await db.queueInspection(inspection(2), "key-2", "user-1");
const photo = await takePhoto("session-1");
await photos.attachSessionToQueuedInspection("session-1", "key-1");
createInspection
.mockResolvedValueOnce({ id: "srv-1" } as never)
.mockResolvedValueOnce({ id: "srv-2" } as never);
photoHandOver.failFor = "key-1";
const first = await sync.syncPendingEntries("user-1");
expect(first).toEqual({ synced: 1, failed: 1, skippedForeign: 0 });
const [kept] = await db.getPendingEntries("pending-inspections");
expect(kept.idempotencyKey).toBe("key-1");
expect(kept.lastError).toBeUndefined();
expect(uploadDocument).not.toHaveBeenCalled();
// Next run: the same key brings back the same server record, the photo follows it.
photoHandOver.failFor = null;
createInspection.mockResolvedValue({ id: "srv-1" } as never);
uploadDocument.mockResolvedValue({ id: "doc-1" } as never);
const second = await sync.syncPendingEntries("user-1");
expect(second).toEqual({ synced: 2, failed: 0, skippedForeign: 0 });
expect(createInspection).toHaveBeenLastCalledWith(inspection(1), { idempotencyKey: "key-1" });
expect(uploadDocument).toHaveBeenCalledWith(
"INSPECTION",
"srv-1",
expect.anything(),
expect.objectContaining({ idempotencyKey: photo.uploadIdempotencyKey }),
);
expect(await db.getPendingEntries("pending-inspections")).toEqual([]);
});
it("never uploads photos after cancelSync()", async () => {
await db.queueInspection(inspection(1), "inspection-key", "user-1");
await takePhoto("session-1");
await photos.attachSessionToQueuedInspection("session-1", "inspection-key");
const request = openRequest();
createInspection.mockReturnValueOnce(request.promise);
uploadDocument.mockResolvedValue({ id: "doc-1" } as never);
const run = sync.syncPendingEntries("user-1");
await vi.waitFor(() => expect(createInspection).toHaveBeenCalledTimes(1));
const cancelled = sync.cancelSync();
request.answer({ id: "srv-1" });
await cancelled;
expect(await run).toEqual({ synced: 1, failed: 0, skippedForeign: 0 });
expect(uploadDocument).not.toHaveBeenCalled();
const [photo] = await photos.listSessionPhotos("session-1");
expect(photo).toMatchObject({ state: "ready", inspectionId: "srv-1" });
});
});