Files
ycode/scripts/model-settings-alignment-regression.ts
T

34 lines
2.7 KiB
TypeScript

import assert from 'node:assert/strict'
import { useSettingsStore } from '../src/renderer/src/stores/settings'
import { useChatStore } from '../src/renderer/src/stores/chat'
import type { CustomModelProfile } from '../src/shared/models'
const profile=(id:string):CustomModelProfile=>({id,name:id,model:id,providerId:'fixture',baseUrl:'https://offline.invalid',hasApiKey:false,keyStorage:'none'})
const session=(id:string,modelId:string)=>({id,modelId,title:id,createdAt:1,updatedAt:1})
let finish!: (value:unknown)=>void
const saved=()=>new Promise(resolve=>{finish=resolve})
const updates:Array<{id:string,patch:any}>=[]
;(globalThis as any).window={ycode:{models:{save:saved,remove:async()=>({models:[],selectedModelId:null})},session:{updateContext:async(id:string,patch:any)=>{updates.push({id,patch});return {...session(id,patch.modelId),...patch}}}}}
useSettingsStore.setState({models:[profile('a'),profile('b')],selectedModelId:'b'})
useChatStore.setState({sessions:[session('A','a'),session('B','b')],currentSessionId:'A',draftId:1,draftContext:{modelId:'a'},messages:[]})
const add=useSettingsStore.getState().saveModel(profile('c'))
useChatStore.setState({currentSessionId:'B'})
finish({models:[profile('a'),profile('b'),profile('c')],selectedModelId:'c'});await add
assert.equal(updates.length,0,'late addition must not change another active session')
assert.equal(useChatStore.getState().sessions.find(s=>s.id==='B')?.modelId,'b')
useChatStore.setState({currentSessionId:'A'})
const rename=useSettingsStore.getState().saveModel({...profile('a2'),previousId:'a'})
finish({models:[profile('a2'),profile('b'),profile('c')],selectedModelId:'c'});await rename
assert.equal(useChatStore.getState().sessions.find(s=>s.id==='A')?.modelId,'a2','rename is not deletion fallback')
assert.equal(useChatStore.getState().draftContext.modelId,'a2')
assert.equal(updates.length,0,'edit does not arbitrarily select')
const addCurrent=useSettingsStore.getState().saveModel(profile('d'))
finish({models:[profile('a2'),profile('b'),profile('c'),profile('d')],selectedModelId:'d'});await addCurrent
assert.equal(updates.at(-1)?.id,'A');assert.equal(updates.at(-1)?.patch.modelId,'d')
const snapshot={id:'history',role:'assistant' as const,content:'answer',createdAt:1,modelId:'a',modelName:'real old model'}
useChatStore.setState({messages:[snapshot]})
await useSettingsStore.getState().deleteModel('d')
assert.equal(useChatStore.getState().sessions[0].modelId,undefined)
assert.equal(useChatStore.getState().draftContext.modelId,undefined)
assert.equal(useChatStore.getState().messages[0],snapshot,'history snapshot never rewritten')
console.log('PASS model settings: deferred owner switch, rename mapping, addition selection, empty deletion, immutable history')