Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions scripts/jev/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ function readSmallFile(file, code, optional = false) {
}

export function resolveJevSettings({ apiKey, model, env = process.env, home = os.homedir() } = {}) {
let key = apiKey ?? env.TYPESAFE_API_KEY;
let selectedModel = model ?? env.JEV_MODEL;
let key = apiKey === undefined ? env.TYPESAFE_API_KEY : apiKey;
let selectedModel = model === undefined ? env.JEV_MODEL : model;
const keyFile = key === undefined ? env.TYPESAFE_API_KEY_FILE : undefined;
let config = {};
// Explicit runtime credentials and model do not depend on this machine's config.
Expand All @@ -66,11 +66,11 @@ export function resolveJevSettings({ apiKey, model, env = process.env, home = os
fail("invalid_config");
}
}
selectedModel ??= config.model;
if (selectedModel === undefined) selectedModel = config.model;
if (typeof selectedModel !== "string" || !/^jev-\d+\.\d+\.\d+$/.test(selectedModel))
fail("pinned_model_required");
if (key === undefined) {
const file = keyFile ?? config.apiKeyFile;
const file = keyFile === undefined ? config.apiKeyFile : keyFile;
if (file !== undefined) {
if (typeof file !== "string" || !path.isAbsolute(file)) fail("invalid_api_key_file");
key = readSmallFile(file, "api_key_file_unreadable");
Expand Down
14 changes: 14 additions & 0 deletions scripts/jev/tests/config.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ test("explicit options then environment override local defaults; complete overri
);
});

test("explicit null settings are invalid and never select private defaults", (t) => {
const { home } = fixture(t);
for (const options of [{ apiKey: null }, { model: null }, { apiKey: null, model: null }]) {
assert.throws(() => resolveJevSettings({ env: {}, home, ...options }));
assert.throws(() =>
resolveJevSettings({
env: { TYPESAFE_API_KEY: "environment-key", JEV_MODEL: "jev-1.13.0" },
home,
...options,
}),
);
}
});

test("XDG and explicit config paths are honored without repository-relative credential discovery", (t) => {
const { home, configFile } = fixture(t);
const result = resolveJevSettings({
Expand Down
Loading