Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import type { ServerUpdateState } from "@t3tools/client-runtime/state/server";
3import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5const branding = vi.hoisted(() => ({ APP_VERSION: "0.0.34" }));
6vi.mock("./branding", () => branding);
7
8import { APP_VERSION } from "./branding";
9import {
10 appendVersionMismatchHint,
11 buildVersionMismatchDismissalKey,
12 dismissServerUpdateFailure,
13 dismissVersionMismatch,
14 isServerUpdateFailureDismissed,
15 isVersionMismatchDismissed,
16 manualServerUpdateCommand,
17 resolveServerConfigVersionMismatch,
18 resolveServerSelfUpdateCapability,
19 resolveVersionMismatch,
20 serverUpdateGuidance,
21 supportsDesktopAppUpdate,
22} from "./versionSkew";
23
24const MISMATCH_HINT =
25 "Version mismatch. Try syncing the client and server to the same T3 Code version.";
26
27describe("versionSkew", () => {
28 it("updates only the proven npm prefix and safely quotes its path", () => {
29 expect(manualServerUpdateCommand("0.0.45", { kind: "npm-global", prefix: "/opt/node" })).toBe(
30 "npm install --global --prefix '/opt/node' t3@0.0.45",
31 );
32 expect(
33 manualServerUpdateCommand("0.0.45", { kind: "npm-global", prefix: "/opt/maria's node" }),
34 ).toBe("npm install --global --prefix '/opt/maria'\\''s node' t3@0.0.45");
35 });
36
37 it("keeps runner and unknown commands as relaunches", () => {
38 expect(manualServerUpdateCommand("0.0.45")).toBe("npx t3@0.0.45");
39 expect(manualServerUpdateCommand("0.0.45", { kind: "npx" })).toBe("npx t3@0.0.45");
40 expect(manualServerUpdateCommand("0.0.45", { kind: "pnpm-dlx" })).toBe("pnpm dlx t3@0.0.45");
41 expect(manualServerUpdateCommand("0.0.45", { kind: "bunx" })).toBe("bunx t3@0.0.45");
42 });
43 beforeEach(() => {
44 branding.APP_VERSION = "0.0.34";
45 });
46
47 it("dismisses only the current failed attempt without clearing its retry state", () => {
48 const failure = {
49 status: "failed",
50 stage: "downloading",
51 fromVersion: "0.0.33",
52 targetVersion: "0.0.34",
53 message: "Download failed.",
54 } as const satisfies ServerUpdateState;
55 const retryFailure = { ...failure };
56 const otherEnvironmentFailure = { ...failure };
57
58 dismissServerUpdateFailure(failure);
59
60 expect(isServerUpdateFailureDismissed(failure)).toBe(true);
61 expect(failure.status).toBe("failed");
62 expect(failure.message).toBe("Download failed.");
63 expect(isServerUpdateFailureDismissed(retryFailure)).toBe(false);
64 expect(isServerUpdateFailureDismissed(otherEnvironmentFailure)).toBe(false);
65 });
66
67 it("does not dismiss an update that is still running", () => {
68 const running = {
69 status: "running",
70 stage: "resuming",
71 fromVersion: "0.0.33",
72 targetVersion: "0.0.34",
73 } as const satisfies ServerUpdateState;
74
75 dismissServerUpdateFailure(running);
76
77 expect(isServerUpdateFailureDismissed(running)).toBe(false);
78 });
79
80 it("does not warn when versions match", () => {
81 expect(resolveVersionMismatch(APP_VERSION)).toBeNull();
82 });
83
84 it("returns a mismatch when the server is behind the client", () => {
85 expect(resolveVersionMismatch("0.0.33")).toEqual({
86 clientVersion: "0.0.34",
87 serverVersion: "0.0.33",
88 hint: MISMATCH_HINT,
89 });
90 });
91
92 it("does not warn when the server is ahead of the client", () => {
93 expect(resolveVersionMismatch("0.0.35")).toBeNull();
94 expect(resolveVersionMismatch("9.9.9")).toBeNull();
95 });
96
97 it("does not warn when a nightly and a stable build share a core version", () => {
98 expect(resolveVersionMismatch("0.0.34-nightly.20260818.1124")).toBeNull();
99
100 branding.APP_VERSION = "0.0.34-nightly.20260818.1124";
101 expect(resolveVersionMismatch("0.0.34")).toBeNull();
102 });
103
104 it.each(["0.0.34-nightly.20260823.1124", "0.0.34-nightly.20260824.1124"])(
105 "warns when nightly server %s is behind a nightly client on the same release",
106 (serverVersion) => {
107 branding.APP_VERSION = "0.0.34-nightly.20260824.1125";
108
109 expect(resolveVersionMismatch(serverVersion)).toEqual({
110 clientVersion: "0.0.34-nightly.20260824.1125",
111 serverVersion,
112 hint: MISMATCH_HINT,
113 });
114 },
115 );
116
117 it("does not warn when a nightly server is ahead on the same release", () => {
118 branding.APP_VERSION = "0.0.34-nightly.20260824.1125";
119
120 expect(resolveVersionMismatch("0.0.34-nightly.20260824.1126")).toBeNull();
121 });
122
123 it("treats a nightly server built past the client as ahead, not skew", () => {
124 expect(resolveVersionMismatch("0.0.35-nightly.20260818.1124")).toBeNull();
125 });
126
127 it("still warns when a nightly client outruns the server by a release", () => {
128 branding.APP_VERSION = "0.0.35-nightly.20260818.1124";
129
130 expect(resolveVersionMismatch("0.0.34")).toEqual({
131 clientVersion: "0.0.35-nightly.20260818.1124",
132 serverVersion: "0.0.34",
133 hint: MISMATCH_HINT,
134 });
135 });
136
137 it("falls back to string inequality when a version is not semver", () => {
138 expect(resolveVersionMismatch("dev")).toEqual({
139 clientVersion: "0.0.34",
140 serverVersion: "dev",
141 hint: MISMATCH_HINT,
142 });
143
144 branding.APP_VERSION = "dev";
145 expect(resolveVersionMismatch("dev")).toBeNull();
146 expect(resolveVersionMismatch("0.0.34")).toMatchObject({ serverVersion: "0.0.34" });
147 });
148
149 it("reads the server version from config descriptors", () => {
150 expect(
151 resolveServerConfigVersionMismatch({
152 environment: {
153 environmentId: EnvironmentId.make("environment-1"),
154 label: "Remote",
155 platform: {
156 os: "darwin",
157 arch: "arm64",
158 },
159 serverVersion: "0.0.33",
160 capabilities: {
161 repositoryIdentity: true,
162 },
163 },
164 }),
165 ).toMatchObject({
166 serverVersion: "0.0.33",
167 });
168 });
169
170 it("keys dismissals by environment, client version, and server version", () => {
171 const environmentId = EnvironmentId.make("environment-dismissal");
172 const key = buildVersionMismatchDismissalKey(environmentId, {
173 clientVersion: APP_VERSION,
174 serverVersion: "9.9.9",
175 });
176
177 expect(key).toBe(`${environmentId}:${APP_VERSION}:9.9.9`);
178 expect(isVersionMismatchDismissed(key)).toBe(false);
179
180 dismissVersionMismatch(key);
181
182 expect(isVersionMismatchDismissed(key)).toBe(true);
183 expect(
184 isVersionMismatchDismissed(
185 buildVersionMismatchDismissalKey(environmentId, {
186 clientVersion: APP_VERSION,
187 serverVersion: "9.9.10",
188 }),
189 ),
190 ).toBe(false);
191 });
192
193 it("appends a hint to connection errors when the server is behind", () => {
194 const mismatch = resolveVersionMismatch("0.0.33");
195
196 expect(appendVersionMismatchHint("Socket closed.", mismatch)).toBe(
197 `Socket closed. Hint: ${MISMATCH_HINT}`,
198 );
199 });
200
201 it("reads desktop-managed update capabilities from config descriptors", () => {
202 expect(
203 resolveServerSelfUpdateCapability({
204 environment: {
205 environmentId: EnvironmentId.make("environment-desktop"),
206 label: "Desktop",
207 platform: { os: "darwin", arch: "arm64" },
208 serverVersion: "9.9.9",
209 capabilities: {
210 repositoryIdentity: true,
211 serverSelfUpdate: "desktop-managed",
212 },
213 },
214 }),
215 ).toBe("desktop-managed");
216 expect(resolveServerSelfUpdateCapability(null)).toBeNull();
217 });
218
219 it("detects remote desktop-app update support from config descriptors", () => {
220 const descriptor = (desktopAppUpdate?: boolean) => ({
221 environment: {
222 environmentId: EnvironmentId.make("environment-desktop"),
223 label: "Desktop",
224 platform: { os: "darwin", arch: "arm64" } as const,
225 serverVersion: "9.9.9",
226 capabilities: {
227 repositoryIdentity: true,
228 serverSelfUpdate: "desktop-managed" as const,
229 ...(desktopAppUpdate === undefined ? {} : { desktopAppUpdate }),
230 },
231 },
232 });
233
234 expect(supportsDesktopAppUpdate(descriptor(true))).toBe(true);
235 expect(supportsDesktopAppUpdate(descriptor(false))).toBe(false);
236 expect(supportsDesktopAppUpdate(descriptor())).toBe(false);
237 expect(supportsDesktopAppUpdate(null)).toBe(false);
238 });
239
240 it("matches version-drift guidance to the advertised update path", () => {
241 expect(serverUpdateGuidance("respawn")).toBe("Update to stay in sync");
242 expect(serverUpdateGuidance("desktop-managed")).toBe("Update the desktop app");
243 });
244});