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 expect(display[0]).toEqual({
77 slug: "bare",
78 name: "bare",
79 isCustom: true,
80 capabilities: liveCapabilities,
81 });
82 expect(display[1]).toEqual({
83 slug: "named",
84 name: "My Model",
85 isCustom: true,
86 capabilities: customCapabilities,
87 });
88 });
89
90 it("shows a redacted provider email in the editor header status line", () => {
91 const instanceId = ProviderInstanceId.make("codex");
92 const driver = ProviderDriverKind.make("codex");
93 const liveProvider: ServerProvider = {
94 instanceId,
95 driver,
96 enabled: true,
97 installed: true,
98 version: "1.0.0",
99 status: "ready",
100 auth: { status: "authenticated", email: "developer@example.com" },
101 checkedAt: "2026-08-27T12:00:00.000Z",
102 models: [],
103 slashCommands: [],
104 skills: [],
105 };
106
107 const markup = renderToStaticMarkup(
108 createElement(ProviderInstanceCard, {
109 instanceId,
110 instance: { driver },
111 driverOption: undefined,
112 liveProvider,
113 mode: "editor",
114 onUpdate: () => undefined,
115 hiddenModels: [],
116 favoriteModels: [],
117 modelOrder: [],
118 onHiddenModelsChange: () => undefined,
119 onFavoriteModelsChange: () => undefined,
120 onToggleFavorite: () => 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 onToggleFavorite: () => undefined,
161 onModelOrderChange: () => undefined,
162 } as const;
163
164 for (const mode of ["list", "editor"] as const) {
165 const markup = renderToStaticMarkup(createElement(ProviderInstanceCard, { ...props, mode }));
166 expect(markup).toContain("Unavailable");
167 expect(markup).toContain("is not a symlink");
168 }
169 });
170});
171
172describe("provider environment helpers", () => {
173 const cursorApiKeyField = {
174 name: "CURSOR_API_KEY",
175 label: "Cursor API key",
176 sensitive: true,
177 };
178
179 it("writes dedicated provider secrets as sensitive environment variables", () => {
180 expect(
181 nextProviderEnvironmentWithFieldValue(
182 [{ name: "EXTRA_FLAG", value: "1", sensitive: false }],
183 cursorApiKeyField,
184 " cursor-key ",
185 ),
186 ).toEqual([
187 { name: "EXTRA_FLAG", value: "1", sensitive: false },
188 { name: "CURSOR_API_KEY", value: "cursor-key", sensitive: true },
189 ]);
190 });
191
192 it("replaces redacted provider secrets without preserving redaction markers", () => {
193 expect(
194 nextProviderEnvironmentWithFieldValue(
195 [
196 {
197 name: "CURSOR_API_KEY",
198 value: "",
199 sensitive: true,
200 valueRedacted: true,
201 },
202 ],
203 cursorApiKeyField,
204 "new-key",
205 ),
206 ).toEqual([{ name: "CURSOR_API_KEY", value: "new-key", sensitive: true }]);
207 });
208
209 it("applies the secure field default when replacing an existing non-sensitive value", () => {
210 expect(
211 nextProviderEnvironmentWithFieldValue(
212 [{ name: "OPENAI_API_KEY", value: "old-key", sensitive: false }],
213 {
214 name: "OPENAI_API_KEY",
215 label: "OpenAI API key",
216 },
217 "new-key",
218 ),
219 ).toEqual([{ name: "OPENAI_API_KEY", value: "new-key", sensitive: true }]);
220 });
221
222 it("separates dedicated provider secrets from the generic environment table", () => {
223 const environment = [
224 { name: "CURSOR_API_KEY", value: "cursor-key", sensitive: true },
225 { name: "EXTRA_FLAG", value: "1", sensitive: false },
226 ];
227
228 expect(readProviderEnvironmentVariable(environment, "CURSOR_API_KEY")?.value).toBe(
229 "cursor-key",
230 );
231 expect(providerEnvironmentWithoutNames(environment, new Set(["CURSOR_API_KEY"]))).toEqual([
232 { name: "EXTRA_FLAG", value: "1", sensitive: false },
233 ]);
234 });
235});