Open History to see the last commit
1import { userEvent } from "vite-plus/test/browser";
2import { describe, expect, it, vi } from "vite-plus/test";
3
4import { render } from "../../../test/render";
5import { ProviderEnvironmentSection } from "./ProviderInstanceCard";
6
7const savedSecret = {
8 name: "FAKE_API_KEY",
9 value: "",
10 sensitive: true,
11 valueRedacted: true,
12} as const;
13
14describe("ProviderEnvironmentSection saved secrets", () => {
15 it("requires a replacement before renaming or unlocking a stored secret", async () => {
16 const onChange = vi.fn();
17 await using screen = await render(
18 <ProviderEnvironmentSection environment={[savedSecret]} onChange={onChange} />,
19 );
20 const name = screen.getByRole("textbox", { name: "Environment variable name 1" });
21 const value = screen.getByLabelText("Environment variable value 1");
22 const lock = screen.getByRole("button", {
23 name: "Mark environment variable FAKE_API_KEY as sensitive",
24 });
25
26 await expect.element(name).toBeDisabled();
27 await expect.element(lock).toBeDisabled();
28 await userEvent.fill(value, "unused-draft");
29 await userEvent.clear(value);
30 await userEvent.tab();
31 expect(onChange).not.toHaveBeenCalled();
32 await expect.element(name).toBeDisabled();
33 await expect.element(lock).toBeDisabled();
34
35 await userEvent.fill(value, "fake-replacement");
36 await userEvent.tab();
37 expect(onChange).toHaveBeenLastCalledWith([
38 { ...savedSecret, value: "fake-replacement", valueRedacted: false },
39 ]);
40 await expect.element(name).toBeEnabled();
41 await expect.element(lock).toBeEnabled();
42 await userEvent.fill(name, "RENAMED_KEY");
43 await userEvent.tab();
44 await userEvent.click(
45 screen.getByRole("button", { name: "Mark environment variable RENAMED_KEY as sensitive" }),
46 );
47 expect(onChange).toHaveBeenLastCalledWith([
48 { name: "RENAMED_KEY", value: "fake-replacement", sensitive: false, valueRedacted: false },
49 ]);
50 await userEvent.click(
51 screen.getByRole("button", { name: "Remove environment variable RENAMED_KEY" }),
52 );
53 expect(onChange).toHaveBeenLastCalledWith([]);
54 });
55});
56
57describe("ProviderEnvironmentSection external settings writes", () => {
58 it("keeps a typed unpublished row when an external write redelivers the environment", async () => {
59 const onChange = vi.fn();
60 await using screen = await render(
61 <ProviderEnvironmentSection environment={[]} onChange={onChange} />,
62 );
63
64 await userEvent.click(screen.getByRole("button", { name: "Add variable" }));
65 await userEvent.type(
66 screen.getByRole("textbox", { name: "Environment variable name 1" }),
67 "KEEP_ME_TYPED",
68 );
69 expect(onChange).not.toHaveBeenCalled();
70
71 // A settings write from elsewhere on the instance redelivers a redacted, stored variable.
72 await screen.rerender(
73 <ProviderEnvironmentSection
74 environment={[{ name: "OTHER_VAR", value: "", sensitive: true, valueRedacted: true }]}
75 onChange={onChange}
76 />,
77 );
78
79 await expect
80 .element(screen.getByRole("textbox", { name: "Environment variable name 2" }))
81 .toHaveValue("KEEP_ME_TYPED");
82 });
83
84 it("does not duplicate a redacted published row on the sibling reseed", async () => {
85 const onChange = vi.fn();
86 await using screen = await render(
87 <ProviderEnvironmentSection environment={[]} onChange={onChange} />,
88 );
89
90 await userEvent.click(screen.getByRole("button", { name: "Add variable" }));
91 await userEvent.fill(
92 screen.getByRole("textbox", { name: "Environment variable name 1" }),
93 "STORE_ME",
94 );
95 await userEvent.fill(screen.getByLabelText("Environment variable value 1"), "fake-secret");
96 await userEvent.tab();
97 expect(onChange).toHaveBeenLastCalledWith([
98 { name: "STORE_ME", value: "fake-secret", sensitive: true, valueRedacted: false },
99 ]);
100
101 await userEvent.click(screen.getByRole("button", { name: "Add variable" }));
102 await userEvent.type(
103 screen.getByRole("textbox", { name: "Environment variable name 2" }),
104 "UNPUBLISHED_ONE",
105 );
106
107 // The stored secret comes back redacted, so the rows reseed without matching the echo.
108 await screen.rerender(
109 <ProviderEnvironmentSection
110 environment={[{ name: "STORE_ME", value: "", sensitive: true, valueRedacted: true }]}
111 onChange={onChange}
112 />,
113 );
114
115 await expect
116 .element(screen.getByRole("textbox", { name: "Environment variable name 1" }))
117 .toHaveValue("STORE_ME");
118 await expect
119 .element(screen.getByRole("textbox", { name: "Environment variable name 2" }))
120 .toHaveValue("UNPUBLISHED_ONE");
121 });
122
123 it("keeps a committed but unpublishable row across an external write", async () => {
124 const onChange = vi.fn();
125 await using screen = await render(
126 <ProviderEnvironmentSection environment={[]} onChange={onChange} />,
127 );
128
129 await userEvent.click(screen.getByRole("button", { name: "Add variable" }));
130 await userEvent.fill(
131 screen.getByRole("textbox", { name: "Environment variable name 1" }),
132 "KEEP-ME",
133 );
134 await userEvent.tab();
135 expect(onChange).not.toHaveBeenCalled();
136
137 await screen.rerender(
138 <ProviderEnvironmentSection
139 environment={[{ name: "OTHER_VAR", value: "", sensitive: true, valueRedacted: true }]}
140 onChange={onChange}
141 />,
142 );
143 await expect
144 .element(screen.getByRole("textbox", { name: "Environment variable name 2" }))
145 .toHaveValue("KEEP-ME");
146 });
147
148 it("drops an acknowledged row when an external write removes it", async () => {
149 const onChange = vi.fn();
150 await using screen = await render(
151 <ProviderEnvironmentSection environment={[]} onChange={onChange} />,
152 );
153
154 await userEvent.click(screen.getByRole("button", { name: "Add variable" }));
155 await userEvent.fill(
156 screen.getByRole("textbox", { name: "Environment variable name 1" }),
157 "PUBLISH_ME",
158 );
159 await userEvent.tab();
160 expect(onChange).toHaveBeenLastCalledWith([{ name: "PUBLISH_ME", value: "", sensitive: true }]);
161
162 // The server echo acknowledges the publish and keeps the rows; nothing is republished.
163 await screen.rerender(
164 <ProviderEnvironmentSection
165 environment={[{ name: "PUBLISH_ME", value: "", sensitive: true }]}
166 onChange={onChange}
167 />,
168 );
169 expect(onChange).toHaveBeenCalledTimes(1);
170 const acknowledged = screen.getByRole("textbox", { name: "Environment variable name 1" });
171
172 // Another client deletes the variable. The row must not come back.
173 await screen.rerender(<ProviderEnvironmentSection environment={[]} onChange={onChange} />);
174 await expect.element(acknowledged).not.toBeInTheDocument();
175 });
176});
177
178describe("ProviderEnvironmentSection name validation", () => {
179 it("marks names that hold back saving", async () => {
180 await using screen = await render(
181 <ProviderEnvironmentSection
182 environment={[{ name: "BAD-REDACTED", value: "", sensitive: true, valueRedacted: true }]}
183 onChange={vi.fn()}
184 />,
185 );
186 const redactedName = screen.getByRole("textbox", { name: "Environment variable name 1" });
187 await expect.element(redactedName).not.toHaveAttribute("aria-invalid");
188
189 await userEvent.click(screen.getByRole("button", { name: "Add variable" }));
190 const name = screen.getByRole("textbox", { name: "Environment variable name 2" });
191 await expect.element(name).not.toHaveAttribute("aria-invalid");
192
193 await userEvent.fill(name, "MY-VAR");
194 await userEvent.tab();
195 await expect.element(name).toHaveAttribute("aria-invalid", "true");
196
197 await userEvent.clear(name);
198 await userEvent.fill(screen.getByLabelText("Environment variable value 2"), "1");
199 await userEvent.tab();
200 await expect.element(name).toHaveAttribute("aria-invalid", "true");
201
202 await userEvent.fill(name, "MY_VAR");
203 await userEvent.tab();
204 await expect.element(name).not.toHaveAttribute("aria-invalid");
205 });
206
207 it("stops blocking saves after clearing a value or toggling sensitivity back on a blank row", async () => {
208 const onChange = vi.fn();
209 await using screen = await render(
210 <ProviderEnvironmentSection
211 environment={[{ name: "EXISTING_VAR", value: "one", sensitive: true }]}
212 onChange={onChange}
213 />,
214 );
215 const existingName = screen.getByRole("textbox", { name: "Environment variable name 1" });
216
217 await userEvent.click(screen.getByRole("button", { name: "Add variable" }));
218 const clearedName = screen.getByRole("textbox", { name: "Environment variable name 2" });
219 const clearedValue = screen.getByLabelText("Environment variable value 2");
220
221 await userEvent.fill(clearedValue, "temp");
222 await userEvent.tab();
223 await expect.element(clearedName).toHaveAttribute("aria-invalid", "true");
224
225 await userEvent.clear(clearedValue);
226 await userEvent.tab();
227 await expect.element(clearedName).not.toHaveAttribute("aria-invalid");
228
229 await userEvent.fill(existingName, "UPDATED_VAR");
230 await userEvent.tab();
231 expect(onChange).toHaveBeenLastCalledWith([
232 { name: "UPDATED_VAR", value: "one", sensitive: true },
233 ]);
234
235 await userEvent.click(screen.getByRole("button", { name: "Add variable" }));
236 const toggledName = screen.getByRole("textbox", { name: "Environment variable name 3" });
237 const toggledLock = screen.getByRole("button", {
238 name: "Mark environment variable 3 as sensitive",
239 });
240
241 await userEvent.click(toggledLock);
242 await expect.element(toggledName).toHaveAttribute("aria-invalid", "true");
243
244 await userEvent.click(toggledLock);
245 await expect.element(toggledName).not.toHaveAttribute("aria-invalid");
246
247 await userEvent.fill(existingName, "FINAL_VAR");
248 await userEvent.tab();
249 expect(onChange).toHaveBeenLastCalledWith([
250 { name: "FINAL_VAR", value: "one", sensitive: true },
251 ]);
252 });
253});