The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { DesktopUpdateChannel, DesktopUpdateState } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { |
| 5 | localUpdaterStaysPut, |
| 6 | nothingToMove, |
| 7 | prepareLocalTrackSwitch, |
| 8 | } from "./updateTrackSwitch.logic"; |
| 9 | |
| 10 | const STABLE = "0.1.3"; |
| 11 | |
| 12 | /** Like the desktop app, saving a track re-checks the feed and reports what it offers. */ |
| 13 | function fakeBridge(offer: string | null | Error) { |
| 14 | const saved: DesktopUpdateChannel[] = []; |
| 15 | const base: DesktopUpdateState = { |
| 16 | enabled: true, |
| 17 | status: "up-to-date", |
| 18 | channel: "nightly", |
| 19 | currentVersion: "0.1.4-nightly.20261027.3796", |
| 20 | hostArch: "x64", |
| 21 | appArch: "x64", |
| 22 | runningUnderArm64Translation: false, |
| 23 | availableVersion: null, |
| 24 | downloadedVersion: null, |
| 25 | releaseNotes: [], |
| 26 | omittedReleaseCount: 0, |
| 27 | downloadPercent: null, |
| 28 | checkedAt: null, |
| 29 | message: null, |
| 30 | errorContext: null, |
| 31 | canRetry: false, |
| 32 | }; |
| 33 | return { |
| 34 | saved, |
| 35 | bridge: { |
| 36 | setUpdateChannel: async (channel: DesktopUpdateChannel) => { |
| 37 | saved.push(channel); |
| 38 | if (offer instanceof Error) throw offer; |
| 39 | return { |
| 40 | ...base, |
| 41 | channel, |
| 42 | status: offer === null ? ("up-to-date" as const) : ("available" as const), |
| 43 | availableVersion: offer, |
| 44 | }; |
| 45 | }, |
| 46 | }, |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | const prepare = (bridge: ReturnType<typeof fakeBridge>["bridge"], reinstall = true) => |
| 51 | prepareLocalTrackSwitch(bridge, { channel: "latest", version: STABLE, reinstall }); |
| 52 | |
| 53 | describe("prepareLocalTrackSwitch", () => { |
| 54 | it("installs the checked release or a newer one on the track, never an older one", async () => { |
| 55 | expect(await prepare(fakeBridge(STABLE).bridge)).toMatchObject({ ok: true }); |
| 56 | // A newer Stable knows every migration the checked one does. |
| 57 | expect(await prepare(fakeBridge("0.1.4").bridge)).toMatchObject({ ok: true }); |
| 58 | |
| 59 | const stale = fakeBridge("0.1.2"); |
| 60 | expect(await prepare(stale.bridge)).toEqual({ |
| 61 | ok: false, |
| 62 | message: "This app found 0.1.2, which is older than 0.1.3. Try again in a few minutes.", |
| 63 | }); |
| 64 | // The saved track is intent; "Switch them" retries without re-picking it. |
| 65 | expect(stale.saved).toEqual(["latest"]); |
| 66 | }); |
| 67 | |
| 68 | it("reports a track that could not be saved", async () => { |
| 69 | expect(await prepare(fakeBridge(new Error("feed check failed")).bridge)).toEqual({ |
| 70 | ok: false, |
| 71 | message: "feed check failed", |
| 72 | }); |
| 73 | }); |
| 74 | |
| 75 | it("only saves the track when this app is already on it", async () => { |
| 76 | const undo = fakeBridge(null); |
| 77 | expect(await prepare(undo.bridge, false)).toEqual({ ok: true, local: null }); |
| 78 | expect(undo.saved).toEqual(["latest"]); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | describe("nothingToMove", () => { |
| 83 | const skippedRemote = { |
| 84 | skipped: "its server predates database checks; update it on Nightly first", |
| 85 | }; |
| 86 | const stableSwitch = { |
| 87 | label: "Stable", |
| 88 | channel: "latest" as const, |
| 89 | savedChannel: "latest" as const, |
| 90 | switchesLocal: false, |
| 91 | crossesLocal: false, |
| 92 | rows: [skippedRemote], |
| 93 | hasTargets: false, |
| 94 | }; |
| 95 | |
| 96 | it("disables a switch that would neither move a machine nor save a track", () => { |
| 97 | expect(nothingToMove(stableSwitch)).toEqual({ |
| 98 | note: null, |
| 99 | disabledReason: "No machine can switch to Stable now.", |
| 100 | }); |
| 101 | }); |
| 102 | |
| 103 | it("says a switch only saves the track when no machine moves", () => { |
| 104 | const onlySaves = { note: "No machine can switch to Stable now. This only saves the track." }; |
| 105 | expect(nothingToMove({ ...stableSwitch, savedChannel: "nightly" })).toEqual({ |
| 106 | ...onlySaves, |
| 107 | disabledReason: null, |
| 108 | }); |
| 109 | // Before the desktop reports its saved track, confirming may still need to save it. |
| 110 | expect(nothingToMove({ ...stableSwitch, savedChannel: null })).toEqual({ |
| 111 | ...onlySaves, |
| 112 | disabledReason: null, |
| 113 | }); |
| 114 | // A desktop host already on the track still gets its saved track changed. |
| 115 | expect(nothingToMove({ ...stableSwitch, hasTargets: true })).toEqual({ |
| 116 | ...onlySaves, |
| 117 | disabledReason: null, |
| 118 | }); |
| 119 | // With its updater off, this app stays on the other track. |
| 120 | expect( |
| 121 | nothingToMove({ ...stableSwitch, rows: [], crossesLocal: true, savedChannel: "nightly" }), |
| 122 | ).toEqual({ |
| 123 | ...onlySaves, |
| 124 | disabledReason: null, |
| 125 | }); |
| 126 | expect(nothingToMove({ ...stableSwitch, rows: [], savedChannel: "nightly" })).toEqual({ |
| 127 | note: "Every connected machine is already on Stable. This only saves the track.", |
| 128 | disabledReason: null, |
| 129 | }); |
| 130 | }); |
| 131 | |
| 132 | it("stays out of a switch that moves a machine", () => { |
| 133 | const none = { note: null, disabledReason: null }; |
| 134 | expect(nothingToMove({ ...stableSwitch, switchesLocal: true, crossesLocal: true })).toEqual( |
| 135 | none, |
| 136 | ); |
| 137 | expect(nothingToMove({ ...stableSwitch, rows: [skippedRemote, { skipped: null }] })).toEqual( |
| 138 | none, |
| 139 | ); |
| 140 | }); |
| 141 | }); |
| 142 | |
| 143 | describe("localUpdaterStaysPut", () => { |
| 144 | it("moves an app whose updater is off only because its policy rules out nightly", () => { |
| 145 | const blocked = { enabled: false, channelBlockedByPolicy: true as const }; |
| 146 | expect(localUpdaterStaysPut(blocked, "latest")).toBe(false); |
| 147 | expect(localUpdaterStaysPut(blocked, "nightly")).toBe(true); |
| 148 | // Off for another reason too, such as T3CODE_DISABLE_AUTO_UPDATE or a .deb install. |
| 149 | expect(localUpdaterStaysPut({ enabled: false }, "latest")).toBe(true); |
| 150 | expect(localUpdaterStaysPut({ enabled: true }, "nightly")).toBe(false); |
| 151 | }); |
| 152 | }); |