Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import * as HostProcess from "@t3tools/shared/HostProcess";
4import * as NetService from "@t3tools/shared/Net";
5import * as ConfigProvider from "effect/ConfigProvider";
6import * as Effect from "effect/Effect";
7import * as FileSystem from "effect/FileSystem";
8import * as Layer from "effect/Layer";
9import * as TestConsole from "effect/testing/TestConsole";
10import { Command } from "effect/cli";
11import { vi } from "vite-plus/test";
12
13import packageJson from "../../package.json" with { type: "json" };
14import * as BootService from "../cloud/bootService.ts";
15import { upCommand } from "./up.ts";
16
17// T3 Connect's flows run only in builds with its public config, and they call
18// the relay; this file swaps in the link and unlink steps.
19const events = vi.hoisted((): Array<string> => []);
20// The CLI link marker, kept in memory instead of the secret store.
21const link = vi.hoisted(() => ({
22 desired: false,
23 mode: "managed" as "managed" | "publish_only",
24}));
25vi.mock("../cloud/CliState.ts", async (importOriginal) => ({
26 ...(await importOriginal<typeof import("../cloud/CliState.ts")>()),
27 readCliDesiredCloudLink: Effect.sync(() => link.desired),
28 readCliDesiredLinkMode: Effect.sync(() => link.mode),
29 setCliDesiredCloudLink: (desired: boolean, mode: "managed" | "publish_only" = "managed") =>
30 Effect.sync(() => {
31 link.desired = desired;
32 link.mode = mode;
33 }),
34}));
35vi.mock("../cloud/publicConfig.ts", async (importOriginal) => ({
36 ...(await importOriginal<typeof import("../cloud/publicConfig.ts")>()),
37 hasCloudPublicConfig: true,
38}));
39vi.mock("./connect.ts", async (importOriginal) => {
40 const actual = await importOriginal<typeof import("./connect.ts")>();
41 return {
42 ...actual,
43 linkEnvironmentForConnect: () =>
44 Effect.sync(() => {
45 events.push("link");
46 link.desired = true;
47 link.mode = "managed";
48 return { identity: "me@example.com" } as const;
49 }),
50 disconnectCloud: () =>
51 Effect.sync(() => {
52 events.push("unlink");
53 link.desired = false;
54 return { status: "succeeded" } as const;
55 }),
56 };
57});
58
59it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("t3 up with T3 Connect", (it) => {
60 it.effect.each([
61 { label: "no link", prior: null, events: ["link", "restart", "unlink", "restart"] },
62 // The publish-only link comes back, with its activity publishing.
63 {
64 label: "a publish-only link",
65 prior: "publish_only" as const,
66 events: ["link", "restart", "restart"],
67 },
68 ])("a failed restart after --connect undoes it, then restarts: $label before", (input) =>
69 Effect.gen(function* () {
70 events.length = 0;
71 link.desired = input.prior !== null;
72 link.mode = input.prior ?? "managed";
73 const fs = yield* FileSystem.FileSystem;
74 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-up-connect-test-" });
75 yield* fs.writeFileString(`${baseDir}/service.env`, "T3CODE_LAN=false\n");
76 const status = {
77 supported: true,
78 installed: true,
79 current: true,
80 unitPath: "/home/me/.config/systemd/user/t3code.service",
81 logPath: "/home/me/.t3/userdata/logs/boot-service.log",
82 };
83 vi.spyOn(BootService, "layer").mockReturnValue(
84 Layer.succeed(
85 BootService.BootService,
86 BootService.BootService.of({
87 status: Effect.succeed(status),
88 withStopped: (operation) => operation,
89 restart: Effect.sync(() => events.push("restart")).pipe(
90 Effect.andThen(
91 Effect.fail(new BootService.BootServiceCommandError({ step: "restart" })),
92 ),
93 ),
94 install: () => Effect.die("an installed, current service is not reinstalled"),
95 uninstall: Effect.succeed(false),
96 }),
97 ),
98 );
99
100 const exit = yield* Command.runWith(upCommand, { version: packageJson.version })([
101 "--base-dir",
102 baseDir,
103 "--connect",
104 ]).pipe(
105 Effect.provideService(HostProcess.Environment, {}),
106 Effect.provide(
107 Layer.mergeAll(
108 ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })),
109 Layer.fresh(TestConsole.layer),
110 ),
111 ),
112 Effect.exit,
113 );
114
115 expect(exit._tag).toBe("Failure");
116 // Any unlink comes before the restart on the old settings.
117 expect(events).toEqual(input.events);
118 expect(link).toEqual(
119 input.prior === null
120 ? { desired: false, mode: "managed" }
121 : { desired: true, mode: input.prior },
122 );
123 expect(yield* fs.readFileString(`${baseDir}/service.env`)).toBe("T3CODE_LAN=false\n");
124 }),
125 );
126});