Skip to content

Commit a4ac493

Browse files
Handle default boolean provider settings
- preserve schema-defined boolean defaults in provider settings - omit default-valued switch fields when clearWhenEmpty is omit - add coverage for boolean read/write defaults
1 parent 77e6873 commit a4ac493

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

‎apps/web/src/components/settings/ProviderSettingsForm.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,46 @@ describe("ProviderSettingsForm helpers", () => {
6666
control: "switch",
6767
label: "Experimental",
6868
clearWhenEmpty: "omit",
69+
defaultBooleanValue: false,
6970
},
7071
false,
7172
);
7273

7374
expect(next).toEqual({ forkOwned: 1 });
7475
});
7576

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+
76109
it("preserves false boolean fields when clearWhenEmpty is persist", () => {
77110
const next = nextProviderConfigWithFieldValue(
78111
undefined,
@@ -91,4 +124,8 @@ describe("ProviderSettingsForm helpers", () => {
91124
it("reads non-boolean config values as false booleans", () => {
92125
expect(readProviderConfigBoolean({ experimental: "true" }, "experimental")).toBe(false);
93126
});
127+
128+
it("reads missing boolean config values from the supplied default", () => {
129+
expect(readProviderConfigBoolean({}, "experimental", true)).toBe(true);
130+
});
94131
});

‎apps/web/src/components/settings/ProviderSettingsForm.tsx‎

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export interface ProviderSettingsFieldModel {
2222
readonly description?: string | undefined;
2323
readonly placeholder?: string | undefined;
2424
readonly clearWhenEmpty: "omit" | "persist";
25+
readonly defaultBooleanValue?: boolean | undefined;
2526
}
2627

2728
function titleizeFieldKey(key: string): string {
@@ -59,6 +60,17 @@ function readProviderSettingsFormSchemaAnnotation(
5960
return Schema.resolveAnnotations(definition.settingsSchema)?.providerSettingsFormSchema ?? {};
6061
}
6162

63+
function readFieldBooleanDefault(
64+
fieldSchema: ProviderClientDefinition["settingsSchema"]["fields"][string],
65+
): boolean | undefined {
66+
try {
67+
const decoded = Schema.decodeUnknownSync(fieldSchema as Schema.Decoder<unknown>)(undefined);
68+
return typeof decoded === "boolean" ? decoded : undefined;
69+
} catch {
70+
return undefined;
71+
}
72+
}
73+
6274
export function deriveProviderSettingsFields(
6375
definition: ProviderClientDefinition,
6476
): ReadonlyArray<ProviderSettingsFieldModel> {
@@ -93,6 +105,9 @@ export function deriveProviderSettingsFields(
93105
? { placeholder: formAnnotation.placeholder }
94106
: {}),
95107
clearWhenEmpty: formAnnotation.clearWhenEmpty ?? "omit",
108+
...(formAnnotation.control === "switch"
109+
? { defaultBooleanValue: readFieldBooleanDefault(fieldSchema) }
110+
: {}),
96111
} satisfies ProviderSettingsFieldModel,
97112
];
98113
});
@@ -104,10 +119,14 @@ export function readProviderConfigString(config: unknown, key: string): string {
104119
return typeof value === "string" ? value : "";
105120
}
106121

107-
export function readProviderConfigBoolean(config: unknown, key: string): boolean {
108-
if (config === null || typeof config !== "object") return false;
122+
export function readProviderConfigBoolean(
123+
config: unknown,
124+
key: string,
125+
defaultValue = false,
126+
): boolean {
127+
if (config === null || typeof config !== "object") return defaultValue;
109128
const value = (config as Record<string, unknown>)[key];
110-
return typeof value === "boolean" ? value : false;
129+
return typeof value === "boolean" ? value : defaultValue;
111130
}
112131

113132
export function nextProviderConfigWithFieldValue(
@@ -119,7 +138,8 @@ export function nextProviderConfigWithFieldValue(
119138
config !== null && typeof config === "object" ? { ...(config as Record<string, unknown>) } : {};
120139

121140
if (typeof value === "boolean") {
122-
if (field.clearWhenEmpty === "omit" && value === false) {
141+
const emptyBooleanValue = field.defaultBooleanValue ?? false;
142+
if (field.clearWhenEmpty === "omit" && value === emptyBooleanValue) {
123143
delete base[field.key];
124144
} else {
125145
base[field.key] = value;
@@ -188,7 +208,7 @@ function ProviderSettingsFieldRow({
188208
{description}
189209
</div>
190210
<Switch
191-
checked={readProviderConfigBoolean(value, field.key)}
211+
checked={readProviderConfigBoolean(value, field.key, field.defaultBooleanValue)}
192212
onCheckedChange={(checked) =>
193213
onChange(nextProviderConfigWithFieldValue(value, field, Boolean(checked)))
194214
}

0 commit comments

Comments
 (0)