Files
buzz/desktop/src/features/agents/ui/PersonaProviderApiKeyField.test.mjs
Will Pfleger 5e0efb0bb9 fix(desktop): disambiguate provider API key labels and annotate mint key (#4406)
Two different credentials were presented under the same name throughout
the app. The top-level credential field for non-Anthropic providers
(OpenAI, OpenAI-compatible, OpenRouter) was labeled "OpenAI API Key" via
a hardcoded binary ternary repeated in three dialogs. The card-minting
key (`OPENAI_API_KEY`) and the runtime credential
(`OPENAI_COMPAT_API_KEY`) have independent endpoint namespaces and
consumers (`OPENAI_COMPAT_BASE_URL`/`OPENAI_COMPAT_API_KEY` for runtime,
`OPENAI_BASE_URL`/`OPENAI_API_KEY` for minting) and must remain separate
— either may require a different credential. This PR makes them
impossible to confuse in the UI.

## Changes

**Provider-accurate labels from the credential table.**
`PROVIDER_CREDENTIAL_CONFIG` entries now carry an `apiKeyLabel` paired
with `secretEnvVar` as a discriminated union (both present or neither —
a future provider cannot ship a secret field with no label).
`getProviderApiKeyLabel(providerId)` is the single source of truth. The
three hardcoded ternaries in `AgentConfigFields`,
`AgentInstanceEditDialog`, and `AgentDefinitionDialog` are replaced by
this helper. Labels: `openai` → "OpenAI Runtime API Key",
`openai-compat` → "OpenAI-compatible Runtime API Key", `openrouter` →
"OpenRouter API Key" (was incorrectly "OpenAI API Key"), `anthropic` →
"Anthropic API Key" (unchanged).

**Field names its backing env var.** `PersonaProviderApiKeyField`
renders the env var name as a monospace hint beneath the label with
`aria-describedby` wiring. All three call sites pass their
`secretEnvVar`. A user who sees `OPENAI_API_KEY` in the mint dialog can
now confirm at a glance that the credential field shows
`OPENAI_COMPAT_API_KEY` — a different key.

**Signpost visible at the decision point.** `CARD_MINT_KEY_ANNOTATIONS`
is exported from `agentConfigOptions.tsx` (single source) and passed as
`keyAnnotations` to all three generic env editors: both `EnvVarsEditor`
branches in Agent Defaults, `EditAgentAdvancedFields`, and
`PersonaAdvancedFields`. `CardMintKeyCue` — a new small component —
renders an always-visible muted cue beneath the Advanced toggle when
`OPENAI_API_KEY` is present in global env (Advanced is collapsed by
default, so the per-row annotation is invisible until the cue guides the
user to open it).

**Model discovery error copy.** The `OPENAI_COMPAT_API_KEY required`
message now reads "Enter an OpenAI runtime API key
(OPENAI_COMPAT_API_KEY) to load OpenAI models." — naming the env var
explicitly so it cannot be confused with the mint key.

## Tests

- `getProviderApiKeyLabel` helper: pinned correct label per provider
including the new distinct labels for `openai` and `openai-compat`
- `PersonaProviderApiKeyField` render: semantic label present; env-var
hint rendered when `envVarName` provided; `aria-describedby` wired to
hint id; hint and describedby absent when prop omitted
- `EnvVarsEditor` render: annotation appears exactly once on the
matching row; absent for non-matching rows
- `personaModelDiscoveryStatus`: pinned new copy naming
`OPENAI_COMPAT_API_KEY` explicitly
- Playwright: stale `"OpenAI API Key"` selectors updated; new
`card-mint-key-cue-visible-and-annotation-in-advanced` test covers
Will's exact path (databricks_v2 global provider + saved
`OPENAI_API_KEY` → cue visible before opening Advanced → annotation
present after opening)

## File sizes (post-format)

| File | Lines |
|------|-------|
| `AgentConfigFields.tsx` | 994 (≤ 996) |
| `AgentInstanceEditDialog.tsx` | 1228 (≤ 1228) |
| `AgentDefinitionDialog.tsx` | 1045 (≤ 1047) |

Related: [#4140](https://github.com/block/buzz/pull/4140)

---------

Signed-off-by: Will Pfleger <pfleger.will@gmail.com>
Co-authored-by: npub1ng3jzsaqxdhrfq22dg85j3lpr0zsh3jp7g2h9jyxl59wraayapnsu6kvfg <9a232143a0336e34814a6a0f4947e11bc50bc641f21572c886fd0ae1f7a4e867@buzz.block.builderlab.xyz>
2026-08-03 11:12:34 -04:00

171 lines
5.5 KiB
JavaScript

/**
* Behavioral tests for PersonaProviderApiKeyField.
*
* Tests the rendering invariants that matter for the disambiguation story:
* - semantic label is present in the rendered output
* - envVarName hint is rendered when the prop is present
* - hint id is wired to the input via aria-describedby
* - hint is absent when envVarName is omitted
* - two simultaneous instances produce unique IDs (no duplicate-ID collision
* in the nested AgentInstanceEditDialog + AgentDefaultsDialog path)
*/
import assert from "node:assert/strict";
import { test } from "node:test";
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { PersonaProviderApiKeyField } from "./PersonaProviderApiKeyField.tsx";
function makeProps(overrides = {}) {
return {
disabled: false,
isInherited: false,
inheritedLabel: "Set in global defaults",
isRequired: false,
label: "OpenAI Runtime API Key",
onValueChange: () => {},
value: "",
...overrides,
};
}
/** Extract the value of the first attribute matching `name="..."` in html. */
function extractAttr(html, attrName) {
const re = new RegExp(`${attrName}="([^"]+)"`);
const m = re.exec(html);
return m ? m[1] : null;
}
/** Extract ALL values of an attribute from html, in document order. */
function extractAllAttrs(html, attrName) {
const re = new RegExp(`${attrName}="([^"]+)"`, "g");
return Array.from(html.matchAll(re), (m) => m[1]);
}
test("PersonaProviderApiKeyField_renders_semantic_label", () => {
const html = renderToStaticMarkup(
React.createElement(PersonaProviderApiKeyField, makeProps()),
);
assert.ok(
html.includes("OpenAI Runtime API Key"),
"semantic label must appear in rendered output",
);
});
test("PersonaProviderApiKeyField_renders_env_var_hint_when_envVarName_present", () => {
const html = renderToStaticMarkup(
React.createElement(
PersonaProviderApiKeyField,
makeProps({ envVarName: "OPENAI_COMPAT_API_KEY" }),
),
);
assert.ok(
html.includes("OPENAI_COMPAT_API_KEY"),
"env-var hint must appear when envVarName is provided",
);
});
test("PersonaProviderApiKeyField_wires_hint_id_via_aria_describedby", () => {
const html = renderToStaticMarkup(
React.createElement(
PersonaProviderApiKeyField,
makeProps({ envVarName: "OPENAI_COMPAT_API_KEY" }),
),
);
// Extract the dynamically-generated hint id from the rendered paragraph.
const hintId = extractAttr(html, "id");
assert.ok(hintId, "hint paragraph must have an id");
assert.ok(
hintId.startsWith("persona-provider-api-key-hint-"),
`hint id must follow the expected prefix, got: ${hintId}`,
);
// The input's aria-describedby must point at the same id.
const describedBy = extractAttr(html, "aria-describedby");
assert.equal(
describedBy,
hintId,
"input aria-describedby must reference the hint's id",
);
});
test("PersonaProviderApiKeyField_omits_hint_when_envVarName_absent", () => {
const html = renderToStaticMarkup(
React.createElement(PersonaProviderApiKeyField, makeProps()),
);
assert.ok(
!html.includes("aria-describedby"),
"no aria-describedby when envVarName is omitted",
);
assert.ok(
!html.includes("persona-provider-api-key-hint"),
"hint id must not appear when envVarName is omitted",
);
});
test("PersonaProviderApiKeyField_two_instances_have_unique_ids_and_each_aria_describedby_resolves_to_own_hint", () => {
// Render BOTH instances in a single renderToStaticMarkup call — this
// mirrors the real nested-dialog DOM where AgentInstanceEditDialog's
// credential field and the nested AgentDefaultsDialog's field are
// simultaneously mounted under the same React root. A shared root is what
// makes React.useId() guarantee uniqueness; two separate renderToStaticMarkup
// calls each reset the counter and would produce the same ID.
const combined = renderToStaticMarkup(
React.createElement(
React.Fragment,
null,
React.createElement(
PersonaProviderApiKeyField,
makeProps({
label: "Anthropic API Key",
envVarName: "ANTHROPIC_API_KEY",
}),
),
React.createElement(
PersonaProviderApiKeyField,
makeProps({
label: "OpenAI Runtime API Key",
envVarName: "OPENAI_COMPAT_API_KEY",
}),
),
),
);
// Two hint paragraph ids must be present and distinct.
const allHintIds = extractAllAttrs(combined, "id").filter((id) =>
id.startsWith("persona-provider-api-key-hint-"),
);
assert.equal(allHintIds.length, 2, "exactly two hint ids must be present");
const [hintIdA, hintIdB] = allHintIds;
assert.notEqual(hintIdA, hintIdB, "two instances must not share a hint id");
// Each input's aria-describedby must match its own hint id (same order).
const allDescribedBy = extractAllAttrs(combined, "aria-describedby");
assert.equal(
allDescribedBy.length,
2,
"exactly two aria-describedby attributes must be present",
);
assert.equal(
allDescribedBy[0],
hintIdA,
"instance A: aria-describedby must reference instance A's own hint",
);
assert.equal(
allDescribedBy[1],
hintIdB,
"instance B: aria-describedby must reference instance B's own hint",
);
// Confirm each instance names its own env var in the rendered output.
assert.ok(
combined.includes("ANTHROPIC_API_KEY"),
"combined output must name ANTHROPIC_API_KEY",
);
assert.ok(
combined.includes("OPENAI_COMPAT_API_KEY"),
"combined output must name OPENAI_COMPAT_API_KEY",
);
});