Open History to see the last commit
1import {
2 EnvironmentId,
3 type ServerConfig,
4 type ServerConfigStreamEvent,
5 type ServerLifecycleWelcomePayload,
6 WS_METHODS,
7} from "@v12/contracts";
8import { describe, expect, it } from "@effect/vitest";
9import * as Effect from "effect/Effect";
10import * as Option from "effect/Option";
11import * as Queue from "effect/Queue";
12import * as Stream from "effect/Stream";
13import * as SubscriptionRef from "effect/SubscriptionRef";
14
15import {
16 AVAILABLE_CONNECTION_STATE,
17 PrimaryConnectionTarget,
18 type PreparedConnection,
19} from "../connection/model.ts";
20import * as EnvironmentSupervisor from "../connection/supervisor.ts";
21import * as Persistence from "../platform/persistence.ts";
22import type { WsRpcProtocolClient } from "../rpc/protocol.ts";
23import type { RpcSession } from "../rpc/session.ts";
24import {
25 applyServerConfigProjection,
26 makeEnvironmentServerConfigState,
27 projectServerWelcome,
28 resolveServerConfigValue,
29} from "./server.ts";
30
31const CONFIG = {
32 availableEditors: [],
33 issues: [],
34 keybindings: {},
35 keybindingsConfigPath: null,
36 observability: null,
37 providers: [],
38 settings: {},
39} as unknown as ServerConfig;
40
41const snapshotEvent = (config: ServerConfig): ServerConfigStreamEvent => ({
42 version: 1,
43 type: "snapshot",
44 config,
45});
46
47const TARGET = new PrimaryConnectionTarget({
48 environmentId: EnvironmentId.make("environment-1"),
49 label: "Test environment",
50 httpBaseUrl: "https://environment.example.test",
51 wsBaseUrl: "wss://environment.example.test",
52});
53
54function session(client: WsRpcProtocolClient): RpcSession {
55 return {
56 client,
57 initialConfig: Effect.succeed(CONFIG),
58 ready: Effect.void,
59 probe: Effect.void,
60 closed: Effect.never,
61 };
62}
63
64describe("server state projection", () => {
65 it("applies every config category to the projected snapshot", () => {
66 const snapshot = applyServerConfigProjection(Option.none(), {
67 version: 1,
68 type: "snapshot",
69 config: CONFIG,
70 });
71 const settings = { ...CONFIG.settings };
72 const projected = applyServerConfigProjection(snapshot, {
73 version: 1,
74 type: "settingsUpdated",
75 payload: { settings },
76 });
77
78 const result = Option.getOrThrow(projected);
79 expect(result.config.settings).toBe(settings);
80 expect(result.latestEvent.type).toBe("settingsUpdated");
81 });
82
83 it("retains welcome when a ready event follows in the same stream chunk", () => {
84 const welcome = {
85 environment: {} as ServerLifecycleWelcomePayload["environment"],
86 cwd: "/repo",
87 projectName: "repo",
88 } as ServerLifecycleWelcomePayload;
89 const [afterWelcome] = projectServerWelcome(Option.none(), {
90 type: "welcome",
91 payload: welcome,
92 });
93 const [afterReady, emitted] = projectServerWelcome(afterWelcome, {
94 type: "ready",
95 payload: {},
96 });
97
98 expect(Option.getOrThrow(afterReady)).toBe(welcome);
99 expect(emitted).toEqual([]);
100 });
101
102 it("prefers an active session config over cache until a live event arrives", () => {
103 const cached = { ...CONFIG, settings: { source: "cache" } } as unknown as ServerConfig;
104 const initial = { ...CONFIG, settings: { source: "session" } } as unknown as ServerConfig;
105 const live = { ...CONFIG, settings: { source: "live" } } as unknown as ServerConfig;
106
107 expect(
108 resolveServerConfigValue(
109 {
110 config: cached,
111 latestEvent: snapshotEvent(cached),
112 source: "cache",
113 },
114 initial,
115 ),
116 ).toBe(initial);
117 expect(
118 resolveServerConfigValue(
119 {
120 config: live,
121 latestEvent: snapshotEvent(live),
122 source: "live",
123 },
124 initial,
125 ),
126 ).toBe(live);
127 });
128
129 it.effect("starts from cached configuration and persists the live projection", () =>
130 Effect.gen(function* () {
131 const events = yield* Queue.unbounded<ServerConfigStreamEvent>();
132 const client = {
133 [WS_METHODS.subscribeServerConfig]: () => Stream.fromQueue(events),
134 } as unknown as WsRpcProtocolClient;
135 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
136 target: TARGET,
137 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
138 session: yield* SubscriptionRef.make(Option.some(session(client))),
139 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
140 connect: Effect.void,
141 disconnect: Effect.void,
142 retryNow: Effect.void,
143 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
144 const savedConfigs = yield* Queue.unbounded<ServerConfig>();
145 const cache = Persistence.EnvironmentCacheStore.of({
146 loadShell: () => Effect.succeed(Option.none()),
147 saveShell: () => Effect.void,
148 loadThread: () => Effect.succeed(Option.none()),
149 saveThread: () => Effect.void,
150 removeThread: () => Effect.void,
151 loadServerConfig: () => Effect.succeed(Option.some(CONFIG)),
152 saveServerConfig: (_environmentId, config) => Queue.offer(savedConfigs, config),
153 loadVcsRefs: () => Effect.succeed(Option.none()),
154 saveVcsRefs: () => Effect.void,
155 clear: () => Effect.void,
156 });
157
158 yield* Effect.scoped(
159 Effect.gen(function* () {
160 const state = yield* makeEnvironmentServerConfigState().pipe(
161 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
162 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
163 );
164 expect(Option.getOrThrow(yield* SubscriptionRef.get(state)).config).toBe(CONFIG);
165
166 const providers: ServerConfig["providers"] = [];
167 yield* Queue.offer(events, {
168 version: 1,
169 type: "providerStatuses",
170 payload: { providers },
171 });
172 const projected = yield* SubscriptionRef.changes(state).pipe(
173 Stream.filter((value) =>
174 Option.match(value, {
175 onNone: () => false,
176 onSome: (projection) => projection.latestEvent.type === "providerStatuses",
177 }),
178 ),
179 Stream.runHead,
180 );
181 expect(Option.getOrThrow(Option.getOrThrow(projected)).config.providers).toBe(providers);
182 }),
183 );
184
185 expect((yield* Queue.take(savedConfigs)).providers).toEqual([]);
186 }),
187 );
188
189 it.effect("does not rewrite cached configuration when no live update arrives", () =>
190 Effect.gen(function* () {
191 const client = {
192 [WS_METHODS.subscribeServerConfig]: () => Stream.empty,
193 } as unknown as WsRpcProtocolClient;
194 const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({
195 target: TARGET,
196 state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE),
197 session: yield* SubscriptionRef.make(Option.some(session(client))),
198 prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()),
199 connect: Effect.void,
200 disconnect: Effect.void,
201 retryNow: Effect.void,
202 } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]);
203 const savedConfigs = yield* Queue.unbounded<ServerConfig>();
204 const cache = Persistence.EnvironmentCacheStore.of({
205 loadShell: () => Effect.succeed(Option.none()),
206 saveShell: () => Effect.void,
207 loadThread: () => Effect.succeed(Option.none()),
208 saveThread: () => Effect.void,
209 removeThread: () => Effect.void,
210 loadServerConfig: () => Effect.succeed(Option.some(CONFIG)),
211 saveServerConfig: (_environmentId, config) => Queue.offer(savedConfigs, config),
212 loadVcsRefs: () => Effect.succeed(Option.none()),
213 saveVcsRefs: () => Effect.void,
214 clear: () => Effect.void,
215 });
216
217 yield* Effect.scoped(
218 makeEnvironmentServerConfigState().pipe(
219 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
220 Effect.provideService(Persistence.EnvironmentCacheStore, cache),
221 ),
222 );
223
224 expect(yield* Queue.poll(savedConfigs)).toEqual(Option.none());
225 }),
226 );
227});