Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import {
3 makeProviderSettingsSchema,
4 ProviderDriverKind,
5 SettingsSecretValue,
6 TrimmedString,
7} from "@t3tools/contracts";
8import * as Schema from "effect/Schema";
9
10import { providerClients } from "./providerDriverMeta";
11import {
12 deriveProviderSettingsFields,
13 nextProviderConfigWithFieldValue,
14 reconcileRowsDraft,
15 seedRowsDraft,
16} from "./ProviderSettingsForm";
17
18describe("ProviderSettingsForm helpers", () => {
19 it("keeps every masked field as a stored secret and offers a reference only where typed so", () => {
20 const settingsSchema = makeProviderSettingsSchema({
21 token: TrimmedString.pipe(
22 Schema.annotateKey({ providerSettingsForm: { control: "password" } }),
23 ),
24 apiKey: SettingsSecretValue,
25 });
26
27 expect(
28 deriveProviderSettingsFields({ settingsSchema }).map(
29 ({ key, secret, acceptsSecretReference }) => ({ key, secret, acceptsSecretReference }),
30 ),
31 ).toEqual([
32 { key: "token", secret: true, acceptsSecretReference: undefined },
33 { key: "apiKey", secret: true, acceptsSecretReference: true },
34 ]);
35 });
36
37 it("preserves unknown config keys while omitting empty configurable fields", () => {
38 const opencode = providerClients.get(ProviderDriverKind.make("opencode"));
39 expect(opencode).toBeDefined();
40
41 const serverUrl = deriveProviderSettingsFields(opencode!).find(
42 (field) => field.key === "serverUrl",
43 );
44 expect(serverUrl).toBeDefined();
45
46 const next = nextProviderConfigWithFieldValue(
47 { forkOwned: 1, serverUrl: "http://127.0.0.1:4096" },
48 serverUrl!,
49 "",
50 );
51
52 expect(next).toEqual({ forkOwned: 1 });
53 });
54
55 it("shows Antigravity credential fields only for the sign-in methods that read them, or while set", () => {
56 const antigravity = providerClients.get(ProviderDriverKind.make("antigravity"));
57 expect(antigravity).toBeDefined();
58 const keysFor = (value?: unknown) =>
59 deriveProviderSettingsFields(antigravity!, value).map((field) => field.key);
60
61 expect(keysFor({})).toEqual(["authMethod", "binaryPath"]);
62 expect(
63 keysFor({
64 authMethod: "oauth-personal",
65 apiKey: { provider: "vault", reference: "antigravity" },
66 gcpLocation: "us-central1",
67 }),
68 ).toEqual(["authMethod", "apiKey", "gcpLocation", "binaryPath"]);
69 expect(keysFor({ authMethod: "oauth-business" })).toEqual([
70 "authMethod",
71 "gcpProject",
72 "gcpLocation",
73 "binaryPath",
74 ]);
75 expect(keysFor({ authMethod: "gemini-api-key" })).toEqual([
76 "authMethod",
77 "apiKey",
78 "binaryPath",
79 ]);
80 expect(keysFor({ authMethod: "agent-platform" })).toEqual([
81 "authMethod",
82 "apiKey",
83 "gcpProject",
84 "gcpLocation",
85 "binaryPath",
86 ]);
87 expect(keysFor()).toEqual(["authMethod", "apiKey", "gcpProject", "gcpLocation", "binaryPath"]);
88 });
89
90 it("does not store a text value its field's schema rejects", () => {
91 const claude = providerClients.get(ProviderDriverKind.make("claudeAgent"));
92 expect(claude).toBeDefined();
93 const autoCompactWindow = deriveProviderSettingsFields(claude!).find(
94 (field) => field.key === "autoCompactWindow",
95 );
96 expect(autoCompactWindow).toBeDefined();
97
98 for (const invalid of ["300k", "50000"]) {
99 expect(
100 nextProviderConfigWithFieldValue({ launchArgs: "--x" }, autoCompactWindow!, invalid),
101 ).toEqual({ launchArgs: "--x" });
102 }
103 expect(
104 nextProviderConfigWithFieldValue({ launchArgs: "--x" }, autoCompactWindow!, "300000"),
105 ).toEqual({ launchArgs: "--x", autoCompactWindow: "300000" });
106 });
107
108 it("stores false boolean fields when true is the default", () => {
109 const next = nextProviderConfigWithFieldValue(
110 undefined,
111 {
112 key: "experimental",
113 control: "switch",
114 label: "Experimental",
115 clearWhenEmpty: "omit",
116 defaultBooleanValue: true,
117 },
118 false,
119 );
120
121 expect(next).toEqual({ experimental: false });
122 });
123});
124
125describe("reconcileRowsDraft", () => {
126 let nextId = 0;
127 const toRows = (values: ReadonlyArray<string>) =>
128 values.map((value) => ({ id: nextId++, value }));
129 const equal = (left: ReadonlyArray<string>, right: ReadonlyArray<string>) =>
130 left.length === right.length && left.every((value, index) => value === right[index]);
131
132 it("keeps rows when an equal value arrives with a new identity", () => {
133 const draft = seedRowsDraft(["a"], toRows);
134 expect(reconcileRowsDraft(draft, ["a"], equal, toRows)).toBe(draft);
135 });
136
137 it("keeps row ids when the published value echoes back", () => {
138 const seeded = seedRowsDraft(["a"], toRows);
139 const edited = { rows: [...seeded.rows, { id: 99, value: "" }], seen: seeded.seen };
140 const draft = { ...edited, lastPublished: ["a", ""] };
141 const next = reconcileRowsDraft(draft, ["a", ""], equal, toRows);
142 expect(next.rows).toBe(draft.rows);
143 expect(next.seen).toEqual(["a", ""]);
144 expect(next.lastPublished).toBeUndefined();
145 });
146
147 it("reseeds rows when the value changes from outside", () => {
148 const draft = { ...seedRowsDraft(["a"], toRows), lastPublished: ["b"] };
149 const next = reconcileRowsDraft(draft, ["c"], equal, toRows);
150 expect(next.rows.map((row) => row.value)).toEqual(["c"]);
151 expect(next.lastPublished).toBeUndefined();
152 });
153});