The best way to control Claude Code, Codex and more from any computer or phone

main
ts 595 lines 23.8 KB
Raw
1 import { requestGuarded, runStreamGuarded } from "../rpc/client.ts";
2 import { EnvironmentSupervisor } from "../connection/supervisor.ts";
3 import * as SubscriptionRef from "effect/SubscriptionRef";
4 import * as Option from "effect/Option";
5 import * as Stream from "effect/Stream";
6 import type { RpcSession } from "../rpc/session.ts";
7 import { describe, expect, it } from "@effect/vitest";
8 import { vi } from "vite-plus/test";
9 import {
10 AuthAccessReadScope,
11 AuthAccessWriteScope,
12 AuthEnvironmentMaintainScope,
13 AuthOrchestrationOperateScope,
14 AuthOrchestrationReadScope,
15 AuthPreviewOperateScope,
16 AuthProvidersManageScope,
17 AuthSettingsWriteScope,
18 AuthSourceControlWriteScope,
19 ThreadId,
20 EnvironmentId,
21 ORCHESTRATION_V2_WS_METHODS,
22 ScheduledTaskId,
23 WS_METHODS,
24 type AuthSessionState,
25 type PeerLinkSummary,
26 } from "@t3tools/contracts";
27 import * as Cause from "effect/Cause";
28 import * as DateTime from "effect/DateTime";
29 import * as Effect from "effect/Effect";
30 import * as Fiber from "effect/Fiber";
31 import * as Layer from "effect/Layer";
32 import { Atom, AtomRegistry, AsyncResult } from "effect/reactivity";
33 import { EnvironmentRegistry } from "../connection/registry.ts";
34 import { createCommandPermissions } from "./commandPermissions.ts";
35 import { createEnvironmentRpcCommand, runAtomCommand } from "./runtime.ts";
36 import { AVAILABLE_CONNECTION_STATE, BearerConnectionTarget } from "../connection/model.ts";
37 import { createPeerLinkEnvironmentAtoms } from "./peerLinks.ts";
38
39 vi.mock("./session.ts", () => ({
40 createEnvironmentSessionAtoms: () => ({ sessionStateAtom: sessions }),
41 }));
42 const sessions = Atom.family((_id: EnvironmentId) =>
43 Atom.make<AsyncResult.AsyncResult<AuthSessionState, string>>(AsyncResult.initial()),
44 );
45 const env = EnvironmentId.make("target");
46 const other = EnvironmentId.make("other");
47 const grant = (allowed: boolean): AuthSessionState => ({
48 authenticated: true,
49 auth: {
50 policy: "remote-reachable",
51 bootstrapMethods: [],
52 sessionMethods: [],
53 sessionCookieName: "test",
54 },
55 scopes: allowed ? [AuthOrchestrationOperateScope] : [],
56 permissions: allowed ? [AuthOrchestrationOperateScope] : [],
57 });
58 const runtime = Atom.runtime(
59 Layer.succeed(EnvironmentRegistry, {
60 run: (_id: EnvironmentId, effect: Effect.Effect<unknown>) => effect,
61 } as unknown as EnvironmentRegistry["Service"]),
62 );
63 const permissions = createCommandPermissions(runtime, WS_METHODS.scheduledTasksDelete);
64 const setup = Effect.gen(function* () {
65 const registry = AtomRegistry.make();
66 registry.mount(sessions(env));
67 yield* Effect.addFinalizer(() => Effect.sync(() => registry.dispose()));
68 return registry;
69 });
70
71 describe("command permissions", () => {
72 it.effect("requires the destination read grant to refresh providers", () =>
73 Effect.gen(function* () {
74 const registry = yield* setup;
75 const refresh = createCommandPermissions(runtime, WS_METHODS.serverRefreshProviders);
76 registry.set(sessions(env), AsyncResult.success(grant(true)));
77 expect(registry.get(refresh.permissionAtom(env))).toBe(false);
78 expect((yield* refresh.authorize(registry, env).pipe(Effect.flip))._tag).toBe(
79 "EnvironmentAuthorizationError",
80 );
81 registry.set(
82 sessions(env),
83 AsyncResult.success({
84 ...grant(false),
85 scopes: [AuthOrchestrationReadScope],
86 permissions: [AuthOrchestrationReadScope],
87 }),
88 );
89 expect(registry.get(refresh.permissionAtom(env))).toBe(true);
90 yield* refresh.authorize(registry, env);
91 registry.set(sessions(other), AsyncResult.success(grant(false)));
92 expect(registry.get(refresh.permissionAtom(other))).toBe(false);
93 expect((yield* refresh.authorize(registry, other).pipe(Effect.flip))._tag).toBe(
94 "EnvironmentAuthorizationError",
95 );
96 registry.set(sessions(env), AsyncResult.success(grant(false)));
97 expect(registry.get(refresh.permissionAtom(env))).toBe(false);
98 expect((yield* refresh.authorize(registry, env).pipe(Effect.flip))._tag).toBe(
99 "EnvironmentAuthorizationError",
100 );
101 }),
102 );
103
104 it.effect("requires the destination preview grant to close a browser tab", () =>
105 Effect.gen(function* () {
106 const registry = yield* setup;
107 const close = createCommandPermissions(runtime, WS_METHODS.previewClose);
108 registry.set(sessions(env), AsyncResult.success(grant(true)));
109 expect(registry.get(close.permissionAtom(env))).toBe(false);
110 expect((yield* close.authorize(registry, env).pipe(Effect.flip))._tag).toBe(
111 "EnvironmentAuthorizationError",
112 );
113 registry.set(
114 sessions(env),
115 AsyncResult.success({
116 ...grant(false),
117 scopes: [AuthPreviewOperateScope],
118 permissions: [AuthPreviewOperateScope],
119 }),
120 );
121 expect(registry.get(close.permissionAtom(env))).toBe(true);
122 yield* close.authorize(registry, env);
123 registry.set(sessions(other), AsyncResult.success(grant(false)));
124 expect(registry.get(close.permissionAtom(other))).toBe(false);
125 expect((yield* close.authorize(registry, other).pipe(Effect.flip))._tag).toBe(
126 "EnvironmentAuthorizationError",
127 );
128 }),
129 );
130
131 it.effect("requires the destination provider grant to remove an incomplete connection", () =>
132 Effect.gen(function* () {
133 const registry = yield* setup;
134 const remove = createCommandPermissions(runtime, WS_METHODS.providerAuthRemoveMethod);
135 registry.set(sessions(env), AsyncResult.success(grant(true)));
136 expect(registry.get(remove.permissionAtom(env))).toBe(false);
137 expect((yield* remove.authorize(registry, env).pipe(Effect.flip))._tag).toBe(
138 "EnvironmentAuthorizationError",
139 );
140 registry.set(
141 sessions(env),
142 AsyncResult.success({
143 ...grant(false),
144 scopes: [AuthProvidersManageScope],
145 permissions: [AuthProvidersManageScope],
146 }),
147 );
148 expect(registry.get(remove.permissionAtom(env))).toBe(true);
149 yield* remove.authorize(registry, env);
150 registry.set(sessions(other), AsyncResult.success(grant(false)));
151 expect(registry.get(remove.permissionAtom(other))).toBe(false);
152 }),
153 );
154
155 it.effect.each([
156 { method: WS_METHODS.serverRunStorageCleanup, scope: AuthSettingsWriteScope },
157 { method: WS_METHODS.serverReclaimDatabaseSpace, scope: AuthEnvironmentMaintainScope },
158 { method: WS_METHODS.providerMcpSignInStart, scope: AuthProvidersManageScope },
159 { method: WS_METHODS.providerMcpSignInComplete, scope: AuthProvidersManageScope },
160 ])("requires the destination $scope grant for $method", ({ method, scope }) =>
161 Effect.gen(function* () {
162 const registry = yield* setup;
163 const command = createCommandPermissions(runtime, method);
164 registry.set(sessions(env), AsyncResult.success(grant(true)));
165 expect(registry.get(command.permissionAtom(env))).toBe(false);
166 expect((yield* command.authorize(registry, env).pipe(Effect.flip))._tag).toBe(
167 "EnvironmentAuthorizationError",
168 );
169 registry.set(
170 sessions(env),
171 AsyncResult.success({ ...grant(true), scopes: [scope], permissions: [scope] }),
172 );
173 expect(registry.get(command.permissionAtom(env))).toBe(true);
174 yield* command.authorize(registry, env);
175 registry.set(sessions(other), AsyncResult.success(grant(false)));
176 expect(registry.get(command.permissionAtom(other))).toBe(false);
177 }),
178 );
179
180 it.effect.each([WS_METHODS.serverCheckOtlpEndpoint, WS_METHODS.serverCheckSecretSource])(
181 "requires the destination settings grant for %s",
182 (method) =>
183 Effect.gen(function* () {
184 const registry = yield* setup;
185 const check = createCommandPermissions(runtime, method);
186 registry.set(sessions(env), AsyncResult.success(grant(true)));
187 expect(registry.get(check.permissionAtom(env))).toBe(false);
188 registry.set(
189 sessions(env),
190 AsyncResult.success({
191 ...grant(false),
192 scopes: [AuthSettingsWriteScope],
193 permissions: [AuthSettingsWriteScope],
194 }),
195 );
196 expect(registry.get(check.permissionAtom(env))).toBe(true);
197 yield* check.authorize(registry, env);
198 }),
199 );
200
201 it.effect("requires the destination settings grant to connect or bind an issue tracker", () =>
202 Effect.scoped(
203 Effect.gen(function* () {
204 const registry = yield* setup;
205 const connect = createCommandPermissions(runtime, WS_METHODS.issueTrackersConnect);
206 const bind = createCommandPermissions(runtime, WS_METHODS.issueTrackersBindProject);
207 registry.set(sessions(env), AsyncResult.success(grant(true)));
208 expect(registry.get(connect.permissionAtom(env))).toBe(false);
209 expect(registry.get(bind.permissionAtom(env))).toBe(false);
210 registry.set(
211 sessions(env),
212 AsyncResult.success({
213 ...grant(false),
214 scopes: [AuthSettingsWriteScope],
215 permissions: [AuthSettingsWriteScope],
216 }),
217 );
218 expect(registry.get(connect.permissionAtom(env))).toBe(true);
219 expect(registry.get(bind.permissionAtom(env))).toBe(true);
220 yield* connect.authorize(registry, env);
221 yield* bind.authorize(registry, env);
222 }),
223 ),
224 );
225
226 it.effect.each([WS_METHODS.scheduledTasksDelete, ORCHESTRATION_V2_WS_METHODS.stopWorkflow])(
227 "uses the target grant for both availability and execution of %s",
228 (method) =>
229 Effect.gen(function* () {
230 const registry = yield* setup;
231 const permissions = createCommandPermissions(runtime, method);
232 registry.set(sessions(env), AsyncResult.success(grant(true)));
233 expect(registry.get(permissions.permissionAtom(env))).toBe(true);
234 expect(permissions.permissionAtom(env)).toBe(permissions.permissionAtom(env));
235 yield* permissions.authorize(registry, env);
236 registry.set(sessions(other), AsyncResult.success(grant(false)));
237 expect(registry.get(permissions.permissionAtom(other))).toBe(false);
238 const denied = yield* permissions.authorize(registry, other).pipe(Effect.flip);
239 expect(denied._tag).toBe("EnvironmentAuthorizationError");
240 }),
241 );
242 it.effect("waits for an initial grant", () =>
243 Effect.gen(function* () {
244 const registry = yield* setup;
245 const fiber = yield* permissions.authorize(registry, env).pipe(Effect.forkChild);
246 yield* Effect.yieldNow;
247 registry.set(sessions(env), AsyncResult.success(grant(true)));
248 yield* Fiber.join(fiber);
249 }),
250 );
251 it.effect(
252 "denies failed and unauthenticated sessions, but accepts a cached refreshing grant",
253 () =>
254 Effect.gen(function* () {
255 const registry = yield* setup;
256 registry.set(sessions(env), AsyncResult.failure(Cause.fail("offline")));
257 expect((yield* permissions.authorize(registry, env).pipe(Effect.flip))._tag).toBe(
258 "EnvironmentAuthorizationError",
259 );
260 registry.set(sessions(env), AsyncResult.success({ ...grant(true), authenticated: false }));
261 expect((yield* permissions.authorize(registry, env).pipe(Effect.flip))._tag).toBe(
262 "EnvironmentAuthorizationError",
263 );
264 registry.set(sessions(env), AsyncResult.waiting(AsyncResult.success(grant(true))));
265 yield* permissions.authorize(registry, env);
266 }),
267 );
268 it("rechecks permission after waiting in a serial command lane", async () => {
269 const registry = AtomRegistry.make();
270 const unmount = registry.mount(sessions(env));
271 registry.set(sessions(env), AsyncResult.success(grant(true)));
272 let release!: () => void;
273 let started!: () => void;
274 const entered = new Promise<void>((resolve) => {
275 started = resolve;
276 });
277 const gate = new Promise<void>((resolve) => {
278 release = resolve;
279 });
280 let calls = 0;
281 const command = createEnvironmentRpcCommand(runtime, {
282 label: "test.delete",
283 tag: WS_METHODS.scheduledTasksDelete,
284 concurrency: { mode: "serial", key: () => "target" },
285 execute: () =>
286 Effect.promise(async () => {
287 calls++;
288 started();
289 await gate;
290 return { id: ScheduledTaskId.make("task") };
291 }),
292 });
293 const target = { environmentId: env, input: { id: ScheduledTaskId.make("task") } };
294 try {
295 const quiet = { reportFailure: false, reportDefect: false };
296 const first = runAtomCommand(registry, command, target, quiet);
297 await entered;
298 const second = runAtomCommand(registry, command, target, quiet);
299 registry.set(sessions(env), AsyncResult.success(grant(false)));
300 release();
301 expect((await first)._tag).toBe("Success");
302 expect((await second)._tag).toBe("Failure");
303 expect(calls).toBe(1);
304 } finally {
305 unmount();
306 registry.dispose();
307 }
308 });
309 });
310
311 it.effect(
312 "requires source control alone for git, and both grants when attaching a worktree to a thread",
313 () =>
314 Effect.gen(function* () {
315 const registry = yield* setup;
316 registry.set(sessions(env), AsyncResult.success(grant(true)));
317 const git = createCommandPermissions(runtime, WS_METHODS.vcsInit);
318 expect((yield* git.authorize(registry, env).pipe(Effect.flip)).requiredPermission).toBe(
319 AuthSourceControlWriteScope,
320 );
321 registry.set(
322 sessions(env),
323 AsyncResult.success({
324 ...grant(false),
325 scopes: [AuthSourceControlWriteScope],
326 permissions: [AuthSourceControlWriteScope],
327 }),
328 );
329 yield* git.authorize(registry, env);
330 const prepare = createCommandPermissions(runtime, WS_METHODS.gitPreparePullRequestThread);
331 const input = {
332 cwd: "/repo",
333 reference: "123",
334 mode: "worktree",
335 threadId: ThreadId.make("thread"),
336 };
337 expect(registry.get(prepare.permissionAtom(env, input))).toBe(false);
338 expect((yield* prepare.authorize(registry, env, input).pipe(Effect.flip)).requiredScope).toBe(
339 AuthOrchestrationOperateScope,
340 );
341 yield* prepare.authorize(registry, env, { ...input, threadId: undefined });
342 registry.set(
343 sessions(env),
344 AsyncResult.success({
345 ...grant(true),
346 scopes: [AuthSourceControlWriteScope, AuthOrchestrationOperateScope],
347 permissions: [AuthSourceControlWriteScope, AuthOrchestrationOperateScope],
348 }),
349 );
350 expect(registry.get(prepare.permissionAtom(env, input))).toBe(true);
351 yield* prepare.authorize(registry, env, input);
352 }),
353 );
354
355 it.effect.each(["commit", "push", "commit_push", "create_pr", "commit_push_pr"] as const)(
356 "%s needs thread read permission only when generating a PR from a thread",
357 (action) =>
358 Effect.gen(function* () {
359 const registry = yield* setup;
360 const command = createCommandPermissions(runtime, WS_METHODS.gitRunStackedAction);
361 const input = { actionId: "action", cwd: "/repo", action };
362 const withThread = { ...input, threadId: ThreadId.make("thread") };
363 const session = {
364 ...grant(false),
365 scopes: [AuthSourceControlWriteScope],
366 permissions: [AuthSourceControlWriteScope],
367 };
368 registry.set(sessions(env), AsyncResult.success(session));
369 expect(registry.get(command.permissionAtom(env, input))).toBe(true);
370 yield* command.authorize(registry, env, input);
371 const readsThread = action === "create_pr" || action === "commit_push_pr";
372 expect(registry.get(command.permissionAtom(env, withThread))).toBe(!readsThread);
373 if (readsThread) {
374 expect(yield* command.authorize(registry, env, withThread).pipe(Effect.flip)).toMatchObject(
375 { requiredPermission: AuthOrchestrationReadScope },
376 );
377 } else {
378 yield* command.authorize(registry, env, withThread);
379 }
380 registry.set(
381 sessions(env),
382 AsyncResult.success({
383 ...session,
384 scopes: [AuthSourceControlWriteScope, AuthOrchestrationReadScope],
385 permissions: [AuthSourceControlWriteScope, AuthOrchestrationReadScope],
386 }),
387 );
388 expect(registry.get(command.permissionAtom(env, withThread))).toBe(true);
389 yield* command.authorize(registry, env, withThread);
390 }),
391 );
392
393 it.effect(
394 "refreshes linked environments after permitted writes and blocks a read-only session",
395 () =>
396 Effect.gen(function* () {
397 const registry = yield* setup;
398 const peer: PeerLinkSummary = {
399 environmentId: EnvironmentId.make("peer"),
400 label: "Box",
401 urls: ["https://box.example.test"],
402 access: "auto",
403 linkedAt: DateTime.makeUnsafe("2026-10-01T12:00:00Z"),
404 expiresAt: DateTime.makeUnsafe("2026-10-31T12:00:00Z"),
405 lastReachedAt: null,
406 lastError: null,
407 status: "reachable",
408 };
409 let links: ReadonlyArray<PeerLinkSummary> = [];
410 const client = {
411 [WS_METHODS.peerLinksList]: () => Effect.sync(() => ({ links })),
412 [WS_METHODS.peerLinksLink]: vi.fn(() =>
413 Effect.sync(() => {
414 links = [peer];
415 return peer;
416 }),
417 ),
418 [WS_METHODS.peerLinksUnlink]: vi.fn(() =>
419 Effect.sync(() => {
420 links = [];
421 return { removed: true };
422 }),
423 ),
424 };
425 const supervisor = {
426 target: new BearerConnectionTarget({
427 environmentId: env,
428 label: "Test",
429 connectionId: "test",
430 }),
431 session: yield* SubscriptionRef.make(Option.some({ client } as unknown as RpcSession)),
432 state: yield* SubscriptionRef.make({
433 ...AVAILABLE_CONNECTION_STATE,
434 phase: "connected" as const,
435 }),
436 } as EnvironmentSupervisor["Service"];
437 const peerRuntime = Atom.runtime(
438 Layer.succeed(EnvironmentRegistry, {
439 run: (id, effect) => {
440 expect(id).toBe(env);
441 return Effect.provideService(effect, EnvironmentSupervisor, supervisor);
442 },
443 followStream: (id, stream) => {
444 expect(id).toBe(env);
445 return Stream.provideService(stream, EnvironmentSupervisor, supervisor);
446 },
447 } as EnvironmentRegistry["Service"]),
448 );
449 const peerLinks = createPeerLinkEnvironmentAtoms(peerRuntime);
450 const list = peerLinks.list({ environmentId: env, input: {} });
451 registry.mount(list);
452 const readLinks = AtomRegistry.getResult(registry, list, { suspendOnWaiting: true });
453 const setAccess = (writable: boolean) =>
454 registry.set(
455 sessions(env),
456 AsyncResult.success({
457 ...grant(false),
458 scopes: [AuthAccessReadScope, ...(writable ? [AuthAccessWriteScope] : [])],
459 permissions: [AuthAccessReadScope, ...(writable ? [AuthAccessWriteScope] : [])],
460 }),
461 );
462 registry.mount(sessions(other));
463 registry.set(
464 sessions(other),
465 AsyncResult.success({
466 ...grant(false),
467 scopes: [AuthAccessWriteScope],
468 permissions: [AuthAccessWriteScope],
469 }),
470 );
471 setAccess(false);
472 expect(yield* readLinks).toEqual({ links: [] });
473 const linkInput = { url: peer.urls[0]!, pairingCode: "test-code", access: peer.access };
474 const link = Effect.exit(
475 peerLinks.link.run(registry, { environmentId: env, input: linkInput }),
476 );
477 const unlink = Effect.exit(
478 peerLinks.unlink.run(registry, {
479 environmentId: env,
480 input: { environmentId: peer.environmentId },
481 }),
482 );
483 expect(registry.get(peerLinks.link.permissionAtom(env))).toBe(false);
484 const deniedLink = yield* link;
485 expect(deniedLink._tag).toBe("Failure");
486 if (deniedLink._tag === "Failure") {
487 expect(Cause.squash(deniedLink.cause)).toMatchObject({
488 _tag: "EnvironmentAuthorizationError",
489 requiredPermission: AuthAccessWriteScope,
490 });
491 }
492 expect(client[WS_METHODS.peerLinksLink]).not.toHaveBeenCalled();
493 setAccess(true);
494 expect(registry.get(peerLinks.link.permissionAtom(env))).toBe(true);
495 expect((yield* link)._tag).toBe("Success");
496 expect(client[WS_METHODS.peerLinksLink]).toHaveBeenCalledExactlyOnceWith(linkInput);
497 expect(yield* readLinks).toEqual({ links: [peer] });
498
499 setAccess(false);
500 expect(registry.get(peerLinks.unlink.permissionAtom(env))).toBe(false);
501 const deniedUnlink = yield* unlink;
502 expect(deniedUnlink._tag).toBe("Failure");
503 if (deniedUnlink._tag === "Failure") {
504 expect(Cause.squash(deniedUnlink.cause)).toMatchObject({
505 _tag: "EnvironmentAuthorizationError",
506 requiredPermission: AuthAccessWriteScope,
507 });
508 }
509 expect(client[WS_METHODS.peerLinksUnlink]).not.toHaveBeenCalled();
510 expect(yield* readLinks).toEqual({ links: [peer] });
511 setAccess(true);
512 expect(registry.get(peerLinks.unlink.permissionAtom(env))).toBe(true);
513 expect((yield* unlink)._tag).toBe("Success");
514 expect(client[WS_METHODS.peerLinksUnlink]).toHaveBeenCalledExactlyOnceWith({
515 environmentId: peer.environmentId,
516 });
517 expect(yield* readLinks).toEqual({ links: [] });
518 }),
519 );
520
521 it.effect("requires access management to answer a link request", () =>
522 Effect.gen(function* () {
523 const registry = yield* setup;
524 registry.set(sessions(env), AsyncResult.success(grant(true)));
525 const answer = createCommandPermissions(runtime, WS_METHODS.peerLinksAnswerRequest);
526 expect(registry.get(answer.permissionAtom(env))).toBe(false);
527 expect((yield* answer.authorize(registry, env).pipe(Effect.flip)).requiredPermission).toBe(
528 AuthAccessWriteScope,
529 );
530 registry.set(
531 sessions(env),
532 AsyncResult.success({
533 ...grant(false),
534 scopes: [AuthAccessWriteScope],
535 permissions: [AuthAccessWriteScope],
536 }),
537 );
538 expect(registry.get(answer.permissionAtom(env))).toBe(true);
539 yield* answer.authorize(registry, env);
540 }),
541 );
542
543 it.effect("honors exact empty permissions and preserves legacy parent grants", () =>
544 Effect.gen(function* () {
545 const registry = yield* setup;
546 const git = createCommandPermissions(runtime, WS_METHODS.vcsInit);
547 registry.set(sessions(env), AsyncResult.success({ ...grant(true), permissions: [] }));
548 expect(registry.get(git.permissionAtom(env))).toBe(false);
549 const denied = yield* git.authorize(registry, env).pipe(Effect.flip);
550 expect(denied).toMatchObject({
551 requiredPermission: AuthSourceControlWriteScope,
552 requiredScope: AuthOrchestrationOperateScope,
553 });
554 const { permissions: _exact, ...legacy } = grant(true);
555 registry.set(sessions(env), AsyncResult.success(legacy));
556 expect(registry.get(git.permissionAtom(env))).toBe(true);
557 yield* git.authorize(registry, env);
558 }),
559 );
560
561 it.effect("rejects protected unary and streamed RPCs outside a guarded command", () =>
562 Effect.gen(function* () {
563 let writes = 0;
564 const session = {
565 client: {
566 [WS_METHODS.scheduledTasksDelete]: () =>
567 Effect.sync(() => {
568 writes++;
569 return { id: ScheduledTaskId.make("task") };
570 }),
571 [WS_METHODS.gitRunStackedAction]: () =>
572 Stream.fromEffect(
573 Effect.sync(() => {
574 writes++;
575 }),
576 ),
577 },
578 } as unknown as RpcSession;
579 const supervisor = {
580 target: { environmentId: env, label: "target" },
581 session: yield* SubscriptionRef.make(Option.some(session)),
582 } as unknown as EnvironmentSupervisor["Service"];
583 const unary = yield* requestGuarded(WS_METHODS.scheduledTasksDelete, {
584 id: ScheduledTaskId.make("task"),
585 }).pipe(Effect.provideService(EnvironmentSupervisor, supervisor), Effect.flip);
586 expect(unary._tag).toBe("EnvironmentAuthorizationError");
587 const streamed = yield* runStreamGuarded(WS_METHODS.gitRunStackedAction, {
588 actionId: "test-action",
589 cwd: "/repo",
590 action: "commit",
591 }).pipe(Stream.runDrain, Effect.provideService(EnvironmentSupervisor, supervisor), Effect.flip);
592 expect(streamed._tag).toBe("EnvironmentAuthorizationError");
593 expect(writes).toBe(0);
594 }),
595 );