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.

2.8 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import { apiErrorFromResponse } from '@/utils/apiClient';

describe('apiClient structured error extraction', () => {
  it('prefers structured JSON error messages over fallback copy', async () => {
    const error = await apiErrorFromResponse(
      new Response(JSON.stringify({ error: 'Canonical API error' }), { status: 400 }),
      'Fallback message',
    );

    expect(error.message).toBe('Canonical API error');
    expect(error.status).toBe(400);
  });

  // A caller-supplied fallback outranks any non-JSON body. The body on that
  // path is unattributed text from whatever answered the request — often a
  // proxy rather than Pulse — while the caller knows what it was asking for
  // (#1640). Canonical JSON still outranks both.
  it('prefers a short plain-text body over caller fallback copy', async () => {
    const error = await apiErrorFromResponse(
      new Response('temporary failure', { status: 500 }),
      'Fallback message',
    );

    expect(error.message).toBe('temporary failure');
    expect(error.status).toBe(500);
  });

  it('falls back to plain text when the response is not JSON', async () => {
    const error = await apiErrorFromResponse(new Response('temporary failure', { status: 500 }));

    expect(error.message).toBe('temporary failure');
    expect(error.status).toBe(500);
  });

  it('preserves structured code and details from canonical API errors', async () => {
    const error = await apiErrorFromResponse(
      new Response(
        JSON.stringify({
          error: 'Failed to connect to VMware vCenter',
          code: 'vmware_connection_failed',
          details: {
            category: 'unsupported_version',
            error: 'VMware vCenter 6.7 is below the supported VI JSON release floor',
          },
        }),
        {
          status: 400,
          headers: { 'Content-Type': 'application/json' },
        },
      ),
      'Fallback message',
    );

    expect(error).toMatchObject({
      message: 'Failed to connect to VMware vCenter',
      status: 400,
      code: 'vmware_connection_failed',
      details: {
        category: 'unsupported_version',
        error: 'VMware vCenter 6.7 is below the supported VI JSON release floor',
      },
    });
  });

  it('preserves generic feature metadata from structured commercial errors', async () => {
    const error = await apiErrorFromResponse(
      new Response(
        JSON.stringify({
          error: 'license_required',
          message: 'Relay access requires a paid feature',
          feature: 'relay',
        }),
        {
          status: 402,
          headers: { 'Content-Type': 'application/json' },
        },
      ),
      'Fallback message',
    );

    expect(error).toMatchObject({
      message: 'Relay access requires a paid feature',
      status: 402,
      feature: 'relay',
    });
  });
});