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