|
2 | 2 | DEFAULT_SERVER_SETTINGS, |
3 | 3 | EnvironmentAuthorizationError, |
4 | 4 | EnvironmentId, |
| 5 | + type PreviewAutomationStreamEvent, |
5 | 6 | type RelayClientInstallProgressEvent, |
6 | 7 | type ServerConfigStreamEvent, |
7 | 8 | type ServerLifecycleStreamEvent, |
@@ -89,6 +90,104 @@ const makeHarness = Effect.fn("TestEnvironmentRpc.makeHarness")(function* () { |
89 | 90 | }); |
90 | 91 |
|
91 | 92 | describe("environment RPC", () => { |
| 93 | + it.effect.each([false, true])( |
| 94 | + "reopens a completed automation registration only when configured (%s)", |
| 95 | + (reconnect) => |
| 96 | + Effect.gen(function* () { |
| 97 | + const first = yield* Queue.unbounded<PreviewAutomationStreamEvent, Cause.Done>(); |
| 98 | + const second = yield* Queue.unbounded<PreviewAutomationStreamEvent, Cause.Done>(); |
| 99 | + const started = yield* Queue.unbounded<number>(); |
| 100 | + const received = yield* Queue.unbounded<PreviewAutomationStreamEvent>(); |
| 101 | + const ended = yield* Deferred.make<void>(); |
| 102 | + let subscriptions = 0; |
| 103 | + const client = { |
| 104 | + [WS_METHODS.previewAutomationConnect]: () => |
| 105 | + Stream.unwrap( |
| 106 | + Effect.gen(function* () { |
| 107 | + subscriptions += 1; |
| 108 | + yield* Queue.offer(started, subscriptions); |
| 109 | + return subscriptions === 1 |
| 110 | + ? Stream.fromQueue(first).pipe( |
| 111 | + Stream.ensuring(Deferred.succeed(ended, undefined)), |
| 112 | + ) |
| 113 | + : Stream.fromQueue(second); |
| 114 | + }), |
| 115 | + ), |
| 116 | + } as unknown as WsRpcProtocolClient; |
| 117 | + const { activeSession, supervisor, retryCount } = yield* makeHarness(); |
| 118 | + yield* SubscriptionRef.set(activeSession, Option.some(session(client))); |
| 119 | + const fiber = yield* subscribe( |
| 120 | + WS_METHODS.previewAutomationConnect, |
| 121 | + { clientId: "desktop", environmentId: TARGET.environmentId }, |
| 122 | + reconnect ? { resubscribeOnCompletionAfter: "1 second" } : undefined, |
| 123 | + ).pipe( |
| 124 | + Stream.runForEach((event) => Queue.offer(received, event)), |
| 125 | + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 126 | + Effect.forkChild, |
| 127 | + ); |
| 128 | + expect(yield* Queue.take(started)).toBe(1); |
| 129 | + yield* Queue.offer(first, { type: "connected", connectionId: "first" }); |
| 130 | + expect(yield* Queue.take(received)).toEqual({ type: "connected", connectionId: "first" }); |
| 131 | + yield* Queue.end(first); |
| 132 | + yield* Deferred.await(ended); |
| 133 | + yield* TestClock.adjust("999 millis"); |
| 134 | + expect(Option.isNone(yield* Queue.poll(started))).toBe(true); |
| 135 | + yield* TestClock.adjust("1 millis"); |
| 136 | + if (reconnect) { |
| 137 | + expect(yield* Queue.take(started)).toBe(2); |
| 138 | + yield* Queue.offer(second, { type: "connected", connectionId: "second" }); |
| 139 | + expect(yield* Queue.take(received)).toEqual({ |
| 140 | + type: "connected", |
| 141 | + connectionId: "second", |
| 142 | + }); |
| 143 | + } |
| 144 | + expect(subscriptions).toBe(reconnect ? 2 : 1); |
| 145 | + expect(yield* Ref.get(retryCount)).toBe(0); |
| 146 | + yield* Fiber.interrupt(fiber); |
| 147 | + yield* TestClock.adjust("5 seconds"); |
| 148 | + expect(Option.isNone(yield* Queue.poll(started))).toBe(true); |
| 149 | + }), |
| 150 | + ); |
| 151 | + |
| 152 | + it.effect("cancels a completion retry when the supervisor replaces its session", () => |
| 153 | + Effect.gen(function* () { |
| 154 | + const started = yield* Queue.unbounded<string>(); |
| 155 | + const ended = yield* Deferred.make<void>(); |
| 156 | + const firstClient = { |
| 157 | + [WS_METHODS.subscribeTerminalEvents]: () => |
| 158 | + Stream.fromEffect(Queue.offer(started, "first")).pipe( |
| 159 | + Stream.drain, |
| 160 | + Stream.ensuring(Deferred.succeed(ended, undefined)), |
| 161 | + ), |
| 162 | + } as unknown as WsRpcProtocolClient; |
| 163 | + const secondClient = { |
| 164 | + [WS_METHODS.subscribeTerminalEvents]: () => |
| 165 | + Stream.fromEffect(Queue.offer(started, "second")).pipe( |
| 166 | + Stream.drain, |
| 167 | + Stream.concat(Stream.never), |
| 168 | + ), |
| 169 | + } as unknown as WsRpcProtocolClient; |
| 170 | + const { activeSession, supervisor } = yield* makeHarness(); |
| 171 | + yield* SubscriptionRef.set(activeSession, Option.some(session(firstClient))); |
| 172 | + const fiber = yield* subscribe( |
| 173 | + WS_METHODS.subscribeTerminalEvents, |
| 174 | + {}, |
| 175 | + { resubscribeOnCompletionAfter: "1 second" }, |
| 176 | + ).pipe( |
| 177 | + Stream.runDrain, |
| 178 | + Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor), |
| 179 | + Effect.forkChild, |
| 180 | + ); |
| 181 | + expect(yield* Queue.take(started)).toBe("first"); |
| 182 | + yield* Deferred.await(ended); |
| 183 | + yield* SubscriptionRef.set(activeSession, Option.some(session(secondClient))); |
| 184 | + expect(yield* Queue.take(started)).toBe("second"); |
| 185 | + yield* TestClock.adjust("5 seconds"); |
| 186 | + expect(Option.isNone(yield* Queue.poll(started))).toBe(true); |
| 187 | + yield* Fiber.interrupt(fiber); |
| 188 | + }), |
| 189 | + ); |
| 190 | + |
92 | 191 | it.effect("reuses the session config stream instead of opening a duplicate subscription", () => |
93 | 192 | Effect.gen(function* () { |
94 | 193 | const event: ServerConfigStreamEvent = { |
|
0 commit comments