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

ProviderSettingsForm.test.ts

89 lines3.0 KB

1import { describe, expect, it } from "vite-plus/test";
2import { makeProviderSettingsSchema, ProviderDriverKind, TrimmedString } from "@t3tools/contracts";
3import * as Schema from "effect/Schema";
4​
5import { providerClients } from "./providerDriverMeta";
6import {
7 deriveProviderSettingsFields,
8 nextProviderConfigWithFieldValue,
9 reconcileRowsDraft,
10 seedRowsDraft,
11} from "./ProviderSettingsForm";
12​
13describe("ProviderSettingsForm helpers", () => {
14 it("keeps every masked field as a stored secret", () => {
15 const settingsSchema = makeProviderSettingsSchema({
16 token: TrimmedString.pipe(
17 Schema.annotateKey({ providerSettingsForm: { control: "password" } }),
18 ),
19 });
20​
21 expect(deriveProviderSettingsFields({ settingsSchema })).toMatchObject([
22 { key: "token", secret: true },
23 ]);
24 });
25​
26 it("preserves unknown config keys while omitting empty configurable fields", () => {
27 const opencode = providerClients.get(ProviderDriverKind.make("opencode"));
28 expect(opencode).toBeDefined();
29​
30 const serverUrl = deriveProviderSettingsFields(opencode!).find(
31 (field) => field.key === "serverUrl",
32 );
33 expect(serverUrl).toBeDefined();
34​
35 const next = nextProviderConfigWithFieldValue(
36 { forkOwned: 1, serverUrl: "http://127.0.0.1:4096" },
37 serverUrl!,
38 "",
39 );
40​
41 expect(next).toEqual({ forkOwned: 1 });
42 });
43​
44 it("stores false boolean fields when true is the default", () => {
45 const next = nextProviderConfigWithFieldValue(
46 undefined,
47 {
48 key: "experimental",
49 control: "switch",
50 label: "Experimental",
51 clearWhenEmpty: "omit",
52 defaultBooleanValue: true,
53 },
54 false,
55 );
56​
57 expect(next).toEqual({ experimental: false });
58 });
59});
60​
61describe("reconcileRowsDraft", () => {
62 let nextId = 0;
63 const toRows = (values: ReadonlyArray<string>) =>
64 values.map((value) => ({ id: nextId++, value }));
65 const equal = (left: ReadonlyArray<string>, right: ReadonlyArray<string>) =>
66 left.length === right.length && left.every((value, index) => value === right[index]);
67​
68 it("keeps rows when an equal value arrives with a new identity", () => {
69 const draft = seedRowsDraft(["a"], toRows);
70 expect(reconcileRowsDraft(draft, ["a"], equal, toRows)).toBe(draft);
71 });
72​
73 it("keeps row ids when the published value echoes back", () => {
74 const seeded = seedRowsDraft(["a"], toRows);
75 const edited = { rows: [...seeded.rows, { id: 99, value: "" }], seen: seeded.seen };
76 const draft = { ...edited, lastPublished: ["a", ""] };
77 const next = reconcileRowsDraft(draft, ["a", ""], equal, toRows);
78 expect(next.rows).toBe(draft.rows);
79 expect(next.seen).toEqual(["a", ""]);
80 expect(next.lastPublished).toBeUndefined();
81 });
82​
83 it("reseeds rows when the value changes from outside", () => {
84 const draft = { ...seedRowsDraft(["a"], toRows), lastPublished: ["b"] };
85 const next = reconcileRowsDraft(draft, ["c"], equal, toRows);
86 expect(next.rows.map((row) => row.value)).toEqual(["c"]);
87 expect(next.lastPublished).toBeUndefined();
88 });
89});