Files
kuentagym/api/test/coach-env.test.js
dsantillan 238eb635c5
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
Configure the Coach from the environment
An instance can now be pointed at a model entirely from its environment — so a
container is redeployed with the Coach already working, and nobody has to open
an admin screen:

    COACH_PROVIDER=compatible
    COACH_BASE_URL=https://ai.example.com     # WITHOUT /v1 — the adapter adds it
    COACH_MODEL=vendor/model-name
    COACH_API_KEY=sk-...                      # or the provider's own variable

Naming a provider is the whole configuration, so it also turns the Coach on: an
instance started this way that then reported itself "off" would be a puzzle with
no clue in it.

Three decisions worth stating:

- The environment is the *base*, the way DEFAULTS is, and the saved file is
  layered on top: an admin who picked a provider in the app meant it, and this
  only fills what is absent.

- The key is deliberately NOT a field of the config. That object is cached,
  written to data/coach.json by any admin action, and included in the backup the
  docs tell people to make. envCredentialFor reads it from the process
  environment instead, so it is written nowhere and never surfaced by any route
  that reports configuration. A pasted credential still outranks it, and a filed
  one that fails to decrypt stays a failure rather than quietly falling back to
  a key nobody knows is being spent.

- What the environment supplies is stripped before the file is written. A saved
  config used to be written back whole, which would have frozen this start-up's
  values on disk — and since the file outranks the environment, a later change
  to COACH_MODEL would have been silently ignored.

Verified through the server's own path (config → credentialFor → jobEnv →
adapter) against the OpenAI-compatible router this fork runs on: a chest+triceps
request came back as a six-exercise routine with the cardio row first and
correctly marked `mode: cardio`.
2026-09-21 15:16:50 -04:00

89 lines
4.1 KiB
JavaScript

/* 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);
});