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 { DesktopUpdateState } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 3 | import { page, userEvent } from "vite-plus/test/browser"; |
| 4 | |
| 5 | import "~/index.css"; |
| 6 | import { act, render } from "../../../test/render"; |
| 7 | import { SidebarUpdatePill } from "./SidebarUpdatePill"; |
| 8 | import type { UpdateEverythingRow } from "./updateEverything.logic"; |
| 9 | |
| 10 | const mocks = vi.hoisted(() => { |
| 11 | const listeners = new Set<() => void>(); |
| 12 | return { |
| 13 | update: null as DesktopUpdateState | null, |
| 14 | hasUpdates: false, |
| 15 | rows: [] as ReadonlyArray<UpdateEverythingRow>, |
| 16 | subscribe: (listener: () => void) => { |
| 17 | listeners.add(listener); |
| 18 | return () => listeners.delete(listener); |
| 19 | }, |
| 20 | notify: () => { |
| 21 | for (const listener of listeners) listener(); |
| 22 | }, |
| 23 | }; |
| 24 | }); |
| 25 | vi.mock("../../state/desktopUpdate", async (importOriginal) => ({ |
| 26 | ...(await importOriginal<typeof import("../../state/desktopUpdate")>()), |
| 27 | useDesktopUpdateState: (await import("react")).useSyncExternalStore.bind( |
| 28 | null, |
| 29 | mocks.subscribe, |
| 30 | () => mocks.update, |
| 31 | ), |
| 32 | })); |
| 33 | // The pill only renders in the desktop app. |
| 34 | vi.mock("../../env", async (importOriginal) => ({ |
| 35 | ...(await importOriginal<typeof import("../../env")>()), |
| 36 | isElectron: true, |
| 37 | })); |
| 38 | vi.mock("./useUpdateEverything", () => ({ |
| 39 | useUpdateEverything: () => ({ |
| 40 | hasUpdates: mocks.hasUpdates, |
| 41 | summary: "Update everything", |
| 42 | version: null, |
| 43 | providerCount: 0, |
| 44 | rows: mocks.rows, |
| 45 | restarts: [], |
| 46 | }), |
| 47 | })); |
| 48 | |
| 49 | function nightlyUpdate(releaseNotes: DesktopUpdateState["releaseNotes"]): DesktopUpdateState { |
| 50 | return { |
| 51 | enabled: true, |
| 52 | status: "available", |
| 53 | channel: "nightly", |
| 54 | currentVersion: "0.0.50-nightly.1", |
| 55 | hostArch: "arm64", |
| 56 | appArch: "arm64", |
| 57 | runningUnderArm64Translation: false, |
| 58 | availableVersion: "0.0.50-nightly.2", |
| 59 | downloadedVersion: null, |
| 60 | releaseNotes, |
| 61 | omittedReleaseCount: 0, |
| 62 | downloadPercent: null, |
| 63 | checkedAt: null, |
| 64 | message: null, |
| 65 | errorContext: null, |
| 66 | canRetry: false, |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | const RELEASE_NOTES: DesktopUpdateState["releaseNotes"] = [ |
| 71 | { version: "0.0.50-nightly.2", items: ["Faster sidebar."], totalItems: 1 }, |
| 72 | ]; |
| 73 | |
| 74 | function setup( |
| 75 | update: DesktopUpdateState | null, |
| 76 | { |
| 77 | hasUpdates = false, |
| 78 | rows = [], |
| 79 | }: { hasUpdates?: boolean; rows?: ReadonlyArray<UpdateEverythingRow> } = {}, |
| 80 | ) { |
| 81 | mocks.update = update; |
| 82 | mocks.hasUpdates = hasUpdates; |
| 83 | mocks.rows = rows; |
| 84 | } |
| 85 | |
| 86 | function publish(update: DesktopUpdateState | null) { |
| 87 | return act(() => { |
| 88 | mocks.update = update; |
| 89 | mocks.notify(); |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | const releaseNotes = () => document.querySelector('[aria-label="Update release notes"]'); |
| 94 | |
| 95 | describe("SidebarUpdatePill release notes", () => { |
| 96 | it("closes when the release notes stop applying", async () => { |
| 97 | setup(nightlyUpdate(RELEASE_NOTES)); |
| 98 | await using _screen = await render(<SidebarUpdatePill />); |
| 99 | await userEvent.tab(); |
| 100 | await vi.waitFor(() => expect(releaseNotes()).not.toBeNull()); |
| 101 | |
| 102 | await publish(nightlyUpdate([])); |
| 103 | await vi.waitFor(() => expect(releaseNotes()).toBeNull()); |
| 104 | }); |
| 105 | |
| 106 | it("opens when release notes arrive while the trigger has keyboard focus", async () => { |
| 107 | setup(nightlyUpdate([])); |
| 108 | await using _screen = await render(<SidebarUpdatePill />); |
| 109 | await userEvent.tab(); |
| 110 | expect(releaseNotes()).toBeNull(); |
| 111 | |
| 112 | await publish( |
| 113 | nightlyUpdate([ |
| 114 | { |
| 115 | version: "0.0.50-nightly.2", |
| 116 | items: ["**One-time tasks.** Pick a date."], |
| 117 | totalItems: 1, |
| 118 | stats: "3 changes from 2 contributors", |
| 119 | footer: "Plus 2 more bug fixes.", |
| 120 | }, |
| 121 | ]), |
| 122 | ); |
| 123 | await expect.element(page.getByText("One-time tasks.", { exact: true })).toBeVisible(); |
| 124 | await expect.element(page.getByText("Plus 2 more bug fixes.")).toBeVisible(); |
| 125 | }); |
| 126 | }); |
| 127 | |
| 128 | describe("SidebarUpdatePill update everything", () => { |
| 129 | it("does not refocus the update button after a pointer cancel", async () => { |
| 130 | setup(null, { hasUpdates: true }); |
| 131 | await using screen = await render(<SidebarUpdatePill />); |
| 132 | const button = screen.getByRole("button", { name: "Update everything" }); |
| 133 | await userEvent.click(button); |
| 134 | const cancel = page.getByRole("button", { name: "Cancel" }); |
| 135 | await expect.element(cancel).toBeVisible(); |
| 136 | |
| 137 | await userEvent.click(cancel); |
| 138 | await vi.waitFor(() => expect(document.querySelector('[role="alertdialog"]')).toBeNull()); |
| 139 | expect(document.activeElement).not.toBe(button.element()); |
| 140 | }); |
| 141 | |
| 142 | it("returns focus to the update button after Escape", async () => { |
| 143 | setup(null, { hasUpdates: true }); |
| 144 | await using screen = await render(<SidebarUpdatePill />); |
| 145 | const button = screen.getByRole("button", { name: "Update everything" }); |
| 146 | await userEvent.click(button); |
| 147 | await expect.element(page.getByRole("button", { name: "Cancel" })).toBeVisible(); |
| 148 | |
| 149 | await userEvent.keyboard("{Escape}"); |
| 150 | await vi.waitFor(() => expect(document.activeElement).toBe(button.element())); |
| 151 | }); |
| 152 | |
| 153 | it("exposes the machines as a table with each value in its column", async () => { |
| 154 | const label = "DESKTOP-WORKSTATION-UPSTAIRS"; |
| 155 | setup(null, { |
| 156 | hasUpdates: true, |
| 157 | rows: [{ environmentId: null, label, app: "this-app", skipped: null, providers: ["Claude"] }], |
| 158 | }); |
| 159 | await using screen = await render(<SidebarUpdatePill />); |
| 160 | await userEvent.click(screen.getByRole("button", { name: "Update everything" })); |
| 161 | |
| 162 | const table = page.getByRole("table"); |
| 163 | await expect.element(table.getByRole("columnheader", { name: "Machine" })).toBeVisible(); |
| 164 | await expect.element(table.getByRole("columnheader", { name: "T3 Code" })).toBeVisible(); |
| 165 | await expect.element(table.getByRole("columnheader", { name: "Providers" })).toBeVisible(); |
| 166 | const row = table.getByRole("row", { name: new RegExp(label) }); |
| 167 | await expect.element(row.getByRole("cell", { name: label, exact: true })).toBeVisible(); |
| 168 | await expect.element(row.getByRole("cell", { name: "Claude", exact: true })).toBeVisible(); |
| 169 | }); |
| 170 | }); |