Files
ApiCore/ApiCore-Frontend/src/lib/offline/pack-fallback.spec.ts
T
Nick SzaunigandClaude Opus 5.5 cdd30cf3ee feat: platform backbone and field capture complete (checkpoint)
Platform backbone (plan 2026-10-13-plattform-rueckgrat), all tasks reviewed:
- webhooks with delivery table, retries and stable delivery id
- calendar handlers; a finished task removes its Outlook event
- tasks, automations, inspections, varroa, treatments, feedings,
  harvests, colonies, wintering, stock and bottling record domain events
  in the same transaction; legacy post-commit calls removed
- row locks through one helper (lockRowForUpdate, lockRowsForShare,
  lockRowsForUpdate)
- job framework with JobRun, cli exit codes, cleanup, task templates,
  daily evaluation, hourly app-backup and the rekey job
- system API and page "Verwaltung > System" with retry
- @nestjs/schedule removed, guard tests against in-process schedulers

Field capture T3 (plan 2026-10-13-t3-erfassung-am-stand), all tasks reviewed:
- inspection photo offline without metadata
- bulk capture for treatments, feedings and varroa (backend + frontend)
- offline stand mode with service worker, offline pages and packs
- offline session, cold start and logout on shared devices

Docs: ARCHITECTURE.md assumptions and sections updated, TASKS.md.
Gates: backend lint, typecheck, 1770 unit, 420 e2e, build, no schema
drift; frontend lint, typecheck, 913 tests, build. Not rolled out.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-10-14 11:36:38 +02:00

74 lines
2.6 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { ApiError } from "@/lib/auth/api-client";
import { listPacks } from "@/lib/apiaries/offline-pack";
import { offlinePack, storedPack } from "@/lib/apiaries/offline-pack.fixtures";
import { withPackFallback } from "./pack-fallback";
vi.mock("@/lib/apiaries/offline-pack", () => ({ listPacks: vi.fn() }));
const PACKS = [storedPack(offlinePack({ apiaryId: "apiary-1", apiaryName: "Obstwiese" }))];
const apiaryNames = vi.fn((packs: typeof PACKS) => packs.map((pack) => pack.pack.apiary.name));
describe("withPackFallback", () => {
beforeEach(() => {
vi.mocked(listPacks).mockReset().mockResolvedValue(PACKS);
apiaryNames.mockClear();
});
it("answers from the server whenever the server answers", async () => {
const result = await withPackFallback(async () => ["vom Server"], apiaryNames, "user-1");
expect(result).toEqual(["vom Server"]);
expect(listPacks).not.toHaveBeenCalled();
});
it("falls back to the pack on a network error", async () => {
const result = await withPackFallback(
() => Promise.reject(new TypeError("Failed to fetch")),
apiaryNames,
"user-1",
);
expect(result).toEqual(["Obstwiese"]);
expect(listPacks).toHaveBeenCalledWith("user-1");
});
it("falls back to the pack when the request timed out", async () => {
const timeout = new DOMException("Zeitüberschreitung der Anfrage", "TimeoutError");
const result = await withPackFallback(() => Promise.reject(timeout), apiaryNames, "user-1");
expect(result).toEqual(["Obstwiese"]);
});
it("rethrows an ApiError instead of showing pack data", async () => {
for (const status of [403, 404, 500]) {
const rejection = new ApiError(status, "Nein");
await expect(
withPackFallback(() => Promise.reject(rejection), apiaryNames, "user-1"),
).rejects.toBe(rejection);
}
expect(listPacks).not.toHaveBeenCalled();
expect(apiaryNames).not.toHaveBeenCalled();
});
it("rethrows when the pack has nothing", async () => {
const offline = new TypeError("Failed to fetch");
await expect(
withPackFallback(() => Promise.reject(offline), () => null, "user-1"),
).rejects.toBe(offline);
});
it("rethrows the network error when the packs cannot be read", async () => {
const offline = new TypeError("Failed to fetch");
vi.mocked(listPacks).mockRejectedValue(new Error("IndexedDB gesperrt"));
await expect(
withPackFallback(() => Promise.reject(offline), apiaryNames, "user-1"),
).rejects.toBe(offline);
});
});