Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vitest";
3
4import { createEnvironmentConnection } from "./connection";
5import type { WsRpcClient } from "~/rpc/wsRpcClient";
6
7function createTestClient(options?: {
8 readonly getSnapshot?: () => Promise<{ readonly snapshotSequence: number }>;
9}) {
10 const lifecycleListeners = new Set<(event: any) => void>();
11 const configListeners = new Set<(event: any) => void>();
12 const terminalListeners = new Set<(event: any) => void>();
13
14 const getSnapshot = vi.fn(
15 options?.getSnapshot ??
16 (async () =>
17 ({
18 snapshotSequence: 1,
19 projects: [],
20 threads: [],
21 }) as any),
22 );
23
24 const client = {
25 dispose: vi.fn(async () => undefined),
26 reconnect: vi.fn(async () => undefined),
27 server: {
28 getConfig: vi.fn(async () => ({
29 environment: {
30 environmentId: EnvironmentId.makeUnsafe("env-1"),
31 },
32 })),
33 subscribeConfig: (listener: (event: any) => void) => {
34 configListeners.add(listener);
35 return () => configListeners.delete(listener);
36 },
37 subscribeLifecycle: (listener: (event: any) => void) => {
38 lifecycleListeners.add(listener);
39 return () => lifecycleListeners.delete(listener);
40 },
41 subscribeAuthAccess: () => () => undefined,
42 refreshProviders: vi.fn(async () => undefined),
43 upsertKeybinding: vi.fn(async () => undefined),
44 getSettings: vi.fn(async () => undefined),
45 updateSettings: vi.fn(async () => undefined),
46 },
47 orchestration: {
48 getSnapshot,
49 dispatchCommand: vi.fn(async () => undefined),
50 getTurnDiff: vi.fn(async () => undefined),
51 getFullThreadDiff: vi.fn(async () => undefined),
52 replayEvents: vi.fn(async () => []),
53 onDomainEvent: () => () => undefined,
54 },
55 terminal: {
56 open: vi.fn(async () => undefined),
57 write: vi.fn(async () => undefined),
58 resize: vi.fn(async () => undefined),
59 clear: vi.fn(async () => undefined),
60 restart: vi.fn(async () => undefined),
61 close: vi.fn(async () => undefined),
62 onEvent: (listener: (event: any) => void) => {
63 terminalListeners.add(listener);
64 return () => terminalListeners.delete(listener);
65 },
66 },
67 projects: {
68 searchEntries: vi.fn(async () => []),
69 writeFile: vi.fn(async () => undefined),
70 },
71 shell: {
72 openInEditor: vi.fn(async () => undefined),
73 },
74 git: {
75 pull: vi.fn(async () => undefined),
76 refreshStatus: vi.fn(async () => undefined),
77 onStatus: vi.fn(() => () => undefined),
78 runStackedAction: vi.fn(async () => ({}) as any),
79 listBranches: vi.fn(async () => []),
80 createWorktree: vi.fn(async () => undefined),
81 removeWorktree: vi.fn(async () => undefined),
82 createBranch: vi.fn(async () => undefined),
83 checkout: vi.fn(async () => undefined),
84 init: vi.fn(async () => undefined),
85 resolvePullRequest: vi.fn(async () => undefined),
86 preparePullRequestThread: vi.fn(async () => undefined),
87 },
88 } as unknown as WsRpcClient;
89
90 return {
91 client,
92 getSnapshot,
93 emitWelcome: (environmentId: EnvironmentId) => {
94 for (const listener of lifecycleListeners) {
95 listener({
96 type: "welcome",
97 payload: {
98 environment: {
99 environmentId,
100 },
101 },
102 });
103 }
104 },
105 emitConfigSnapshot: (environmentId: EnvironmentId) => {
106 for (const listener of configListeners) {
107 listener({
108 type: "snapshot",
109 config: {
110 environment: {
111 environmentId,
112 },
113 },
114 });
115 }
116 },
117 };
118}
119
120describe("createEnvironmentConnection", () => {
121 it("bootstraps a snapshot immediately for a new connection", async () => {
122 const environmentId = EnvironmentId.makeUnsafe("env-1");
123 const { client, getSnapshot } = createTestClient();
124 const syncSnapshot = vi.fn();
125
126 const connection = createEnvironmentConnection({
127 kind: "saved",
128 knownEnvironment: {
129 id: "env-1",
130 label: "Remote env",
131 source: "manual",
132 target: {
133 httpBaseUrl: "http://example.test",
134 wsBaseUrl: "ws://example.test",
135 },
136 environmentId,
137 },
138 client,
139 applyEventBatch: vi.fn(),
140 syncSnapshot,
141 applyTerminalEvent: vi.fn(),
142 });
143
144 await Promise.resolve();
145 await Promise.resolve();
146
147 expect(getSnapshot).toHaveBeenCalledTimes(1);
148 expect(syncSnapshot).toHaveBeenCalledWith(
149 expect.objectContaining({ snapshotSequence: 1 }),
150 environmentId,
151 );
152
153 await connection.dispose();
154 });
155
156 it("rejects welcome/config identity drift", async () => {
157 const environmentId = EnvironmentId.makeUnsafe("env-1");
158 const { client, emitWelcome } = createTestClient();
159
160 const connection = createEnvironmentConnection({
161 kind: "saved",
162 knownEnvironment: {
163 id: "env-1",
164 label: "Remote env",
165 source: "manual",
166 target: {
167 httpBaseUrl: "http://example.test",
168 wsBaseUrl: "ws://example.test",
169 },
170 environmentId,
171 },
172 client,
173 applyEventBatch: vi.fn(),
174 syncSnapshot: vi.fn(),
175 applyTerminalEvent: vi.fn(),
176 });
177
178 expect(() => emitWelcome(EnvironmentId.makeUnsafe("env-2"))).toThrow(
179 "Environment connection env-1 changed identity to env-2 via server lifecycle welcome.",
180 );
181
182 await connection.dispose();
183 });
184
185 it("rejects ensureBootstrapped when snapshot recovery fails", async () => {
186 const environmentId = EnvironmentId.makeUnsafe("env-1");
187 const snapshotError = new Error("snapshot failed");
188 const { client } = createTestClient({
189 getSnapshot: async () => {
190 throw snapshotError;
191 },
192 });
193
194 const connection = createEnvironmentConnection({
195 kind: "saved",
196 knownEnvironment: {
197 id: "env-1",
198 label: "Remote env",
199 source: "manual",
200 target: {
201 httpBaseUrl: "http://example.test",
202 wsBaseUrl: "ws://example.test",
203 },
204 environmentId,
205 },
206 client,
207 applyEventBatch: vi.fn(),
208 syncSnapshot: vi.fn(),
209 applyTerminalEvent: vi.fn(),
210 });
211
212 await expect(connection.ensureBootstrapped()).rejects.toThrow("snapshot failed");
213
214 await connection.dispose();
215 });
216});