Open History to see the last commit
1import { AuthProvidersManageScope, EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 buildProviderEnvironmentOptions,
6 classifyProviderEnvironmentAccess,
7 isProviderSettingsEnvironmentAvailable,
8 resolvePrimaryOperateAccess,
9 resolveRemoteOperateAccess,
10 resolveSelectedProviderEnvironmentId,
11} from "./ProviderSettingsPanel.logic";
12
13const primaryId = EnvironmentId.make("primary");
14const relayId = EnvironmentId.make("relay");
15const sshId = EnvironmentId.make("ssh");
16
17const environments = [
18 { environmentId: sshId, label: "Zulu SSH" },
19 { environmentId: relayId, label: "Alpha Relay" },
20 { environmentId: primaryId, label: "This device" },
21] as const;
22
23describe("provider environment selection", () => {
24 it("requires a connected environment with server config for searchable provider settings", () => {
25 expect(
26 isProviderSettingsEnvironmentAvailable({
27 connectionPhase: "connected",
28 hasServerConfig: true,
29 }),
30 ).toBe(true);
31 expect(
32 isProviderSettingsEnvironmentAvailable({
33 connectionPhase: "reconnecting",
34 hasServerConfig: true,
35 }),
36 ).toBe(false);
37 expect(
38 isProviderSettingsEnvironmentAvailable({
39 connectionPhase: "connected",
40 hasServerConfig: false,
41 }),
42 ).toBe(false);
43 });
44
45 it("sorts the primary environment first and the rest by label", () => {
46 expect(
47 buildProviderEnvironmentOptions(environments, primaryId).map(
48 (environment) => environment.environmentId,
49 ),
50 ).toEqual([primaryId, relayId, sshId]);
51 });
52
53 it("keeps a valid selection, then falls back to primary or the first environment", () => {
54 const options = buildProviderEnvironmentOptions(environments, primaryId);
55
56 expect(resolveSelectedProviderEnvironmentId(options, sshId, primaryId)).toBe(sshId);
57 expect(
58 resolveSelectedProviderEnvironmentId(
59 options.filter((environment) => environment.environmentId !== sshId),
60 sshId,
61 primaryId,
62 ),
63 ).toBe(primaryId);
64 expect(resolveSelectedProviderEnvironmentId(options.slice(1), primaryId, primaryId)).toBe(
65 relayId,
66 );
67 expect(resolveSelectedProviderEnvironmentId([], null, primaryId)).toBeNull();
68 });
69});
70
71describe("provider environment access", () => {
72 it("allows connected environments with config and operate access", () => {
73 expect(
74 classifyProviderEnvironmentAccess({
75 connectionPhase: "connected",
76 hasServerConfig: true,
77 operateAccess: "granted",
78 }),
79 ).toEqual({ kind: "editable" });
80 });
81
82 it("waits for config before exposing controls", () => {
83 expect(
84 classifyProviderEnvironmentAccess({
85 connectionPhase: "connected",
86 hasServerConfig: false,
87 operateAccess: "granted",
88 }),
89 ).toEqual({ kind: "loading", reason: "config" });
90 });
91
92 it("waits for unresolved operate access instead of assuming it is editable", () => {
93 expect(
94 classifyProviderEnvironmentAccess({
95 connectionPhase: "connected",
96 hasServerConfig: true,
97 operateAccess: "pending",
98 }),
99 ).toEqual({ kind: "loading", reason: "permissions" });
100 });
101
102 it("represents known missing operate access as read only", () => {
103 expect(
104 classifyProviderEnvironmentAccess({
105 connectionPhase: "connected",
106 hasServerConfig: true,
107 operateAccess: "denied",
108 }),
109 ).toEqual({ kind: "read-only" });
110 });
111
112 it.each(["available", "offline", "connecting", "reconnecting"] as const)(
113 "keeps %s environments unavailable",
114 (connectionPhase) => {
115 expect(
116 classifyProviderEnvironmentAccess({
117 connectionPhase,
118 hasServerConfig: true,
119 operateAccess: "granted",
120 }),
121 ).toEqual({ kind: "unavailable" });
122 },
123 );
124
125 it("separates connection errors from other unavailable states", () => {
126 expect(
127 classifyProviderEnvironmentAccess({
128 connectionPhase: "error",
129 hasServerConfig: true,
130 operateAccess: "granted",
131 }),
132 ).toEqual({ kind: "error" });
133 });
134});
135
136describe("primary operate access", () => {
137 const authenticated = {
138 authenticated: true as const,
139 scopes: [AuthProvidersManageScope],
140 };
141
142 it("keeps cached session data authoritative while SWR revalidates", () => {
143 expect(
144 resolvePrimaryOperateAccess({
145 isPrimary: true,
146 hasDesktopBridge: false,
147 session: authenticated,
148 isPending: true,
149 hasError: false,
150 }),
151 ).toBe("granted");
152 });
153
154 it("reports pending only before any session has resolved", () => {
155 expect(
156 resolvePrimaryOperateAccess({
157 isPrimary: true,
158 hasDesktopBridge: false,
159 session: null,
160 isPending: true,
161 hasError: false,
162 }),
163 ).toBe("pending");
164 });
165
166 it("denies writes when the session fetch fails", () => {
167 expect(
168 resolvePrimaryOperateAccess({
169 isPrimary: true,
170 hasDesktopBridge: false,
171 session: null,
172 isPending: false,
173 hasError: true,
174 }),
175 ).toBe("denied");
176 });
177
178 it("denies unauthenticated sessions and sessions without the operate scope", () => {
179 expect(
180 resolvePrimaryOperateAccess({
181 isPrimary: true,
182 hasDesktopBridge: false,
183 session: { authenticated: false },
184 isPending: false,
185 hasError: false,
186 }),
187 ).toBe("denied");
188 expect(
189 resolvePrimaryOperateAccess({
190 isPrimary: true,
191 hasDesktopBridge: false,
192 session: { authenticated: true, scopes: ["orchestration:read"] },
193 isPending: false,
194 hasError: false,
195 }),
196 ).toBe("denied");
197 expect(
198 resolvePrimaryOperateAccess({
199 isPrimary: true,
200 hasDesktopBridge: false,
201 session: null,
202 isPending: false,
203 hasError: false,
204 }),
205 ).toBe("denied");
206 });
207
208 it("waits for explicit grants on desktop and remote environments", () => {
209 expect(
210 resolvePrimaryOperateAccess({
211 isPrimary: true,
212 hasDesktopBridge: true,
213 session: null,
214 isPending: true,
215 hasError: false,
216 }),
217 ).toBe("pending");
218 expect(
219 resolvePrimaryOperateAccess({
220 isPrimary: false,
221 hasDesktopBridge: false,
222 session: null,
223 isPending: true,
224 hasError: false,
225 }),
226 ).toBe("pending");
227 });
228});
229
230describe("remote operate access", () => {
231 it("does not treat the old orchestration grant as provider management", () => {
232 expect(
233 resolveRemoteOperateAccess({
234 session: {
235 authenticated: true,
236 scopes: ["orchestration:operate"],
237 auth: { serverUpdateScope: "environment:maintain" },
238 },
239 isPending: false,
240 hasError: false,
241 }),
242 ).toBe("denied");
243 });
244
245 it("accepts the orchestration grant from a server that predates providers:manage", () => {
246 expect(
247 resolveRemoteOperateAccess({
248 session: { authenticated: true, scopes: ["orchestration:operate"], auth: {} },
249 isPending: false,
250 hasError: false,
251 }),
252 ).toBe("granted");
253 });
254 it("derives access from the environment session's granted scopes", () => {
255 expect(
256 resolveRemoteOperateAccess({
257 session: { authenticated: true, scopes: [AuthProvidersManageScope] },
258 isPending: false,
259 hasError: false,
260 }),
261 ).toBe("granted");
262 expect(
263 resolveRemoteOperateAccess({
264 session: { authenticated: true, scopes: ["orchestration:read"] },
265 isPending: false,
266 hasError: false,
267 }),
268 ).toBe("denied");
269 expect(
270 resolveRemoteOperateAccess({
271 session: { authenticated: false },
272 isPending: false,
273 hasError: false,
274 }),
275 ).toBe("denied");
276 });
277
278 it("reports pending before the first session resolve, then keeps cached data", () => {
279 expect(resolveRemoteOperateAccess({ session: null, isPending: true, hasError: false })).toBe(
280 "pending",
281 );
282 expect(
283 resolveRemoteOperateAccess({
284 session: { authenticated: true, scopes: [AuthProvidersManageScope] },
285 isPending: true,
286 hasError: false,
287 }),
288 ).toBe("granted");
289 });
290
291 it("denies writes when the session fetch fails or scopes are missing", () => {
292 expect(resolveRemoteOperateAccess({ session: null, isPending: false, hasError: true })).toBe(
293 "denied",
294 );
295 expect(
296 resolveRemoteOperateAccess({
297 session: { authenticated: true },
298 isPending: false,
299 hasError: false,
300 }),
301 ).toBe("denied");
302 });
303});