Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderSettingsForm.test.ts

100 lines3.4 KB

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("stores false boolean fields when true is the default", () => {
56 const next = nextProviderConfigWithFieldValue(
57 undefined,
58 {
59 key: "experimental",
60 control: "switch",
61 label: "Experimental",
62 clearWhenEmpty: "omit",
63 defaultBooleanValue: true,
64 },
65 false,
66 );
67​
68 expect(next).toEqual({ experimental: false });
69 });
70});
71​
72describe("reconcileRowsDraft", () => {
73 let nextId = 0;
74 const toRows = (values: ReadonlyArray<string>) =>
75 values.map((value) => ({ id: nextId++, value }));
76 const equal = (left: ReadonlyArray<string>, right: ReadonlyArray<string>) =>
77 left.length === right.length && left.every((value, index) => value === right[index]);
78​
79 it("keeps rows when an equal value arrives with a new identity", () => {
80 const draft = seedRowsDraft(["a"], toRows);
81 expect(reconcileRowsDraft(draft, ["a"], equal, toRows)).toBe(draft);
82 });
83​
84 it("keeps row ids when the published value echoes back", () => {
85 const seeded = seedRowsDraft(["a"], toRows);
86 const edited = { rows: [...seeded.rows, { id: 99, value: "" }], seen: seeded.seen };
87 const draft = { ...edited, lastPublished: ["a", ""] };
88 const next = reconcileRowsDraft(draft, ["a", ""], equal, toRows);
89 expect(next.rows).toBe(draft.rows);
90 expect(next.seen).toEqual(["a", ""]);
91 expect(next.lastPublished).toBeUndefined();
92 });
93​
94 it("reseeds rows when the value changes from outside", () => {
95 const draft = { ...seedRowsDraft(["a"], toRows), lastPublished: ["b"] };
96 const next = reconcileRowsDraft(draft, ["c"], equal, toRows);
97 expect(next.rows.map((row) => row.value)).toEqual(["c"]);
98 expect(next.lastPublished).toBeUndefined();
99 });
100});