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