Team Ai
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 21d agoView on Hugging Face
0likes1.2kdownloads
vitest-setup-client.ts85 linesDownload Raw Back to ui
1/// <reference types="@vitest/browser/matchers" />2/// <reference types="@vitest/browser/providers/playwright" />3 4import { beforeEach, vi } from 'vitest';5 6// Mock fetch for API calls during client tests.7// In test environment there is no backend server, so we intercept8// the specific endpoints the app uses and return valid mock data.9// The passthrough target is captured once at module load: capturing it10// inside beforeEach grabs the previous test's spy (vi.spyOn returns the11// existing spy), making the default branch recurse on itself as soon as12// a test fetches a URL outside the mocked set.13const originalFetch = globalThis.fetch.bind(globalThis);14 15beforeEach(() => {16	vi.spyOn(globalThis, 'fetch').mockImplementation(17		async (input: RequestInfo | URL, init?: RequestInit) => {18			const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;19 20			// Mock server props endpoint21			if (url.includes('/server')) {22				return new Response(23					JSON.stringify({24						git_branch: 'test',25						git_commit: 'test',26						mode: 'router',27						version: 'test'28					}),29					{ headers: { 'Content-Type': 'application/json' }, status: 200 }30				);31			}32 33			// Mock models list endpoint34			if (/\/v1\/models|\/models\b/.test(url)) {35				return new Response(36					JSON.stringify({37						data: [38							{39								created: 0,40								id: 'test-model.gguf',41								in_cache: false,42								meta: {},43								object: 'model',44								owned_by: 'llamacpp',45								path: 'models/test-model.gguf',46								status: { value: 'unloaded' }47							}48						],49						models: [50							{51								details: {},52								model: 'test-model.gguf',53								name: 'Test Model'54							}55						],56						object: 'list'57					}),58					{ headers: { 'Content-Type': 'application/json' }, status: 200 }59				);60			}61 62			// Mock /props endpoint (used for modalities)63			if (url.includes('/props')) {64				return new Response(65					JSON.stringify({66						default_generation_settings: { n_ctx: 2048 }67					}),68					{ headers: { 'Content-Type': 'application/json' }, status: 200 }69				);70			}71 72			// Mock /tools endpoint (used for built-in tools list)73			if (url.includes('/tools')) {74				return new Response(JSON.stringify([]), {75					headers: { 'Content-Type': 'application/json' },76					status: 20077				});78			}79 80			// Default: use real fetch81			return originalFetch(input, init);82		}83	);84});85