




























@@ -1,6 +1,15 @@
11import { describe, it, expect } from "vitest";
22import { ProxyConfigSchema } from "./zod-schema.proxy.js";
334+function expectProxyConfigFailure(value: unknown) {
5+const result = ProxyConfigSchema.safeParse(value);
6+expect(result.success).toBe(false);
7+if (result.success) {
8+throw new Error("Expected proxy config to fail schema validation.");
9+}
10+return result.error.issues;
11+}
12+413describe("ProxyConfigSchema", () => {
514it("accepts undefined (optional)", () => {
615expect(ProxyConfigSchema.parse(undefined)).toBeUndefined();
@@ -32,7 +41,8 @@ describe("ProxyConfigSchema", () => {
3241});
33423443it("rejects unknown loopbackMode values", () => {
35-expect(() => ProxyConfigSchema.parse({ loopbackMode: "bypass" })).toThrow();
44+const issues = expectProxyConfigFailure({ loopbackMode: "bypass" });
45+expect(issues.map((issue) => issue.path.join("."))).toContain("loopbackMode");
3646});
37473848it("rejects HTTPS proxy URLs because the node:http routing layer requires HTTP proxies", () => {
@@ -53,14 +63,18 @@ describe("ProxyConfigSchema", () => {
5363});
54645565it("rejects proxyUrl values that are not HTTP forward proxies", () => {
56-expect(() =>
57-ProxyConfigSchema.parse({ enabled: true, proxyUrl: "socks5://127.0.0.1" }),
58-).toThrow();
59-expect(() => ProxyConfigSchema.parse({ enabled: true, proxyUrl: "not-a-url" })).toThrow();
66+const socksIssues = expectProxyConfigFailure({
67+enabled: true,
68+proxyUrl: "socks5://127.0.0.1",
69+});
70+const invalidUrlIssues = expectProxyConfigFailure({ enabled: true, proxyUrl: "not-a-url" });
71+expect(socksIssues.map((issue) => issue.path.join("."))).toContain("proxyUrl");
72+expect(invalidUrlIssues.map((issue) => issue.path.join("."))).toContain("proxyUrl");
6073});
61746275it("rejects unknown keys (strict)", () => {
63-expect(() => ProxyConfigSchema.parse({ unknownKey: true })).toThrow();
76+const issues = expectProxyConfigFailure({ unknownKey: true });
77+expect(issues[0]?.code).toBe("unrecognized_keys");
6478});
65796680it("accepts enabled: false to disable the proxy", () => {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。