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

ProviderSettingsPanel.logic.test.ts

282 lines8.1 KB

1import { AuthOrchestrationOperateScope, 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: [AuthOrchestrationOperateScope],
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("treats a failed session fetch as a transport problem, not a denial", () => {
167 expect(
168 resolvePrimaryOperateAccess({
169 isPrimary: true,
170 hasDesktopBridge: false,
171 session: null,
172 isPending: false,
173 hasError: true,
174 }),
175 ).toBe("granted");
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("grants desktop bridge and remote environments without blocking on the primary session", () => {
209 expect(
210 resolvePrimaryOperateAccess({
211 isPrimary: true,
212 hasDesktopBridge: true,
213 session: null,
214 isPending: true,
215 hasError: false,
216 }),
217 ).toBe("granted");
218 expect(
219 resolvePrimaryOperateAccess({
220 isPrimary: false,
221 hasDesktopBridge: false,
222 session: null,
223 isPending: true,
224 hasError: false,
225 }),
226 ).toBe("granted");
227 });
228});
229​
230describe("remote operate access", () => {
231 it("derives access from the environment session's granted scopes", () => {
232 expect(
233 resolveRemoteOperateAccess({
234 session: { authenticated: true, scopes: [AuthOrchestrationOperateScope] },
235 isPending: false,
236 hasError: false,
237 }),
238 ).toBe("granted");
239 expect(
240 resolveRemoteOperateAccess({
241 session: { authenticated: true, scopes: ["orchestration:read"] },
242 isPending: false,
243 hasError: false,
244 }),
245 ).toBe("denied");
246 expect(
247 resolveRemoteOperateAccess({
248 session: { authenticated: false },
249 isPending: false,
250 hasError: false,
251 }),
252 ).toBe("denied");
253 });
254​
255 it("reports pending before the first session resolve, then keeps cached data", () => {
256 expect(resolveRemoteOperateAccess({ session: null, isPending: true, hasError: false })).toBe(
257 "pending",
258 );
259 expect(
260 resolveRemoteOperateAccess({
261 session: { authenticated: true, scopes: [AuthOrchestrationOperateScope] },
262 isPending: true,
263 hasError: false,
264 }),
265 ).toBe("granted");
266 });
267​
268 it("stays optimistic when the session fetch fails or an older server omits scopes", () => {
269 // Transport failures and pre-scope-reporting servers are not permission
270 // decisions; the environment RPC layer still rejects unauthorized writes.
271 expect(resolveRemoteOperateAccess({ session: null, isPending: false, hasError: true })).toBe(
272 "granted",
273 );
274 expect(
275 resolveRemoteOperateAccess({
276 session: { authenticated: true },
277 isPending: false,
278 hasError: false,
279 }),
280 ).toBe("granted");
281 });
282});