Files
ApiCore/ApiCore-Frontend/src/lib/inspections/use-last-inspection.spec.tsx
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

91 lines
3.3 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import type { ReactNode } from "react";
import { fetchColonyInspections } from "./api";
import { useLastInspection } from "./use-last-inspection";
import { listPacks } from "@/lib/apiaries/offline-pack";
import {
offlinePack,
packColony,
packInspection,
storedPack,
} from "@/lib/apiaries/offline-pack.fixtures";
import type { Inspection } from "./types";
vi.mock("./api", () => ({ fetchColonyInspections: vi.fn() }));
vi.mock("@/lib/auth/auth-context", () => ({ useCurrentUserId: () => "user-1" }));
vi.mock("@/lib/apiaries/offline-pack", async (importOriginal) => ({
...(await importOriginal<typeof import("@/lib/apiaries/offline-pack")>()),
listPacks: vi.fn(),
}));
function day(isoDay: string): string {
return `${isoDay}T00:00:00.000Z`;
}
function renderLastInspection(client: QueryClient) {
const wrapper = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={client}>{children}</QueryClientProvider>
);
return renderHook(() => useLastInspection("colony-1"), { wrapper });
}
function newClient() {
return new QueryClient({ defaultOptions: { queries: { retry: false } } });
}
describe("useLastInspection", () => {
beforeEach(() => {
vi.mocked(fetchColonyInspections).mockReset();
vi.mocked(listPacks).mockReset().mockResolvedValue([]);
});
it("takes the newest inspection the server has", async () => {
vi.mocked(fetchColonyInspections).mockResolvedValue([
{ ...packInspection("old", "colony-1", day("2026-09-01")), userId: "u" },
{ ...packInspection("new", "colony-1", day("2026-10-01")), userId: "u" },
]);
const { result } = renderLastInspection(newClient());
await waitFor(() => expect(result.current.inspection?.id).toBe("new"));
expect(listPacks).not.toHaveBeenCalled();
});
it("reads the last inspection from the loaded pack while offline", async () => {
vi.mocked(fetchColonyInspections).mockRejectedValue(new TypeError("Failed to fetch"));
vi.mocked(listPacks).mockResolvedValue([
storedPack(
offlinePack({
colonies: [packColony("colony-1", "A1")],
inspections: [
packInspection("p-old", "colony-1", day("2026-09-20")),
packInspection("p-new", "colony-1", day("2026-10-02")),
packInspection("other", "colony-2", day("2026-10-05")),
],
}),
),
]);
const { result } = renderLastInspection(newClient());
await waitFor(() => expect(result.current.inspection?.id).toBe("p-new"));
expect(result.current.isLoading).toBe(false);
});
it("shows the colony page's inspections at once while it asks the server again", async () => {
vi.mocked(fetchColonyInspections).mockReturnValue(new Promise<Inspection[]>(() => undefined));
const client = newClient();
client.setQueryData<Inspection[]>(
["colonies", "colony-1", "inspections"],
[{ ...packInspection("cached", "colony-1", day("2026-10-01")), userId: "u" }],
);
const { result } = renderLastInspection(client);
expect(result.current.inspection?.id).toBe("cached");
expect(result.current.isLoading).toBe(false);
});
});