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

ProviderInstanceCard.test.ts

146 lines4.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import type { ServerProviderModel } from "@t3tools/contracts";
3​
4import {
5 deriveProviderModelsForDisplay,
6 nextProviderEnvironmentWithFieldValue,
7 providerEnvironmentWithoutNames,
8 readProviderEnvironmentVariable,
9} from "./ProviderInstanceCard";
10​
11describe("deriveProviderModelsForDisplay", () => {
12 it("uses current config custom models instead of stale live custom rows", () => {
13 const liveModels: ReadonlyArray<ServerProviderModel> = [
14 {
15 slug: "server-model",
16 name: "Server Model",
17 isCustom: false,
18 capabilities: null,
19 },
20 {
21 slug: "removed-custom",
22 name: "Removed Custom",
23 isCustom: true,
24 capabilities: null,
25 },
26 {
27 slug: "kept-custom",
28 name: "Kept Custom",
29 isCustom: true,
30 capabilities: null,
31 },
32 ];
33​
34 expect(
35 deriveProviderModelsForDisplay({
36 liveModels,
37 customModels: [{ slug: "kept-custom", name: "kept-custom", capabilities: null }],
38 }).map((model) => model.slug),
39 ).toEqual(["server-model", "kept-custom"]);
40 });
41​
42 it("prefers the entry's name and capabilities over the stale live custom row", () => {
43 const liveCapabilities = { optionDescriptors: [] };
44 const customCapabilities = {
45 optionDescriptors: [
46 {
47 id: "reasoningEffort",
48 label: "Reasoning",
49 type: "select" as const,
50 options: [{ id: "high", label: "High", isDefault: true }],
51 currentValue: "high",
52 },
53 ],
54 };
55 const liveModels: ReadonlyArray<ServerProviderModel> = [
56 { slug: "bare", name: "bare", isCustom: true, capabilities: liveCapabilities },
57 { slug: "named", name: "named", isCustom: true, capabilities: liveCapabilities },
58 ];
59​
60 const display = deriveProviderModelsForDisplay({
61 liveModels,
62 customModels: [
63 { slug: "bare", name: "bare", capabilities: null },
64 { slug: "named", name: "My Model", capabilities: customCapabilities },
65 ],
66 });
67​
68 expect(display[0]).toEqual({
69 slug: "bare",
70 name: "bare",
71 isCustom: true,
72 capabilities: liveCapabilities,
73 });
74 expect(display[1]).toEqual({
75 slug: "named",
76 name: "My Model",
77 isCustom: true,
78 capabilities: customCapabilities,
79 });
80 });
81});
82​
83describe("provider environment helpers", () => {
84 const cursorApiKeyField = {
85 name: "CURSOR_API_KEY",
86 label: "Cursor API key",
87 sensitive: true,
88 };
89​
90 it("writes dedicated provider secrets as sensitive environment variables", () => {
91 expect(
92 nextProviderEnvironmentWithFieldValue(
93 [{ name: "EXTRA_FLAG", value: "1", sensitive: false }],
94 cursorApiKeyField,
95 " cursor-key ",
96 ),
97 ).toEqual([
98 { name: "EXTRA_FLAG", value: "1", sensitive: false },
99 { name: "CURSOR_API_KEY", value: "cursor-key", sensitive: true },
100 ]);
101 });
102​
103 it("replaces redacted provider secrets without preserving redaction markers", () => {
104 expect(
105 nextProviderEnvironmentWithFieldValue(
106 [
107 {
108 name: "CURSOR_API_KEY",
109 value: "",
110 sensitive: true,
111 valueRedacted: true,
112 },
113 ],
114 cursorApiKeyField,
115 "new-key",
116 ),
117 ).toEqual([{ name: "CURSOR_API_KEY", value: "new-key", sensitive: true }]);
118 });
119​
120 it("applies the secure field default when replacing an existing non-sensitive value", () => {
121 expect(
122 nextProviderEnvironmentWithFieldValue(
123 [{ name: "OPENAI_API_KEY", value: "old-key", sensitive: false }],
124 {
125 name: "OPENAI_API_KEY",
126 label: "OpenAI API key",
127 },
128 "new-key",
129 ),
130 ).toEqual([{ name: "OPENAI_API_KEY", value: "new-key", sensitive: true }]);
131 });
132​
133 it("separates dedicated provider secrets from the generic environment table", () => {
134 const environment = [
135 { name: "CURSOR_API_KEY", value: "cursor-key", sensitive: true },
136 { name: "EXTRA_FLAG", value: "1", sensitive: false },
137 ];
138​
139 expect(readProviderEnvironmentVariable(environment, "CURSOR_API_KEY")?.value).toBe(
140 "cursor-key",
141 );
142 expect(providerEnvironmentWithoutNames(environment, new Set(["CURSOR_API_KEY"]))).toEqual([
143 { name: "EXTRA_FLAG", value: "1", sensitive: false },
144 ]);
145 });
146});