Open History to see the last commit
1import { EnvironmentId } from "@t3tools/contracts";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4// Pinned so the direction cases below read as fixed versions instead of
5// arithmetic on whatever version this checkout happens to be at.
6const branding = vi.hoisted(() => ({ APP_VERSION: "0.0.34" }));
7vi.mock("./branding", () => branding);
8
9import { APP_VERSION } from "./branding";
10import {
11 appendVersionMismatchHint,
12 buildVersionMismatchDismissalKey,
13 dismissVersionMismatch,
14 isVersionMismatchDismissed,
15 resolveServerConfigVersionMismatch,
16 resolveServerSelfUpdateCapability,
17 resolveVersionMismatch,
18 serverUpdateGuidance,
19} from "./versionSkew";
20
21const MISMATCH_HINT =
22 "Version mismatch. Try syncing the client and server to the same T3 Code version.";
23
24describe("versionSkew", () => {
25 beforeEach(() => {
26 branding.APP_VERSION = "0.0.34";
27 });
28
29 it("does not warn when versions match", () => {
30 expect(resolveVersionMismatch(APP_VERSION)).toBeNull();
31 });
32
33 it("returns a mismatch when the server is behind the client", () => {
34 expect(resolveVersionMismatch("0.0.33")).toEqual({
35 clientVersion: "0.0.34",
36 serverVersion: "0.0.33",
37 hint: MISMATCH_HINT,
38 });
39 });
40
41 it("does not warn when the server is ahead of the client", () => {
42 expect(resolveVersionMismatch("0.0.35")).toBeNull();
43 expect(resolveVersionMismatch("9.9.9")).toBeNull();
44 });
45
46 it("does not warn when a nightly and a stable build share a core version", () => {
47 expect(resolveVersionMismatch("0.0.34-nightly.20260818.1124")).toBeNull();
48
49 branding.APP_VERSION = "0.0.34-nightly.20260818.1124";
50 expect(resolveVersionMismatch("0.0.34")).toBeNull();
51 });
52
53 it.each(["0.0.34-nightly.20260823.1124", "0.0.34-nightly.20260824.1124"])(
54 "warns when nightly server %s is behind a nightly client on the same release",
55 (serverVersion) => {
56 branding.APP_VERSION = "0.0.34-nightly.20260824.1125";
57
58 expect(resolveVersionMismatch(serverVersion)).toEqual({
59 clientVersion: "0.0.34-nightly.20260824.1125",
60 serverVersion,
61 hint: MISMATCH_HINT,
62 });
63 },
64 );
65
66 it("does not warn when a nightly server is ahead on the same release", () => {
67 branding.APP_VERSION = "0.0.34-nightly.20260824.1125";
68
69 expect(resolveVersionMismatch("0.0.34-nightly.20260824.1126")).toBeNull();
70 });
71
72 it("treats a nightly server built past the client as ahead, not skew", () => {
73 expect(resolveVersionMismatch("0.0.35-nightly.20260818.1124")).toBeNull();
74 });
75
76 it("still warns when a nightly client outruns the server by a release", () => {
77 branding.APP_VERSION = "0.0.35-nightly.20260818.1124";
78
79 expect(resolveVersionMismatch("0.0.34")).toEqual({
80 clientVersion: "0.0.35-nightly.20260818.1124",
81 serverVersion: "0.0.34",
82 hint: MISMATCH_HINT,
83 });
84 });
85
86 it("falls back to string inequality when a version is not semver", () => {
87 expect(resolveVersionMismatch("dev")).toEqual({
88 clientVersion: "0.0.34",
89 serverVersion: "dev",
90 hint: MISMATCH_HINT,
91 });
92
93 branding.APP_VERSION = "dev";
94 expect(resolveVersionMismatch("dev")).toBeNull();
95 expect(resolveVersionMismatch("0.0.34")).toMatchObject({ serverVersion: "0.0.34" });
96 });
97
98 it("reads the server version from config descriptors", () => {
99 expect(
100 resolveServerConfigVersionMismatch({
101 environment: {
102 environmentId: EnvironmentId.make("environment-1"),
103 label: "Remote",
104 platform: {
105 os: "darwin",
106 arch: "arm64",
107 },
108 serverVersion: "0.0.33",
109 capabilities: {
110 repositoryIdentity: true,
111 },
112 },
113 }),
114 ).toMatchObject({
115 serverVersion: "0.0.33",
116 });
117 });
118
119 it("keys dismissals by environment, client version, and server version", () => {
120 const environmentId = EnvironmentId.make("environment-dismissal");
121 const key = buildVersionMismatchDismissalKey(environmentId, {
122 clientVersion: APP_VERSION,
123 serverVersion: "9.9.9",
124 });
125
126 expect(key).toBe(`${environmentId}:${APP_VERSION}:9.9.9`);
127 expect(isVersionMismatchDismissed(key)).toBe(false);
128
129 dismissVersionMismatch(key);
130
131 expect(isVersionMismatchDismissed(key)).toBe(true);
132 expect(
133 isVersionMismatchDismissed(
134 buildVersionMismatchDismissalKey(environmentId, {
135 clientVersion: APP_VERSION,
136 serverVersion: "9.9.10",
137 }),
138 ),
139 ).toBe(false);
140 });
141
142 it("appends a hint to connection errors when the server is behind", () => {
143 const mismatch = resolveVersionMismatch("0.0.33");
144
145 expect(appendVersionMismatchHint("Socket closed.", mismatch)).toBe(
146 `Socket closed. Hint: ${MISMATCH_HINT}`,
147 );
148 });
149
150 it("reads desktop-managed update capabilities from config descriptors", () => {
151 expect(
152 resolveServerSelfUpdateCapability({
153 environment: {
154 environmentId: EnvironmentId.make("environment-desktop"),
155 label: "Desktop",
156 platform: { os: "darwin", arch: "arm64" },
157 serverVersion: "9.9.9",
158 capabilities: {
159 repositoryIdentity: true,
160 serverSelfUpdate: "desktop-managed",
161 },
162 },
163 }),
164 ).toBe("desktop-managed");
165 expect(resolveServerSelfUpdateCapability(null)).toBeNull();
166 });
167
168 it("matches version-drift guidance to the advertised update path", () => {
169 expect(serverUpdateGuidance("respawn", "Remote server")).toBe(
170 "Update the Remote server so they stay in sync.",
171 );
172 expect(serverUpdateGuidance("desktop-managed", "Desktop server")).toBe(
173 "Update the desktop app that runs the Desktop server.",
174 );
175 expect(serverUpdateGuidance(null, "Local server")).toBe(
176 "Relaunch the Local server with the copied command to sync them.",
177 );
178 });
179});