Open History to see the last commit
1import { createElement } from "react";
2import { renderToStaticMarkup } from "react-dom/server";
3import { describe, expect, it } from "vite-plus/test";
4import {
5 ProviderDriverKind,
6 ProviderInstanceId,
7 type ServerProvider,
8 type ServerProviderModel,
9} from "@t3tools/contracts";
10
11import {
12 deriveProviderModelsForDisplay,
13 nextProviderEnvironmentWithFieldValue,
14 providerEnvironmentWithoutNames,
15 ProviderInstanceCard,
16 readProviderEnvironmentVariable,
17} from "./ProviderInstanceCard";
18
19describe("deriveProviderModelsForDisplay", () => {
20 it("uses current config custom models instead of stale live custom rows", () => {
21 const liveModels: ReadonlyArray<ServerProviderModel> = [
22 {
23 slug: "server-model",
24 name: "Server Model",
25 isCustom: false,
26 capabilities: null,
27 },
28 {
29 slug: "removed-custom",
30 name: "Removed Custom",
31 isCustom: true,
32 capabilities: null,
33 },
34 {
35 slug: "kept-custom",
36 name: "Kept Custom",
37 isCustom: true,
38 capabilities: null,
39 },
40 ];
41
42 expect(
43 deriveProviderModelsForDisplay({
44 liveModels,
45 customModels: [{ slug: "kept-custom", name: "kept-custom", capabilities: null }],
46 }).map((model) => model.slug),
47 ).toEqual(["server-model", "kept-custom"]);
48 });
49
50 it("prefers the entry's name and capabilities over the stale live custom row", () => {
51 const liveCapabilities = { optionDescriptors: [] };
52 const customCapabilities = {
53 optionDescriptors: [
54 {
55 id: "reasoningEffort",
56 label: "Reasoning",
57 type: "select" as const,
58 options: [{ id: "high", label: "High", isDefault: true }],
59 currentValue: "high",
60 },
61 ],
62 };
63 const liveModels: ReadonlyArray<ServerProviderModel> = [
64 { slug: "bare", name: "bare", isCustom: true, capabilities: liveCapabilities },
65 { slug: "named", name: "named", isCustom: true, capabilities: liveCapabilities },
66 ];
67
68 const display = deriveProviderModelsForDisplay({
69 liveModels,
70 customModels: [
71 { slug: "bare", name: "bare", capabilities: null },
72 { slug: "named", name: "My Model", capabilities: customCapabilities },
73 ],
74 });
75
76 // A bare entry keeps the driver default the server filled in.
77 expect(display[0]).toEqual({
78 slug: "bare",
79 name: "bare",
80 isCustom: true,
81 capabilities: liveCapabilities,
82 });
83 expect(display[1]).toEqual({
84 slug: "named",
85 name: "My Model",
86 isCustom: true,
87 capabilities: customCapabilities,
88 });
89 });
90
91 it("shows a redacted provider email in the editor header status line", () => {
92 const instanceId = ProviderInstanceId.make("codex");
93 const driver = ProviderDriverKind.make("codex");
94 const liveProvider: ServerProvider = {
95 instanceId,
96 driver,
97 enabled: true,
98 installed: true,
99 version: "1.0.0",
100 status: "ready",
101 auth: { status: "authenticated", email: "developer@example.com" },
102 checkedAt: "2026-08-27T12:00:00.000Z",
103 models: [],
104 slashCommands: [],
105 skills: [],
106 };
107
108 const markup = renderToStaticMarkup(
109 createElement(ProviderInstanceCard, {
110 instanceId,
111 instance: { driver },
112 driverOption: undefined,
113 liveProvider,
114 mode: "editor",
115 onUpdate: () => undefined,
116 hiddenModels: [],
117 favoriteModels: [],
118 modelOrder: [],
119 onHiddenModelsChange: () => undefined,
120 onFavoriteModelsChange: () => undefined,
121 onModelOrderChange: () => undefined,
122 }),
123 );
124
125 expect(markup).toContain("Authenticated as");
126 expect(markup).toContain('aria-label="Toggle account email visibility"');
127 expect(markup).toContain("blur-xs");
128 expect(markup).not.toContain("developer@example.com");
129 });
130 it("surfaces a failed probe message in both the list row and the editor", () => {
131 const instanceId = ProviderInstanceId.make("codex_work");
132 const driver = ProviderDriverKind.make("codex");
133 const message =
134 "Codex app-server provider probe failed: Cannot create Codex shadow home entry 'auth.json' because '/home/me/.codex-t3/work/auth.json' already exists and is not a symlink.";
135 const liveProvider: ServerProvider = {
136 instanceId,
137 driver,
138 enabled: true,
139 installed: true,
140 version: null,
141 status: "error",
142 auth: { status: "unknown" },
143 checkedAt: "2026-08-28T12:00:00.000Z",
144 models: [],
145 slashCommands: [],
146 skills: [],
147 message,
148 };
149 const props = {
150 instanceId,
151 instance: { driver },
152 driverOption: undefined,
153 liveProvider,
154 onUpdate: () => undefined,
155 hiddenModels: [],
156 favoriteModels: [],
157 modelOrder: [],
158 onHiddenModelsChange: () => undefined,
159 onFavoriteModelsChange: () => undefined,
160 onModelOrderChange: () => undefined,
161 } as const;
162
163 for (const mode of ["list", "editor"] as const) {
164 const markup = renderToStaticMarkup(createElement(ProviderInstanceCard, { ...props, mode }));
165 expect(markup).toContain("Unavailable");
166 expect(markup).toContain("is not a symlink");
167 }
168 });
169});
170
171describe("provider environment helpers", () => {
172 const cursorApiKeyField = {
173 name: "CURSOR_API_KEY",
174 label: "Cursor API key",
175 sensitive: true,
176 };
177
178 it("writes dedicated provider secrets as sensitive environment variables", () => {
179 expect(
180 nextProviderEnvironmentWithFieldValue(
181 [{ name: "EXTRA_FLAG", value: "1", sensitive: false }],
182 cursorApiKeyField,
183 " cursor-key ",
184 ),
185 ).toEqual([
186 { name: "EXTRA_FLAG", value: "1", sensitive: false },
187 { name: "CURSOR_API_KEY", value: "cursor-key", sensitive: true },
188 ]);
189 });
190
191 it("replaces redacted provider secrets without preserving redaction markers", () => {
192 expect(
193 nextProviderEnvironmentWithFieldValue(
194 [
195 {
196 name: "CURSOR_API_KEY",
197 value: "",
198 sensitive: true,
199 valueRedacted: true,
200 },
201 ],
202 cursorApiKeyField,
203 "new-key",
204 ),
205 ).toEqual([{ name: "CURSOR_API_KEY", value: "new-key", sensitive: true }]);
206 });
207
208 it("applies the secure field default when replacing an existing non-sensitive value", () => {
209 expect(
210 nextProviderEnvironmentWithFieldValue(
211 [{ name: "OPENAI_API_KEY", value: "old-key", sensitive: false }],
212 {
213 name: "OPENAI_API_KEY",
214 label: "OpenAI API key",
215 },
216 "new-key",
217 ),
218 ).toEqual([{ name: "OPENAI_API_KEY", value: "new-key", sensitive: true }]);
219 });
220
221 it("separates dedicated provider secrets from the generic environment table", () => {
222 const environment = [
223 { name: "CURSOR_API_KEY", value: "cursor-key", sensitive: true },
224 { name: "EXTRA_FLAG", value: "1", sensitive: false },
225 ];
226
227 expect(readProviderEnvironmentVariable(environment, "CURSOR_API_KEY")?.value).toBe(
228 "cursor-key",
229 );
230 expect(providerEnvironmentWithoutNames(environment, new Set(["CURSOR_API_KEY"]))).toEqual([
231 { name: "EXTRA_FLAG", value: "1", sensitive: false },
232 ]);
233 });
234});