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("treats a nightly server built past the client as ahead, not skew", () => {
54 expect(resolveVersionMismatch("0.0.35-nightly.20260818.1124")).toBeNull();
55 });
56
57 it("still warns when a nightly client outruns the server by a release", () => {
58 branding.APP_VERSION = "0.0.35-nightly.20260818.1124";
59
60 expect(resolveVersionMismatch("0.0.34")).toEqual({
61 clientVersion: "0.0.35-nightly.20260818.1124",
62 serverVersion: "0.0.34",
63 hint: MISMATCH_HINT,
64 });
65 });
66
67 it("falls back to string inequality when a version is not semver", () => {
68 expect(resolveVersionMismatch("dev")).toEqual({
69 clientVersion: "0.0.34",
70 serverVersion: "dev",
71 hint: MISMATCH_HINT,
72 });
73
74 branding.APP_VERSION = "dev";
75 expect(resolveVersionMismatch("dev")).toBeNull();
76 expect(resolveVersionMismatch("0.0.34")).toMatchObject({ serverVersion: "0.0.34" });
77 });
78
79 it("reads the server version from config descriptors", () => {
80 expect(
81 resolveServerConfigVersionMismatch({
82 environment: {
83 environmentId: EnvironmentId.make("environment-1"),
84 label: "Remote",
85 platform: {
86 os: "darwin",
87 arch: "arm64",
88 },
89 serverVersion: "0.0.33",
90 capabilities: {
91 repositoryIdentity: true,
92 },
93 },
94 }),
95 ).toMatchObject({
96 serverVersion: "0.0.33",
97 });
98 });
99
100 it("keys dismissals by environment, client version, and server version", () => {
101 const environmentId = EnvironmentId.make("environment-dismissal");
102 const key = buildVersionMismatchDismissalKey(environmentId, {
103 clientVersion: APP_VERSION,
104 serverVersion: "9.9.9",
105 });
106
107 expect(key).toBe(`${environmentId}:${APP_VERSION}:9.9.9`);
108 expect(isVersionMismatchDismissed(key)).toBe(false);
109
110 dismissVersionMismatch(key);
111
112 expect(isVersionMismatchDismissed(key)).toBe(true);
113 expect(
114 isVersionMismatchDismissed(
115 buildVersionMismatchDismissalKey(environmentId, {
116 clientVersion: APP_VERSION,
117 serverVersion: "9.9.10",
118 }),
119 ),
120 ).toBe(false);
121 });
122
123 it("appends a hint to connection errors when the server is behind", () => {
124 const mismatch = resolveVersionMismatch("0.0.33");
125
126 expect(appendVersionMismatchHint("Socket closed.", mismatch)).toBe(
127 `Socket closed. Hint: ${MISMATCH_HINT}`,
128 );
129 });
130
131 it("reads desktop-managed update capabilities from config descriptors", () => {
132 expect(
133 resolveServerSelfUpdateCapability({
134 environment: {
135 environmentId: EnvironmentId.make("environment-desktop"),
136 label: "Desktop",
137 platform: { os: "darwin", arch: "arm64" },
138 serverVersion: "9.9.9",
139 capabilities: {
140 repositoryIdentity: true,
141 serverSelfUpdate: "desktop-managed",
142 },
143 },
144 }),
145 ).toBe("desktop-managed");
146 expect(resolveServerSelfUpdateCapability(null)).toBeNull();
147 });
148
149 it("matches version-drift guidance to the advertised update path", () => {
150 expect(serverUpdateGuidance("respawn", "Remote server")).toBe(
151 "Update the Remote server so they stay in sync.",
152 );
153 expect(serverUpdateGuidance("desktop-managed", "Desktop server")).toBe(
154 "Update the desktop app that runs the Desktop server.",
155 );
156 expect(serverUpdateGuidance(null, "Local server")).toBe(
157 "Relaunch the Local server with the copied command to sync them.",
158 );
159 });
160});