Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(e2e): drive the provider API-style probe against the real DOM
vastsae03edca59d42
diff --git a/apps/desktop/src/components/settings/ProviderSetupDialog.tsx b/apps/desktop/src/components/settings/ProviderSetupDialog.tsxindex dc1611a5d..7fc6adbb3 100644--- a/apps/desktop/src/components/settings/ProviderSetupDialog.tsx+++ b/apps/desktop/src/components/settings/ProviderSetupDialog.tsx@@ -23,17 +23,7 @@ import { useProviderModels } from "./useProviderModels";import { ModelSelectionPanes, useModelSelection } from "./ModelSelectionPanes";import { CUSTOM_SERVICE, ServicePicker } from "./ServicePicker";import type { ProviderCopyDraft } from "./provider-copy";-import { CUSTOM_PROVIDER_API_STYLES, isAccountOnlyApiStyle, needsCustomApiStyleChoice, providerSetupPreset } from "./provider-api-style";--const API_STYLE_LABEL_KEYS: Record<CatalogApiStyle, string> = {- chat_completions: "settings.apiStyleChatCompletions",- responses: "settings.apiStyleResponses",- anthropic_messages: "settings.apiStyleAnthropic",- google_generative_ai: "settings.apiStyleGoogle",- openai_codex_responses: "settings.apiStyleCodexResponses",- pi_messages: "settings.apiStylePiMessages",- opencode_go: "settings.apiStyleOpenCodeGo",-};+import { API_STYLE_LABEL_KEYS, CUSTOM_PROVIDER_API_STYLES, isAccountOnlyApiStyle, needsCustomApiStyleChoice, providerSetupPreset } from "./provider-api-style";type BaseUrlIssue = "invalid";diff --git a/apps/desktop/src/components/settings/provider-api-style.ts b/apps/desktop/src/components/settings/provider-api-style.tsindex 638a6578a..7f1df51ce 100644--- a/apps/desktop/src/components/settings/provider-api-style.ts+++ b/apps/desktop/src/components/settings/provider-api-style.ts@@ -15,6 +15,17 @@ export const CUSTOM_PROVIDER_API_STYLES = API_STYLES.filter((style) => !isAccountOnlyApiStyle(style) && style !== OPENCODE_GO_API_STYLE,);+/** Localized names for every transport in the catalog. */+export const API_STYLE_LABEL_KEYS: Record<CatalogApiStyle, string> = {+ chat_completions: "settings.apiStyleChatCompletions",+ responses: "settings.apiStyleResponses",+ anthropic_messages: "settings.apiStyleAnthropic",+ google_generative_ai: "settings.apiStyleGoogle",+ openai_codex_responses: "settings.apiStyleCodexResponses",+ pi_messages: "settings.apiStylePiMessages",+ opencode_go: "settings.apiStyleOpenCodeGo",+};+export function needsCustomApiStyleChoice(style: CatalogApiStyle,savedStyle?: string,diff --git a/scripts/e2e/provider-api-style.tsx b/scripts/e2e/provider-api-style.tsxindex 202a009a7..0aedce10b 100644--- a/scripts/e2e/provider-api-style.tsx+++ b/scripts/e2e/provider-api-style.tsx@@ -5,12 +5,22 @@ import { I18nextProvider } from "react-i18next";import { catalogs } from "@pi-desktop/i18n";import type { ProviderPublic, ProviderCreateInput, ProviderUpdateInput } from "@pi-desktop/shared";import { ProviderSetupDialog, type ProviderSetupDialogProps } from "../../apps/desktop/src/components/settings/ProviderSetupDialog";+import { API_STYLE_LABEL_KEYS, CUSTOM_PROVIDER_API_STYLES } from "../../apps/desktop/src/components/settings/provider-api-style";import { copyProviderConfiguration } from "../../apps/desktop/src/components/settings/provider-copy";import { api } from "../../apps/desktop/src/lib/api";declare global { var providerApiStyleProbe: () => Promise<unknown>; }const assert = (value: unknown, message: string) => { if (!value) throw new Error(message); };const pause = (ms = 0) => new Promise((resolve) => setTimeout(resolve, ms));+/* Anchored menus mount, position and reveal on animation frames, so wait for a+ frame instead of guessing a duration; the timeout keeps a throttled window+ from hanging the probe. */+const frame = () => new Promise<void>((resolve) => {+ const timeout = setTimeout(resolve, 50);+ requestAnimationFrame(() => { clearTimeout(timeout); resolve(); });+});+const ACCOUNT_ONLY_STYLES = ["openai_codex_responses", "pi_messages"] as const;+type AccountOnlyStyle = (typeof ACCOUNT_ONLY_STYLES)[number];const fixture = (apiStyle: string): ProviderPublic => ({id: "legacy", name: "Legacy custom", vendorKey: "custom", type: "openai_compatible",protocol: "openai_compatible", enabled: true, authKind: "api_key_and_base_url",@@ -42,6 +52,8 @@ globalThis.providerApiStyleProbe = async () => {discoveries.push(structuredClone(input));return { models: [], source: "remote" };};+ /* The root container only mounts React; every production surface under test+ renders itself through a portal, so all queries below are document-rooted. */const host = document.createElement("div");document.body.append(host);const root = createRoot(host);@@ -52,42 +64,123 @@ globalThis.providerApiStyleProbe = async () => {<ProviderSetupDialog key={++key} onClose={() => { closes++; }} onSaved={() => {}} {...props} /></I18nextProvider>));};- const click = (element: HTMLElement | null) => {+ const click = (element: HTMLElement | null | undefined) => {assert(element, "missing click target");flushSync(() => element!.click());};- const button = (key: string) => [...host.querySelectorAll<HTMLButtonElement>("button")]- .find((element) => element.textContent?.trim() === i18n.t(key))!;- const select = () => host.querySelector<HTMLSelectElement>(".provider-setup-custom-auth-row select")!;- const choose = (style: string) => {- const element = select();- flushSync(() => {- element.value = style;- element.dispatchEvent(new Event("change", { bubbles: true }));- });+ const button = (key: string) => [...document.querySelectorAll<HTMLButtonElement>("button")]+ .find((element) => element.textContent?.trim() === i18n.t(key));+ const control = (key: string) => {+ const element = button(key);+ assert(element, `missing control ${key}`);+ return element;+ };+ /* A Field explains itself through the help icon's accessible name, so read the+ row's explanations from the affordance that carries them. */+ const rowHints = () => [...document.querySelectorAll<HTMLElement>(+ ".provider-setup-custom-auth-row .ui-help-icon")]+ .map((element) => element.getAttribute("aria-label") ?? "");+ const apiStyleLabel = (style: string) =>+ i18n.t(API_STYLE_LABEL_KEYS[style as keyof typeof API_STYLE_LABEL_KEYS]);+ const customLabels = () => CUSTOM_PROVIDER_API_STYLES.map(apiStyleLabel);++ /*+ The API format row is a SettingsMenuSelect: a trigger button that reports the+ current label, and options rendered by AnchoredMenu through a portal into+ document.body. Open the real control, read the real option list, and click+ the real option button.+ */+ const apiStyleTrigger = () => document.querySelector<HTMLButtonElement>(+ ".provider-setup-custom-auth-row .settings-menu-select-trigger");+ const apiStyleMenu = () => document.querySelector<HTMLElement>(".settings-menu-select-menu");+ const apiStyleOptions = () => [...(apiStyleMenu()?.querySelectorAll<HTMLButtonElement>(+ ".settings-menu-select-option") ?? [])];+ const optionLabel = (option: HTMLElement) =>+ option.querySelector(".settings-menu-select-option-label")?.textContent?.trim()+ ?? option.textContent?.trim() ?? "";+ const optionSnapshot = () => apiStyleOptions().map((option) => ({+ label: optionLabel(option),+ current: option.getAttribute("aria-selected") === "true",+ disabled: option.disabled,+ }));+ const openApiStyleMenu = async () => {+ const trigger = apiStyleTrigger();+ assert(trigger, "api style control missing");+ assert(trigger.getAttribute("aria-haspopup") === "listbox", "api style control is not a menu select");+ assert(!apiStyleMenu(), "api style menu was already open");+ // AnchoredMenu closes a menu whose anchor sits outside the viewport.+ trigger.scrollIntoView({ block: "center" });+ await frame();+ click(trigger);+ await frame();+ assert(apiStyleMenu(), "api style menu did not open");+ assert(trigger.getAttribute("aria-expanded") === "true", "api style menu did not report aria-expanded");+ return trigger;+ };+ const closeApiStyleMenu = async () => {+ const trigger = apiStyleTrigger();+ assert(trigger, "api style control missing");+ assert(apiStyleMenu(), "api style menu is not open");+ click(trigger);+ await frame();+ assert(!apiStyleMenu(), "api style menu stayed open");};+ const chooseApiStyle = async (style: string) => {+ await openApiStyleMenu();+ const option = apiStyleOptions().find((item) => optionLabel(item) === apiStyleLabel(style));+ assert(option, `${style}: api style option missing`);+ assert(!option.disabled, `${style}: api style option is disabled`);+ click(option);+ await frame();+ assert(!apiStyleMenu(), `${style}: api style menu stayed open after choosing`);+ assert(apiStyleTrigger()?.getAttribute("aria-expanded") === "false", `${style}: menu did not collapse`);+ assert(apiStyleTrigger()?.textContent?.trim() === apiStyleLabel(style), `${style}: choice did not reach the control`);+ };+ /* The legacy protocol stays listed, selected and unusable as a new choice. */+ const assertAccountOnlyOption = async (style: AccountOnlyStyle, context: string) => {+ assert(apiStyleTrigger()?.textContent?.trim() === apiStyleLabel(style), `${context}: shows the wrong format`);+ await openApiStyleMenu();+ const options = optionSnapshot();+ assert(options.length === CUSTOM_PROVIDER_API_STYLES.length + 1, `${context}: option list length`);+ assert(options[0]!.label === apiStyleLabel(style), `${context}: account format is not the first option`);+ assert(options[0]!.current, `${context}: account format is not the current selection`);+ assert(options[0]!.disabled, `${context}: account format must remain visible without being a new choice`);+ assert(JSON.stringify(options.slice(1).map((option) => option.label)) === JSON.stringify(customLabels()),+ `${context}: custom formats changed`);+ await closeApiStyleMenu();+ };+const results: string[] = [];try {for (const locale of ["en", "zh-CN"]) {await i18n.changeLanguage(locale);render();- click(host.querySelector(".provider-service-trigger"));+ const serviceTrigger = document.querySelector<HTMLButtonElement>(".provider-service-trigger");+ assert(serviceTrigger, "service picker trigger missing");+ serviceTrigger.scrollIntoView({ block: "center" });+ await frame();+ click(serviceTrigger);click(document.querySelector(".provider-service-option"));- const options = [...select().options].map((option) => option.value);- assert(JSON.stringify(options) === JSON.stringify([- "chat_completions", "responses", "anthropic_messages", "google_generative_ai",- ]), `${locale}: new custom form exposes an account format`);+ await frame();+ await openApiStyleMenu();+ const newCustomOptions = optionSnapshot();+ assert(JSON.stringify(newCustomOptions.map((option) => option.label)) === JSON.stringify(customLabels()),+ `${locale}: new custom form exposes an account format`);+ for (const style of ACCOUNT_ONLY_STYLES) {+ assert(!newCustomOptions.some((option) => option.label === apiStyleLabel(style)),+ `${locale}: new custom form offers the account format ${style}`);+ }+ await closeApiStyleMenu();results.push(`${locale}:new-custom-options`);- for (const style of ["openai_codex_responses", "pi_messages"]) {+ for (const style of ACCOUNT_ONLY_STYLES) {const original = fixture(style);const before = JSON.stringify(original);render({ provider: original });- assert(select().value === style && select().selectedOptions[0].disabled,- "legacy format must remain visible without being a new choice");- assert(host.textContent?.includes(i18n.t("settings.apiStyleLegacyAccount")), "legacy explanation missing");- assert(!button("settings.saveProvider").disabled, "legacy unchanged save blocked");- click(button("settings.saveProvider")); await pause();+ await assertAccountOnlyOption(style, `${locale}:${style}:edit`);+ assert(rowHints().includes(i18n.t("settings.apiStyleLegacyAccount")), "legacy explanation missing");+ assert(!control("settings.saveProvider").disabled, "legacy unchanged save blocked");+ click(control("settings.saveProvider")); await pause();const update = updates.at(-1)!;assert(update.apiStyle === style && update.name === original.name && update.baseUrl === original.baseUrl,"legacy edit changed protocol/name/URL through preset matching");@@ -95,32 +188,32 @@ globalThis.providerApiStyleProbe = async () => {assert(update.models?.[0].alias === "Fixture alias", "legacy bindings changed");render({ provider: original });- choose("responses");- click(button("settings.saveProvider")); await pause();+ await chooseApiStyle("responses");+ click(control("settings.saveProvider")); await pause();assert(updates.at(-1)?.apiStyle === "responses", "explicit legacy protocol change not saved");const draft = copyProviderConfiguration(original, "Copy fixture");render({ initialDraft: draft });- assert(select().value === style, "copy silently converted protocol");- assert(button("settings.saveProvider").disabled, "copy accepts account-only protocol");- assert(host.textContent?.includes(i18n.t("settings.apiStyleChooseCustom")), "copy choice explanation missing");+ await assertAccountOnlyOption(style, `${locale}:${style}:copy`);+ assert(control("settings.saveProvider").disabled, "copy accepts account-only protocol");+ assert(rowHints().includes(i18n.t("settings.apiStyleChooseCustom")), "copy choice explanation missing");const discoveryCount = discoveries.length;await pause(650);assert(discoveries.length === discoveryCount, "unsupported copy triggered discovery");const createCount = creates.length;- click(button("settings.saveProvider")); await pause();+ click(control("settings.saveProvider")); await pause();assert(creates.length === createCount, "blocked copy was saved");- click(button("settings.cancel"));+ click(control("settings.cancel"));assert(closes > 0 && creates.length === createCount, "cancel saved a copy");render({ initialDraft: draft });- choose("anthropic_messages");+ await chooseApiStyle("anthropic_messages");await pause(650);const discovery = discoveries.at(-1)!;assert(discovery.apiStyle === "anthropic_messages" && !discovery.providerId && !discovery.apiKey,"copy discovery reused source protocol or credentials");- assert(!button("settings.saveProvider").disabled, "supported copy is blocked");- click(button("settings.saveProvider")); await pause();+ assert(!control("settings.saveProvider").disabled, "supported copy is blocked");+ click(control("settings.saveProvider")); await pause();const created = creates.at(-1)!;assert(created.apiStyle === "anthropic_messages" && created.authKind === "api_key_and_base_url","copy did not persist the explicitly chosen protocol");