import "fake-indexeddb/auto"; import { IDBFactory } from "fake-indexeddb"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; vi.mock("@/lib/auth/auth-context", () => ({ useCurrentUserId: () => "user-1" })); type DbModule = typeof import("@/lib/offline/db"); let db: DbModule; let HivePendingNote: typeof import("./hive-pending-note").HivePendingNote; function renderNote() { const client = new QueryClient({ defaultOptions: { queries: { retry: false } } }); return render( , ); } const change = (colonyId: string) => ({ colonyId, occurredOn: "2026-10-15", source: "CARD" as const, ops: [{ op: "setLabel" as const, id: "b", label: "x" }], }); describe("HivePendingNote", () => { beforeEach(async () => { globalThis.indexedDB = new IDBFactory(); vi.resetModules(); db = await import("@/lib/offline/db"); HivePendingNote = (await import("./hive-pending-note")).HivePendingNote; }); it("says nothing while no change waits", async () => { await db.queueHiveChange(change("colony-2"), "k0", "user-1"); renderNote(); await new Promise((resolve) => setTimeout(resolve, 20)); expect(screen.queryByRole("status")).not.toBeInTheDocument(); }); it("names one waiting change", async () => { await db.queueHiveChange(change("colony-1"), "k1", "user-1"); renderNote(); expect(await screen.findByText("1 Änderung wartet auf Netz")).toBeInTheDocument(); }); it("counts a queued inspection with a hive change too", async () => { await db.queueHiveChange(change("colony-1"), "k1", "user-1"); await db.queueInspection( { colonyId: "colony-1", date: "2026-10-15T00:00:00.000Z", hiveChange: { ops: change("x").ops } }, "k2", "user-1", ); renderNote(); expect(await screen.findByText("2 Änderungen warten auf Netz")).toBeInTheDocument(); }); it("points to the queue when a change was refused, and opens the queue sheet", async () => { await db.queueHiveChange(change("colony-1"), "k1", "user-1"); const [entry] = await db.getPendingEntries("pending-hive-changes"); await db.markFailed("pending-hive-changes", entry.id, { status: 409, message: "Konflikt", details: { code: "HIVE_CONFLICT", reason: "MISSING_ID: x" }, at: "2026-10-15T10:00:00.000Z", }); renderNote(); expect(await screen.findByText(/Eine Änderung an dieser Beute wurde abgelehnt/)).toBeInTheDocument(); expect(screen.queryByText(/wartet auf Netz/)).not.toBeInTheDocument(); await userEvent.click(screen.getByRole("button", { name: "Warteschlange öffnen" })); expect(await screen.findByRole("dialog", { name: "Wartende Einträge" })).toBeInTheDocument(); }); });