import { AgridCore } from '@/agrid-core' import type { JsonType, AgridCoreOptions, AgridFetchOptions, AgridFetchResponse, AgridFlagsResponse } from '@/types' const version = '2.0.0-alpha' export interface AgridCoreTestClientMocks { fetch: jest.Mock, [string, AgridFetchOptions]> storage: { getItem: jest.Mock setItem: jest.Mock } } export class AgridCoreTestClient extends AgridCore { public _cachedDistinctId?: string constructor( private mocks: AgridCoreTestClientMocks, apiKey: string, options?: AgridCoreOptions ) { super(apiKey, options) this.setupBootstrap(options) } // Expose protected methods for testing public getFlags( distinctId: string, groups: Record = {}, personProperties: Record = {}, groupProperties: Record> = {}, extraPayload: Record = {} ): Promise { return super.getFlags(distinctId, groups, personProperties, groupProperties, extraPayload) } getPersistedProperty(key: string): T { return this.mocks.storage.getItem(key) } setPersistedProperty(key: string, value: T | null): void { return this.mocks.storage.setItem(key, value) } fetch(url: string, options: AgridFetchOptions): Promise { return this.mocks.fetch(url, options) } getLibraryId(): string { return 'agrid-core-tests' } getLibraryVersion(): string { return version } getCustomUserAgent(): string { return 'agrid-core-tests' } } export const createTestClient = ( apiKey: string, options?: AgridCoreOptions, setupMocks?: (mocks: AgridCoreTestClientMocks) => void, storageCache: { [key: string]: string | JsonType } = {} ): [AgridCoreTestClient, AgridCoreTestClientMocks] => { const mocks = { fetch: jest.fn(), storage: { getItem: jest.fn((key) => storageCache[key]), setItem: jest.fn((key, val) => { storageCache[key] = val == null ? undefined : val }), }, } mocks.fetch.mockImplementation(() => Promise.resolve({ status: 200, text: () => Promise.resolve('ok'), json: () => Promise.resolve({ status: 'ok' }), }) ) setupMocks?.(mocks) return [new AgridCoreTestClient(mocks, apiKey, { disableCompression: true, ...options }), mocks] }