Team Ai
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 21d agoView on Hugging Face
0likes1.2kdownloads
request-helpers.test.ts130 linesDownload Raw Back to unit
1import {2	extractJsonRpcMethods,3	formatDiagnosticErrorMessage,4	getRequestBody,5	getRequestMethod,6	getRequestUrl,7	summarizeRequestBody8} from '$lib/utils/request-helpers';9import { describe, expect, it } from 'vitest';10 11describe('getRequestUrl', () => {12	it('returns a plain string input as-is', () => {13		expect(getRequestUrl('https://example.com/mcp')).toBe('https://example.com/mcp');14	});15 16	it('returns href from a URL object', () => {17		expect(getRequestUrl(new URL('https://example.com/mcp'))).toBe('https://example.com/mcp');18	});19 20	it('returns url from a Request object', () => {21		const req = new Request('https://example.com/mcp');22 23		expect(getRequestUrl(req)).toBe('https://example.com/mcp');24	});25});26 27describe('getRequestMethod', () => {28	it('prefers method from init', () => {29		expect(getRequestMethod('https://example.com', { method: 'POST' })).toBe('POST');30	});31 32	it('falls back to Request.method', () => {33		const req = new Request('https://example.com', { method: 'PUT' });34 35		expect(getRequestMethod(req)).toBe('PUT');36	});37 38	it('falls back to baseInit.method', () => {39		expect(getRequestMethod('https://example.com', undefined, { method: 'DELETE' })).toBe('DELETE');40	});41 42	it('defaults to GET', () => {43		expect(getRequestMethod('https://example.com')).toBe('GET');44	});45});46 47describe('getRequestBody', () => {48	it('returns body from init', () => {49		expect(getRequestBody('https://example.com', { body: 'payload' })).toBe('payload');50	});51 52	it('returns undefined when no body is present', () => {53		expect(getRequestBody('https://example.com')).toBeUndefined();54	});55});56 57describe('summarizeRequestBody', () => {58	it('returns empty for null', () => {59		expect(summarizeRequestBody(null)).toEqual({ kind: 'empty' });60	});61 62	it('returns empty for undefined', () => {63		expect(summarizeRequestBody(undefined)).toEqual({ kind: 'empty' });64	});65 66	it('returns string kind with size', () => {67		expect(summarizeRequestBody('hello')).toEqual({ kind: 'string', size: 5 });68	});69 70	it('returns blob kind with size', () => {71		const blob = new Blob(['abc']);72 73		expect(summarizeRequestBody(blob)).toEqual({ kind: 'blob', size: 3 });74	});75 76	it('returns formdata kind', () => {77		expect(summarizeRequestBody(new FormData())).toEqual({ kind: 'formdata' });78	});79 80	it('returns arraybuffer kind with size', () => {81		expect(summarizeRequestBody(new ArrayBuffer(8))).toEqual({ kind: 'arraybuffer', size: 8 });82	});83});84 85describe('formatDiagnosticErrorMessage', () => {86	it('appends CORS hint for Failed to fetch', () => {87		expect(formatDiagnosticErrorMessage(new TypeError('Failed to fetch'))).toBe(88			'Failed to fetch (check CORS?)'89		);90	});91 92	it('passes through other error messages unchanged', () => {93		expect(formatDiagnosticErrorMessage(new Error('timeout'))).toBe('timeout');94	});95 96	it('handles non-Error values', () => {97		expect(formatDiagnosticErrorMessage('some string')).toBe('some string');98	});99});100 101describe('extractJsonRpcMethods', () => {102	it('extracts methods from a JSON-RPC array', () => {103		const body = JSON.stringify([104			{ id: 1, jsonrpc: '2.0', method: 'initialize' },105			{ jsonrpc: '2.0', method: 'notifications/initialized' }106		]);107 108		expect(extractJsonRpcMethods(body)).toEqual(['initialize', 'notifications/initialized']);109	});110 111	it('extracts method from a single JSON-RPC message', () => {112		const body = JSON.stringify({ id: 1, jsonrpc: '2.0', method: 'tools/list' });113 114		expect(extractJsonRpcMethods(body)).toEqual(['tools/list']);115	});116 117	it('returns undefined for non-string body', () => {118		expect(extractJsonRpcMethods(null)).toBeUndefined();119		expect(extractJsonRpcMethods(undefined)).toBeUndefined();120	});121 122	it('returns undefined for invalid JSON', () => {123		expect(extractJsonRpcMethods('not json')).toBeUndefined();124	});125 126	it('returns undefined when no methods found', () => {127		expect(extractJsonRpcMethods(JSON.stringify({ foo: 'bar' }))).toBeUndefined();128	});129});130