Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

update.test.ts

130 lines5.9 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Option from "effect/Option";
7import * as Path from "effect/Path";
8import { HttpClient, HttpClientResponse } from "effect/http";
9import { afterEach, vi } from "vite-plus/test";
10​
11import { repointLauncher, resolveLauncherPath, resolveNewestVersion } from "./update.ts";
12​
13afterEach(() => vi.restoreAllMocks());
14​
15it.effect("authenticates each release page until the requested channel is found", () =>
16 Effect.gen(function* () {
17 const pages: string[] = [];
18 const httpClient = HttpClient.make((request) => {
19 const url = new URL(request.url);
20 assert.equal(url.origin, "https://api.github.com");
21 assert.equal(url.pathname, "/repos/pingdotgg/t3code/releases");
22 assert.equal(request.headers.accept, "application/vnd.github+json");
23 assert.equal(request.headers.authorization, "Bearer gh-token");
24 const page = url.searchParams.get("page") ?? "";
25 pages.push(page);
26 const tag_name = page === "1" ? "v1.2.4-preview.20260101.1" : "v1.2.3";
27 return Effect.succeed(HttpClientResponse.fromWeb(request, Response.json([{ tag_name }])));
28 });
29 const version = yield* resolveNewestVersion("stable").pipe(
30 Effect.provideService(HostProcess.Environment, { GH_TOKEN: "gh-token" }),
31 Effect.provideService(HttpClient.HttpClient, httpClient),
32 );
33 assert.equal(version, "1.2.3");
34 assert.deepStrictEqual(pages, ["1", "2"]);
35 }),
36);
37​
38it.layer(NodeServices.layer)("t3 update launcher", (it) => {
39 it.effect("repoints a symlink that lives in a runtime versions tree", () =>
40 Effect.gen(function* () {
41 const fs = yield* FileSystem.FileSystem;
42 const path = yield* Path.Path;
43 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
44 const oldExe = path.join(root, "runtime/versions/1.0.0/t3");
45 const newExe = path.join(root, "runtime/versions/2.0.0/t3");
46 const launcher = path.join(root, "bin/t3");
47 for (const file of [oldExe, newExe]) {
48 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
49 yield* fs.writeFileString(file, "");
50 }
51 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
52 yield* fs.symlink(oldExe, launcher);
53​
54 const repointed = yield* repointLauncher({
55 launchedAs: launcher,
56 versionsDir: path.join(root, "runtime/versions"),
57 targetEntryPath: newExe,
58 });
59​
60 assert.deepStrictEqual(Option.getOrUndefined(repointed), launcher);
61 assert.equal(yield* fs.readLink(launcher), newExe);
62 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
63 );
64​
65 it.effect("leaves a plain copy or a foreign symlink alone", () =>
66 Effect.gen(function* () {
67 const fs = yield* FileSystem.FileSystem;
68 const path = yield* Path.Path;
69 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
70 const newExe = path.join(root, "runtime/versions/2.0.0/t3");
71 const copy = path.join(root, "copy/t3");
72 const foreign = path.join(root, "foreign/t3");
73 const elsewhere = path.join(root, "elsewhere/t3");
74 const otherHome = path.join(root, "other/runtime/versions/1.0.0/t3");
75 const otherLauncher = path.join(root, "other/bin/t3");
76 for (const file of [newExe, copy, elsewhere, otherHome]) {
77 yield* fs.makeDirectory(path.dirname(file), { recursive: true });
78 yield* fs.writeFileString(file, "");
79 }
80 yield* fs.makeDirectory(path.dirname(foreign), { recursive: true });
81 yield* fs.symlink(elsewhere, foreign);
82 yield* fs.makeDirectory(path.dirname(otherLauncher), { recursive: true });
83 yield* fs.symlink(otherHome, otherLauncher);
84​
85 for (const launchedAs of [copy, foreign, otherLauncher, undefined]) {
86 const repointed = yield* repointLauncher({
87 launchedAs,
88 versionsDir: path.join(root, "runtime/versions"),
89 targetEntryPath: newExe,
90 });
91 assert.equal(repointed._tag, "None", launchedAs ?? "undefined");
92 }
93 assert.equal(yield* fs.readLink(foreign), elsewhere);
94 assert.equal(yield* fs.readLink(otherLauncher), otherHome);
95 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
96 );
97​
98 it.effect("finds the launcher a bare command name resolved to on PATH", () =>
99 Effect.gen(function* () {
100 const fs = yield* FileSystem.FileSystem;
101 const path = yield* Path.Path;
102 const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-update-" });
103 const launcher = path.join(root, "bin/t3");
104 yield* fs.makeDirectory(path.dirname(launcher), { recursive: true });
105 yield* fs.writeFileString(launcher, "");
106​
107 const bare = yield* resolveLauncherPath.pipe(
108 Effect.provideService(HostProcess.InvokedAs, "t3"),
109 Effect.provideService(HostProcess.Environment, {
110 PATH: `${path.join(root, "missing")}:${path.join(root, "bin")}`,
111 }),
112 Effect.provideService(HostProcess.WorkingDirectory, root),
113 );
114 const relative = yield* resolveLauncherPath.pipe(
115 Effect.provideService(HostProcess.InvokedAs, "./bin/t3"),
116 Effect.provideService(HostProcess.Environment, { PATH: "" }),
117 Effect.provideService(HostProcess.WorkingDirectory, root),
118 );
119 const absent = yield* resolveLauncherPath.pipe(
120 Effect.provideService(HostProcess.InvokedAs, "t3"),
121 Effect.provideService(HostProcess.Environment, { PATH: path.join(root, "missing") }),
122 Effect.provideService(HostProcess.WorkingDirectory, root),
123 );
124​
125 assert.equal(bare, launcher);
126 assert.equal(relative, launcher);
127 assert.equal(absent, undefined);
128 }).pipe(Effect.scoped, Effect.provideService(HostProcess.Platform, "linux")),
129 );
130});