Open History to see the last commit
1import { isValidElement, type ReactElement, type ReactNode } from "react";
2import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
3import type { DesktopUpdateState } from "@t3tools/contracts";
4
5const testState = vi.hoisted(() => ({
6 addToast: vi.fn(),
7}));
8
9vi.mock("./ui/toast", () => ({
10 toastManager: { add: testState.addToast },
11}));
12
13import { showDesktopUpdateDownloadedToast } from "./desktopUpdate.toast";
14
15type ClickableElement = ReactElement<{ readonly onClick?: () => void }>;
16
17function findReleaseNotesLink(node: ReactNode): ClickableElement | null {
18 if (Array.isArray(node)) {
19 for (const child of node) {
20 const found = findReleaseNotesLink(child);
21 if (found) return found;
22 }
23 return null;
24 }
25 if (!isValidElement(node)) return null;
26 const element = node as ReactElement<{ readonly children?: ReactNode }>;
27 if (element.type === "button") return element as ClickableElement;
28 if (typeof element.type === "function") {
29 const render = element.type as (props: unknown) => ReactNode;
30 return findReleaseNotesLink(render(element.props));
31 }
32 return findReleaseNotesLink(element.props.children);
33}
34
35function getDescription(): ReactNode {
36 const toast = testState.addToast.mock.calls[0]?.[0] as { description?: ReactNode } | undefined;
37 return toast?.description ?? null;
38}
39
40function downloadedState(overrides: Partial<DesktopUpdateState> = {}): DesktopUpdateState {
41 return {
42 enabled: true,
43 status: "downloaded",
44 channel: "latest",
45 currentVersion: "0.0.29",
46 hostArch: "arm64",
47 appArch: "arm64",
48 runningUnderArm64Translation: false,
49 availableVersion: "0.0.30",
50 downloadedVersion: "0.0.30",
51 releaseNotes: [],
52 omittedReleaseCount: 0,
53 downloadPercent: 100,
54 checkedAt: null,
55 message: null,
56 errorContext: null,
57 canRetry: true,
58 ...overrides,
59 };
60}
61
62describe("showDesktopUpdateDownloadedToast", () => {
63 beforeEach(() => {
64 testState.addToast.mockReset();
65 });
66
67 afterEach(() => {
68 vi.unstubAllEnvs();
69 });
70
71 it("opens release notes from the configured repository", async () => {
72 vi.stubEnv("T3CODE_DESKTOP_UPDATE_REPOSITORY", "JustYannicc/t3code");
73 const openExternal = vi.fn().mockResolvedValue(true);
74
75 showDesktopUpdateDownloadedToast({ openExternal }, downloadedState());
76 findReleaseNotesLink(getDescription())?.props.onClick?.();
77
78 await vi.waitFor(() => {
79 expect(openExternal).toHaveBeenCalledWith(
80 "https://github.com/JustYannicc/t3code/releases/tag/v0.0.30",
81 );
82 });
83 });
84
85 it("opens the downloaded version's release notes", async () => {
86 const openExternal = vi.fn().mockResolvedValue(true);
87
88 showDesktopUpdateDownloadedToast({ openExternal }, downloadedState());
89 const link = findReleaseNotesLink(getDescription());
90 link?.props.onClick?.();
91 await vi.waitFor(() => {
92 expect(openExternal).toHaveBeenCalledWith(
93 "https://github.com/pingdotgg/t3code/releases/tag/v0.0.30",
94 );
95 });
96 expect(testState.addToast).toHaveBeenCalledTimes(1);
97 });
98
99 it("falls back to the version the download was started for", async () => {
100 const openExternal = vi.fn().mockResolvedValue(true);
101
102 // The `update-downloaded` event can land after the download RPC resolves.
103 showDesktopUpdateDownloadedToast(
104 { openExternal },
105 downloadedState({ downloadedVersion: null }),
106 );
107 findReleaseNotesLink(getDescription())?.props.onClick?.();
108
109 await vi.waitFor(() => {
110 expect(openExternal).toHaveBeenCalledWith(
111 "https://github.com/pingdotgg/t3code/releases/tag/v0.0.30",
112 );
113 });
114 });
115
116 it("omits the link when the updater reports no version at all", () => {
117 showDesktopUpdateDownloadedToast(
118 { openExternal: vi.fn() },
119 downloadedState({ availableVersion: null, downloadedVersion: null }),
120 );
121
122 expect(findReleaseNotesLink(getDescription())).toBeNull();
123 });
124
125 it.each([
126 ["returns false", vi.fn().mockResolvedValue(false)],
127 ["rejects", vi.fn().mockRejectedValue(new Error("open failed"))],
128 ])("shows an error when opening release notes %s", async (_description, openExternal) => {
129 showDesktopUpdateDownloadedToast({ openExternal }, downloadedState());
130 findReleaseNotesLink(getDescription())?.props.onClick?.();
131
132 await vi.waitFor(() => {
133 expect(testState.addToast).toHaveBeenLastCalledWith({
134 type: "error",
135 title: "Unable to open release notes",
136 });
137 });
138 });
139});