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:
1 parent
10cbe9bf2d
commit
c01a38835a
340 files changed
+25695
-1808
No files matched your search
@@ -0,0 +1,104 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { getConnectivity, reportRequestOutcome } from "@/lib/offline/connectivity";
|
||||
import {
|
||||
DEFAULT_TIMEOUT_MS,
|
||||
DOWNLOAD_TIMEOUT_MS,
|
||||
NON_IDEMPOTENT_WRITE_TIMEOUT_MS,
|
||||
UPLOAD_TIMEOUT_MS,
|
||||
} from "@/lib/http/fetch-with-timeout";
|
||||
import { expectTimeoutAt, stubHangingFetch } from "@/lib/http/deadline-test-helper";
|
||||
import { setAccessToken } from "./token-store";
|
||||
import { ApiError, apiFetch, apiFetchBlob, apiFetchFormData } from "./api-client";
|
||||
|
||||
vi.mock("../config/runtime-config", () => ({
|
||||
getApiUrl: vi.fn().mockResolvedValue("http://api.test"),
|
||||
}));
|
||||
|
||||
function jsonResponse(status: number, body: unknown): Response {
|
||||
return new Response(JSON.stringify(body), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
describe("api-client", () => {
|
||||
beforeEach(() => {
|
||||
setAccessToken(null);
|
||||
reportRequestOutcome("reached-server");
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("ApiError carries the parsed JSON body", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn().mockResolvedValue(jsonResponse(400, { message: "Ungültig", invalidColonyIds: ["c1"] })),
|
||||
);
|
||||
const error = await apiFetch("/x").catch((e: unknown) => e);
|
||||
expect(error).toBeInstanceOf(ApiError);
|
||||
const apiError = error as ApiError;
|
||||
expect(apiError.status).toBe(400);
|
||||
expect(apiError.message).toBe("Ungültig");
|
||||
expect((apiError.body as { invalidColonyIds: string[] }).invalidColonyIds).toEqual(["c1"]);
|
||||
});
|
||||
|
||||
it("reports network-error on a failed fetch and reached-server on any HTTP answer", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn().mockRejectedValue(new TypeError("Failed to fetch")));
|
||||
await expect(apiFetch("/x")).rejects.toBeInstanceOf(TypeError);
|
||||
expect(getConnectivity()).toBe("offline");
|
||||
|
||||
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(jsonResponse(500, { message: "boom" })));
|
||||
await expect(apiFetch("/x")).rejects.toBeInstanceOf(ApiError);
|
||||
expect(getConnectivity()).toBe("online");
|
||||
});
|
||||
|
||||
describe("timeouts", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
stubHangingFetch();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("apiFetch: GET and keyed writes 4 s, unkeyed writes 15 s", async () => {
|
||||
await expectTimeoutAt(() => apiFetch("/x"), DEFAULT_TIMEOUT_MS);
|
||||
await expectTimeoutAt(
|
||||
() => apiFetch("/x", { method: "POST", headers: { "Idempotency-Key": "k" } }),
|
||||
DEFAULT_TIMEOUT_MS,
|
||||
);
|
||||
await expectTimeoutAt(() => apiFetch("/x", { method: "POST" }), NON_IDEMPOTENT_WRITE_TIMEOUT_MS);
|
||||
});
|
||||
|
||||
it("apiFetchFormData uses 30 s by default and honours timeoutMs", async () => {
|
||||
await expectTimeoutAt(() => apiFetchFormData("/x", new FormData()), UPLOAD_TIMEOUT_MS);
|
||||
await expectTimeoutAt(
|
||||
() => apiFetchFormData("/x", new FormData(), { timeoutMs: 5_000 }),
|
||||
5_000,
|
||||
);
|
||||
});
|
||||
|
||||
it("apiFetchFormData passes options.headers through", async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue(jsonResponse(200, {}));
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
await apiFetchFormData("/x", new FormData(), { headers: { "Idempotency-Key": "k9" } });
|
||||
const init = fetchMock.mock.calls[0][1] as RequestInit;
|
||||
expect(new Headers(init.headers).get("Idempotency-Key")).toBe("k9");
|
||||
});
|
||||
|
||||
it("apiFetchBlob uses 60 s", async () => {
|
||||
await expectTimeoutAt(() => apiFetchBlob("/x", "f.csv"), DOWNLOAD_TIMEOUT_MS);
|
||||
});
|
||||
});
|
||||
|
||||
it("apiFetchBlob builds an ApiError with the parsed body", async () => {
|
||||
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(jsonResponse(403, { message: "Nein", why: 1 })));
|
||||
const error = (await apiFetchBlob("/x", "f.csv").catch((e: unknown) => e)) as ApiError;
|
||||
expect(error).toBeInstanceOf(ApiError);
|
||||
expect(error.status).toBe(403);
|
||||
expect(error.body).toEqual({ message: "Nein", why: 1 });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user