import type { ConceptPlan, ConceptSearchParams, ConceptSearchResult, ConceptTreeParams, ConceptUsageParams, PublishedConceptsParams, SemanticRecommendationsResult, TaxonomyDataPort, WatchResult, } from '../core/ports' import type {ConceptRecommendation, DocumentConcepts} from '../types' /** Fixtures + canned responses for a {@link createFakeDataPort}. */ export interface FakeDataPortConfig { tree?: DocumentConcepts | null loading?: boolean error?: Error | null recommendations?: ConceptRecommendation[] recsError?: string | null /** Concept ids the fake reports as published. `undefined` stands for "not loaded yet". */ publishedIds?: string[] /** Candidates the insert picker's search returns, before the fake applies `excludeIds`. */ searchResults?: ConceptSearchResult[] /** * Tagged-resource counts, keyed by concept id. `undefined` stands for "not * loaded yet"; an explicit `{}` is a loaded, wholly untagged scheme — the * distinction the governance view draws between a spinner and a run of zeros. */ usage?: Record } /** A {@link TaxonomyDataPort} that also exposes recorded interactions for assertions. */ export interface FakeDataPort extends TaxonomyDataPort { /** Plans passed to the applyConceptPlan function, in call order. */ readonly appliedPlans: ConceptPlan[] /** Number of `triggerSearch()` calls. */ triggeredSearches: number /** The `schemeId` passed to the most recent `triggerSearch()` call. */ lastSearchSchemeId: string | undefined /** Params from the most recent `useWatchTree` call. */ lastWatchParams: ConceptTreeParams | null /** Params from the most recent `useWatchPublishedConcepts` call. */ lastPublishedParams: PublishedConceptsParams | null /** Params from the most recent `useWatchConceptUsage` call. */ lastUsageParams: ConceptUsageParams | null /** Params from the most recent `useSearchConcepts` call. */ lastSearchParams: ConceptSearchParams | null } /** * In-memory `TaxonomyDataPort` for component/tree tests. Returns the configured * fixtures synchronously (no Studio, no network) and records mutations and * search triggers for assertions. The hook members call no React hooks, so they * return stable references and never suspend. Provide via: * ``. */ export function createFakeDataPort(config: FakeDataPortConfig = {}): FakeDataPort { const watchResult: WatchResult = { data: config.tree ?? null, loading: config.loading ?? false, error: config.error ?? null, } const recsResult: SemanticRecommendationsResult = { conceptRecs: config.recommendations ?? [], recsError: config.recsError ?? null, triggerSearch: (schemeId?: string) => { fake.triggeredSearches += 1 fake.lastSearchSchemeId = schemeId }, } const publishedResult: WatchResult> = { data: config.publishedIds ? new Set(config.publishedIds) : null, loading: config.loading ?? false, error: config.error ?? null, } const usageResult: WatchResult> = { data: config.usage ? new Map(Object.entries(config.usage)) : null, loading: config.loading ?? false, error: config.error ?? null, } // The real query excludes the scheme's members in GROQ; the fake applies the // same exclusion here, so a test can assert the picker never offers one. const search = (params: ConceptSearchParams): Promise => { fake.lastSearchParams = params const matches = (config.searchResults ?? []).filter( (result) => !params.excludeIds.includes(result.id), ) return Promise.resolve(matches) } const apply = (plan: ConceptPlan): Promise => { fake.appliedPlans.push(plan) return Promise.resolve() } const fake: FakeDataPort = { appliedPlans: [], triggeredSearches: 0, lastSearchSchemeId: undefined, lastWatchParams: null, lastPublishedParams: null, lastUsageParams: null, lastSearchParams: null, useWatchTree: (params: ConceptTreeParams) => { fake.lastWatchParams = params return watchResult }, useWatchPublishedConcepts: (params: PublishedConceptsParams) => { fake.lastPublishedParams = params return publishedResult }, useWatchConceptUsage: (params: ConceptUsageParams) => { fake.lastUsageParams = params return usageResult }, useSearchConcepts: () => search, useApplyConceptPlan: () => apply, useSemanticRecommendations: () => recsResult, } return fake }