char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1import { CONFIG_LOCALSTORAGE_KEY , STORAGE_APP_NAME } from '$lib/constants' ; 2import { afterEach , beforeAll , beforeEach , describe , expect , it } from 'vitest' ; 3 4// node env unit project has no DOM, install a minimal localStorage backed by a Map 5beforeAll (() => { 6const store = new Map < string , string >(); 7const polyfill :Storage = { 8clear :() => store . clear (), 9getItem :( k ) => ( store . has ( k ) ?store . get ( k ) ! :null ), 10key :( i ) => Array . from ( store . keys ())[ i ] ?? null , 11get length () { 12return store . size ; 13}, 14removeItem :( k ) => { 15store . delete ( k ); 16}, 17setItem :( k , v ) => { 18store . set ( k , String ( v )); 19} 20}; 21 22( globalThis as unknown as { localStorage :Storage }). localStorage = polyfill ; 23}); 24 25/** 26* Migration `mcp-default-overrides-merge-v1` folds the values of the parallel 27* `mcpDefaultServerOverrides` config entry onto `mcpServers[i].enabled` (the 28* single source of truth for new-chat defaults). The legacy key is kept on 29* disk for downgrade compatibility. 30*/ 31describe ( 'mcp-default-overrides-merge-v1 migration' , () => { 32const MIGRATION_STATE_KEY = ` ${ STORAGE_APP_NAME } .migration-state` ; 33const MCP_DEFAULT_OVERRIDES_KEY = ` ${ STORAGE_APP_NAME } .mcpDefaultServerOverrides` ; 34 35beforeEach ( async () => { 36localStorage . clear (); 37// Reset the migration run counter so `runAllMigrations` is guaranteed to execute. 38await import ( '$lib/services/migration.service' ). then (( mod ) => 39mod . MigrationService . resetState () 40); 41}); 42 43afterEach (() => { 44localStorage . clear (); 45}); 46 47async function runMigrations () { 48const { MigrationService} = await import ( '$lib/services/migration.service' ); 49 50await MigrationService . runAllMigrations (); 51} 52 53function readConfig () :Record < string , unknown > { 54const raw = localStorage . getItem ( CONFIG_LOCALSTORAGE_KEY ); 55 56return raw ?( JSON . parse ( raw ) as Record < string , unknown >) :{}; 57} 58 59function writeConfig ( config :Record < string , unknown >) { 60localStorage . setItem ( CONFIG_LOCALSTORAGE_KEY , JSON . stringify ( config )); 61} 62 63it ( 'applies matching overrides onto mcpServers[i].enabled and preserves the legacy key' , async () => { 64writeConfig ({ 65[ MCP_DEFAULT_OVERRIDES_KEY ] :JSON . stringify ([ 66{ enabled :true , serverId :'exa' }, 67{ enabled :false , serverId :'hf' } 68]), 69mcpServers :JSON . stringify ([ 70{ enabled :false , id :'exa' , url :'https://mcp.exa.ai/mcp' }, 71{ enabled :false , id :'hf' , url :'https://huggingface.co/mcp' } 72]) 73}); 74 75await runMigrations (); 76 77const after = readConfig (); 78const servers = JSON . parse ( after . mcpServers as string ) as Array <{ 79id :string ; 80enabled :boolean ; 81}>; 82 83expect ( servers . find (( s ) => s . id === 'exa' )?. enabled ). toBe ( true ); 84expect ( servers . find (( s ) => s . id === 'hf' )?. enabled ). toBe ( false ); 85expect ( MCP_DEFAULT_OVERRIDES_KEY in after ). toBe ( true ); 86}); 87 88it ( 'skips override ids that do not match any configured server' , async () => { 89writeConfig ({ 90[ MCP_DEFAULT_OVERRIDES_KEY ] :JSON . stringify ([ 91{ enabled :true , serverId :'orphan' }, 92{ enabled :true , serverId :'exa' } 93]), 94mcpServers :JSON . stringify ([{ enabled :false , id :'exa' , url :'https://mcp.exa.ai/mcp' }]) 95}); 96 97await runMigrations (); 98 99const after = readConfig (); 100const servers = JSON . parse ( after . mcpServers as string ) as Array <{ 101id :string ; 102enabled :boolean ; 103}>; 104 105expect ( servers ). toHaveLength ( 1 ); 106expect ( servers [ 0 ]. enabled ). toBe ( true ); 107expect ( MCP_DEFAULT_OVERRIDES_KEY in after ). toBe ( true ); 108}); 109 110it ( 'is a no-op when there are no legacy overrides' , async () => { 111writeConfig ({ 112mcpServers :JSON . stringify ([{ enabled :true , id :'exa' , url :'https://mcp.exa.ai/mcp' }]) 113}); 114 115await runMigrations (); 116 117const after = readConfig (); 118const servers = JSON . parse ( after . mcpServers as string ) as Array <{ 119id :string ; 120enabled :boolean ; 121}>; 122 123expect ( servers [ 0 ]. enabled ). toBe ( true ); 124expect ( MCP_DEFAULT_OVERRIDES_KEY in after ). toBe ( false ); 125}); 126 127it ( 'does not rewrite mcpServers when override.enabled already matches' , async () => { 128const originalServers = JSON . stringify ([ 129{ enabled :true , id :'exa' , url :'https://mcp.exa.ai/mcp' } 130]); 131 132writeConfig ({ 133[ MCP_DEFAULT_OVERRIDES_KEY ] :JSON . stringify ([{ enabled :true , serverId :'exa' }]), 134mcpServers :originalServers 135}); 136 137await runMigrations (); 138 139const after = readConfig (); 140 141expect ( after . mcpServers ). toBe ( originalServers ); 142expect ( MCP_DEFAULT_OVERRIDES_KEY in after ). toBe ( true ); 143}); 144 145it ( 'records itself as completed so subsequent loads do not re-run' , async () => { 146writeConfig ({ 147[ MCP_DEFAULT_OVERRIDES_KEY ] :JSON . stringify ([{ enabled :true , serverId :'exa' }]), 148mcpServers :JSON . stringify ([{ enabled :false , id :'exa' , url :'https://mcp.exa.ai/mcp' }]) 149}); 150 151const { MigrationService} = await import ( '$lib/services/migration.service' ); 152 153await MigrationService . runAllMigrations (); 154 155const stateRaw = localStorage . getItem ( MIGRATION_STATE_KEY ); 156 157expect ( stateRaw ). not . toBeNull (); 158const state = JSON . parse ( stateRaw ! ) as { completed :string []; failed :string [] }; 159 160expect ( state . completed ). toContain ( 'mcp-default-overrides-merge-v1' ); 161expect ( state . failed ). not . toContain ( 'mcp-default-overrides-merge-v1' ); 162}); 163});