Files
kandev/apps/web/hooks/domains/plugins/use-auto-update-settings.test.ts
T
João Salavisa 5db3e48262 feat: add opt-in plugin auto-updater (#1903)
* feat: add opt-in plugin auto-updater

Automatically update installed plugins in the background, off by default
and opt-in. Builds on the existing verified install pipeline — an
auto-update is just an automatic reinstall of a newer catalog package_url.

Two-tier toggle on Settings > Plugins: an instance-wide default (SQLite
plugin_settings) plus a per-plugin override (tri-state auto_update on the
plugin record, carried forward on upgrade). The AutoUpdatePoller
(~90-min cadence + a boot sweep) upgrades only plugins that are currently
active, opted in, and report update_available; disabled/errored plugins
stay on their installed version.

New endpoints: GET/PUT /api/plugins/settings and
PUT /api/plugins/:id/auto-update.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(plugins): recheck auto-update eligibility at install and fix test mock

Address PR #1903 review feedback:
- Re-check each plugin's current status + effective opt-in immediately before
  InstallFromURL (eligibleForAutoUpdate), closing the TOCTOU window between the
  candidate snapshot / catalog fetch and the install so a plugin disabled or
  opted out mid-sweep is never reinstalled and reactivated.
- Correct the marketplace spec to describe the real failure behavior (shared
  with manual updates) instead of promising a version rollback the install
  pipeline does not perform.
- Fix the frontend CI failure: the plugins page test mocked the whole
  plugins-api module without the new settings/auto-update functions, so
  useAutoUpdateSettings threw at mount.
- Add an aria-label to the per-plugin auto-update Switch and Reset control.
- Add a use-auto-update-settings hook test (load, failure, optimistic setDefault,
  rollback) and an eligibleForAutoUpdate unit test.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-24 13:04:54 +01:00

84 lines
3.0 KiB
TypeScript

import { act, renderHook, waitFor } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { toast } from "sonner";
vi.mock("sonner", () => ({ toast: { error: vi.fn() } }));
const { getPluginSettingsSpy, updatePluginSettingsSpy } = vi.hoisted(() => ({
getPluginSettingsSpy: vi.fn(),
updatePluginSettingsSpy: vi.fn(),
}));
vi.mock("@/lib/api/domains/plugins-api", () => ({
getPluginSettings: (...args: unknown[]) => getPluginSettingsSpy(...args),
updatePluginSettings: (...args: unknown[]) => updatePluginSettingsSpy(...args),
}));
import { useAutoUpdateSettings } from "./use-auto-update-settings";
beforeEach(() => {
getPluginSettingsSpy.mockReset();
updatePluginSettingsSpy.mockReset();
(toast.error as ReturnType<typeof vi.fn>).mockReset();
});
afterEach(() => {
vi.clearAllMocks();
});
describe("useAutoUpdateSettings", () => {
it("loads the instance-wide default on mount", async () => {
getPluginSettingsSpy.mockResolvedValue({ auto_update_default: true });
const { result } = renderHook(() => useAutoUpdateSettings());
// Starts off, not yet loaded.
expect(result.current.autoUpdateDefault).toBe(false);
expect(result.current.loaded).toBe(false);
await waitFor(() => expect(result.current.loaded).toBe(true));
expect(result.current.autoUpdateDefault).toBe(true);
});
it("marks loaded (default off) when the initial fetch fails, without throwing", async () => {
getPluginSettingsSpy.mockRejectedValue(new Error("backend unreachable"));
const { result } = renderHook(() => useAutoUpdateSettings());
await waitFor(() => expect(result.current.loaded).toBe(true));
expect(result.current.autoUpdateDefault).toBe(false);
expect(toast.error).not.toHaveBeenCalled();
});
it("optimistically applies setDefault and reconciles with the persisted value", async () => {
getPluginSettingsSpy.mockResolvedValue({ auto_update_default: false });
updatePluginSettingsSpy.mockResolvedValue({ auto_update_default: true });
const { result } = renderHook(() => useAutoUpdateSettings());
await waitFor(() => expect(result.current.loaded).toBe(true));
await act(async () => {
await result.current.setDefault(true);
});
expect(updatePluginSettingsSpy).toHaveBeenCalledWith(true);
expect(result.current.autoUpdateDefault).toBe(true);
expect(toast.error).not.toHaveBeenCalled();
});
it("rolls back the optimistic update and toasts when the write fails", async () => {
getPluginSettingsSpy.mockResolvedValue({ auto_update_default: false });
updatePluginSettingsSpy.mockRejectedValue(new Error("write failed"));
const { result } = renderHook(() => useAutoUpdateSettings());
await waitFor(() => expect(result.current.loaded).toBe(true));
await act(async () => {
await result.current.setDefault(true);
});
expect(result.current.autoUpdateDefault).toBe(false); // rolled back
expect(toast.error).toHaveBeenCalledWith("write failed");
});
});