Repository navigation
Expand file tree
/
Copy pathselfUpdate.test.ts
More file actions
495 lines (472 loc) · 19.7 KB
/
Copy pathselfUpdate.test.ts
File metadata and controls
495 lines (472 loc) · 19.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
import * as NodeServices from "@effect/platform-node/NodeServices";
import { expect, it } from "@effect/vitest";
import type { ManagedPolicyDocument } from "@t3tools/contracts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Deferred from "effect/Deferred";
import * as Duration from "effect/Duration";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Fiber from "effect/Fiber";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Stream from "effect/Stream";
import * as TestClock from "effect/testing/TestClock";
import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import packageJson from "../../package.json" with { type: "json" };
import * as ServerConfig from "../config.ts";
import * as DesktopAppUpdate from "../desktopUpdate/DesktopAppUpdate.ts";
import * as ManagedPolicy from "../policy/ManagedPolicy.ts";
import * as ProcessRunner from "../processRunner.ts";
import * as ServiceLauncherClient from "./serviceLauncherClient.ts";
import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts";
import * as ServerSelfUpdate from "./selfUpdate.ts";
interface HarnessOptions {
readonly mode?: "web" | "desktop";
readonly managed?: boolean;
readonly preflight?: "ready" | "blocked";
readonly preflightDuration?: Duration.Input;
readonly preflightStarted?: Deferred.Deferred<void>;
readonly beforeDownload?: Effect.Effect<void>;
readonly httpClient?: HttpClient.HttpClient;
readonly requestUpdate?: ServiceLauncherClient.ServiceLauncherClient["Service"]["requestUpdate"];
readonly desktopAppUpdate?: DesktopAppUpdate.DesktopAppUpdate["Service"];
readonly policy?: ManagedPolicyDocument;
}
const archiveBytes = new TextEncoder().encode("not really a tarball");
const releaseHttpClient = (order: string[], urls: string[], beforeDownload?: Effect.Effect<void>) =>
HttpClient.make((request) =>
Effect.gen(function* () {
urls.push(request.url);
if (request.url.endsWith("/SHA256SUMS")) {
const digest = yield* Effect.promise(() => crypto.subtle.digest("SHA-256", archiveBytes));
const hex = Array.from(new Uint8Array(digest), (byte) =>
byte.toString(16).padStart(2, "0"),
).join("");
return HttpClientResponse.fromWeb(
request,
new Response(`${hex} t3-1.1.0-linux-x64.tar.gz\n`),
);
}
if (beforeDownload) yield* beforeDownload;
order.push("download");
return HttpClientResponse.fromWeb(request, new Response(archiveBytes));
}),
);
const makeHarness = Effect.fn("test.make_self_update_harness")(function* (
options: HarnessOptions = {},
) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-self-update-test-" });
const order: string[] = [];
const urls: string[] = [];
const runner = ProcessRunner.ProcessRunner.of({
run: (input) =>
Effect.gen(function* () {
if (input.command === "tar") {
order.push("extract");
const stagingDir = input.args[input.args.indexOf("-C") + 1];
if (stagingDir === undefined) return yield* Effect.die("missing tar target");
yield* fs.writeFileString(path.join(stagingDir, "t3"), "#!/bin/sh\n").pipe(Effect.orDie);
return {
stdout: "",
stderr: "",
code: ChildProcessSpawner.ExitCode(0),
timedOut: false,
stdoutTruncated: false,
stderrTruncated: false,
stdoutInvalidUtf8: false,
stderrInvalidUtf8: false,
};
}
if (options.preflightStarted) yield* Deferred.succeed(options.preflightStarted, undefined);
if (options.preflightDuration !== undefined) {
// Like the real runner, fail once the caller's timeout elapses.
const timeout = Duration.fromInputUnsafe(input.timeout ?? "60 seconds");
const finished = yield* Effect.sleep(options.preflightDuration).pipe(
Effect.timeoutOption(timeout),
);
if (finished._tag === "None") {
return yield* new ProcessRunner.ProcessTimeoutError({
command: input.command,
argumentCount: input.args.length,
timeoutMs: Duration.toMillis(timeout),
});
}
}
order.push("preflight");
const result =
options.preflight === "blocked"
? { status: "blocked", version: "1.1.0", reason: "local update required" }
: {
status: "ready",
version: "1.1.0",
launcherProtocol: SERVICE_LAUNCHER_PROTOCOL,
};
return {
stdout: JSON.stringify(result),
stderr: "",
code: ChildProcessSpawner.ExitCode(0),
timedOut: false,
stdoutTruncated: false,
stderrTruncated: false,
stdoutInvalidUtf8: false,
stderrInvalidUtf8: false,
};
}),
});
const launcher = ServiceLauncherClient.ServiceLauncherClient.of({
managed: options.managed ?? true,
requestUpdate:
options.requestUpdate ??
(() =>
Effect.sync(() => {
order.push("accept");
return "launcher-id";
})),
prepareTrial: Effect.undefined,
});
const config = yield* ServerConfig.ServerConfig.pipe(
Effect.provide(ServerConfig.layerTest(process.cwd(), baseDir)),
);
const selfUpdate = yield* ServerSelfUpdate.make().pipe(
Effect.provideService(ProcessRunner.ProcessRunner, runner),
Effect.provideService(ServiceLauncherClient.ServiceLauncherClient, launcher),
Effect.provideService(
DesktopAppUpdate.DesktopAppUpdate,
options.desktopAppUpdate ?? {
available: false,
run: () => Effect.die("unexpected desktop app update run"),
},
),
Effect.provideService(
HttpClient.HttpClient,
options.httpClient ?? releaseHttpClient(order, urls, options.beforeDownload),
),
Effect.provideService(HostProcess.Platform, "linux"),
Effect.provideService(HostProcess.Architecture, "x64"),
Effect.provide(
Layer.merge(
ServerConfig.layer({ ...config, mode: options.mode ?? "web" }),
ManagedPolicy.layerTest(options.policy),
),
),
);
return { selfUpdate, order, urls, baseDir };
});
it.layer(NodeServices.layer)("server self update", (it) => {
it.effect.each([
{ asset: "checksums", status: 504 },
{ asset: "archive", status: 503 },
{ asset: "checksums", status: 403 },
])("reports an HTTP $status downloading $asset and allows retry", ({ asset, status }) =>
Effect.gen(function* () {
let failDownload = true;
const { selfUpdate, order, baseDir } = yield* makeHarness({
httpClient: releaseHttpClient([], []).pipe(
HttpClient.transform((response, request) =>
failDownload && request.url.endsWith("/SHA256SUMS") === (asset === "checksums")
? Effect.succeed(
HttpClientResponse.fromWeb(
HttpClientRequest.get("https://user:password@cdn.example/private?token=secret"),
new Response("private response body", { status }),
),
)
: response,
),
),
});
const error = yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip);
expect(error.reason).toBe(
`Could not prepare t3@1.1.0 while downloading the t3 release ${asset} (HTTP ${status} from cdn.example).`,
);
expect(order).toEqual([]);
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
expect(yield* fs.readDirectory(path.join(baseDir, "runtime", "versions"))).toEqual([]);
failDownload = false;
expect((yield* selfUpdate.update({ targetVersion: "1.1.0" })).updateId).toBe("launcher-id");
expect(order).toEqual(["extract", "preflight", "accept"]);
}),
);
// regression: #5181 make remote updates rollback-safe
it.effect("rejects invalid versions and desktop-managed servers before staging", () =>
Effect.gen(function* () {
const web = yield* makeHarness();
expect(
(yield* web.selfUpdate.update({ targetVersion: "latest" }).pipe(Effect.flip)).reason,
).toBe("'latest' is not an exact t3 version.");
const desktop = yield* makeHarness({ mode: "desktop" });
expect(
(yield* desktop.selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason,
).toContain("desktop app");
expect([...web.order, ...desktop.order]).toEqual([]);
}),
);
it.effect("refuses every update when the managed policy turns updates off", () =>
Effect.gen(function* () {
const policy: ManagedPolicyDocument = {
schemaVersion: 1,
organization: "Example Corp",
limits: { updates: { mode: "off" } },
};
const service = yield* makeHarness({ policy });
const desktop = yield* makeHarness({
mode: "desktop",
policy,
desktopAppUpdate: {
available: true,
run: () => Effect.die("unexpected desktop app update run"),
commit: () => Effect.never,
},
});
for (const harness of [service, desktop]) {
expect(
(yield* harness.selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason,
).toBe("Example Corp manages T3 Code updates.");
}
expect([...service.order, ...desktop.order]).toEqual([]);
}),
);
it.effect("lets a user start an update, but only to stable without nightly", () =>
Effect.gen(function* () {
const { selfUpdate, order, urls } = yield* makeHarness({
policy: {
schemaVersion: 1,
organization: "Example Corp",
limits: {
updates: {
mode: "auto",
allowNightly: false,
feedUrl: "https://updates.example.com/t3code/",
},
},
},
});
expect(
(yield* selfUpdate.update({ targetVersion: "1.2.0-nightly.20261028.1" }).pipe(Effect.flip))
.reason,
).toBe("Example Corp manages T3 Code updates. Only stable releases can be installed.");
expect(order).toEqual([]);
// The mirror lists only 1.1.0's archive.
expect((yield* selfUpdate.update({ targetVersion: "1.2.0" }).pipe(Effect.flip)).reason).toBe(
"Example Corp's update mirror has no t3@1.2.0 for this machine.",
);
expect((yield* selfUpdate.update({ targetVersion: "1.1.0" })).updateId).toBe("launcher-id");
// The release comes from the policy's mirror, not GitHub.
expect(urls.length).toBeGreaterThan(0);
for (const url of urls)
expect(url).toMatch(/^https:\/\/updates\.example\.com\/t3code\/v1\.[12]\.0\//);
}),
);
it.effect("delegates desktop-managed updates to the desktop app when available", () =>
Effect.gen(function* () {
const stages: string[] = [];
const { selfUpdate, order } = yield* makeHarness({
mode: "desktop",
desktopAppUpdate: {
available: true,
run: (reportProgress) =>
reportProgress("downloading").pipe(
Effect.andThen(reportProgress("installing")),
Effect.as({ targetVersion: "1.2.0", method: "desktop-app" as const }),
),
commit: () => Effect.never,
},
});
const result = yield* selfUpdate.update({ targetVersion: "1.1.0" }, (stage) =>
Effect.sync(() => void stages.push(stage)),
);
expect(result).toEqual({ targetVersion: "1.2.0", method: "desktop-app" });
expect(stages).toEqual(["downloading", "installing"]);
expect(order).toEqual([]);
}),
);
it.effect("allows only one update at a time", () =>
Effect.gen(function* () {
const requested = yield* Deferred.make<void>();
const accepted = yield* Deferred.make<string>();
const { selfUpdate } = yield* makeHarness({
requestUpdate: () =>
Deferred.succeed(requested, undefined).pipe(Effect.andThen(Deferred.await(accepted))),
});
const first = yield* Effect.forkChild(selfUpdate.update({ targetVersion: "1.1.0" }), {
startImmediately: true,
});
yield* Deferred.await(requested);
expect((yield* selfUpdate.update({ targetVersion: "1.1.1" }).pipe(Effect.flip)).reason).toBe(
"A server update is already in progress.",
);
yield* Deferred.succeed(accepted, "launcher-id");
expect((yield* Fiber.join(first)).updateId).toBe("launcher-id");
}),
);
// regression: #28198 a slow staged preflight on a loaded host no longer fails the update
it.effect("waits for a staged preflight that takes a minute on a loaded host", () =>
Effect.gen(function* () {
const preflightStarted = yield* Deferred.make<void>();
const { selfUpdate, order } = yield* makeHarness({
preflightDuration: "60 seconds",
preflightStarted,
});
const update = yield* Effect.forkChild(selfUpdate.update({ targetVersion: "1.1.0" }), {
startImmediately: true,
});
yield* Deferred.await(preflightStarted);
yield* TestClock.adjust("60 seconds");
const exit = yield* Fiber.await(update);
expect(exit._tag === "Failure" ? String(exit.cause) : "launcher handoff").toBe(
"launcher handoff",
);
expect(order).toEqual(["download", "extract", "preflight", "accept"]);
}),
);
it.effect("keeps preparing after the progress stream closes", () =>
Effect.gen(function* () {
const releaseDownload = yield* Deferred.make<void>();
const sawDownloading = yield* Deferred.make<void>();
const accepted = yield* Deferred.make<void>();
const { selfUpdate, order } = yield* makeHarness({
beforeDownload: Deferred.await(releaseDownload),
requestUpdate: () =>
Effect.sync(() => {
order.push("accept");
}).pipe(Effect.andThen(Deferred.succeed(accepted, undefined)), Effect.as("launcher-id")),
});
const consumed = yield* ServerSelfUpdate.updateProgressStream(selfUpdate, {
targetVersion: "1.1.0",
}).pipe(
Stream.tap((event) =>
event.type === "progress" && event.stage === "downloading"
? Deferred.succeed(sawDownloading, undefined)
: Effect.void,
),
Stream.runDrain,
Effect.forkChild,
);
yield* Deferred.await(sawDownloading);
yield* Fiber.interrupt(consumed);
expect((yield* selfUpdate.update({ targetVersion: "1.2.0" }).pipe(Effect.flip)).reason).toBe(
"A server update is already in progress.",
);
yield* Deferred.succeed(releaseDownload, undefined);
yield* Deferred.await(accepted);
expect(order).toEqual(["download", "extract", "preflight", "accept"]);
expect((yield* selfUpdate.update({ targetVersion: "1.1.0" }).pipe(Effect.flip)).reason).toBe(
"A server update is already in progress.",
);
}),
);
});
it.layer(NodeServices.layer)("pruning runtimes on start", (it) => {
const staleVersion = "0.0.1";
const otherProcesses = ProcessRunner.ProcessRunner.of({
run: () =>
Effect.succeed({
stdout: "p42\nftxt\nn/bin/sh\n",
stderr: "",
code: ChildProcessSpawner.ExitCode(0),
timedOut: false,
stdoutTruncated: false,
stderrTruncated: false,
stdoutInvalidUtf8: false,
stderrInvalidUtf8: false,
}),
});
const pruneOnStart = Effect.fn("test.prune_on_start")(function* (input: {
readonly executable: "own runtime" | "elsewhere";
readonly managed?: boolean;
readonly symlinkedHome?: boolean;
}) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-prune-on-start-" });
const realHome = path.join(tempDir, "home");
const baseDir = input.symlinkedHome ? path.join(tempDir, "linked-home") : realHome;
const versionsDir = path.join(baseDir, "runtime", "versions");
yield* TestClock.setTime(24 * 60 * 60 * 1000);
for (const version of [staleVersion, packageJson.version]) {
const directory = path.join(realHome, "runtime", "versions", version);
yield* fs.makeDirectory(directory, { recursive: true });
yield* fs.writeFileString(path.join(directory, "t3"), "");
const sentinel = path.join(directory, ".install-complete");
yield* fs.writeFileString(sentinel, `${version}\n`);
yield* fs.utimes(sentinel, 0, 0);
}
if (input.symlinkedHome) yield* fs.symlink(realHome, baseDir);
// Like `process.execPath`, the running runtime is reported by its real path.
const executablePath =
input.executable === "own runtime"
? yield* fs.realPath(path.join(versionsDir, packageJson.version, "t3"))
: "/usr/local/bin/node";
const removed = yield* ServerSelfUpdate.pruneRuntimesOnStart({
baseDir,
managed: input.managed ?? false,
}).pipe(
Effect.provideService(HostProcess.ExecutablePath, executablePath),
Effect.provideService(HostProcess.Platform, "darwin"),
Effect.provideService(HostProcess.Environment, {}),
Effect.provideService(ProcessRunner.ProcessRunner, otherProcesses),
);
return { removed, remaining: (yield* fs.readDirectory(versionsDir)).toSorted() };
});
// The SSH launcher runs `t3 serve` straight from the pinned runtime, with no boot service.
it.effect("prunes for a server started from its home's pinned runtime", () =>
Effect.gen(function* () {
const { removed, remaining } = yield* pruneOnStart({ executable: "own runtime" });
expect(removed).toEqual([staleVersion]);
expect(remaining).toEqual([packageJson.version]);
}),
);
it.effect("recognizes its runtime through a symlinked T3 home", () =>
Effect.gen(function* () {
const { removed } = yield* pruneOnStart({ executable: "own runtime", symlinkedHome: true });
expect(removed).toEqual([staleVersion]);
}),
);
it.effect("prunes for a boot-service server wherever it runs from", () =>
Effect.gen(function* () {
const { removed } = yield* pruneOnStart({ executable: "elsewhere", managed: true });
expect(removed).toEqual([staleVersion]);
}),
);
it.effect("leaves runtimes alone for an unmanaged server started elsewhere", () =>
Effect.gen(function* () {
const { removed, remaining } = yield* pruneOnStart({ executable: "elsewhere" });
expect(removed).toEqual([]);
expect(remaining).toEqual([staleVersion, packageJson.version].toSorted());
}),
);
});
it("a host crosses tracks only to a release that knows its whole database", () => {
const cross = (
serverVersion: string,
targetVersion: string,
targetDatabaseMigration: number | undefined,
databaseMigration = 72,
) =>
ServerSelfUpdate.trackSwitchBlockReason({
serverVersion,
targetVersion,
targetDatabaseMigration,
databaseMigration: () => databaseMigration,
});
const nightly = "0.1.4-nightly.20261027.3796";
expect(cross(nightly, "0.1.4", 72)).toBeNull();
// Another client moved this host to a newer Nightly after the switch was planned.
expect(cross(nightly, "0.1.4", 72, 73)).toContain("does not support yet");
expect(cross(nightly, "0.1.4", undefined)).toContain("Cannot confirm");
// A Stable tag can carry a migration the latest Nightly lacks.
expect(cross("0.1.4", nightly, 72, 73)).toContain("does not support yet");
expect(cross("0.1.3", "0.1.4", undefined)).toBeNull();
// A database that exists but cannot be read proves nothing.
expect(
ServerSelfUpdate.trackSwitchBlockReason({
serverVersion: nightly,
targetVersion: "0.1.4",
targetDatabaseMigration: 72,
databaseMigration: () => undefined,
}),
).toContain("Cannot confirm");
});