Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

versionSkew.test.ts

215 lines7.0 KB

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