33 lines
3.2 KiB
JavaScript
33 lines
3.2 KiB
JavaScript
|
|
import test from 'node:test';
|
||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import path from 'node:path';
|
||
|
|
import { fileURLToPath } from 'node:url';
|
||
|
|
import { createServer } from 'vite';
|
||
|
|
|
||
|
|
test('real app state preserves the CLI port and rejects stale editor/full-form saves', async () => {
|
||
|
|
const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..');
|
||
|
|
const adapter={id:'adapter-1',type:'openai',displayName:'Fixture',baseURL:'https://fixture.invalid',apiKey:'fixture-only',tooltipData:'fixture',modelID:'model',reasoningEffort:'high',openAIEndpoint:'/v1/responses'};
|
||
|
|
let config={revision:'r1',backendListenAddr:'127.0.0.1:28090',proxyListenAddr:'127.0.0.1:28080',cliHttpsListenAddr:'127.0.0.1:39999',modelAdapters:[adapter],routing:{mode:'local'},homeMetrics:{persistentMode:true,refreshIntervalSeconds:60},tabServerBaseURL:'',tabServerToken:''};
|
||
|
|
const writes=[];
|
||
|
|
globalThis.__configReviewFixture={load:()=>structuredClone(config),save:payload=>{assert.equal(payload.revision,config.revision);writes.push(structuredClone(payload));config={...structuredClone(payload),revision:'r'+(writes.length+1)};}};
|
||
|
|
const apiNames=['getAppVersion','getHomeMetricsSummary','getModelAdapterTestResults','getProxyState','listRemoteModels','openConfigWindow','loadUserConfig','openLogsDirectory','openModelConfig','openModelEditor','saveUserConfig','startProxyService','stopProxyService','testModelAdapter'];
|
||
|
|
const server=await createServer({root,configFile:false,optimizeDeps:{noDiscovery:true,include:[]},ssr:{noExternal:['@wailsio/runtime']},resolve:{alias:{'@':path.join(root,'src')}},server:{middlewareMode:true,hmr:false,watch:null},plugins:[{
|
||
|
|
name:'isolated-native-bridge-fixture',enforce:'pre',
|
||
|
|
resolveId(id){if(id==='@wailsio/runtime')return '\0review-runtime';if(id.replaceAll('\\','/').match(/\/services\/clientApi(?:\.js)?$/))return '\0review-api';},
|
||
|
|
load(id){if(id==='\0review-runtime')return 'export const Events={On:()=>()=>{}};';if(id==='\0review-api')return apiNames.map(name=>`export async function ${name}(...args){${name==='loadUserConfig'?'return globalThis.__configReviewFixture.load();':name==='saveUserConfig'?'return globalThis.__configReviewFixture.save(args[0]);':'return {};'}}`).join('\n');},
|
||
|
|
}]});
|
||
|
|
try {
|
||
|
|
const state=await server.ssrLoadModule('/src/state/appState.js');
|
||
|
|
globalThis.window={setInterval:()=>1,clearInterval:()=>{}};
|
||
|
|
await state.reloadUserConfig();
|
||
|
|
assert.equal(state.appState.configCLIHTTPSListenAddr,'127.0.0.1:39999');
|
||
|
|
const saved=await state.saveIncludeCacheWriteInHitRate(true);
|
||
|
|
assert.equal(saved.ok,true);assert.equal(writes[0].cliHttpsListenAddr,'127.0.0.1:39999');assert.equal(writes[0].revision,'r1');
|
||
|
|
config={...config,revision:'other-window'};
|
||
|
|
const draft={...adapter,displayName:'unsaved draft'};
|
||
|
|
const stale=await state.saveModelAdapterAt(0,draft,'r2','adapter-1');
|
||
|
|
assert.equal(stale.ok,false);assert.match(stale.error,/CONFIG_CONFLICT/);assert.equal(draft.displayName,'unsaved draft');assert.equal(writes.length,1);
|
||
|
|
const full=await state.persistUserConfig();assert.equal(full.ok,false);assert.equal(writes.length,1);
|
||
|
|
} finally {await server.close();delete globalThis.__configReviewFixture;delete globalThis.window;}
|
||
|
|
});
|