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