Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

6.3 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

vi.mock('@/utils/apiClient', () => ({
  apiFetchJSON: vi.fn(),
}));

import { UpdatesAPI } from '@/api/updates';
import type { UpdateChannel } from '@/types/config';
import { apiFetchJSON } from '@/utils/apiClient';

describe('UpdatesAPI', () => {
  const apiFetchJSONMock = vi.mocked(apiFetchJSON);

  beforeEach(() => {
    apiFetchJSONMock.mockReset();
  });

  it('preserves the app version and agent update target from version payloads', async () => {
    const response = {
      version: '6.0.0-rc.6+git.174.g259476907.dirty',
      build: 'development',
      runtime: 'go',
      isDocker: false,
      isSourceBuild: false,
      isDevelopment: true,
      deploymentType: 'development',
      agentUpdateTargetVersion: '6.0.0-rc.6',
    };
    apiFetchJSONMock.mockResolvedValueOnce(response as any);

    await expect(UpdatesAPI.getVersion()).resolves.toEqual(response);
    expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/version');
  });

  it('fetches release notes for the running version', async () => {
    const response = {
      version: '6.1.0-rc.1',
      releaseNotes: '## Highlights\n- Reviewed actions',
      releaseDate: '2026-07-13T12:00:00Z',
      isPrerelease: true,
    };
    apiFetchJSONMock.mockResolvedValueOnce(response as any);

    await expect(UpdatesAPI.getReleaseNotes()).resolves.toEqual(response);
    expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/updates/release-notes');
  });

  it('encodes optional update-check channel safely', async () => {
    apiFetchJSONMock.mockResolvedValueOnce({ available: false } as any);
    await UpdatesAPI.checkForUpdates('rc');

    expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/updates/check?channel=rc');
  });

  it('passes explicit freshness without overriding the saved channel', async () => {
    await UpdatesAPI.checkForUpdates(undefined, true);
    expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/updates/check?force=true');
  });

  it('omits blank channel for update checks', async () => {
    apiFetchJSONMock.mockResolvedValueOnce({ available: false } as any);
    await UpdatesAPI.checkForUpdates('   ' as UpdateChannel);

    expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/updates/check');
  });

  it('rejects empty apply-update download URL before making a request', async () => {
    await expect(UpdatesAPI.applyUpdate('   ')).rejects.toThrow('Download URL is required');
    expect(apiFetchJSONMock).not.toHaveBeenCalled();
  });

  it('trims download URL before apply-update request', async () => {
    apiFetchJSONMock.mockResolvedValueOnce({ status: 'started', message: 'ok' } as any);
    await UpdatesAPI.applyUpdate('  https://example.com/pulse.tar.gz  ');

    expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/updates/apply', {
      method: 'POST',
      body: JSON.stringify({ downloadUrl: 'https://example.com/pulse.tar.gz' }),
    });
  });

  it('rejects empty rollback event ID before making a request', async () => {
    await expect(UpdatesAPI.rollbackUpdate('   ')).rejects.toThrow('Event ID is required');
    expect(apiFetchJSONMock).not.toHaveBeenCalled();
  });

  it('trims event ID before rollback request', async () => {
    apiFetchJSONMock.mockResolvedValueOnce({ status: 'started', message: 'ok' } as any);
    await UpdatesAPI.rollbackUpdate('  01JZEXAMPLE  ');

    expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/updates/rollback', {
      method: 'POST',
      body: JSON.stringify({ eventId: '01JZEXAMPLE' }),
    });
  });

  it('encodes the update-history limit', async () => {
    apiFetchJSONMock.mockResolvedValueOnce([] as any);
    await UpdatesAPI.listUpdateHistory(5);

    expect(apiFetchJSONMock).toHaveBeenCalledWith('/api/updates/history?limit=5');
  });

  it('encodes update-plan version and channel safely', async () => {
    apiFetchJSONMock.mockResolvedValueOnce({ canAutoUpdate: true } as any);
    await UpdatesAPI.getUpdatePlan('v1.2.3-rc.1+build', 'rc');

    expect(apiFetchJSONMock).toHaveBeenCalledWith(
      '/api/updates/plan?version=v1.2.3-rc.1%2Bbuild&channel=rc',
    );
  });

  it('preserves update-plan readiness payloads from the backend', async () => {
    const response = {
      canAutoUpdate: true,
      requiresRoot: true,
      rollbackSupport: true,
      readiness: {
        status: 'blocked',
        summary: 'Resolve 1 blocked upgrade check before installing this update.',
        checks: [
          {
            id: 'agent-token-scopes',
            status: 'blocked',
            title: 'Agent token scopes',
            summary:
              'Registered agents exist, but no loaded API token grants agent reporting scope.',
          },
        ],
      },
    };
    apiFetchJSONMock.mockResolvedValueOnce(response as any);

    await expect(UpdatesAPI.getUpdatePlan('v6.0.0')).resolves.toEqual(response);
  });

  it('rejects blank update-plan version before making a request', async () => {
    await expect(UpdatesAPI.getUpdatePlan('   ', 'stable')).rejects.toThrow('Version is required');
    expect(apiFetchJSONMock).not.toHaveBeenCalled();
  });

  it('preserves digest-pinned Pro docker update commands from update checks', async () => {
    // Docker deployments of the Pro runtime cannot self-update; the check
    // response relays the license server broker's digest-pinned image
    // commands, and the transport must carry them through untouched.
    const digest = 'sha256:' + 'ab'.repeat(32);
    const pinnedRef = `registry.pulserelay.pro/pulse/pulse-pro@${digest}`;
    const response = {
      available: true,
      currentVersion: '6.0.0',
      latestVersion: '6.0.5',
      releaseNotes: '',
      releaseDate: '',
      downloadUrl: 'https://license.example.invalid/v1/downloads/pulse-pro?version=v6.0.5',
      isPrerelease: false,
      isMajorUpgrade: false,
      dockerUpdate: {
        version: 'v6.0.5',
        image: 'registry.pulserelay.pro/pulse/pulse-pro',
        imageDigest: digest,
        loginCommand:
          "printf '%s' '<activation-key>' | docker login registry.pulserelay.pro -u 'lic_test' --password-stdin",
        composePullCommand: `PULSE_IMAGE='${pinnedRef}' docker compose pull`,
        composeUpCommand: `PULSE_IMAGE='${pinnedRef}' docker compose up -d`,
      },
    };
    apiFetchJSONMock.mockResolvedValueOnce(response as any);

    await expect(UpdatesAPI.checkForUpdates()).resolves.toEqual(response);
  });
});