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