// A daemon E2E cannot press Back while the browser's key save is in flight, // and the daemon accepts an OpenRouter account on an Ollama Cloud setup (both // openai). Compile the real connect steps, stubbing only the API they call. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; import { compileModule } from 'svelte/compiler'; const moduleURL = (code) => `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`; const apiURL = moduleURL(` export const saves = [], listings = []; const deferred = (record) => new Promise((resolve, reject) => record.push({ resolve, reject })); export function saveKeyAccount(extension, endpoint, key, label) { return deferred(saves); } export function providerModels(extension, endpoint) { return new Promise((resolve, reject) => listings.push({ extension, endpoint, resolve, reject })); } `); const api = await import(apiURL); const typeless = async (path) => stripTypeScriptTypes((await readFile(new URL(path, import.meta.url), 'utf8')).replace(/^import type .*;\n/gm, ''), { mode: 'transform' }); const providersURL = moduleURL(await typeless('../src/lib/settings/providers.ts')); const imports = new Map([['../api', apiURL], ['./providers', providersURL]]); let code = compileModule(await typeless('../src/lib/settings/connect.svelte.ts'), { filename: 'connect.svelte.js', generate: 'client' }).js.code; code = code.replace(/from (['"])(.*?)\1/g, (_, _quote, path) => `from ${JSON.stringify(imports.get(path) ?? import.meta.resolve(path))}`); const { ConnectFlow } = await import(moduleURL(code)); const { connectOptions } = await import(providersURL); const option = (id) => connectOptions([]).find((item) => item.id === id); const settled = () => new Promise((resolve) => setImmediate(resolve)); function wizard() { api.saves.length = 0; api.listings.length = 0; let reloads = 0; const flow = new ConnectFlow(() => ({ profiles: {}, default_profile: null }), async () => { reloads++; }); return { flow, reloads: () => reloads }; } async function submitKey(flow, id, key) { flow.choose(option(id)); flow.form.key = key; const done = flow.credentials(); await settled(); return done; } test('a key saved after Back and another choice stays off the provider chosen since', async () => { const { flow, reloads } = wizard(); const openrouter = submitKey(flow, 'openrouter', 'sk-or'); flow.back(); flow.choose(option('ollama')); api.saves[0].resolve({ id: 'openrouter-key' }); await openrouter; assert.equal(reloads(), 1, 'the saved account is real, so the list shows it'); assert.equal(flow.step, 'credentials'); assert.equal(flow.option.id, 'ollama'); assert.equal(flow.account, null); assert.equal(api.listings.length, 0); // Ollama Cloud's own key still goes through, to its own models. flow.form.key = 'sk-ollama'; const ollama = flow.credentials(); await settled(); api.saves[1].resolve({ id: 'ollama-key' }); await ollama; assert.equal(flow.step, 'model'); assert.equal(flow.account, 'ollama-key'); assert.deepEqual(api.listings.map(({ endpoint }) => endpoint), ['https://ollama.com/v1']); }); test('a stale save cannot clear the busy state or raise an error on the current attempt', async () => { const { flow } = wizard(); const first = submitKey(flow, 'openrouter', 'sk-or'); flow.back(); const second = submitKey(flow, 'ollama', 'sk-ollama'); api.saves[0].reject(new Error('openrouter refused the key')); await first; assert.equal(flow.busy, true); assert.equal(flow.problem, ''); api.saves[1].resolve({ id: 'ollama-key' }); await second; assert.equal(flow.busy, false); assert.equal(flow.account, 'ollama-key'); }); test('closing the wizard drops a pending save and model listing', async () => { const { flow, reloads } = wizard(); const saving = submitKey(flow, 'openrouter', 'sk-or'); flow.close(); api.saves[0].resolve({ id: 'openrouter-key' }); await saving; assert.equal(reloads(), 1); assert.equal(flow.account, null); assert.equal(api.listings.length, 0); const listing = submitKey(flow, 'ollama', 'sk-ollama'); api.saves[1].resolve({ id: 'ollama-key' }); await listing; flow.close(); api.listings[0].resolve([{ id: 'gpt-oss:120b', label: 'gpt-oss', efforts: [] }]); await settled(); assert.equal(flow.list, undefined); assert.equal(flow.pick.model, ''); });