Open History to see the last commit
1import { EnvironmentId, type ServerSelfUpdateCapability } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 collectRestartingMachines,
6 collectServerUpdateTargets,
7 runUpdateEverything,
8 type UpdateEverythingMachine,
9} from "./updateEverything.logic";
10
11function machine(
12 id: string,
13 serverVersion: string,
14 selfUpdate: ServerSelfUpdateCapability | undefined,
15 overrides: Partial<UpdateEverythingMachine> = {},
16): UpdateEverythingMachine {
17 return {
18 environmentId: EnvironmentId.make(id),
19 label: id,
20 serverConfig: {
21 environment: {
22 environmentId: EnvironmentId.make(id),
23 label: id,
24 platform: { os: "linux", arch: "x64" },
25 serverVersion,
26 capabilities: {
27 repositoryIdentity: true,
28 ...(selfUpdate ? { serverSelfUpdate: selfUpdate } : {}),
29 ...(selfUpdate === "desktop-managed" ? { desktopAppUpdate: true } : {}),
30 },
31 },
32 settings: { continueThreadsAfterServerUpdate: false },
33 },
34 connected: true,
35 hostedByThisApp: false,
36 canMaintain: true,
37 updateState: { status: "idle" },
38 ...overrides,
39 };
40}
41
42describe("collectServerUpdateTargets", () => {
43 it("updates remote servers that can move to the target and names every skipped one", () => {
44 const { targets, skipped } = collectServerUpdateTargets(
45 [
46 machine("this-app", "0.0.40", "desktop-managed", { hostedByThisApp: true }),
47 machine("service", "0.0.40", "boot-service"),
48 machine("nightly-service", "0.0.49-nightly.20261001.3", "boot-service"),
49 machine("other-desktop", "0.0.49-nightly.20261001.3", "desktop-managed"),
50 machine("offline", "0.0.40", "boot-service", { connected: false }),
51 machine("never-connected", "0.0.40", "boot-service", { serverConfig: null }),
52 machine("no-scope", "0.0.40", "boot-service", { canMaintain: false }),
53 machine("busy", "0.0.40", "boot-service", {
54 updateState: {
55 status: "running",
56 stage: "downloading",
57 fromVersion: "0.0.40",
58 targetVersion: "0.0.50",
59 },
60 }),
61 machine("current", "0.0.50", "boot-service"),
62 machine("current-offline", "0.0.50", "boot-service", { connected: false }),
63 machine("manual", "0.0.40", undefined),
64 ],
65 "0.0.50",
66 );
67
68 expect(targets.map((target) => [target.serverLabel, target.targetVersion])).toEqual([
69 ["service", "0.0.50"],
70 ["other-desktop", "0.0.50"],
71 ]);
72 expect(skipped.map((entry) => [entry.label, entry.reason])).toEqual([
73 ["nightly-service", "on the nightly channel"],
74 ["offline", "not connected"],
75 ["never-connected", "not connected"],
76 ["no-scope", "no permission"],
77 ["busy", "already updating"],
78 ["manual", "needs a manual update"],
79 ]);
80 });
81});
82
83describe("collectRestartingMachines", () => {
84 it("lists updated servers, and this app's servers only when it installs", () => {
85 const withContinuation = (
86 id: string,
87 overrides: Partial<UpdateEverythingMachine> = {},
88 enabled = false,
89 ) => {
90 const base = machine(id, "0.0.40", "boot-service", overrides);
91 return {
92 ...base,
93 serverConfig: {
94 environment: {
95 ...base.serverConfig!.environment,
96 capabilities: {
97 ...base.serverConfig!.environment.capabilities,
98 threadRestartContinuation: true,
99 },
100 },
101 settings: { continueThreadsAfterServerUpdate: enabled },
102 },
103 };
104 };
105 const machines = [
106 withContinuation("this-app", { hostedByThisApp: true }, true),
107 withContinuation("alvin", {}, true),
108 withContinuation("opted-out", {}, false),
109 machine("old", "0.0.40", "boot-service"),
110 withContinuation("providers-only"),
111 ];
112 const servers = ["alvin", "opted-out", "old"].map((id) => ({
113 environmentId: EnvironmentId.make(id),
114 }));
115
116 expect(collectRestartingMachines(machines, servers, true)).toEqual([
117 { environmentId: "this-app", continuesThreads: true },
118 { environmentId: "alvin", continuesThreads: true },
119 { environmentId: "opted-out", continuesThreads: false },
120 { environmentId: "old", continuesThreads: false },
121 ]);
122 expect(
123 collectRestartingMachines(machines, servers, false).map((entry) => entry.environmentId),
124 ).toEqual(["alvin", "opted-out", "old"]);
125 });
126});
127
128describe("runUpdateEverything", () => {
129 it("still updates remote machines when the local download fails", async () => {
130 const calls: string[] = [];
131 const result = await runUpdateEverything({
132 downloadLocal: async () => false,
133 updateProviders: async () => {
134 calls.push("providers");
135 },
136 updateServers: async () => {
137 calls.push("servers");
138 },
139 });
140
141 expect(calls).toEqual(["providers", "servers"]);
142 expect(result.localReady).toBe(false);
143 });
144});