Open History to see the last commit
1import type { DesktopUpdateChannel, DesktopUpdateState } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { localUpdaterStaysPut, prepareLocalTrackSwitch } from "./updateTrackSwitch.logic";
5
6const STABLE = "0.1.3";
7
8/** Like the desktop app, saving a track re-checks the feed and reports what it offers. */
9function fakeBridge(offer: string | null | Error) {
10 const saved: DesktopUpdateChannel[] = [];
11 const base: DesktopUpdateState = {
12 enabled: true,
13 status: "up-to-date",
14 channel: "nightly",
15 currentVersion: "0.1.4-nightly.20261027.3796",
16 hostArch: "x64",
17 appArch: "x64",
18 runningUnderArm64Translation: false,
19 availableVersion: null,
20 downloadedVersion: null,
21 releaseNotes: [],
22 omittedReleaseCount: 0,
23 downloadPercent: null,
24 checkedAt: null,
25 message: null,
26 errorContext: null,
27 canRetry: false,
28 };
29 return {
30 saved,
31 bridge: {
32 setUpdateChannel: async (channel: DesktopUpdateChannel) => {
33 saved.push(channel);
34 if (offer instanceof Error) throw offer;
35 return {
36 ...base,
37 channel,
38 status: offer === null ? ("up-to-date" as const) : ("available" as const),
39 availableVersion: offer,
40 };
41 },
42 },
43 };
44}
45
46const prepare = (bridge: ReturnType<typeof fakeBridge>["bridge"], reinstall = true) =>
47 prepareLocalTrackSwitch(bridge, { channel: "latest", version: STABLE, reinstall });
48
49describe("prepareLocalTrackSwitch", () => {
50 it("installs the checked release or a newer one on the track, never an older one", async () => {
51 expect(await prepare(fakeBridge(STABLE).bridge)).toMatchObject({ ok: true });
52 // A newer Stable knows every migration the checked one does.
53 expect(await prepare(fakeBridge("0.1.4").bridge)).toMatchObject({ ok: true });
54
55 const stale = fakeBridge("0.1.2");
56 expect(await prepare(stale.bridge)).toEqual({
57 ok: false,
58 message: "This app found 0.1.2, which is older than 0.1.3. Try again in a few minutes.",
59 });
60 // The saved track is intent; "Switch them" retries without re-picking it.
61 expect(stale.saved).toEqual(["latest"]);
62 });
63
64 it("reports a track that could not be saved", async () => {
65 expect(await prepare(fakeBridge(new Error("feed check failed")).bridge)).toEqual({
66 ok: false,
67 message: "feed check failed",
68 });
69 });
70
71 it("only saves the track when this app is already on it", async () => {
72 const undo = fakeBridge(null);
73 expect(await prepare(undo.bridge, false)).toEqual({ ok: true, local: null });
74 expect(undo.saved).toEqual(["latest"]);
75 });
76});
77
78describe("localUpdaterStaysPut", () => {
79 it("moves an app whose updater is off only because its policy rules out nightly", () => {
80 const blocked = { enabled: false, channelBlockedByPolicy: true as const };
81 expect(localUpdaterStaysPut(blocked, "latest")).toBe(false);
82 expect(localUpdaterStaysPut(blocked, "nightly")).toBe(true);
83 // Off for another reason too, such as T3CODE_DISABLE_AUTO_UPDATE or a .deb install.
84 expect(localUpdaterStaysPut({ enabled: false }, "latest")).toBe(true);
85 expect(localUpdaterStaysPut({ enabled: true }, "nightly")).toBe(false);
86 });
87});