|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { expect, it } from "@effect/vitest"; |
| 3 | +import { |
| 4 | + HostProcessArchitecture, |
| 5 | + HostProcessEnvironment, |
| 6 | + HostProcessPlatform, |
| 7 | +} from "@t3tools/shared/hostProcess"; |
| 8 | +import * as Effect from "effect/Effect"; |
| 9 | +import * as FileSystem from "effect/FileSystem"; |
| 10 | +import * as Path from "effect/Path"; |
| 11 | +import { HttpClient, HttpClientResponse } from "effect/unstable/http"; |
| 12 | +import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"; |
| 13 | + |
| 14 | +import * as ServerConfig from "../config.ts"; |
| 15 | +import * as DesktopAppUpdate from "../desktopUpdate/DesktopAppUpdate.ts"; |
| 16 | +import * as ProcessRunner from "../processRunner.ts"; |
| 17 | +import { repointLaunchersFork } from "./launcherRepoint.fork.ts"; |
| 18 | +import * as ServerSelfUpdate from "./selfUpdate.ts"; |
| 19 | +import * as ServiceLauncherClient from "./serviceLauncherClient.ts"; |
| 20 | +import { SERVICE_LAUNCHER_PROTOCOL } from "./serviceProtocol.ts"; |
| 21 | + |
| 22 | +// A home with an installed old runtime, and an install.sh-style `bin/t3` |
| 23 | +// symlink pointing at it. |
| 24 | +const makeHome = Effect.fn("test.make_launcher_home")(function* () { |
| 25 | + const fs = yield* FileSystem.FileSystem; |
| 26 | + const path = yield* Path.Path; |
| 27 | + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-launcher-repoint-test-" }); |
| 28 | + const baseDir = path.join(root, "home"); |
| 29 | + const versionsDir = path.join(baseDir, "runtime", "versions"); |
| 30 | + const binDir = path.join(root, "bin"); |
| 31 | + yield* fs.makeDirectory(path.join(versionsDir, "1.0.0"), { recursive: true }); |
| 32 | + yield* fs.makeDirectory(binDir, { recursive: true }); |
| 33 | + yield* fs.writeFileString(path.join(versionsDir, "1.0.0", "t3"), "#!/bin/sh\n"); |
| 34 | + const launcher = path.join(binDir, "t3"); |
| 35 | + const target = (version: string) => path.join(versionsDir, version, "t3"); |
| 36 | + return { fs, path, root, baseDir, binDir, launcher, target }; |
| 37 | +}); |
| 38 | + |
| 39 | +const repoint = ( |
| 40 | + home: Effect.Success<ReturnType<typeof makeHome>>, |
| 41 | + environment: NodeJS.ProcessEnv, |
| 42 | +) => |
| 43 | + repointLaunchersFork(home.fs, home.path, home.baseDir, "1.1.0").pipe( |
| 44 | + Effect.provideService(HostProcessEnvironment, environment), |
| 45 | + Effect.provideService(HostProcessPlatform, "linux"), |
| 46 | + ); |
| 47 | + |
| 48 | +const runResult = (stdout: string) => ({ |
| 49 | + stdout, |
| 50 | + stderr: "", |
| 51 | + code: ChildProcessSpawner.ExitCode(0), |
| 52 | + timedOut: false, |
| 53 | + stdoutTruncated: false, |
| 54 | + stderrTruncated: false, |
| 55 | + stdoutInvalidUtf8: false, |
| 56 | + stderrInvalidUtf8: false, |
| 57 | +}); |
| 58 | + |
| 59 | +// The self-update service with a fake release download, tar, and preflight, |
| 60 | +// so the hook is proven at its seam rather than only in isolation. |
| 61 | +const makeSelfUpdate = Effect.fn("test.make_launcher_self_update")(function* ( |
| 62 | + home: Effect.Success<ReturnType<typeof makeHome>>, |
| 63 | + preflight: "ready" | "blocked", |
| 64 | +) { |
| 65 | + const { fs, path } = home; |
| 66 | + const archiveBytes = new TextEncoder().encode("not really a tarball"); |
| 67 | + const digest = yield* Effect.promise(() => crypto.subtle.digest("SHA-256", archiveBytes)); |
| 68 | + const hex = Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")); |
| 69 | + const httpClient = HttpClient.make((request) => |
| 70 | + Effect.succeed( |
| 71 | + HttpClientResponse.fromWeb( |
| 72 | + request, |
| 73 | + new Response( |
| 74 | + request.url.endsWith("/SHA256SUMS") |
| 75 | + ? `${hex.join("")} t3-1.1.0-linux-x64.tar.gz\n` |
| 76 | + : archiveBytes, |
| 77 | + ), |
| 78 | + ), |
| 79 | + ), |
| 80 | + ); |
| 81 | + const preflightResult = |
| 82 | + preflight === "ready" |
| 83 | + ? { status: "ready", version: "1.1.0", launcherProtocol: SERVICE_LAUNCHER_PROTOCOL } |
| 84 | + : { status: "blocked", version: "1.1.0", reason: "local update required" }; |
| 85 | + // @effect-diagnostics-next-line preferSchemaOverJson:off - fake child-process stdout. |
| 86 | + const preflightStdout = JSON.stringify(preflightResult); |
| 87 | + const runner = ProcessRunner.ProcessRunner.of({ |
| 88 | + run: (input) => { |
| 89 | + if (input.command !== "tar") return Effect.succeed(runResult(preflightStdout)); |
| 90 | + const stagingDir = input.args[input.args.indexOf("-C") + 1]; |
| 91 | + if (stagingDir === undefined) return Effect.die("missing tar target"); |
| 92 | + return fs |
| 93 | + .writeFileString(path.join(stagingDir, "t3"), "") |
| 94 | + .pipe(Effect.orDie, Effect.as(runResult(""))); |
| 95 | + }, |
| 96 | + }); |
| 97 | + const config = yield* ServerConfig.ServerConfig.pipe( |
| 98 | + Effect.provide(ServerConfig.layerTest(process.cwd(), home.baseDir)), |
| 99 | + ); |
| 100 | + return yield* ServerSelfUpdate.make().pipe( |
| 101 | + Effect.provideService(ProcessRunner.ProcessRunner, runner), |
| 102 | + Effect.provideService( |
| 103 | + ServiceLauncherClient.ServiceLauncherClient, |
| 104 | + ServiceLauncherClient.ServiceLauncherClient.of({ |
| 105 | + managed: true, |
| 106 | + requestUpdate: () => Effect.succeed("launcher-id"), |
| 107 | + prepareTrial: Effect.undefined, |
| 108 | + }), |
| 109 | + ), |
| 110 | + Effect.provideService(DesktopAppUpdate.DesktopAppUpdate, { |
| 111 | + available: false, |
| 112 | + run: () => Effect.die("unexpected desktop app update run"), |
| 113 | + commit: () => Effect.die("unexpected desktop app update commit"), |
| 114 | + }), |
| 115 | + Effect.provideService(HttpClient.HttpClient, httpClient), |
| 116 | + Effect.provideService(HostProcessPlatform, "linux"), |
| 117 | + Effect.provideService(HostProcessArchitecture, "x64"), |
| 118 | + Effect.provide(ServerConfig.layer({ ...config, mode: "web" })), |
| 119 | + ); |
| 120 | +}); |
| 121 | + |
| 122 | +it.layer(NodeServices.layer)("launcher repoint on a client update", (it) => { |
| 123 | + it.effect("repoints the owned launcher once the service launcher accepts the update", () => |
| 124 | + Effect.gen(function* () { |
| 125 | + const home = yield* makeHome(); |
| 126 | + yield* home.fs.symlink(home.target("1.0.0"), home.launcher); |
| 127 | + const selfUpdate = yield* makeSelfUpdate(home, "ready"); |
| 128 | + yield* selfUpdate |
| 129 | + .update({ targetVersion: "1.1.0" }) |
| 130 | + .pipe(Effect.provideService(HostProcessEnvironment, { PATH: home.binDir })); |
| 131 | + expect(yield* home.fs.readLink(home.launcher)).toBe(home.target("1.1.0")); |
| 132 | + }), |
| 133 | + ); |
| 134 | + |
| 135 | + it.effect("leaves the launcher when the staged preflight refuses the update", () => |
| 136 | + Effect.gen(function* () { |
| 137 | + const home = yield* makeHome(); |
| 138 | + yield* home.fs.symlink(home.target("1.0.0"), home.launcher); |
| 139 | + const selfUpdate = yield* makeSelfUpdate(home, "blocked"); |
| 140 | + yield* selfUpdate |
| 141 | + .update({ targetVersion: "1.1.0" }) |
| 142 | + .pipe(Effect.provideService(HostProcessEnvironment, { PATH: home.binDir }), Effect.flip); |
| 143 | + expect(yield* home.fs.readLink(home.launcher)).toBe(home.target("1.0.0")); |
| 144 | + }), |
| 145 | + ); |
| 146 | + |
| 147 | + it.effect("finds the install default bin dir when the service PATH omits it", () => |
| 148 | + Effect.gen(function* () { |
| 149 | + const home = yield* makeHome(); |
| 150 | + const defaultBin = home.path.join(home.root, ".local", "bin"); |
| 151 | + yield* home.fs.makeDirectory(defaultBin, { recursive: true }); |
| 152 | + yield* home.fs.symlink(home.target("1.0.0"), home.path.join(defaultBin, "t3")); |
| 153 | + expect(yield* repoint(home, { HOME: home.root, PATH: "/nonexistent" })).toEqual([ |
| 154 | + home.path.join(defaultBin, "t3"), |
| 155 | + ]); |
| 156 | + expect(yield* home.fs.readLink(home.path.join(defaultBin, "t3"))).toBe(home.target("1.1.0")); |
| 157 | + }), |
| 158 | + ); |
| 159 | + |
| 160 | + it.effect("leaves a launcher for another home and a plain copy alone", () => |
| 161 | + Effect.gen(function* () { |
| 162 | + const home = yield* makeHome(); |
| 163 | + const foreignTarget = home.path.join( |
| 164 | + home.root, |
| 165 | + "other", |
| 166 | + "runtime", |
| 167 | + "versions", |
| 168 | + "1.0.0", |
| 169 | + "t3", |
| 170 | + ); |
| 171 | + yield* home.fs.symlink(foreignTarget, home.launcher); |
| 172 | + const copyDir = home.path.join(home.root, "copy"); |
| 173 | + yield* home.fs.makeDirectory(copyDir); |
| 174 | + yield* home.fs.writeFileString(home.path.join(copyDir, "t3"), "#!/bin/sh\n"); |
| 175 | + expect(yield* repoint(home, { PATH: `${home.binDir}:${copyDir}` })).toEqual([]); |
| 176 | + expect(yield* home.fs.readLink(home.launcher)).toBe(foreignTarget); |
| 177 | + expect(yield* home.fs.readFileString(home.path.join(copyDir, "t3"))).toBe("#!/bin/sh\n"); |
| 178 | + }), |
| 179 | + ); |
| 180 | + |
| 181 | + it.effect("does nothing when no launcher is installed", () => |
| 182 | + Effect.gen(function* () { |
| 183 | + const home = yield* makeHome(); |
| 184 | + expect(yield* repoint(home, { PATH: home.binDir })).toEqual([]); |
| 185 | + }), |
| 186 | + ); |
| 187 | +}); |
0 commit comments