Felipe97/llama-cpp-compiled
01.2k
1import { MCP_SERVER_ID_PREFIX } from '$lib/constants';2import { parseMcpServerSettings } from '$lib/utils/mcp';3import { describe, expect, it, vi } from 'vitest';4 5/**6 * Tests for the mcpServers settings parser.7 *8 * The parser has to be resilient to anything that may live in the9 * user's localStorage: malformed JSON, wrong shapes, missing fields,10 * falsy-but-not-zero numbers, and entry arrays that have been mutated11 * by the user via the settings form.12 */13describe('parseMcpServerSettings', () => {14 it('returns an empty array for falsy or whitespace-only input', () => {15 expect(parseMcpServerSettings(null)).toEqual([]);16 expect(parseMcpServerSettings(undefined)).toEqual([]);17 expect(parseMcpServerSettings('')).toEqual([]);18 expect(parseMcpServerSettings(' ')).toEqual([]);19 });20 21 it('returns an empty array and logs a warning for invalid JSON strings', () => {22 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});23 24 expect(parseMcpServerSettings('{not-json')).toEqual([]);25 expect(warn).toHaveBeenCalled();26 27 warn.mockRestore();28 });29 30 it('returns an empty array for valid JSON that is not an array', () => {31 expect(parseMcpServerSettings('"plain-string"')).toEqual([]);32 expect(parseMcpServerSettings('{"id":"foo"}')).toEqual([]);33 expect(parseMcpServerSettings('42')).toEqual([]);34 expect(parseMcpServerSettings('null')).toEqual([]);35 });36 37 it('drops entries with no parseable id and substitutes a stable fallback', () => {38 const parsed = parseMcpServerSettings(39 JSON.stringify([{ enabled: true, url: 'https://a.test' }, { url: 'https://b.test' }])40 );41 42 expect(parsed).toHaveLength(2);43 expect(parsed[0]?.id).toBe(`${MCP_SERVER_ID_PREFIX}-1`);44 expect(parsed[1]?.id).toBe(`${MCP_SERVER_ID_PREFIX}-2`);45 });46 47 it('reuses the first id when it is present and falls back only for missing ones', () => {48 const parsed = parseMcpServerSettings(49 JSON.stringify([50 { id: 'custom-1', url: 'https://a.test' },51 { url: 'https://b.test' },52 { id: 'custom-3', url: 'https://c.test' }53 ])54 );55 56 expect(parsed[0]?.id).toBe('custom-1');57 expect(parsed[1]?.id).toBe(`${MCP_SERVER_ID_PREFIX}-2`);58 expect(parsed[2]?.id).toBe('custom-3');59 });60 61 it('does not emit a per-server timeout, the request timeout is a live global setting', () => {62 // A stored per-server requestTimeoutSeconds was never editable in63 // any UI and froze the global setting at server creation time,64 // making the Settings value a no-op for existing servers. The65 // parser drops the field so the global applies live everywhere.66 const parsed = parseMcpServerSettings(67 JSON.stringify([{ id: 'a', requestTimeoutSeconds: 45, url: 'https://a.test' }])68 );69 70 expect(parsed[0]).not.toHaveProperty('requestTimeoutSeconds');71 });72 73 it('treats whitespace-only headers strings as undefined', () => {74 const parsed = parseMcpServerSettings(75 JSON.stringify([76 { headers: ' ', id: 'a', url: 'https://a.test' },77 { headers: '{"X-Foo":"bar"}', id: 'b', url: 'https://b.test' }78 ])79 );80 81 // The parser trims headers and coerces empty/whitespace to undefined.82 expect(parsed[0]?.headers).toBeUndefined();83 expect(parsed[1]?.headers).toBe('{"X-Foo":"bar"}');84 });85 86 it('defaults coercion for booleans (undefined -> false, true -> true)', () => {87 const parsed = parseMcpServerSettings(88 JSON.stringify([89 { id: 'a', url: 'https://a.test' },90 { enabled: true, id: 'b', url: 'https://b.test' },91 { enabled: false, id: 'c', url: 'https://c.test' },92 { id: 'd', url: 'https://d.test', useProxy: true }93 ])94 );95 96 expect(parsed[0]?.enabled).toBe(false);97 expect(parsed[1]?.enabled).toBe(true);98 expect(parsed[2]?.enabled).toBe(false);99 expect(parsed[0]?.useProxy).toBe(false);100 expect(parsed[3]?.useProxy).toBe(true);101 });102 103 it('keeps disabled entries in the list, enabled is state and never a visibility filter', () => {104 // Regression guard for issue #25625: filtering the server list on105 // `enabled` hides a toggled-off server from every UI surface with106 // no way to re-enable it. Any list derived from this parser must107 // contain disabled entries.108 const parsed = parseMcpServerSettings(109 JSON.stringify([110 { enabled: true, id: 'on', url: 'https://on.test' },111 { enabled: false, id: 'off', url: 'https://off.test' }112 ])113 );114 115 expect(parsed.map((entry) => entry.id)).toEqual(['on', 'off']);116 expect(parsed[1]?.enabled).toBe(false);117 });118 119 it('preserves input order when mapping entries', () => {120 const source = [121 { id: 'gamma', url: 'https://c.test' },122 { id: 'alpha', url: 'https://a.test' },123 { id: 'beta', url: 'https://b.test' }124 ];125 const parsed = parseMcpServerSettings(JSON.stringify(source));126 127 expect(parsed.map((entry) => entry.id)).toEqual(['gamma', 'alpha', 'beta']);128 });129 130 it('passes non-string raw input through the JSON-equality path', () => {131 const parsed = parseMcpServerSettings([132 { id: 'a', url: 'https://a.test' },133 { enabled: true, id: 'b', url: 'https://b.test' }134 ]);135 136 expect(parsed).toHaveLength(2);137 expect(parsed[0]?.id).toBe('a');138 expect(parsed[1]?.enabled).toBe(true);139 });140 141 it('coerces non-string url values to an empty string rather than throwing', () => {142 const parsed = parseMcpServerSettings(143 JSON.stringify([{ id: 'a', url: 42 }, { id: 'b' }, { id: 'c', url: 'https://c.test' }])144 );145 146 expect(parsed[0]?.url).toBe('');147 expect(parsed[1]?.url).toBe('');148 expect(parsed[2]?.url).toBe('https://c.test');149 });150});151 