Skip to content

Open-source, local-first AI agent for real work.

2.7 KB
Raw Blame
/**
 * @vitest-environment jsdom
 */
import settings from "@/shared/i18n/locales/en/settings.json";
import { DEFAULT_NOTIFICATION_PREFERENCES } from "@/shared/notification-preferences";
import { GeneralSettings } from "@domains/settings/components/GeneralSettings";
import { updaterCurrentNotesDialogOpenAtom, updaterStateAtom } from "@shared/store/updater-atoms";
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { createInstance } from "i18next";
import { createStore, Provider } from "jotai";
import { I18nextProvider } from "react-i18next";
import { afterEach, describe, expect, it, vi } from "vitest";
import { UpdateCheckerAction } from "./UpdateChecker";

const labels = {
	check: "检查更新",
	checking: "正在检查更新",
	checkingBtn: "检查中...",
	currentVersion: (v: string) => v,
	download: "下载",
	downloading: (p: number) => `下载中 ${p}%`,
	idle: (v: string) => v,
	newVersion: (v: string) => v,
	restart: "重启",
};

describe("UpdateCheckerAction", () => {
	afterEach(() => {
		cleanup();
		vi.unstubAllGlobals();
	});

	it("下载中按钮禁用并显示百分比,不能再次检查", () => {
		const onCheck = vi.fn();
		render(<UpdateCheckerAction checking={false} labels={labels} onCheck={onCheck} phase="downloading" progress={0.42} />);
		const button = screen.getByTestId("updater-check") as HTMLButtonElement;
		expect(button.disabled).toBe(true);
		expect(button.textContent).toContain("下载中 42%");
	});

	it("shows the install channel in settings and keeps the current release notes accessible", async () => {
		const i18n = createInstance();
		await i18n.init({ lng: "en", resources: { en: { settings } } });
		const store = createStore();
		store.set(updaterStateAtom, { currentVersion: "1.0.0", phase: "idle", externallyManaged: true });
		vi.stubGlobal("vetta", {
			config: { get: async () => ({ notificationPreferences: DEFAULT_NOTIFICATION_PREFERENCES }) },
			models: { get: async () => ({ providers: {} }) },
		});
		render(
			<Provider store={store}>
				<I18nextProvider i18n={i18n}>
					<GeneralSettings />
				</I18nextProvider>
			</Provider>,
		);
		expect(screen.getByText(/same channel you installed it from/).textContent).toContain("1.0.0");
		expect(screen.queryByRole("button", { name: settings.updaterCheck })).toBeNull();
		fireEvent.click(screen.getByRole("button", { name: settings.updaterViewCurrent }));
		expect(store.get(updaterCurrentNotesDialogOpenAtom)).toBe(true);
	});

	it("空闲时可点击检查", () => {
		render(<UpdateCheckerAction checking={false} labels={labels} onCheck={() => {}} phase="idle" />);
		const button = screen.getByTestId("updater-check") as HTMLButtonElement;
		expect(button.disabled).toBe(false);
		expect(button.textContent).toContain("检查更新");
	});
});