|
1 | | -import { assert, it } from "@effect/vitest"; |
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | +import { assert, expect, it } from "@effect/vitest"; |
| 3 | +import { HostProcessEnvironment } from "@t3tools/shared/hostProcess"; |
| 4 | +import * as NetService from "@t3tools/shared/Net"; |
| 5 | +import * as ConfigProvider from "effect/ConfigProvider"; |
| 6 | +import * as Effect from "effect/Effect"; |
| 7 | +import * as FileSystem from "effect/FileSystem"; |
| 8 | +import * as Layer from "effect/Layer"; |
| 9 | +import * as Terminal from "effect/Terminal"; |
| 10 | +import { Command } from "effect/unstable/cli"; |
| 11 | +import { afterEach, vi } from "vite-plus/test"; |
2 | 12 |
|
3 | | -import { formatServiceStatus } from "./service.ts"; |
| 13 | +import packageJson from "../../package.json" with { type: "json" }; |
| 14 | +import * as BootService from "../cloud/bootService.ts"; |
| 15 | +import { |
| 16 | + formatServiceStatus, |
| 17 | + offerServiceDuringOnboarding, |
| 18 | + reconcileService, |
| 19 | + recoverServiceOnboardingOffer, |
| 20 | + serviceCommand, |
| 21 | +} from "./service.ts"; |
| 22 | + |
| 23 | +afterEach(() => vi.restoreAllMocks()); |
4 | 24 |
|
5 | 25 | const status = { |
6 | 26 | supported: true, |
@@ -35,3 +55,149 @@ it("explains where the service is supported", () => { |
35 | 55 | "Supported on: Linux with systemd, macOS with launchd", |
36 | 56 | ); |
37 | 57 | }); |
| 58 | + |
| 59 | +it("reports a newer installed service and gives an exact-version repair command", () => { |
| 60 | + const output = formatServiceStatus( |
| 61 | + { ...status, current: false, installedVersion: "0.0.32-nightly.1" }, |
| 62 | + "0.0.31", |
| 63 | + ); |
| 64 | + |
| 65 | + assert.include(output, "[email protected] (newer than this [email protected] CLI)"); |
| 66 | + assert.include(output, "npx [email protected] service update"); |
| 67 | + assert.notInclude(output, "npx t3@latest service update"); |
| 68 | +}); |
| 69 | + |
| 70 | +const newerServiceStatus = { ...status, current: false, installedVersion: "999.0.0" }; |
| 71 | + |
| 72 | +function makeTestService(serviceStatus: BootService.BootServiceStatus) { |
| 73 | + const installOptions: Array<Parameters<BootService.BootService["Service"]["install"]>[0]> = []; |
| 74 | + const service = BootService.BootService.of({ |
| 75 | + status: Effect.succeed(serviceStatus), |
| 76 | + install: (options) => |
| 77 | + Effect.sync(() => { |
| 78 | + installOptions.push(options); |
| 79 | + return { |
| 80 | + nodePath: "/test/node", |
| 81 | + launcherPath: "/test/service-launcher.mjs", |
| 82 | + baseDir: "/test/t3", |
| 83 | + unitPath: serviceStatus.unitPath, |
| 84 | + logPath: serviceStatus.logPath, |
| 85 | + }; |
| 86 | + }), |
| 87 | + uninstall: Effect.succeed(false), |
| 88 | + }); |
| 89 | + return { service, installOptions }; |
| 90 | +} |
| 91 | + |
| 92 | +it.layer(Layer.mergeAll(NodeServices.layer, NetService.layer))("service commands", (it) => { |
| 93 | + it.effect.each(["install", "update"] as const)( |
| 94 | + "%s refuses a downgrade before changing the service", |
| 95 | + (command) => |
| 96 | + Effect.gen(function* () { |
| 97 | + const fs = yield* FileSystem.FileSystem; |
| 98 | + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" }); |
| 99 | + const { service, installOptions } = makeTestService(newerServiceStatus); |
| 100 | + vi.spyOn(BootService, "layer").mockReturnValue( |
| 101 | + Layer.succeed(BootService.BootService, service), |
| 102 | + ); |
| 103 | + |
| 104 | + const error = yield* Command.runWith(serviceCommand, { version: packageJson.version })([ |
| 105 | + command, |
| 106 | + "--base-dir", |
| 107 | + baseDir, |
| 108 | + ]).pipe( |
| 109 | + Effect.provideService(HostProcessEnvironment, {}), |
| 110 | + Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))), |
| 111 | + Effect.flip, |
| 112 | + ); |
| 113 | + |
| 114 | + expect(error).toMatchObject({ |
| 115 | + _tag: "BootServiceDowngradeRefusedError", |
| 116 | + installedVersion: "999.0.0", |
| 117 | + targetVersion: packageJson.version, |
| 118 | + }); |
| 119 | + expect(installOptions).toEqual([]); |
| 120 | + }), |
| 121 | + ); |
| 122 | + |
| 123 | + it.effect.each(["install", "update"] as const)("%s allows an explicit downgrade", (command) => |
| 124 | + Effect.gen(function* () { |
| 125 | + const fs = yield* FileSystem.FileSystem; |
| 126 | + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-cli-test-" }); |
| 127 | + const { service, installOptions } = makeTestService(newerServiceStatus); |
| 128 | + vi.spyOn(BootService, "layer").mockReturnValue( |
| 129 | + Layer.succeed(BootService.BootService, service), |
| 130 | + ); |
| 131 | + |
| 132 | + yield* Command.runWith(serviceCommand, { version: packageJson.version })([ |
| 133 | + command, |
| 134 | + "--base-dir", |
| 135 | + baseDir, |
| 136 | + "--allow-downgrade", |
| 137 | + ]).pipe( |
| 138 | + Effect.provideService(HostProcessEnvironment, {}), |
| 139 | + Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))), |
| 140 | + ); |
| 141 | + |
| 142 | + expect(installOptions).toEqual([{ allowDowngrade: true }]); |
| 143 | + }), |
| 144 | + ); |
| 145 | +}); |
| 146 | + |
| 147 | +it.effect.each([ |
| 148 | + { name: "a new service", state: { ...status, installed: false, current: false } }, |
| 149 | + { name: "an older service", state: { ...status, current: false, installedVersion: "0.0.0" } }, |
| 150 | + { |
| 151 | + name: "the same version", |
| 152 | + state: { ...status, current: false, installedVersion: packageJson.version }, |
| 153 | + }, |
| 154 | + { name: "an unknown version", state: { ...status, current: false } }, |
| 155 | +])("installs or repairs $name without an override", ({ state }) => |
| 156 | + Effect.gen(function* () { |
| 157 | + const { service, installOptions } = makeTestService(state); |
| 158 | + |
| 159 | + const result = yield* reconcileService().pipe( |
| 160 | + Effect.provideService(BootService.BootService, service), |
| 161 | + ); |
| 162 | + |
| 163 | + expect(result.changed).toBe(true); |
| 164 | + expect(installOptions).toEqual([undefined]); |
| 165 | + }), |
| 166 | +); |
| 167 | + |
| 168 | +it.effect("leaves a newer service unchanged during onboarding without prompting", () => |
| 169 | + Effect.gen(function* () { |
| 170 | + const { service, installOptions } = makeTestService(newerServiceStatus); |
| 171 | + const terminal = Terminal.make({ |
| 172 | + columns: Effect.succeed(80), |
| 173 | + rows: Effect.succeed(24), |
| 174 | + readInput: Effect.die("Onboarding must not prompt to replace a newer service."), |
| 175 | + readLine: Effect.die("Onboarding must not prompt to replace a newer service."), |
| 176 | + display: () => Effect.die("Onboarding must not prompt to replace a newer service."), |
| 177 | + }); |
| 178 | + |
| 179 | + const ready = yield* offerServiceDuringOnboarding.pipe( |
| 180 | + Effect.provideService(BootService.BootService, service), |
| 181 | + Effect.provideService(Terminal.Terminal, terminal), |
| 182 | + Effect.provide(NodeServices.layer), |
| 183 | + ); |
| 184 | + |
| 185 | + expect(ready).toBe(false); |
| 186 | + expect(installOptions).toEqual([]); |
| 187 | + }), |
| 188 | +); |
| 189 | + |
| 190 | +it.effect("keeps onboarding successful when a newer version appears before install", () => |
| 191 | + Effect.gen(function* () { |
| 192 | + const ready = yield* recoverServiceOnboardingOffer( |
| 193 | + Effect.fail( |
| 194 | + new BootService.BootServiceDowngradeRefusedError({ |
| 195 | + installedVersion: "999.0.0", |
| 196 | + targetVersion: packageJson.version, |
| 197 | + }), |
| 198 | + ), |
| 199 | + ); |
| 200 | + |
| 201 | + expect(ready).toBe(false); |
| 202 | + }), |
| 203 | +); |
0 commit comments