Introduce adapter injection and migrate test suite
All checks were successful
CI / check (push) Successful in 2m13s
CI / build-image (push) Has been skipped

Replace direct adapter/persistence imports with context-based injection
(AdapterContext + useAdapters) so tests use in-memory implementations
instead of vi.mock. Migrate component tests from context mocking to
AllProviders with real hooks. Extract export/import logic from ActionBar
into useEncounterExportImport hook. Add bestiary-cache and
bestiary-index-adapter test suites. Raise adapter coverage thresholds
(68→80 lines, 56→62 branches).

77 test files, 891 tests, all passing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lukas
2026-04-01 23:55:45 +02:00
parent 228c1c667f
commit 2c643cc98b
42 changed files with 1879 additions and 1190 deletions

View File

@@ -4,6 +4,8 @@ import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it, vi } from "vitest";
import { createTestAdapters } from "../../__tests__/adapters/in-memory-adapters.js";
import { AdapterProvider } from "../../contexts/adapter-context.js";
import { SourceFetchPrompt } from "../source-fetch-prompt.js";
const MONSTER_MANUAL_REGEX = /Monster Manual/;
@@ -13,6 +15,9 @@ afterEach(cleanup);
const mockFetchAndCacheSource = vi.fn();
const mockUploadAndCacheSource = vi.fn();
// Uses context mock because fetchAndCacheSource/uploadAndCacheSource involve
// real fetch() calls. The test controls success/failure to verify the
// component's loading and error UI, not the fetching logic itself.
vi.mock("../../contexts/bestiary-context.js", () => ({
useBestiaryContext: () => ({
fetchAndCacheSource: mockFetchAndCacheSource,
@@ -20,22 +25,23 @@ vi.mock("../../contexts/bestiary-context.js", () => ({
}),
}));
vi.mock("../../adapters/bestiary-index-adapter.js", () => ({
getDefaultFetchUrl: (code: string) =>
`https://example.com/bestiary/${code}.json`,
getSourceDisplayName: (code: string) =>
code === "MM" ? "Monster Manual" : code,
loadBestiaryIndex: () => ({ sources: {}, creatures: [] }),
getAllSourceCodes: () => [],
}));
function renderPrompt(sourceCode = "MM") {
const onSourceLoaded = vi.fn();
const adapters = createTestAdapters();
adapters.bestiaryIndex = {
...adapters.bestiaryIndex,
getDefaultFetchUrl: (code: string) =>
`https://example.com/bestiary/${code}.json`,
getSourceDisplayName: (code: string) =>
code === "MM" ? "Monster Manual" : code,
};
const result = render(
<SourceFetchPrompt
sourceCode={sourceCode}
onSourceLoaded={onSourceLoaded}
/>,
<AdapterProvider adapters={adapters}>
<SourceFetchPrompt
sourceCode={sourceCode}
onSourceLoaded={onSourceLoaded}
/>
</AdapterProvider>,
);
return { ...result, onSourceLoaded };
}