|
1 | | -import { EnvironmentId } from "@t3tools/contracts"; |
| 1 | +import { EnvironmentId, type ServerConfig } from "@t3tools/contracts"; |
| 2 | +import { Atom } from "effect/reactivity"; |
2 | 3 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
3 | 4 |
|
4 | 5 | import { appAtomRegistry } from "~/rpc/atomRegistry"; |
5 | 6 | import { serverEnvironment } from "~/state/server"; |
6 | | -import { startDesktopUpdateLaunchNotice } from "./updateEverythingRun"; |
| 7 | +import type { ServerUpdateOutcome, ServerUpdateTarget } from "../ServerUpdateAction"; |
| 8 | +import type { UpdateEverythingRun } from "./updateEverything.logic"; |
| 9 | +import { |
| 10 | + retryFailedUpdateRunServers, |
| 11 | + setUpdateEverythingRun, |
| 12 | + startDesktopUpdateLaunchNotice, |
| 13 | +} from "./updateEverythingRun"; |
7 | 14 |
|
8 | 15 | function relaunchWhileRestarting(environmentId: EnvironmentId) { |
9 | 16 | const now = 1_800_000_000_000; |
@@ -68,3 +75,65 @@ describe("startDesktopUpdateLaunchNotice", () => { |
68 | 75 | } |
69 | 76 | }); |
70 | 77 | }); |
| 78 | + |
| 79 | +describe("retryFailedUpdateRunServers", () => { |
| 80 | + it("settles a failed server that already restarted on the target and retries only the others", () => { |
| 81 | + const targetVersion = "9.9.9"; |
| 82 | + const serverVersions = new Map([ |
| 83 | + [EnvironmentId.make("already-updated"), targetVersion], |
| 84 | + [EnvironmentId.make("behind"), "9.9.8"], |
| 85 | + ]); |
| 86 | + using _config = vi |
| 87 | + .spyOn(serverEnvironment, "configValueAtom") |
| 88 | + .mockImplementation((environmentId) => |
| 89 | + Atom.make({ |
| 90 | + environment: { serverVersion: serverVersions.get(environmentId!) }, |
| 91 | + } as unknown as ServerConfig), |
| 92 | + ); |
| 93 | + const rejection = "Remote updates must select a newer server version."; |
| 94 | + const failedMachine = (environmentId: EnvironmentId) => ({ |
| 95 | + environmentId, |
| 96 | + label: environmentId, |
| 97 | + updatesProviders: false, |
| 98 | + server: { |
| 99 | + environmentId, |
| 100 | + serverLabel: environmentId, |
| 101 | + selfUpdate: "boot-service" as const, |
| 102 | + targetVersion, |
| 103 | + }, |
| 104 | + handedOffVersion: null, |
| 105 | + startedAt: 0, |
| 106 | + result: "failed" as const, |
| 107 | + failure: rejection, |
| 108 | + providerFailure: null, |
| 109 | + }); |
| 110 | + setUpdateEverythingRun(() => ({ |
| 111 | + localVersion: null, |
| 112 | + phase: "servers", |
| 113 | + restartAt: null, |
| 114 | + machines: [...serverVersions.keys()].map(failedMachine), |
| 115 | + })); |
| 116 | + const update = vi.fn( |
| 117 | + (_target: ServerUpdateTarget) => new Promise<ServerUpdateOutcome>(() => {}), |
| 118 | + ); |
| 119 | + |
| 120 | + retryFailedUpdateRunServers(update); |
| 121 | + |
| 122 | + let run: UpdateEverythingRun | null = null; |
| 123 | + setUpdateEverythingRun((current) => { |
| 124 | + run = current; |
| 125 | + return null; |
| 126 | + }); |
| 127 | + expect(update.mock.calls.map(([target]) => target.environmentId)).toEqual(["behind"]); |
| 128 | + expect( |
| 129 | + (run as UpdateEverythingRun | null)?.machines.map(({ environmentId, result, server }) => ({ |
| 130 | + environmentId, |
| 131 | + result, |
| 132 | + retryable: server !== null, |
| 133 | + })), |
| 134 | + ).toEqual([ |
| 135 | + { environmentId: "already-updated", result: "done", retryable: false }, |
| 136 | + { environmentId: "behind", result: null, retryable: true }, |
| 137 | + ]); |
| 138 | + }); |
| 139 | +}); |
0 commit comments