Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

fix(web): 代理选项的保存被拒与读取失败,原因都留在设置区里,不再只活六秒

lmliheng68a61b3a16d9
diff --git a/changelog/unreleased/2026-11-02-proxy-settings-refusal.md b/changelog/unreleased/2026-11-02-proxy-settings-refusal.md
new file mode 100644
index 00000000..6745a853
--- /dev/null
+++ b/changelog/unreleased/2026-11-02-proxy-settings-refusal.md
@@ -0,0 +1,56 @@
+# A refused proxy save, and settings that fail to load, say why in the section now
+
+- **Date:** 2026-11-02
+- **Type:** fix
+- **Scope:** `web`
+- **Breaking:** no
+
+[中文](2026-11-02-proxy-settings-refusal.zh.md)
+
+Settings → Server → proxy options had two failures that handed their reason to a toast that dismisses
+itself after six seconds:
+
+- **A save refused for anything but the address** (a lost session, a dead server, a 500). Nothing was
+ written — both switches and the address come back exactly as they were, and Save again is the retry —
+ but all a toast left on screen was a form that looked untouched. The address's own rejection
+ (`invalid_proxy_url`) already draws under the input, and stays there; this change does not touch it.
+- **Settings that fail to load.** Every control stays disabled and shows the form's own defaults rather
+ than the server's, and the page said nothing about it that outlived six seconds — what remained was a
+ pane of greyed controls with no explanation.
+
+Both sentences are drawn in the section now: the refused save's under the address and above the Save
+row (which is its retry), the failed read's above the controls (read first, because the switches and
+the address below it do not represent the server while it stands). Both are danger-toned,
+`role="alert"`, clamped to two lines with the whole sentence on hover, and both stay — the save's until
+the next save replaces it, the read's until the section is left and opened again (a remount re-fetches,
+and that is the retry). Neither carries a button of its own.
+
+## Details
+
+- `packages/web/src/features/settings/proxy-save-refusal.ts` (new): `proxySaveRefusal(error)` returns
+ `{ field: "address" | "section", text }` — which refusal goes in which slot is one function with a
+ test rather than an `if` inside a render. `invalid_proxy_url` takes `address` (the server names the
+ one field it validates); everything else takes `section`. The sentence comes from the shared
+ `refusalText()`, so a server's answer is still localized.
+- `packages/web/src/features/settings/proxy-section.tsx`: two states beside the existing `addressError`
+ (which draws under the input) — `saveRefusal` (cleared when the next save starts) and `loadRefusal`
+ (cleared only by a remount). Neither failure path uses `toastError` any more. The reachability test
+ (7.66) is untouched.
+
+## Tests
+
+- `packages/web/test/proxy-save-refusal.test.ts` (new, 5 cases): `invalid_proxy_url` lands in the
+ address slot with the localized sentence (not the English original); any other server code lands in
+ the section slot; a sentence the client raised itself is used as it is; a value with nothing to say
+ falls back to the generic text; the sentence follows the active language.
+- `packages/web/e2e/proxy-settings-refusal.spec.mjs` (new, two cases on a real server): a refused save —
+ one sentence in the section, no clickable toast, still there after seven seconds, the server still
+ without that address, the input still holding what was typed; once the write is let through, the
+ sentence goes and the address really lands. A failed read — the sentence above the controls, both
+ switches, the address, Save and the probe all disabled, still there after seven seconds; leaving the
+ section and opening it again (a remount is the retry) clears it and opens the controls.
+
+## Compatibility
+
+- No interface, request or storage change: a save is the same `PUT /api/admin/settings`, a read the same
+ `GET /api/admin/settings`. Only where the sentence is drawn on failure has moved.
diff --git a/changelog/unreleased/2026-11-02-proxy-settings-refusal.zh.md b/changelog/unreleased/2026-11-02-proxy-settings-refusal.zh.md
new file mode 100644
index 00000000..9c217ece
--- /dev/null
+++ b/changelog/unreleased/2026-11-02-proxy-settings-refusal.zh.md
@@ -0,0 +1,46 @@
+# 代理选项的保存被拒、设置读不出来时,原因都留在设置区里,不再只活六秒
+
+- **日期:** 2026-11-02
+- **类型:** fix
+- **范围:** `web`
+- **破坏性:** 无
+
+[English](2026-11-02-proxy-settings-refusal.md)
+
+设置 → 服务器 →「代理选项」上有两条失败的路,此前都把原因交给六秒后自己消失的 toast:
+
+- **保存被拒,而原因不是地址**(会话丢了、服务端死了、500)。这一次什么都没写进去 —— 两个开关与
+ 地址都原样不动,再按一次保存就是重试 —— 但 toast 留在屏上的只是一个看起来没被碰过的表单。
+ 地址那一类拒绝(`invalid_proxy_url`)本来就已经画在地址框下面,这一条不碰它。
+- **设置读不出来**。此时每一个控件都停用、显示的是表单自己的默认值而不是服务端的值,而页面对此
+ 一个字都没有留下 —— 读完六秒的 toast,剩下的是一整节灰着的控件。
+
+现在两句话都画在设置区里:保存被拒的那句在地址下面、保存按钮上面(它就是这次保存的重试),读不出来
+的那句在控件上面(先读到它,因为下面的开关与地址此刻并不代表服务端)。两句都是 danger 色、
+`role="alert"`、限两行、整句挂悬停,并且一直留着 —— 保存那句留到下一次保存把它换掉,读不出来那句留到
+离开这一节再回来(重新挂载即重新拉取,那就是重试)。两条句子都没有自己的按钮。
+
+## 细节
+
+- `packages/web/src/features/settings/proxy-save-refusal.ts`(新):`proxySaveRefusal(error)` 给出
+ `{ field: "address" | "section", text }` —— 什么拒绝画在哪一个槽,是一句话加一条用例,而不是散在渲染
+ 里的一个 `if`。`invalid_proxy_url` 走 `address`(服务端点名了唯一被校验的那个字段),其余一律走
+ `section`;句子走共享的 `refusalText()`,因此服务端的答复照样本地化。
+- `packages/web/src/features/settings/proxy-section.tsx`:`addressError`(原有,画在地址框下面)之外多
+ 两个状态 —— `saveRefusal`(下一次保存开始时清空)与 `loadRefusal`(只在重新挂载时自然清掉);两条
+ 失败的路不再 `toastError`。连通性测速那一条(7.66)不动。
+
+## 测试
+
+- `packages/web/test/proxy-save-refusal.test.ts`(新,5 条):`invalid_proxy_url` 落地址槽且是本地化的
+ 那句(不是英文原文);别的服务端码落设置区槽;客户端自己抛的句子原样用;什么都没说的值退回通用那句;
+ 句子跟着当前语言。
+- `packages/web/e2e/proxy-settings-refusal.spec.mjs`(新,真服务端两条):保存被拒 —— 句子在设置区里
+ 一条、通知区 0 个可点按钮、七秒后仍在、服务端仍没有那个地址、输入框仍留着敲进去的值,放行后再存
+ 一次句子消失而地址真的落了库;读不出来 —— 句子在控件上面、两个开关 / 地址 / 保存 / 测速全禁用、
+ 七秒后仍在,退出再进这一节(重新挂载即重试)后句子消失、控件打开。
+
+## 兼容
+
+- 接口、请求与存储都没变:保存还是同一个 `PUT /api/admin/settings`,读还是同一个
+ `GET /api/admin/settings`。改变的只有失败时那句话画在哪里。
diff --git a/packages/web/e2e/proxy-settings-refusal.spec.mjs b/packages/web/e2e/proxy-settings-refusal.spec.mjs
new file mode 100644
index 00000000..877e9237
--- /dev/null
+++ b/packages/web/e2e/proxy-settings-refusal.spec.mjs
@@ -0,0 +1,186 @@
+/**
+ * Regression: the two refusals the proxy options (Settings → 服务器 → 代理选项) have no field to
+ * draw on say why in the section itself, and the sentences outlive the six seconds a toast lives.
+ *
+ * A save refused for anything but the address — a lost session, a dead server, a 500 — writes
+ * nothing, so the two switches and the address come back exactly as they were and Save is the
+ * retry; the reason used to arrive as a notification over that untouched form. Settings that fail
+ * to LOAD are the sharper half: every control stays disabled showing the form's defaults, and the
+ * page said nothing about it that survived six seconds.
+ *
+ * The address's own rejection (`invalid_proxy_url`) is not this spec's business — it already draws
+ * under the input, and the assertion that it still does is here only so the two slots cannot be
+ * confused with each other.
+ *
+ * Both refusals are stubbed at `page.route` rather than provoked from the server: what is under
+ * test is what the page does with an answer, and the sentence asserted is the app's own for the
+ * code the server would send (`internal`), behind the shared lead-in.
+ */
+import { test, expect } from "@playwright/test";
+import { ADMIN_ID, ADMIN_PASSWORD, login } from "./auth.mjs";
+
+const BASE = process.env.BASE_URL;
+const MOCK = process.env.MOCK_URL;
+
+/** The line the section draws: the shared lead-in plus the app's sentence for `internal`. */
+const REFUSED = "操作被拒绝:服务器内部错误,请稍后重试。";
+
+/** The address a save of this spec sends; its own form is what proves nothing was written. */
+const TYPED = "http://127.0.0.1:9";
+
+/**
+ * Sign in as the seeded administrator (the section is admin-only) and give the Project a model —
+ * the onboarding overlay covers the page without one.
+ */
+async function ready(page) {
+ await login(page.request, ADMIN_ID, ADMIN_PASSWORD);
+ const projectId = (await (await page.request.get(`${BASE}/api/projects`)).json()).projects[0]
+ .projectId;
+ const put = await page.request.put(`${BASE}/api/projects/${projectId}/models`, {
+ data: {
+ defaultModel: { provider: "custom", modelId: "claude-4-8" },
+ models: [
+ {
+ provider: "custom",
+ modelId: "claude-4-8",
+ apiKey: "sk-mock",
+ baseUrl: MOCK,
+ contextWindow: 200000,
+ },
+ ],
+ },
+ });
+ expect(put.ok(), "put models").toBeTruthy();
+ return projectId;
+}
+
+/** The settings dialog, opened the way a person opens it: the user menu, then 代理选项. */
+async function openProxy(page) {
+ // The user menu shares its name with the Project switcher, so take the last match (as chat.spec).
+ await page.getByRole("button", { name: ADMIN_ID }).last().click();
+ await page.getByRole("menuitem", { name: "设置", exact: true }).click();
+ const settings = page.getByRole("dialog", { name: "设置" });
+ await expect(settings).toBeVisible();
+ await settings.getByRole("button", { name: "代理选项", exact: true }).click();
+ return settings;
+}
+
+/** The form's own `<section>`: the two switches, the address and (when refused) the sentence. */
+function proxyForm(settings) {
+ return settings.locator("section").filter({ hasText: "应用程序使用代理" });
+}
+
+/**
+ * Answer one method of `/api/admin/settings` with a refusal (500 `internal`) until `letThrough()`.
+ * Counted, so the spec can tell whether a request was really sent at all.
+ */
+async function stubSettings(page, method, seen) {
+ let refuse = true;
+ await page.route("**/api/admin/settings", async (route) => {
+ if (route.request().method() !== method) return route.fallback();
+ seen.push(route.request().postData() ?? "");
+ if (!refuse) return route.fallback();
+ await route.fulfill({
+ status: 500,
+ contentType: "application/json",
+ body: JSON.stringify({ error: { code: "internal", message: "Internal server error" } }),
+ });
+ });
+ return () => {
+ refuse = false;
+ };
+}
+
+test("a refused save keeps its reason above the Save row, past the toast it used to be", async ({
+ page,
+}) => {
+ const projectId = await ready(page);
+ const attempts = [];
+ const letThrough = await stubSettings(page, "PUT", attempts);
+
+ await page.goto("/");
+ const settings = await openProxy(page);
+ const form = proxyForm(settings);
+ await expect(form.getByLabel("代理地址")).toBeEnabled();
+ await expect(form.getByRole("alert")).toHaveCount(0);
+ const notices = page.getByRole("region", { name: "通知" });
+
+ // What the server holds before the refused save: nothing — the address is empty.
+ const before = (await (await page.request.get(`${BASE}/api/admin/settings`)).json()).settings;
+ expect(before.proxyUrl ?? null).toBeNull();
+
+ await form.getByLabel("代理地址").fill(TYPED);
+ await settings.getByRole("button", { name: "保存", exact: true }).click();
+
+ // The sentence, in the form it was refused in — once, and not as a notification over it.
+ await expect(form.getByRole("alert")).toHaveText(REFUSED);
+ await expect(form.getByRole("alert")).toHaveCount(1);
+ expect(attempts).toHaveLength(1);
+ await expect(notices.getByRole("button")).toHaveCount(0);
+
+ // Nothing was written: the server still has no address, and the form still holds what was typed.
+ const after = (await (await page.request.get(`${BASE}/api/admin/settings`)).json()).settings;
+ expect(after.proxyUrl ?? null).toBeNull();
+ await expect(form.getByLabel("代理地址")).toHaveValue(TYPED);
+ // The address slot is not where a server failure goes.
+ await expect(form.getByText("代理地址无效", { exact: false })).toHaveCount(0);
+
+ // Six seconds is how long a toast lives; this sentence has to still be there after it.
+ await page.waitForTimeout(7_000);
+ await expect(form.getByRole("alert")).toHaveText(REFUSED);
+ await expect(notices.getByRole("button")).toHaveCount(0);
+
+ // The next save is the retry: it lands, so the sentence of the last attempt goes with it.
+ letThrough();
+ await settings.getByRole("button", { name: "保存", exact: true }).click();
+ await expect(form.getByRole("alert")).toHaveCount(0);
+ const saved = (await (await page.request.get(`${BASE}/api/admin/settings`)).json()).settings;
+ expect(saved.proxyUrl).toBe(TYPED);
+ expect(attempts).toHaveLength(2);
+ // Left as it was found: the next run of this file starts from an empty address.
+ const reset = await page.request.put(`${BASE}/api/admin/settings`, {
+ data: { proxyForApp: true, proxyForAgent: true, proxyUrl: "" },
+ });
+ expect(reset.ok(), "reset the proxy").toBeTruthy();
+ expect(projectId).toBeTruthy();
+});
+
+test("settings that fail to load say so above the controls, which stay shut", async ({ page }) => {
+ await ready(page);
+ const reads = [];
+ const letThrough = await stubSettings(page, "GET", reads);
+
+ await page.goto("/");
+ const settings = await openProxy(page);
+ const form = proxyForm(settings);
+
+ // The sentence comes first in the section — it is read before the defaults under it.
+ await expect(form.getByRole("alert")).toHaveText(REFUSED);
+ await expect(form.getByRole("alert")).toHaveCount(1);
+ expect(reads.length).toBeGreaterThanOrEqual(1);
+ await expect(page.getByRole("region", { name: "通知" }).getByRole("button")).toHaveCount(0);
+
+ // Nothing hydrated: both switches, the address, Save and the probe are disabled — and the
+ // sentence is the only thing that says why.
+ for (const name of ["应用程序使用代理", "Agent 环境使用代理"]) {
+ await expect(form.getByRole("switch", { name })).toBeDisabled();
+ }
+ await expect(form.getByLabel("代理地址")).toBeDisabled();
+ await expect(settings.getByRole("button", { name: "保存", exact: true })).toBeDisabled();
+ await expect(settings.getByRole("button", { name: "测速", exact: true })).toBeDisabled();
+
+ // Still there after a toast's lifetime — the controls below it never come back on their own.
+ await page.waitForTimeout(7_000);
+ await expect(form.getByRole("alert")).toHaveText(REFUSED);
+ await expect(form.getByLabel("代理地址")).toBeDisabled();
+
+ // Leaving the section and coming back is the retry: this time the read lands, the sentence goes
+ // and the controls open.
+ letThrough();
+ const remountAt = reads.length;
+ await settings.getByRole("button", { name: "通用", exact: true }).click();
+ await settings.getByRole("button", { name: "代理选项", exact: true }).click();
+ await expect(proxyForm(settings).getByRole("alert")).toHaveCount(0);
+ await expect(proxyForm(settings).getByLabel("代理地址")).toBeEnabled();
+ expect(reads.length).toBeGreaterThan(remountAt);
+});
diff --git a/packages/web/src/features/settings/proxy-save-refusal.ts b/packages/web/src/features/settings/proxy-save-refusal.ts
new file mode 100644
index 00000000..7049e425
--- /dev/null
+++ b/packages/web/src/features/settings/proxy-save-refusal.ts
@@ -0,0 +1,30 @@
+/**
+ * Where a refused save of the proxy options is drawn, and what it says.
+ *
+ * The form has exactly one validated field — the address — and the server names that rejection
+ * (`invalid_proxy_url`), so that sentence belongs under the input it is about. Every other way a
+ * save can fail (a lost session, a dead server, a 500) is about the form as a whole: nothing was
+ * written, both switches and the address are exactly as they were, and Save is the retry. Those
+ * go to the section, which is what keeps them on screen at all — a toast dismisses itself after
+ * six seconds and leaves a form that looks untouched (7.19's rule, app-wide).
+ *
+ * The branch lives here rather than in the component so that which slot a failure lands in is one
+ * sentence of code with a test, not a condition inside a render.
+ */
+import { ApiError } from "../../api/client";
+import { refusalText } from "../../lib/api-error";
+
+export type ProxySaveRefusal = {
+ /** `address` draws under the address input; `section` draws under the form it was about. */
+ field: "address" | "section";
+ text: string;
+};
+
+/** Which slot the failed save's sentence goes to, already localized. */
+export function proxySaveRefusal(error: unknown): ProxySaveRefusal {
+ const text = refusalText(error);
+ if (error instanceof ApiError && error.code === "invalid_proxy_url") {
+ return { field: "address", text };
+ }
+ return { field: "section", text };
+}
diff --git a/packages/web/src/features/settings/proxy-section.tsx b/packages/web/src/features/settings/proxy-section.tsx
index e90497aa..668475b7 100644
--- a/packages/web/src/features/settings/proxy-section.tsx
+++ b/packages/web/src/features/settings/proxy-section.tsx
@@ -37,6 +37,15 @@
* the button, so the page shows concretely what will be requested and a reader can see that
* these are plain unauthenticated GETs. A frontend copy of that list could drift from the URLs
* the server really fetches, so it is fetched, not hard-coded.
+ *
+ * Two more refusals say why in this section rather than in a toast (7.67, the same rule). A save
+ * refused for anything but the address — a lost session, a dead server, a 500 — wrote nothing, so
+ * the form comes back exactly as it was and Save is the retry: its sentence sits above the Save
+ * row until the next save replaces it. Settings that fail to LOAD leave every control disabled
+ * with no explanation at all, so that sentence goes above the controls rather than below them —
+ * up there it is read first, which is what it is for: for as long as it stands, the two switches
+ * and the address field are showing the form's defaults, not what the server holds. Leaving the
+ * section and coming back re-mounts it, and that is the retry; neither sentence carries a button.
*/
import { useEffect, useRef, useState } from "react";
import type {
@@ -45,22 +54,14 @@ import type {
ProxyProbeTargetDto,
ServerSettings,
} from "@adelie/server/api";
-import {
- Button,
- Input,
- SettingsSection,
- ToggleRow,
- toastError,
- toastInfo,
- toastSuccess,
-} from "@adelie/ui";
+import { Button, Input, SettingsSection, ToggleRow, toastInfo, toastSuccess } from "@adelie/ui";
import * as api from "../../api/endpoints";
-import { ApiError } from "../../api/client";
+import { refusalText } from "../../lib/api-error";
import { S } from "../../lib/strings";
-import { apiErrorText } from "../../lib/api-error";
import { toneInk } from "../../lib/tone";
import { probeRefusalFor } from "./proxy-probe";
import type { ProxyProbeRefusal } from "./proxy-probe";
+import { proxySaveRefusal } from "./proxy-save-refusal";
/** Brand names: identical in every locale, so they are keyed off the server's provider id rather than doubled into both dictionaries. */
const PROVIDER_LABEL: Record<ProxyProbeProvider, string> = {
@@ -83,6 +84,16 @@ export function ProxySection() {
const [proxyUrl, setProxyUrl] = useState("");
/** Inline error under the address input (the server's invalid_proxy_url rejection). */
const [addressError, setAddressError] = useState<string | null>(null);
+ /**
+ * Why the last save was refused for anything but the address, drawn above the Save row until
+ * the next save replaces it. Nothing was written, so the form and the sentence agree.
+ */
+ const [saveRefusal, setSaveRefusal] = useState<string | null>(null);
+ /**
+ * Why the settings could not be read, drawn above the controls. Non-null means nothing has been
+ * hydrated: every control below is disabled and showing the form's defaults, not the server's.
+ */
+ const [loadRefusal, setLoadRefusal] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
/** What the probe would request, listed before it is run (null until it arrives). */
const [targets, setTargets] = useState<ProxyProbeTargetDto[] | null>(null);
@@ -123,11 +134,13 @@ export function ProxySection() {
if (!cancelled) adopt(res.settings);
})
.catch((e: unknown) => {
- // Controls stay disabled; leaving and returning to the section retries the fetch.
- if (!cancelled) toastError(apiErrorText(e));
+ // Kept above the controls, not toasted: they stay disabled until a later mount retries,
+ // and a section of dead controls with no sentence beside it is the worse answer.
+ if (!cancelled) setLoadRefusal(refusalText(e));
});
- // The target list is static server-side, so its failure is not worth a second toast on
- // top of the settings one — the block simply stays empty until a retry succeeds.
+ // The target list is static server-side, so its failure gets no sentence of its own on top of
+ // the settings one — the block simply stays empty, and its button stays disabled, until a
+ // retry succeeds.
void api
.adminGetProxyProbeTargets()
.then((res) => {
@@ -151,18 +164,18 @@ export function ProxySection() {
}
setBusy(true);
setAddressError(null);
+ // Another attempt: the sentence about the last one is stale from this moment on.
+ setSaveRefusal(null);
try {
const res = await api.adminPutSettings({ proxyForApp, proxyForAgent, proxyUrl });
adopt(res.settings);
toastSuccess(S.common.saved);
} catch (e) {
- // The address is the only validated field: its rejection renders inline;
- // anything else (auth, network) is a generic failure toast.
- if (e instanceof ApiError && e.code === "invalid_proxy_url") {
- setAddressError(apiErrorText(e));
- } else {
- toastError(apiErrorText(e));
- }
+ // The address is the only validated field, so its rejection renders under it; anything else
+ // (auth, network, a 500) is about the whole form and renders above the Save row.
+ const refusal = proxySaveRefusal(e);
+ if (refusal.field === "address") setAddressError(refusal.text);
+ else setSaveRefusal(refusal.text);
} finally {
setBusy(false);
}
@@ -216,6 +229,20 @@ export function ProxySection() {
</Button>
}
>
+ {/* Above the controls, because it is about all of them: with no settings read, the two
+ switches and the address below show the form's defaults, and this sentence is the only
+ thing that says so. Clamped to two lines with the whole of it on hover, because a
+ server's words run long. */}
+ {loadRefusal !== null && (
+ <p
+ role="alert"
+ data-tooltip={S.common.refused(loadRefusal)}
+ data-tooltip-content="text"
+ className={`line-clamp-2 break-words text-xs ${toneInk.danger}`}
+ >
+ {S.common.refused(loadRefusal)}
+ </p>
+ )}
<ToggleRow
variant="plain"
label={S.settings.proxyForApp}
@@ -242,6 +269,18 @@ export function ProxySection() {
if (addressError !== null) setAddressError(null);
}}
/>
+ {/* The save's own failure, just above the Save button that is its retry. The address's
+ rejection is drawn under the input instead (7.67). */}
+ {saveRefusal !== null && (
+ <p
+ role="alert"
+ data-tooltip={S.common.refused(saveRefusal)}
+ data-tooltip-content="text"
+ className={`line-clamp-2 break-words text-xs ${toneInk.danger}`}
+ >
+ {S.common.refused(saveRefusal)}
+ </p>
+ )}
</SettingsSection>
<section className="mt-6">
<div className="flex items-center justify-between gap-3">
diff --git a/packages/web/test/proxy-save-refusal.test.ts b/packages/web/test/proxy-save-refusal.test.ts
new file mode 100644
index 00000000..a61da160
--- /dev/null
+++ b/packages/web/test/proxy-save-refusal.test.ts
@@ -0,0 +1,62 @@
+/**
+ * Where a refused save of the proxy options lands (`proxySaveRefusal`). The address is the only
+ * field the server validates, so its own rejection belongs under the input; every other way a
+ * save can fail is about the whole form, and a toast would dismiss itself over a form that looks
+ * untouched.
+ *
+ * - `invalid_proxy_url` → the address slot, in the app's own words for that code.
+ * - Any other server code → the section slot, still localized (not the raw English message).
+ * - A value the client raised itself → the section slot, with its own sentence.
+ * - Nothing to say → the section slot, with the generic failure.
+ */
+import { afterEach, describe, expect, it } from "vitest";
+import { ApiError } from "../src/api/client";
+import { S, setActiveStrings, zh as ZH } from "../src/lib/strings";
+import { en as EN } from "../src/lib/strings-en";
+import { proxySaveRefusal } from "../src/features/settings/proxy-save-refusal";
+
+const serverError = (status: number, code: string): ApiError =>
+ new ApiError(status, code, "RAW ENGLISH SERVER MESSAGE");
+
+afterEach(() => {
+ setActiveStrings(ZH);
+});
+
+describe("proxySaveRefusal", () => {
+ it("draws the address's own rejection under the input", () => {
+ const refusal = proxySaveRefusal(serverError(400, "invalid_proxy_url"));
+ expect(refusal.field).toBe("address");
+ // Localized from the code — the raw server message never reaches the reader.
+ expect(refusal.text).toBe(S.errors.byCode.invalid_proxy_url);
+ expect(refusal.text).not.toBe("RAW ENGLISH SERVER MESSAGE");
+ });
+
+ it("draws every other server refusal in the section", () => {
+ const refusal = proxySaveRefusal(serverError(500, "internal"));
+ expect(refusal.field).toBe("section");
+ expect(refusal.text).toBe(S.errors.byCode.internal);
+ expect(refusal.text).not.toBe("RAW ENGLISH SERVER MESSAGE");
+ });
+
+ it("keeps the app's own sentence for a failure raised here", () => {
+ const refusal = proxySaveRefusal(new Error("连接已断开"));
+ expect(refusal.field).toBe("section");
+ expect(refusal.text).toBe("连接已断开");
+ });
+
+ it("falls back to the generic text when the value says nothing", () => {
+ for (const value of [undefined, null, "boom", {}]) {
+ const refusal = proxySaveRefusal(value);
+ expect(refusal.field).toBe("section");
+ expect(refusal.text).toBe(S.common.unknownError);
+ }
+ });
+
+ it("follows the active language", () => {
+ setActiveStrings(EN);
+ const refusal = proxySaveRefusal(serverError(500, "internal"));
+ expect(refusal.field).toBe("section");
+ expect(refusal.text).toBe(EN.errors.byCode.internal);
+ expect(refusal.text).not.toBe(ZH.errors.byCode.internal);
+ });
+});