-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathSSHRuntime.syncContract.test.ts
More file actions
529 lines (458 loc) · 17.7 KB
/
Copy pathSSHRuntime.syncContract.test.ts
File metadata and controls
529 lines (458 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import { afterEach, describe, expect, it, mock, spyOn } from "bun:test";
import { execSync, spawnSync } from "node:child_process";
import { mkdtemp, rm } from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import * as disposableExec from "@/node/utils/disposableExec";
import type { ExecOptions, ExecStream, InitLogger } from "./Runtime";
import { SSHRuntime } from "./SSHRuntime";
import type { RemoteProjectLayout } from "./remoteProjectLayout";
import type { SSHRuntimeConfig } from "./sshConnectionPool";
import type { PtyHandle, PtySessionParams, SSHTransport } from "./transports";
const noop = (): void => undefined;
const noopAsync = (): Promise<void> => Promise.resolve();
const tempDirs: string[] = [];
const noopInitLogger: InitLogger = {
logStep: noop,
logStdout: noop,
logStderr: noop,
logComplete: noop,
};
function createMockTransport(config: SSHRuntimeConfig): SSHTransport {
return {
spawnRemoteProcess() {
return Promise.reject(new Error("Unexpected transport use in SSHRuntime sync contract test"));
},
isConnectionFailure() {
return false;
},
acquireConnection() {
return Promise.resolve();
},
getConfig() {
return config;
},
createPtySession(_params: PtySessionParams): Promise<PtyHandle> {
return Promise.reject(new Error("Unexpected PTY creation in SSHRuntime sync contract test"));
},
};
}
function createTextStream(text: string): ReadableStream<Uint8Array> {
const encoded = new TextEncoder().encode(text);
return new ReadableStream<Uint8Array>({
start(controller) {
if (encoded.byteLength > 0) {
controller.enqueue(encoded);
}
controller.close();
},
});
}
const discardChunk = (_chunk: Uint8Array): Promise<void> => Promise.resolve();
function createExecStream(stdout: string, stderr = "", exitCode = 0): ExecStream {
return {
stdout: createTextStream(stdout),
stderr: createTextStream(stderr),
stdin: new WritableStream<Uint8Array>({
write: discardChunk,
close: noopAsync,
abort: noopAsync,
}),
exitCode: Promise.resolve(exitCode),
duration: Promise.resolve(0),
};
}
function createMockExecResult(
result: Promise<{ stdout: string; stderr: string }>
): ReturnType<typeof disposableExec.execFileAsync> {
void result.catch(noop);
return {
result,
get promise() {
return result;
},
child: {},
[Symbol.dispose]: noop,
} as unknown as ReturnType<typeof disposableExec.execFileAsync>;
}
class CommandCaptureSSHRuntime extends SSHRuntime {
readonly commands: string[] = [];
constructor() {
const config: SSHRuntimeConfig = {
host: "example.test",
srcBaseDir: "/remote/src",
};
super(config, createMockTransport(config));
}
override exec(command: string, _options: ExecOptions): Promise<ExecStream> {
this.commands.push(command);
return Promise.resolve(createExecStream(""));
}
}
class LocalUnpackSSHRuntime extends SSHRuntime {
readonly commands: string[] = [];
constructor(private readonly baseRepoPath: string) {
const config: SSHRuntimeConfig = {
host: "example.test",
srcBaseDir: "/remote/src",
};
super(config, createMockTransport(config));
}
override exec(command: string, options: ExecOptions): Promise<ExecStream> {
this.commands.push(command);
if (!command.includes("unpack-objects -r")) {
const result = spawnSync("sh", ["-c", command], {
cwd: options.cwd,
encoding: "utf8",
});
return Promise.resolve(
createExecStream(result.stdout || "", result.stderr || "", result.status ?? 1)
);
}
const chunks: Buffer[] = [];
let resolveExitCode: (exitCode: number) => void = noop;
const exitCode = new Promise<number>((resolve) => {
resolveExitCode = resolve;
});
let unpackRan = false;
const runUnpack = () => {
if (unpackRan) {
return;
}
unpackRan = true;
const result = spawnSync("git", ["-C", this.baseRepoPath, "unpack-objects", "-r"], {
input: Buffer.concat(chunks),
});
resolveExitCode(result.status ?? 1);
};
return Promise.resolve({
stdout: createTextStream(""),
stderr: createTextStream(""),
stdin: new WritableStream<Uint8Array>({
write(chunk) {
chunks.push(Buffer.from(chunk));
return Promise.resolve();
},
close() {
runUnpack();
return Promise.resolve();
},
abort() {
resolveExitCode(1);
return Promise.resolve();
},
}),
exitCode,
duration: exitCode.then(() => 0),
});
}
}
interface GitPushPrivateApi {
syncProjectSnapshotViaGitPush(
projectPath: string,
layout: RemoteProjectLayout,
currentSnapshotPath: string,
initLogger: InitLogger,
abortSignal?: AbortSignal,
options?: { forceNoThin?: boolean }
): Promise<void>;
}
interface BundleSyncPrivateApi {
transferBundleToRemote: (
projectPath: string,
remoteBundlePath: string,
initLogger: InitLogger,
abortSignal?: AbortSignal
) => Promise<void>;
syncProjectSnapshotViaBundle(
projectPath: string,
layout: RemoteProjectLayout,
currentSnapshotPath: string,
snapshotDigest: string,
baseRepoPathArg: string,
initLogger: InitLogger,
abortSignal?: AbortSignal
): Promise<void>;
}
interface SnapshotPrivateApi {
computeSnapshotDigest(projectPath: string): Promise<string>;
resolveLocalSyncRefManifest(projectPath: string): Promise<string | null>;
}
interface FreshWorkspaceSourcePrivateApi {
resolveFreshWorkspaceSourceBase(
baseRepoPathArg: string,
trunkBranch: string,
fetchedOrigin: boolean,
fallbackRef: string | null,
initLogger: InitLogger,
abortSignal?: AbortSignal
): Promise<string>;
fetchOriginTrunk(
workspacePath: string,
trunkBranch: string,
initLogger: InitLogger,
abortSignal?: AbortSignal,
nhp?: string
): Promise<boolean>;
}
interface MissingObjectRepairPrivateApi {
checkBaseRepoBundleConnectivity(
baseRepoPathArg: string,
abortSignal?: AbortSignal
): Promise<{ healthy: true } | { healthy: false; detail: string }>;
repairBaseRepoMissingObjectsFromLocal(
projectPath: string,
baseRepoPathArg: string,
initLogger: InitLogger,
abortSignal?: AbortSignal
): Promise<void>;
}
function createLayout(): RemoteProjectLayout {
return {
projectId: "project-id",
projectRoot: "/remote/src/project",
baseRepoPath: "/remote/src/project/.mux-base.git",
currentSnapshotPath: "/remote/src/project/.mux-meta/current-snapshot",
};
}
async function createTempGitRepo(): Promise<string> {
const repoPath = await mkdtemp(path.join(os.tmpdir(), "mux-ssh-sync-contract-"));
tempDirs.push(repoPath);
execSync(
[
`git -C "${repoPath}" init -b main`,
`git -C "${repoPath}" config user.email "test@test.com"`,
`git -C "${repoPath}" config user.name "Test"`,
`sh -c 'printf initial > "${repoPath}/file.txt"'`,
`git -C "${repoPath}" add file.txt`,
`git -C "${repoPath}" commit -m "initial"`,
].join(" && "),
{ stdio: "pipe" }
);
return repoPath;
}
afterEach(async () => {
mock.restore();
await Promise.all(
tempDirs.splice(0).map((tempDir) => rm(tempDir, { recursive: true, force: true }))
);
});
describe("SSHRuntime authoritative sync contract", () => {
it("derives snapshot identity from branch refs instead of tag-only drift", async () => {
const repoPath = await createTempGitRepo();
const runtime = new CommandCaptureSSHRuntime();
const privateApi = runtime as unknown as SnapshotPrivateApi;
const initialDigest = await privateApi.computeSnapshotDigest(repoPath);
const initialManifest = await privateApi.resolveLocalSyncRefManifest(repoPath);
execSync(`git -C "${repoPath}" tag v1.0.0`, { stdio: "pipe" });
expect(await privateApi.computeSnapshotDigest(repoPath)).toBe(initialDigest);
expect(await privateApi.resolveLocalSyncRefManifest(repoPath)).toBe(initialManifest);
execSync(`git -C "${repoPath}" branch feature/snapshot-contract`, { stdio: "pipe" });
expect(await privateApi.computeSnapshotDigest(repoPath)).not.toBe(initialDigest);
expect(await privateApi.resolveLocalSyncRefManifest(repoPath)).not.toBe(initialManifest);
});
it("pushes pruneable bundle branches separately from shared tags", async () => {
const runtime = new CommandCaptureSSHRuntime();
const layout = createLayout();
const gitCalls: string[][] = [];
spyOn(disposableExec, "execFileAsync").mockImplementation((file, args) => {
expect(file).toBe("git");
gitCalls.push([...args]);
const isTagCheck = args.includes("for-each-ref") && args.includes("refs/tags");
return createMockExecResult(
Promise.resolve({ stdout: isTagCheck ? "refs/tags/v1.0.0\n" : "", stderr: "" })
);
});
await (runtime as unknown as GitPushPrivateApi).syncProjectSnapshotViaGitPush(
"/local/project",
layout,
layout.currentSnapshotPath,
noopInitLogger
);
const pushCalls = gitCalls.filter((args) => args.includes("push"));
const tagCheckCalls = gitCalls.filter((args) => args.includes("for-each-ref"));
expect(pushCalls).toHaveLength(2);
expect(tagCheckCalls).toHaveLength(1);
expect(tagCheckCalls[0]).toContain("--count=1");
expect(tagCheckCalls[0]).toContain("refs/tags");
expect(pushCalls[0]).toContain("--prune");
expect(pushCalls[0]).toContain("--atomic");
expect(pushCalls[0]).toContain("+refs/heads/*:refs/mux-bundle/*");
expect(pushCalls[0]).not.toContain("+refs/tags/*:refs/tags/*");
expect(pushCalls[1]).not.toContain("--prune");
expect(pushCalls[1]).not.toContain("--atomic");
expect(pushCalls[1]).toContain("+refs/tags/*:refs/tags/*");
expect(pushCalls[1]).not.toContain("+refs/heads/*:refs/mux-bundle/*");
});
it("skips the metadata tag push when the local repo has no tags", async () => {
const runtime = new CommandCaptureSSHRuntime();
const layout = createLayout();
const gitCalls: string[][] = [];
spyOn(disposableExec, "execFileAsync").mockImplementation((file, args) => {
expect(file).toBe("git");
gitCalls.push([...args]);
return createMockExecResult(Promise.resolve({ stdout: "", stderr: "" }));
});
await (runtime as unknown as GitPushPrivateApi).syncProjectSnapshotViaGitPush(
"/local/project",
layout,
layout.currentSnapshotPath,
noopInitLogger
);
const pushCalls = gitCalls.filter((args) => args.includes("push"));
const tagCheckCalls = gitCalls.filter((args) => args.includes("for-each-ref"));
expect(pushCalls).toHaveLength(1);
expect(tagCheckCalls).toHaveLength(1);
expect(pushCalls[0]).toContain("--prune");
expect(pushCalls[0]).toContain("--atomic");
expect(pushCalls[0]).toContain("+refs/heads/*:refs/mux-bundle/*");
expect(pushCalls[0]).not.toContain("+refs/tags/*:refs/tags/*");
});
it("forces --no-thin pushes when the retry path requests a self-contained pack", async () => {
// After an `unresolved deltas` / `unpacker error` push failure, the retry
// loop opts the next attempt out of thin-pack encoding so the receiver
// does not need to resolve delta bases. Without this flag the retry could
// resend a thin pack and fail the same way.
const runtime = new CommandCaptureSSHRuntime();
const layout = createLayout();
const gitCalls: string[][] = [];
spyOn(disposableExec, "execFileAsync").mockImplementation((file, args) => {
expect(file).toBe("git");
gitCalls.push([...args]);
const isTagCheck = args.includes("for-each-ref") && args.includes("refs/tags");
return createMockExecResult(
Promise.resolve({ stdout: isTagCheck ? "refs/tags/v1.0.0\n" : "", stderr: "" })
);
});
await (runtime as unknown as GitPushPrivateApi).syncProjectSnapshotViaGitPush(
"/local/project",
layout,
layout.currentSnapshotPath,
noopInitLogger,
undefined,
{ forceNoThin: true }
);
const pushCalls = gitCalls.filter((args) => args.includes("push"));
expect(pushCalls).toHaveLength(2);
// Branch push and tag push both carry --no-thin.
expect(pushCalls[0]).toContain("--no-thin");
expect(pushCalls[1]).toContain("--no-thin");
});
it("omits --no-thin on the happy push path", async () => {
// Default sync (no retry pressure) must still use Git's thin-pack
// optimization. --no-thin is opt-in via the retry loop only.
const runtime = new CommandCaptureSSHRuntime();
const layout = createLayout();
const gitCalls: string[][] = [];
spyOn(disposableExec, "execFileAsync").mockImplementation((_file, args) => {
gitCalls.push([...args]);
return createMockExecResult(Promise.resolve({ stdout: "", stderr: "" }));
});
await (runtime as unknown as GitPushPrivateApi).syncProjectSnapshotViaGitPush(
"/local/project",
layout,
layout.currentSnapshotPath,
noopInitLogger
);
const pushCalls = gitCalls.filter((args) => args.includes("push"));
expect(pushCalls.every((args) => !args.includes("--no-thin"))).toBe(true);
});
it("uses the upstream source branch over the synced local snapshot for fresh workspaces", async () => {
const runtime = new CommandCaptureSSHRuntime();
const privateApi = runtime as unknown as FreshWorkspaceSourcePrivateApi;
const sourceBase = await privateApi.resolveFreshWorkspaceSourceBase(
"/remote/src/project/.mux-base.git",
"main",
true,
"refs/mux-bundle/main",
noopInitLogger
);
expect(sourceBase).toBe("origin/main");
expect(runtime.commands.some((command) => command.includes("merge-base --is-ancestor"))).toBe(
false
);
expect(runtime.commands).toContain(
"git -C /remote/src/project/.mux-base.git rev-parse --verify --quiet 'refs/remotes/origin/main'"
);
});
it("falls back to the local snapshot explicitly when the upstream source is unavailable", async () => {
const runtime = new CommandCaptureSSHRuntime();
const privateApi = runtime as unknown as FreshWorkspaceSourcePrivateApi;
const stderrLines: string[] = [];
const initLogger = {
...noopInitLogger,
logStderr(line: string) {
stderrLines.push(line);
},
};
const sourceBase = await privateApi.resolveFreshWorkspaceSourceBase(
"/remote/src/project/.mux-base.git",
"main",
false,
"refs/mux-bundle/main",
initLogger
);
expect(sourceBase).toBe("refs/mux-bundle/main");
expect(runtime.commands).toHaveLength(0);
expect(stderrLines[0]).toContain("using local snapshot refs/mux-bundle/main");
});
it("fetches source branches into explicit remote-tracking refs", async () => {
const runtime = new CommandCaptureSSHRuntime();
const privateApi = runtime as unknown as FreshWorkspaceSourcePrivateApi;
await privateApi.fetchOriginTrunk("/remote/src/project/.mux-base.git", "main", noopInitLogger);
expect(runtime.commands).toContain(
"git fetch origin '+refs/heads/main:refs/remotes/origin/main'"
);
});
it("repairs missing objects in reusable base repos with a full local pack", async () => {
const repoPath = await createTempGitRepo();
const baseParent = await mkdtemp(path.join(os.tmpdir(), "mux-ssh-missing-objects-base-"));
tempDirs.push(baseParent);
const baseRepoPath = path.join(baseParent, "base.git");
const worktreePath = path.join(baseParent, "repaired-worktree");
execSync(`git clone --bare "${repoPath}" "${baseRepoPath}"`, { stdio: "pipe" });
execSync(`git -C "${baseRepoPath}" update-ref refs/mux-bundle/main refs/heads/main`, {
stdio: "pipe",
});
execSync(`find "${path.join(baseRepoPath, "objects")}" -type f -delete`, { stdio: "pipe" });
const runtime = new LocalUnpackSSHRuntime(baseRepoPath);
const privateApi = runtime as unknown as MissingObjectRepairPrivateApi;
const beforeRepair = await privateApi.checkBaseRepoBundleConnectivity(baseRepoPath);
expect(beforeRepair.healthy).toBe(false);
await privateApi.repairBaseRepoMissingObjectsFromLocal(repoPath, baseRepoPath, noopInitLogger);
const afterRepair = await privateApi.checkBaseRepoBundleConnectivity(baseRepoPath);
expect(afterRepair.healthy).toBe(true);
expect(runtime.commands).toContain(`git -C ${baseRepoPath} unpack-objects -r`);
execSync(
`git -C "${baseRepoPath}" worktree add "${worktreePath}" -B repaired refs/mux-bundle/main`,
{ stdio: "pipe" }
);
const repairedContent = execSync(`cat "${path.join(worktreePath, "file.txt")}"`, {
encoding: "utf8",
});
expect(repairedContent).toBe("initial");
});
it("fetches pruneable bundle branches separately from shared tags", async () => {
const runtime = new CommandCaptureSSHRuntime();
const layout = createLayout();
const privateApi = runtime as unknown as BundleSyncPrivateApi;
privateApi.transferBundleToRemote = () => Promise.resolve();
await privateApi.syncProjectSnapshotViaBundle(
"/local/project",
layout,
layout.currentSnapshotPath,
"snapshot-digest",
'"/remote/src/project/.mux-base.git"',
noopInitLogger
);
const fetchCommands = runtime.commands.filter((command) => command.includes(" fetch "));
expect(fetchCommands).toHaveLength(2);
expect(fetchCommands[0]).toContain("fetch --prune");
expect(fetchCommands[0]).toContain("'+refs/heads/*:refs/mux-bundle/*'");
expect(fetchCommands[0]).not.toContain("refs/tags");
expect(fetchCommands[0]).not.toContain("--prune-tags");
expect(fetchCommands[1]).not.toContain("--prune");
expect(fetchCommands[1]).toContain("'+refs/tags/*:refs/tags/*'");
expect(fetchCommands[1]).not.toContain("refs/heads/*:refs/mux-bundle/*");
});
});