import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { fixture, compatibleFixture } from './helpers.mjs'; import { Store } from '../src/server/store.mjs'; import { Provider } from '../src/server/provider.mjs'; import { ConfigImporter } from '../src/core/models/config-import.mjs'; const windows = { skip: process.platform !== 'win32' }; const create = store => { const provider = new Provider(); provider.setKey(''); return { provider, hub: provider.modelHub(store) }; }; const reopen = f => { f.store.close(); const store = new Store(f.data); f.onCleanup(() => store.close()); return { store, ...create(store) }; }; const connection = (name, extra = {}) => ({ name, model: name, baseUrl: 'http://127.0.0.1:12001/v1', ...extra }); test('normal key entry survives a fresh provider by default without plaintext in public views or SQLite files', windows, t => { const f = fixture(t, { modernDefaults: true }), { hub } = create(f.store); const key = 'OWNED_DEFAULT_CREDENTIAL_SENTINEL_0a3b29ef1d7d'; const saved = hub.upsert(connection('default-manual', { apiKey: key })); assert.equal(saved.savedCredential?.status, 'saved'); assert.equal(JSON.stringify(hub.view()).includes(key), false); for (const filename of fs.readdirSync(f.data)) { const file = path.join(f.data, filename); if (fs.statSync(file).isFile()) for (const needle of [Buffer.from(key), Buffer.from(key, 'utf16le'), Buffer.from(Buffer.from(key).toString('base64'))]) assert.equal(fs.readFileSync(file).includes(needle), false, filename); } const fresh = reopen(f), restored = fresh.hub.connection(saved.id); assert.equal(fresh.hub.status(restored).hasKey, true); assert.equal(fresh.hub.credential(restored).key, key); assert.equal(fresh.hub.credential(restored).revision, saved.credentialRevision); }); test('new upstream model selection saves one default credential and restores every referenced model', windows, async t => { const f = fixture(t, { modernDefaults: true }), { hub } = create(f.store); const server = await compatibleFixture(t, () => ({ data: [{ id: 'owned-one' }, { id: 'owned-two' }] })); const preview = await hub.modelCatalog().fetch({ draft: { name: 'owned-upstream', protocol: 'chat-completions', baseUrl: server.baseUrl }, apiKey: 'owned-catalog-default' }); const result = hub.modelCatalog().apply({ previewId: preview.id, models: ['owned-one', 'owned-two'], routing: 'auto' }); assert.equal(Object.keys(f.store.storage.meta('modelCredentials', { entries: {} }).entries).length, 1); const fresh = reopen(f), models = result.added.map(id => fresh.hub.connection(id)); assert.equal(models.every(model => fresh.hub.status(model).hasKey), true); assert.equal(fresh.hub.credential(models[0]), fresh.hub.credential(models[1])); assert.equal(fresh.hub.state.selected.type, 'pool'); assert.equal(JSON.stringify(fresh.hub.view()).includes('owned-catalog-default'), false); }); test('confirmed configuration import persists keys by default and retains that policy for automatic sync', windows, t => { const f = fixture(t, { modernDefaults: true }), { hub } = create(f.store), importer = new ConfigImporter(hub); const filename = path.join(f.root, 'owned-default-import.json'); const write = key => fs.writeFileSync(filename, JSON.stringify({ schema: 1, connections: [connection('owned-import', { apiKey: key })] })); write('owned-import-first'); const preview = importer.preview('workbench-json', filename), accepted = importer.apply(preview.id, true); const id = accepted.imported[0]; assert.equal(hub.status(hub.connection(id)).savedCredential?.status, 'saved'); assert.equal(hub.state.imports[accepted.sourceId].persistCredentials, true); write('owned-import-next'); assert.equal(importer.sync()[0].imported.length, 1); const fresh = reopen(f); assert.equal(fresh.hub.status(fresh.hub.connection(id)).hasKey, true); assert.equal(fresh.hub.credential(fresh.hub.connection(id)).key, 'owned-import-next'); }); test('editing a saved connection without a new key preserves its encrypted block and restart recovery', windows, t => { const f = fixture(t, { modernDefaults: true }), { hub } = create(f.store); const saved = hub.upsert(connection('owned-edit', { apiKey: 'owned-edit-key' })); const block = structuredClone(hub.vault().state.entries[saved.id]); hub.upsert({ ...saved, name: 'renamed connection', concurrency: 3 }); assert.deepEqual(hub.vault().state.entries[saved.id], block); const fresh = reopen(f); assert.equal(fresh.hub.status(fresh.hub.connection(saved.id)).hasKey, true); assert.equal(fresh.hub.connection(saved.id).name, 'renamed connection'); }); for (const replacement of [{ apiKey: 'owned-temporary-replacement', persistKey: false }, { apiKey: '' }]) { test(`${replacement.apiKey ? 'choosing temporary use' : 'clearing a key'} removes the old saved copy instead of restoring it later`, windows, t => { const f = fixture(t, { modernDefaults: true }), { hub } = create(f.store); const saved = hub.upsert(connection('owned-replacement', { apiKey: 'owned-original-saved' })); const changed = hub.upsert({ ...saved, ...replacement }); assert.equal(changed.hasKey, !!replacement.apiKey); assert.equal(hub.vault().state.entries[saved.id], undefined); const fresh = reopen(f); assert.equal(fresh.hub.status(fresh.hub.connection(saved.id)).hasKey, false); }); } test('temporary import remains temporary during automatic sync, including sources created by older versions', windows, t => { const f = fixture(t, { modernDefaults: true }), { hub } = create(f.store), importer = new ConfigImporter(hub); const filename = path.join(f.root, 'owned-temporary-import.json'); const write = key => fs.writeFileSync(filename, JSON.stringify({ schema: 1, connections: [connection('owned-temporary-import', { apiKey: key })] })); write('owned-temporary-first'); const accepted = importer.apply(importer.preview('workbench-json', filename).id, true, false); delete hub.state.imports[accepted.sourceId].persistCredentials; hub.save(); write('owned-temporary-next'); assert.equal(importer.sync()[0].imported.length, 1); assert.equal(hub.state.imports[accepted.sourceId].persistCredentials, false); const fresh = reopen(f); assert.equal(fresh.hub.status(fresh.hub.connection(accepted.imported[0])).hasKey, false); }); test('legacy configuration and its default encrypted key commit together and survive non-credential edits', windows, t => { const f = fixture(t, { modernDefaults: true }), { hub } = create(f.store); const config = { ...f.store.config, mode: 'compatible', baseUrl: 'http://127.0.0.1:12001/v1', model: 'owned-legacy' }; hub.configureLegacy(config, { apiKey: 'owned-legacy-default' }); assert.equal(hub.vault().status(hub.connection('legacy-default')).status, 'saved'); hub.configureLegacy({ ...f.store.config, maxSteps: 30 }, {}); const fresh = reopen(f); assert.equal(fresh.provider.hasKey(), true); assert.equal(fresh.provider.apiKey, 'owned-legacy-default'); assert.equal(fresh.store.config.maxSteps, 30); }); test('default encryption failure rejects a configuration change and keeps both database and resident credentials intact', windows, t => { const f = fixture(t, { modernDefaults: true }), { hub, provider } = create(f.store); hub.configureLegacy({ ...f.store.config, mode: 'compatible', baseUrl: 'http://127.0.0.1:12001/v1', model: 'owned-before' }, { apiKey: 'owned-before' }); const config = structuredClone(f.store.config), registry = structuredClone(hub.state), vaultState = structuredClone(hub.vault().state); const snapshot = () => f.store.storage.database.prepare('SELECT * FROM metadata ORDER BY rowid').all(); const rows = snapshot(); hub.vault().protect = () => { throw new Error('owned default encryption failure'); }; assert.throws(() => hub.configureLegacy({ ...config, model: 'owned-after' }, { apiKey: 'owned-after' }), /owned default encryption failure/); assert.deepEqual(f.store.config, config); assert.deepEqual(hub.state, registry); assert.deepEqual(hub.vault().state, vaultState); assert.equal(provider.apiKey, 'owned-before'); assert.deepEqual(snapshot(), rows); }); test('explicitly saving the current key keeps its resident object and request identity and restores it without re-entry', windows, t => { const f = fixture(t, { modernDefaults: true }), { hub } = create(f.store); const temporary = hub.upsert(connection('owned-current', { apiKey: 'owned-current-key', persistKey: false })); const resident = hub.credential(temporary), identity = hub.identity(temporary); const result = hub.manageCredential({ id: temporary.id, revision: temporary.revision, action: 'save-current' }); assert.equal(result.credential.status, 'saved'); assert.equal(hub.credential(temporary), resident); assert.equal(hub.identity(temporary), identity); assert.equal(JSON.stringify(result).includes('owned-current-key'), false); const fresh = reopen(f); assert.equal(fresh.hub.credential(fresh.hub.connection(temporary.id)).key, 'owned-current-key'); assert.equal(fresh.hub.credential(fresh.hub.connection(temporary.id)).revision, resident.revision); }); test('failed current-key saving leaves the live key and every original metadata row unchanged', windows, t => { const f = fixture(t, { modernDefaults: true }), { hub } = create(f.store); const temporary = hub.upsert(connection('owned-current-failure', { apiKey: 'owned-current-failure-key', persistKey: false })); const resident = hub.credential(temporary), registry = structuredClone(hub.state); const snapshot = () => f.store.storage.database.prepare('SELECT * FROM metadata ORDER BY rowid').all(); const rows = snapshot(); f.store.storage.faultInjector = point => { if (point === 'before_commit') throw new Error('owned current-key disk failure'); }; assert.throws(() => hub.manageCredential({ id: temporary.id, revision: temporary.revision, action: 'save-current' }), /owned current-key disk failure/); assert.equal(hub.credential(temporary), resident); assert.deepEqual(hub.state, registry); assert.deepEqual(snapshot(), rows); f.store.storage.faultInjector = null; });