feat: offline-fähige PWA mit IndexedDB-Sync für Durchsichten
Service Worker (public/sw.js) cached die App-Shell für Seiten-GETs (network-first, cache-fallback). Durchsichten, die offline gespeichert werden, landen in einer IndexedDB-Warteschlange (idb) statt zu scheitern und werden automatisch synchronisiert, sobald wieder Netzwerk verfügbar ist (Mount + online-Event), mit manuellem Retry über ein Badge im App-Header. Ein modulweiter In-Flight-Lock in sync.ts verhindert doppelte Synchronisierung bei gleichzeitigen Aufrufen (gefunden über React-StrictMode-Doppel-Mount, das ohne den Lock dieselbe Durchsicht zweimal anlegte).
This commit is contained in:
1 parent
20239885b4
commit
eb66c4bd53
11 files changed
+319
-2
No files matched your search
@@ -0,0 +1,78 @@
|
||||
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";
|
||||
|
||||
vi.mock("@/lib/inspections/api", () => ({
|
||||
createInspection: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("./db", () => ({
|
||||
getPendingInspections: vi.fn(),
|
||||
removePendingInspection: vi.fn(),
|
||||
}));
|
||||
|
||||
const mockedCreateInspection = vi.mocked(createInspection);
|
||||
const mockedGetPending = vi.mocked(getPendingInspections);
|
||||
const mockedRemovePending = vi.mocked(removePendingInspection);
|
||||
|
||||
function pendingEntry(id: number) {
|
||||
return {
|
||||
id,
|
||||
input: { colonyId: `colony-${id}`, date: "2026-01-01T00:00:00.000Z" },
|
||||
queuedAt: "2026-01-01T00:00:00.000Z",
|
||||
};
|
||||
}
|
||||
|
||||
describe("syncPendingInspections", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it("syncs every queued entry and removes it from the queue", async () => {
|
||||
mockedGetPending.mockResolvedValue([pendingEntry(1), pendingEntry(2)]);
|
||||
mockedCreateInspection.mockResolvedValue({ id: "x" } as never);
|
||||
|
||||
const result = await syncPendingInspections();
|
||||
|
||||
expect(result).toEqual({ synced: 2, failed: 0 });
|
||||
expect(mockedRemovePending).toHaveBeenCalledWith(1);
|
||||
expect(mockedRemovePending).toHaveBeenCalledWith(2);
|
||||
});
|
||||
|
||||
it("keeps a rejected entry queued and continues with the rest on a server-side ApiError", async () => {
|
||||
mockedGetPending.mockResolvedValue([pendingEntry(1), pendingEntry(2)]);
|
||||
mockedCreateInspection
|
||||
.mockRejectedValueOnce(new ApiError(400, "invalid colony"))
|
||||
.mockResolvedValueOnce({ id: "x" } as never);
|
||||
|
||||
const result = await syncPendingInspections();
|
||||
|
||||
expect(result).toEqual({ synced: 1, failed: 1 });
|
||||
expect(mockedRemovePending).not.toHaveBeenCalledWith(1);
|
||||
expect(mockedRemovePending).toHaveBeenCalledWith(2);
|
||||
});
|
||||
|
||||
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"));
|
||||
|
||||
const result = await syncPendingInspections();
|
||||
|
||||
expect(result).toEqual({ synced: 0, failed: 1 });
|
||||
expect(mockedCreateInspection).toHaveBeenCalledTimes(1);
|
||||
expect(mockedRemovePending).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
const [first, second] = await Promise.all([syncPendingInspections(), syncPendingInspections()]);
|
||||
|
||||
expect(first).toBe(second);
|
||||
expect(mockedGetPending).toHaveBeenCalledTimes(1);
|
||||
expect(mockedCreateInspection).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user