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