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