Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { ProviderDriverKind } from "@t3tools/contracts";
3
4import { DRIVER_OPTION_BY_VALUE } from "./providerDriverMeta";
5import {
6 deriveProviderSettingsFields,
7 nextProviderConfigWithFieldValue,
8 readProviderConfigBoolean,
9 readProviderConfigString,
10} from "./ProviderSettingsForm";
11
12describe("ProviderSettingsForm helpers", () => {
13 it("derives visible provider config fields from the client definition schema", () => {
14 const codex = DRIVER_OPTION_BY_VALUE[ProviderDriverKind.make("codex")];
15
16 expect(codex).toBeDefined();
17 expect(deriveProviderSettingsFields(codex!).map((field) => field.key)).toEqual([
18 "binaryPath",
19 "homePath",
20 "shadowHomePath",
21 ]);
22 });
23
24 it("sources labels and descriptions from schema annotations", () => {
25 const opencode = DRIVER_OPTION_BY_VALUE[ProviderDriverKind.make("opencode")];
26 expect(opencode).toBeDefined();
27
28 const serverPassword = deriveProviderSettingsFields(opencode!).find(
29 (field) => field.key === "serverPassword",
30 );
31
32 expect(serverPassword).toMatchObject({
33 label: "Server password",
34 description: "Stored in plain text on disk.",
35 control: "password",
36 });
37 });
38
39 it("preserves unknown config keys while omitting empty configurable fields", () => {
40 const opencode = DRIVER_OPTION_BY_VALUE[ProviderDriverKind.make("opencode")];
41 expect(opencode).toBeDefined();
42
43 const serverUrl = deriveProviderSettingsFields(opencode!).find(
44 (field) => field.key === "serverUrl",
45 );
46 expect(serverUrl).toBeDefined();
47
48 const next = nextProviderConfigWithFieldValue(
49 { forkOwned: 1, serverUrl: "http://127.0.0.1:4096" },
50 serverUrl!,
51 "",
52 );
53
54 expect(next).toEqual({ forkOwned: 1 });
55 });
56
57 it("reads non-string config values as blank strings", () => {
58 expect(readProviderConfigString({ binaryPath: 123 }, "binaryPath")).toBe("");
59 });
60
61 it("omits false boolean fields when clearWhenEmpty is omit", () => {
62 const next = nextProviderConfigWithFieldValue(
63 { forkOwned: 1, experimental: true },
64 {
65 key: "experimental",
66 control: "switch",
67 label: "Experimental",
68 clearWhenEmpty: "omit",
69 defaultBooleanValue: false,
70 },
71 false,
72 );
73
74 expect(next).toEqual({ forkOwned: 1 });
75 });
76
77 it("omits true boolean fields when true is the default", () => {
78 const next = nextProviderConfigWithFieldValue(
79 { forkOwned: 1, experimental: false },
80 {
81 key: "experimental",
82 control: "switch",
83 label: "Experimental",
84 clearWhenEmpty: "omit",
85 defaultBooleanValue: true,
86 },
87 true,
88 );
89
90 expect(next).toEqual({ forkOwned: 1 });
91 });
92
93 it("stores false boolean fields when true is the default", () => {
94 const next = nextProviderConfigWithFieldValue(
95 undefined,
96 {
97 key: "experimental",
98 control: "switch",
99 label: "Experimental",
100 clearWhenEmpty: "omit",
101 defaultBooleanValue: true,
102 },
103 false,
104 );
105
106 expect(next).toEqual({ experimental: false });
107 });
108
109 it("preserves false boolean fields when clearWhenEmpty is persist", () => {
110 const next = nextProviderConfigWithFieldValue(
111 undefined,
112 {
113 key: "experimental",
114 control: "switch",
115 label: "Experimental",
116 clearWhenEmpty: "persist",
117 },
118 false,
119 );
120
121 expect(next).toEqual({ experimental: false });
122 });
123
124 it("reads non-boolean config values as false booleans", () => {
125 expect(readProviderConfigBoolean({ experimental: "true" }, "experimental")).toBe(false);
126 });
127
128 it("reads missing boolean config values from the supplied default", () => {
129 expect(readProviderConfigBoolean({}, "experimental", true)).toBe(true);
130 });
131});