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

versionSkew.test.ts

136 lines4.7 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​
5const branding = vi.hoisted(() => ({ APP_VERSION: "0.0.34" }));
6vi.mock("./branding", () => branding);
7​
8import { APP_VERSION } from "./branding";
9import {
10 buildVersionMismatchDismissalKey,
11 dismissServerUpdateFailure,
12 dismissVersionMismatch,
13 isServerUpdateFailureDismissed,
14 isVersionMismatchDismissed,
15 manualServerUpdateCommand,
16 resolveVersionMismatch,
17} from "./versionSkew";
18​
19const MISMATCH_HINT =
20 "Version mismatch. Try syncing the client and server to the same T3 Code version.";
21​
22describe("versionSkew", () => {
23 it("updates only the proven npm prefix and safely quotes its path", () => {
24 expect(manualServerUpdateCommand("0.0.45", { kind: "npm-global", prefix: "/opt/node" })).toBe(
25 "npm install --global --prefix '/opt/node' t3@0.0.45",
26 );
27 expect(
28 manualServerUpdateCommand("0.0.45", { kind: "npm-global", prefix: "/opt/maria's node" }),
29 ).toBe("npm install --global --prefix '/opt/maria'\\''s node' t3@0.0.45");
30 });
31 beforeEach(() => {
32 branding.APP_VERSION = "0.0.34";
33 });
34​
35 it("dismisses only the current failed attempt without clearing its retry state", () => {
36 const failure = {
37 status: "failed",
38 stage: "downloading",
39 fromVersion: "0.0.33",
40 targetVersion: "0.0.34",
41 message: "Download failed.",
42 } as const satisfies ServerUpdateState;
43 const retryFailure = { ...failure };
44 const otherEnvironmentFailure = { ...failure };
45​
46 dismissServerUpdateFailure(failure);
47​
48 expect(isServerUpdateFailureDismissed(failure)).toBe(true);
49 expect(failure.status).toBe("failed");
50 expect(failure.message).toBe("Download failed.");
51 expect(isServerUpdateFailureDismissed(retryFailure)).toBe(false);
52 expect(isServerUpdateFailureDismissed(otherEnvironmentFailure)).toBe(false);
53 });
54​
55 it("returns a mismatch when the server is behind the client", () => {
56 expect(resolveVersionMismatch("0.0.33")).toEqual({
57 clientVersion: "0.0.34",
58 serverVersion: "0.0.33",
59 hint: MISMATCH_HINT,
60 });
61 });
62​
63 it("does not warn when the server is ahead of the client", () => {
64 expect(resolveVersionMismatch("0.0.35")).toBeNull();
65 expect(resolveVersionMismatch("9.9.9")).toBeNull();
66 });
67​
68 it("does not warn when a nightly and a stable build share a core version", () => {
69 expect(resolveVersionMismatch("0.0.34-nightly.20260818.1124")).toBeNull();
70​
71 branding.APP_VERSION = "0.0.34-nightly.20260818.1124";
72 expect(resolveVersionMismatch("0.0.34")).toBeNull();
73 });
74​
75 it.each(["0.0.34-nightly.20260823.1124", "0.0.34-nightly.20260824.1124"])(
76 "warns when nightly server %s is behind a nightly client on the same release",
77 (serverVersion) => {
78 branding.APP_VERSION = "0.0.34-nightly.20260824.1125";
79​
80 expect(resolveVersionMismatch(serverVersion)).toEqual({
81 clientVersion: "0.0.34-nightly.20260824.1125",
82 serverVersion,
83 hint: MISMATCH_HINT,
84 });
85 },
86 );
87​
88 // regression: #7579 versions were compared as strings, not semver
89 it("treats a nightly server built past the client as ahead, not skew", () => {
90 expect(resolveVersionMismatch("0.0.35-nightly.20260818.1124")).toBeNull();
91 });
92​
93 it("still warns when a nightly client outruns the server by a release", () => {
94 branding.APP_VERSION = "0.0.35-nightly.20260818.1124";
95​
96 expect(resolveVersionMismatch("0.0.34")).toEqual({
97 clientVersion: "0.0.35-nightly.20260818.1124",
98 serverVersion: "0.0.34",
99 hint: MISMATCH_HINT,
100 });
101 });
102​
103 it("falls back to string inequality when a version is not semver", () => {
104 expect(resolveVersionMismatch("dev")).toEqual({
105 clientVersion: "0.0.34",
106 serverVersion: "dev",
107 hint: MISMATCH_HINT,
108 });
109​
110 branding.APP_VERSION = "dev";
111 expect(resolveVersionMismatch("dev")).toBeNull();
112 expect(resolveVersionMismatch("0.0.34")).toMatchObject({ serverVersion: "0.0.34" });
113 });
114​
115 it("keys dismissals by environment, client version, and server version", () => {
116 const environmentId = EnvironmentId.make("environment-dismissal");
117 const key = buildVersionMismatchDismissalKey(environmentId, {
118 clientVersion: APP_VERSION,
119 serverVersion: "9.9.9",
120 });
121​
122 expect(isVersionMismatchDismissed(key)).toBe(false);
123​
124 dismissVersionMismatch(key);
125​
126 expect(isVersionMismatchDismissed(key)).toBe(true);
127 expect(
128 isVersionMismatchDismissed(
129 buildVersionMismatchDismissalKey(environmentId, {
130 clientVersion: APP_VERSION,
131 serverVersion: "9.9.10",
132 }),
133 ),
134 ).toBe(false);
135 });
136});