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