/* An instance configured entirely from its environment. * * A container should be able to start with its provider already in place — endpoint, model, key — * with nobody opening an admin screen. What is asserted here is that the environment configures * the Coach, that a pasted key still wins over it, and that the key never lands in the file the * docs tell people to back up. */ import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { tempData } from './helpers.mjs'; tempData(); process.env.COACH_PROVIDER = 'compatible'; process.env.COACH_BASE_URL = 'https://ai.example.com'; process.env.COACH_MODEL = 'vendor/model-1'; process.env.COACH_API_KEY = 'sk-from-the-environment'; const cfg = await import('../coach/config.js'); const { baseUrlFor } = await import('../coach/core/providers.js'); const coachFile = () => path.join(process.env.DATA_DIR, 'coach.json'); const onDisk = () => (fs.existsSync(coachFile()) ? fs.readFileSync(coachFile(), 'utf8') : ''); test('the environment alone configures the provider, the endpoint, the model and the key', () => { assert.equal(cfg.isEnabled(), true, 'naming a provider is the configuration'); assert.equal(cfg.isConnected(), true); const c = cfg.load(); assert.equal(c.provider, 'compatible'); assert.equal(cfg.modelFor(c), 'vendor/model-1'); assert.equal(baseUrlFor('compatible', c), 'https://ai.example.com'); const cred = cfg.credentialFor('u1'); assert.equal(cred.ok, true); assert.equal(cred.auth.token, 'sk-from-the-environment'); assert.equal(cred.fromEnv, true, 'a caller can tell a key it did not file'); }); test('a credential pasted in the app still wins over the environment one', () => { cfg.saveAuth('compatible', { type: 'apikey', account: 'admin', data: cfg.encrypt({ token: 'sk-pasted' }) }); const cred = cfg.credentialFor('u1'); assert.equal(cred.auth.token, 'sk-pasted'); assert.equal(cred.fromEnv, undefined); assert.equal(cfg.isConnected(), true); // …and once it is removed, the environment is what is left. cfg.saveAuth('compatible', null); assert.equal(cfg.credentialFor('u1').auth.token, 'sk-from-the-environment'); }); test('the environment key is never written to the config file', () => { cfg.save({ caps: { perProfileDaily: 5, instanceDaily: 0 } }); const written = onDisk(); assert.ok(written.includes('perProfileDaily'), 'the patch itself is saved'); assert.ok(!written.includes('sk-from-the-environment'), 'the key must not be in a file people back up'); assert.ok(!written.includes('sk-pasted'), 'nor a pasted one in the clear — that is encrypted'); assert.ok(!written.includes('COACH_API_KEY')); }); test('what the environment supplies is not frozen into the file', () => { // A saved config used to be written back whole, which would have pinned this start-up's values // into the file — and the file outranks the environment, so a later change to COACH_MODEL would // have been silently ignored. const written = JSON.parse(onDisk() || '{}'); assert.equal(written.provider, undefined); assert.equal(written.enabled, undefined); const text = JSON.stringify(written); assert.ok(!text.includes('vendor/model-1'), 'the model the environment set is not pinned'); assert.ok(!text.includes('ai.example.com'), 'nor the endpoint'); }); test('the provider\'s own variable works too, and no provider at all turns nothing on', () => { delete process.env.COACH_API_KEY; process.env.OPENAI_COMPAT_API_KEY = 'sk-provider-variable'; cfg.reset(); assert.equal(cfg.credentialFor('u1').auth.token, 'sk-provider-variable'); delete process.env.COACH_PROVIDER; delete process.env.COACH_BASE_URL; delete process.env.COACH_MODEL; delete process.env.OPENAI_COMPAT_API_KEY; cfg.reset(); // Back to the built-in default (the fixture), which is not a Coach anybody can use: the whole // point of naming a provider in the environment is that naming nothing leaves the feature off. assert.equal(cfg.load().provider, 'fixture'); assert.equal(cfg.isEnabled(), false, 'no provider named, nothing turned on'); assert.equal(cfg.isConnected(), false); });