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.

28.8 KB
Raw Blame
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { NodesAPI } from '../nodes';
import { apiFetch, apiFetchJSON } from '@/utils/apiClient';
import type { NodeConfig, PVENodeConfig } from '@/types/nodes';

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

describe('NodesAPI', () => {
  const makePveNode = (
    overrides: Partial<PVENodeConfig & { type: 'pve' }> = {},
  ): PVENodeConfig & { type: 'pve' } => ({
    id: 'pve-1',
    type: 'pve',
    name: 'PVE 1',
    host: 'https://pve.local:8006',
    user: 'root@pam',
    verifySSL: true,
    monitorVMs: true,
    monitorContainers: true,
    monitorStorage: true,
    monitorBackups: true,
    monitorPhysicalDisks: false,
    ...overrides,
  });

  beforeEach(() => {
    vi.clearAllMocks();
  });

  describe('createHostAgentInstallToken', () => {
    it.each([false, true])(
      'submits one non-retrying host mint with command access %s',
      async (enableCommands) => {
        const record = {
          id: 'install-1',
          name: 'Host install',
          prefix: 'synthetic',
          suffix: 'only',
          createdAt: '2026-11-02',
        };
        vi.mocked(apiFetchJSON).mockResolvedValueOnce({ token: 'synthetic-install-token', record });

        const result = await NodesAPI.createHostAgentInstallToken({
          enableCommands,
          name: 'Host install',
        });

        expect(apiFetchJSON).toHaveBeenCalledExactlyOnceWith('/api/agent-install-command', {
          method: 'POST',
          retry: false,
          body: JSON.stringify({ type: 'host', enableCommands, name: 'Host install' }),
        });
        expect(result).toEqual({ token: 'synthetic-install-token', record });
      },
    );
  });

  describe('getNodes', () => {
    it('fetches all nodes', async () => {
      const mockNodes: NodeConfig[] = [{ id: 'node1', name: 'Node 1' } as NodeConfig];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockNodes);

      const result = await NodesAPI.getNodes();

      expect(apiFetchJSON).toHaveBeenCalledWith('/api/config/nodes');
      expect(result).toEqual(mockNodes);
    });

    it('normalizes canonical cluster endpoint fields', async () => {
      const mockNodes = [
        makePveNode({
          clusterEndpoints: [
            {
              nodeId: ' node-1 ',
              nodeIdentity: ' production-pve-1 ',
              nativeNodeId: 7,
              nodeName: ' pve-1 ',
              nativeName: ' pve-1 ',
              nativeAliases: [' pve-old ', ''],
              displayName: ' Render East ',
              host: ' https://pve-1.local:8006 ',
              guestURL: ' https://guest.local ',
              ip: ' 10.0.0.1 ',
              ipOverride: ' 10.0.0.10 ',
              fingerprint: ' fp ',
              tlsEndpoint: ' https://pve-1.local:8006 ',
              usesPrimaryTLS: true,
              tlsFirstUse: false,
              online: true,
              lastSeen: ' 2026-03-04T00:00:00Z ',
              pulseReachable: null,
              lastPulseCheck: ' 2026-03-04T00:00:01Z ',
              pulseError: ' timeout ',
            },
          ],
        }),
      ];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockNodes);

      const result = await NodesAPI.getNodes();
      const endpoint = (
        result[0] as NodeConfig & { clusterEndpoints: Array<Record<string, unknown>> }
      ).clusterEndpoints[0];

      expect(endpoint).toMatchObject({
        nodeId: 'node-1',
        nodeIdentity: 'production-pve-1',
        nativeNodeId: 7,
        nodeName: 'pve-1',
        nativeName: 'pve-1',
        nativeAliases: ['pve-old'],
        displayName: 'Render East',
        host: 'https://pve-1.local:8006',
        guestURL: 'https://guest.local',
        ip: '10.0.0.1',
        ipOverride: '10.0.0.10',
        fingerprint: ' fp ',
        tlsEndpoint: 'https://pve-1.local:8006',
        usesPrimaryTLS: true,
        tlsFirstUse: false,
        online: true,
        lastSeen: '2026-03-04T00:00:00Z',
        lastPulseCheck: '2026-03-04T00:00:01Z',
        pulseError: 'timeout',
      });
      expect(endpoint.pulseReachable).toBeUndefined();
    });

    it('uses canonical scalar coercion for non-string and invalid cluster endpoint fields', async () => {
      const mockNodes = [
        makePveNode({
          id: 'pve-2',
          name: 'PVE 2',
          clusterEndpoints: [
            {
              nodeId: 22,
              nodeName: null,
              host: ' https://pve-2.local:8006 ',
              guestURL: '   ',
              ip: 10,
              online: 'true',
              lastSeen: undefined,
              pulseReachable: false,
            },
          ] as unknown as PVENodeConfig['clusterEndpoints'],
        }),
      ];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockNodes);

      const result = await NodesAPI.getNodes();
      const endpoint = (
        result[0] as NodeConfig & { clusterEndpoints: Array<Record<string, unknown>> }
      ).clusterEndpoints[0];

      expect(endpoint).toMatchObject({
        nodeId: '22',
        nodeName: '',
        host: 'https://pve-2.local:8006',
        guestURL: undefined,
        ip: '10',
        online: false,
        lastSeen: '',
        pulseReachable: false,
      });
    });

    it('preserves nodes without array clusterEndpoints', async () => {
      const mockNodes = [
        {
          id: 'pve-3',
          type: 'pve',
          name: 'PVE 3',
          clusterEndpoints: 'not-an-array',
        } as unknown as NodeConfig,
      ];
      vi.mocked(apiFetchJSON).mockResolvedValueOnce(mockNodes);

      const result = await NodesAPI.getNodes();

      expect(result).toEqual(mockNodes);
    });
  });

  describe('addNode', () => {
    it('adds a new node', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ success: true });

      const node = { id: 'node1', name: 'Node 1' } as NodeConfig;
      await NodesAPI.addNode(node);

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/config/nodes',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify(node),
        }),
      );
    });
  });

  describe('updateNode', () => {
    it('updates a node', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ success: true });

      const node = { id: 'node1', name: 'Updated Node' } as NodeConfig;
      await NodesAPI.updateNode('node1', node);

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/config/nodes/node1',
        expect.objectContaining({
          method: 'PUT',
          body: JSON.stringify(node),
        }),
      );
    });

    it('encodes special characters in node id', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ success: true });

      await NodesAPI.updateNode('node/1', {} as NodeConfig);

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/config/nodes/node%2F1',
        expect.objectContaining({ method: 'PUT' }),
      );
    });

    it('sends per-member cluster endpoint overrides in the PUT payload', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ success: true });

      const node = makePveNode({
        clusterEndpointOverrides: [
          { nodeName: 'pve2', ipOverride: '10.0.0.2' },
          { nodeName: 'pve3', ipOverride: '' },
        ],
      });
      await NodesAPI.updateNode('pve-1', node as NodeConfig);

      const body = JSON.parse(vi.mocked(apiFetchJSON).mock.calls[0][1]!.body as string);
      expect(body.clusterEndpointOverrides).toEqual([
        { nodeName: 'pve2', ipOverride: '10.0.0.2' },
        { nodeName: 'pve3', ipOverride: '' },
      ]);
    });

    it('sends presentation-only cluster node display names by immutable identity', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ success: true });
      await NodesAPI.updateNode(
        'pve-1',
        makePveNode({
          clusterNodeDisplayNameOverrides: [
            { nodeIdentity: 'production-pve2', displayName: 'Render East' },
            { nodeIdentity: 'production-pve3', displayName: '' },
          ],
        }) as NodeConfig,
      );

      const body = JSON.parse(vi.mocked(apiFetchJSON).mock.calls[0][1]!.body as string);
      expect(body.clusterNodeDisplayNameOverrides).toEqual([
        { nodeIdentity: 'production-pve2', displayName: 'Render East' },
        { nodeIdentity: 'production-pve3', displayName: '' },
      ]);
    });
  });

  describe('deleteNode', () => {
    it('deletes a node', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ success: true });

      await NodesAPI.deleteNode('node1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/config/nodes/node1',
        expect.objectContaining({ method: 'DELETE' }),
      );
    });
  });

  describe('testConnection', () => {
    it('tests connection to a node', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ status: 'ok' });

      const node = { id: 'node1', name: 'Node 1' } as NodeConfig;
      await NodesAPI.testConnection(node);

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/config/nodes/test-connection',
        expect.objectContaining({ method: 'POST' }),
      );
    });
  });

  describe('testExistingNode', () => {
    it('tests connection to existing node', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ status: 'ok', latency: 10 });

      const result = await NodesAPI.testExistingNode('node1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/config/nodes/node1/test',
        expect.objectContaining({ method: 'POST' }),
      );
      expect(result.latency).toBe(10);
    });

    it('sends edited TLS settings, including false and an empty fingerprint', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ status: 'success' });

      await NodesAPI.testExistingNode('pve:saved cluster', { verifySSL: false, fingerprint: '' });

      expect(apiFetchJSON).toHaveBeenCalledWith('/api/config/nodes/pve%3Asaved%20cluster/test', {
        method: 'POST',
        body: JSON.stringify({ verifySSL: false, fingerprint: '' }),
      });
    });
  });

  it('tests one existing member with an explicit empty pin and no credential transfer', async () => {
    vi.mocked(apiFetchJSON).mockResolvedValueOnce({ status: 'success' });
    await NodesAPI.testExistingNode('pve-0', {
      endpoint: 'https://peer.test:8006',
      verifySSL: true,
      fingerprint: '',
    });
    expect(apiFetchJSON).toHaveBeenCalledWith('/api/config/nodes/pve-0/test', {
      method: 'POST',
      body: JSON.stringify({
        endpoint: 'https://peer.test:8006',
        verifySSL: true,
        fingerprint: '',
      }),
    });
  });

  it('does not normalise malformed nonempty member trust into CA mode', async () => {
    vi.mocked(apiFetchJSON).mockResolvedValueOnce([
      makePveNode({
        clusterEndpoints: [
          {
            nodeId: 'peer',
            nodeName: 'peer',
            host: 'https://peer.test:8006',
            ip: '',
            online: true,
            lastSeen: '',
            fingerprint: '   ',
          },
        ],
      }),
    ]);
    const [node] = await NodesAPI.getNodes();
    expect('clusterEndpoints' in node && node.clusterEndpoints?.[0].fingerprint).toBe('   ');
  });

  describe('refreshClusterNodes', () => {
    it('refreshes cluster nodes', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ status: 'ok', newNodeCount: 3 });

      const result = await NodesAPI.refreshClusterNodes('node1');

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/config/nodes/node1/refresh-cluster',
        expect.objectContaining({ method: 'POST' }),
      );
      expect(result.newNodeCount).toBe(3);
    });
  });

  describe('getAgentInstallCommand', () => {
    it('gets agent install command', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        command: ' curl ... ',
        token: 'secret-token',
      });

      const result = await NodesAPI.getAgentInstallCommand({
        type: 'pve',
        enableProxmox: true,
        enableCommands: true,
        insecure: true,
      });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/agent-install-command',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify({
            type: 'pve',
            enableProxmox: true,
            enableCommands: true,
            insecure: true,
          }),
        }),
      );
      expect(result).toEqual({ command: 'curl ...', token: 'secret-token' });
    });

    it('supports the PBS proxmox install command contract', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ command: 'curl pbs ...' });

      const result = await NodesAPI.getAgentInstallCommand({ type: 'pbs', enableProxmox: true });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/agent-install-command',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify({ type: 'pbs', enableProxmox: true }),
        }),
      );
      expect(result.command).toBe('curl pbs ...');
    });

    it('rejects an agent credential embedded in the copied command', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        command: 'bash install.sh --token synthetic-secret',
        token: 'synthetic-secret',
      });
      await expect(
        NodesAPI.getAgentInstallCommand({ type: 'pve', enableProxmox: true }),
      ).rejects.toThrow('Install credentials must be entered separately from the command');
    });

    it('rejects blank install commands', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({ command: '   ', token: 'secret-token' });

      await expect(
        NodesAPI.getAgentInstallCommand({ type: 'pve', enableProxmox: true }),
      ).rejects.toThrow('Invalid agent install command response');
    });
  });

  describe('getProxmoxSetupCommand', () => {
    it('uses the canonical PVE setup contract with a separate revealed setup token', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        type: 'pve',
        host: 'https://pve.example:8006',
        url: 'https://pulse.example/api/setup-script?type=pve',
        downloadURL: 'https://pulse.example/api/setup-script?type=pve',
        scriptFileName: 'pulse-setup-pve.sh',
        command: 'curl pve ...',
        setupToken: 'setup-token-123',
        tokenHint: 'set…123',
        expires: 1_900_000_000,
      });

      const result = await NodesAPI.getProxmoxSetupCommand({
        type: 'pve',
        host: 'pve.example',
        backupPerms: true,
      });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/setup-script-url',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify({
            type: 'pve',
            host: 'pve.example',
            backupPerms: true,
          }),
        }),
      );
      expect(result.type).toBe('pve');
      expect(result.host).toBe('https://pve.example:8006');
      expect(result.downloadURL).toBe('https://pulse.example/api/setup-script?type=pve');
      expect(result.scriptFileName).toBe('pulse-setup-pve.sh');
      expect(result.tokenHint).toBe('set…123');
      expect(result.expires).toBe(1_900_000_000);
      expect(result.setupToken).toMatch(/token-123$/);
    });

    it.each(['url', 'downloadURL', 'command', 'commandWithEnv', 'commandWithoutEnv'])(
      'rejects a setup credential embedded in %s without returning it in the error',
      async (field) => {
        const secret = 'synthetic-setup-secret';
        vi.mocked(apiFetchJSON).mockResolvedValueOnce({
          type: 'pve',
          host: 'https://pve.example:8006',
          url: 'https://pulse.example/api/setup-script?type=pve',
          downloadURL: 'https://pulse.example/api/setup-script?type=pve',
          scriptFileName: 'pulse-setup-pve.sh',
          command: 'safe command',
          setupToken: secret,
          tokenHint: 'syn…ret',
          expires: 1_900_000_000,
          [field]: `unsafe-${secret}`,
        });
        await expect(
          NodesAPI.getProxmoxSetupCommand({ type: 'pve', host: 'pve.example', backupPerms: false }),
        ).rejects.toThrow('Setup credentials must be entered separately from commands and URLs');
      },
    );

    it('sends the typed connection name so the setup token can carry it into auto-registration', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        type: 'pve',
        host: 'https://pve.example:8006',
        url: 'https://pulse.example/api/setup-script?type=pve',
        downloadURL: 'https://pulse.example/api/setup-script?type=pve',
        scriptFileName: 'pulse-setup-pve.sh',
        command: 'curl pve ...',
        setupToken: 'setup-token-123',
        tokenHint: 'set…123',
        expires: 1_900_000_000,
      });

      await NodesAPI.getProxmoxSetupCommand({
        type: 'pve',
        host: 'pve.example',
        backupPerms: false,
        name: 'enacon',
      });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/setup-script-url',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify({
            type: 'pve',
            host: 'pve.example',
            backupPerms: false,
            name: 'enacon',
          }),
        }),
      );
    });

    it('normalizes the canonical setup-script-url response fields', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        type: ' pve ',
        host: ' https://pve.example:8006 ',
        url: ' https://pulse.example/api/setup-script?type=pve ',
        downloadURL: ' https://pulse.example/api/setup-script?type=pve ',
        scriptFileName: ' pulse-setup-pve.sh ',
        command: ' curl pve ... ',
        commandWithEnv: ' curl env pve ... ',
        commandWithoutEnv: ' curl bare pve ... ',
        setupToken: ' setup-token-123 ',
        expires: 1_900_000_000,
        tokenHint: ' set…123 ',
      });

      const result = await NodesAPI.getProxmoxSetupCommand({
        type: 'pve',
        host: 'pve.example',
        backupPerms: true,
      });

      expect(result).toEqual({
        type: 'pve',
        host: 'https://pve.example:8006',
        url: 'https://pulse.example/api/setup-script?type=pve',
        downloadURL: 'https://pulse.example/api/setup-script?type=pve',
        scriptFileName: 'pulse-setup-pve.sh',
        command: 'curl pve ...',
        commandWithEnv: 'curl env pve ...',
        commandWithoutEnv: 'curl bare pve ...',
        expires: 1_900_000_000,
        tokenHint: 'set…123',
        setupToken: 'setup-token-123',
      });
    });

    it('supports the PBS proxmox setup command contract', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        type: 'pbs',
        host: 'https://pbs.example:8007',
        url: 'https://pulse.example/api/setup-script?type=pbs',
        downloadURL: 'https://pulse.example/api/setup-script?type=pbs',
        scriptFileName: 'pulse-setup-pbs.sh',
        command: 'curl pbs ...',
        setupToken: 'pbs-token-123',
        tokenHint: 'pbs…123',
        expires: 1_900_000_000,
      });

      const result = await NodesAPI.getProxmoxSetupCommand({
        type: 'pbs',
        host: 'pbs.example',
        backupPerms: false,
      });

      expect(apiFetchJSON).toHaveBeenCalledWith(
        '/api/setup-script-url',
        expect.objectContaining({
          method: 'POST',
          body: JSON.stringify({
            type: 'pbs',
            host: 'pbs.example',
            backupPerms: false,
          }),
        }),
      );
      expect(result.command).toBe('curl pbs ...');
      expect(result.type).toBe('pbs');
      expect(result.host).toBe('https://pbs.example:8007');
      expect(result.downloadURL).toBe('https://pulse.example/api/setup-script?type=pbs');
      expect(result.scriptFileName).toBe('pulse-setup-pbs.sh');
      expect(result.tokenHint).toBe('pbs…123');
      expect(result.setupToken).toMatch(/token-123$/);
    });

    it('falls back to command for commandWithEnv when the backend omits it', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        type: 'pbs',
        host: 'https://pbs.example:8007',
        url: 'https://pulse.example/api/setup-script?type=pbs',
        downloadURL: 'https://pulse.example/api/setup-script?type=pbs',
        scriptFileName: 'pulse-setup-pbs.sh',
        command: 'curl pbs ...',
        setupToken: 'pbs-token-123',
        tokenHint: 'pbs…123',
        expires: 1_900_000_000,
      });

      const result = await NodesAPI.getProxmoxSetupCommand({
        type: 'pbs',
        host: 'pbs.example',
        backupPerms: false,
      });

      expect(result.commandWithEnv).toBe('curl pbs ...');
    });

    it('rejects malformed canonical setup-script-url responses', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        type: 'pbs',
        host: 'https://pbs.example:8007',
        url: '',
        downloadURL: 'https://pulse.example/api/setup-script?type=pbs',
        scriptFileName: 'pulse-setup-pbs.sh',
        command: 'curl pbs ...',
        setupToken: 'pbs-token-123',
        tokenHint: 'pbs…123',
        expires: 1_900_000_000,
      });

      await expect(
        NodesAPI.getProxmoxSetupCommand({
          type: 'pve',
          host: 'pbs.example',
          backupPerms: false,
        }),
      ).rejects.toThrow('Invalid Proxmox setup response type');
    });

    it('rejects expired canonical setup-script-url responses', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        type: 'pve',
        host: 'https://pve.example:8006',
        url: 'https://pulse.example/api/setup-script?type=pve',
        downloadURL: 'https://pulse.example/api/setup-script?type=pve',
        scriptFileName: 'pulse-setup-pve.sh',
        command: 'curl pve ...',
        setupToken: 'setup-token-123',
        tokenHint: 'set…123',
        expires: 1,
      });

      await expect(
        NodesAPI.getProxmoxSetupCommand({
          type: 'pve',
          host: 'pve.example',
          backupPerms: true,
        }),
      ).rejects.toThrow('Invalid Proxmox setup response expiry');
    });

    it('rejects setup-script-url responses missing the canonical script filename', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        type: 'pve',
        host: 'https://pve.example:8006',
        url: 'https://pulse.example/api/setup-script?type=pve',
        downloadURL: 'https://pulse.example/api/setup-script?type=pve',
        command: 'curl pve ...',
        setupToken: 'setup-token-123',
        tokenHint: 'set…123',
        expires: 1_900_000_000,
      });

      await expect(
        NodesAPI.getProxmoxSetupCommand({
          type: 'pve',
          host: 'pve.example',
          backupPerms: true,
        }),
      ).rejects.toThrow('Invalid Proxmox setup response scriptFileName');
    });

    it('rejects setup-script-url responses missing the canonical token hint', async () => {
      vi.mocked(apiFetchJSON).mockResolvedValueOnce({
        type: 'pve',
        host: 'https://pve.example:8006',
        url: 'https://pulse.example/api/setup-script?type=pve',
        downloadURL: 'https://pulse.example/api/setup-script?type=pve',
        scriptFileName: 'pulse-setup-pve.sh',
        command: 'curl pve ...',
        setupToken: 'setup-token-123',
        expires: 1_900_000_000,
      });

      await expect(
        NodesAPI.getProxmoxSetupCommand({
          type: 'pve',
          host: 'pve.example',
          backupPerms: true,
        }),
      ).rejects.toThrow('Invalid Proxmox setup response token hint');
    });
  });

  describe('downloadProxmoxSetupScript', () => {
    it('downloads the PVE setup script through the canonical client path', async () => {
      vi.mocked(apiFetch).mockResolvedValueOnce(
        new Response('#!/bin/bash\necho pve', {
          status: 200,
          headers: {
            'Content-Type': 'text/x-shellscript; charset=utf-8',
            'Content-Disposition': 'attachment; filename="pulse-setup-pve.sh"',
          },
        }),
      );

      const result = await NodesAPI.downloadProxmoxSetupScript({
        setupToken: 'synthetic-setup-token',
        type: 'pve',
        host: 'https://pve.example:8006',
        url: 'https://pulse.example/base/api/setup-script?type=pve',
        downloadURL: 'https://pulse.example/base/api/setup-script?type=pve',
        scriptFileName: 'pulse-setup-pve.sh',
        command: 'curl pve ...',
        commandWithEnv: 'curl env pve ...',
        commandWithoutEnv: 'curl bare pve ...',
        expires: 1_900_000_000,
        tokenHint: 'set…123',
      });

      expect(apiFetch).toHaveBeenCalledWith('https://pulse.example/base/api/setup-script?type=pve');
      expect(result).toEqual({
        content: '#!/bin/bash\necho pve',
        contentType: 'text/x-shellscript; charset=utf-8',
        fileName: 'pulse-setup-pve.sh',
      });
    });

    it('downloads the PBS setup script without the PVE-only backup perms flag', async () => {
      vi.mocked(apiFetch).mockResolvedValueOnce(
        new Response('#!/bin/bash\necho pbs', {
          status: 200,
          headers: {
            'Content-Type': 'text/x-shellscript; charset=utf-8',
            'Content-Disposition': 'attachment; filename="pulse-setup-pbs.sh"',
          },
        }),
      );

      const result = await NodesAPI.downloadProxmoxSetupScript({
        setupToken: 'synthetic-setup-token',
        type: 'pbs',
        host: 'pbs.example',
        url: 'https://pulse.example/base/api/setup-script?type=pbs',
        downloadURL: 'https://pulse.example/base/api/setup-script?type=pbs',
        scriptFileName: 'pulse-setup-pbs.sh',
        command: 'curl pbs ...',
        commandWithEnv: 'curl env pbs ...',
        commandWithoutEnv: 'curl bare pbs ...',
        expires: 1_900_000_000,
        tokenHint: 'pbs…123',
      });

      expect(apiFetch).toHaveBeenCalledWith('https://pulse.example/base/api/setup-script?type=pbs');
      expect(result).toEqual({
        content: '#!/bin/bash\necho pbs',
        contentType: 'text/x-shellscript; charset=utf-8',
        fileName: 'pulse-setup-pbs.sh',
      });
    });

    it('rejects malformed canonical setup-script download responses', async () => {
      vi.mocked(apiFetch).mockResolvedValueOnce(
        new Response('#!/bin/bash\necho bad', {
          status: 200,
          headers: {
            'Content-Type': 'text/plain; charset=utf-8',
            'Content-Disposition': 'attachment; filename="pulse-setup-pve.sh"',
          },
        }),
      );

      await expect(
        NodesAPI.downloadProxmoxSetupScript({
          setupToken: 'synthetic-setup-token',
          type: 'pve',
          host: 'https://pve.example:8006',
          url: 'https://pulse.example/base/api/setup-script?type=pve',
          downloadURL: 'https://pulse.example/base/api/setup-script?type=pve',
          scriptFileName: 'pulse-setup-pve.sh',
          command: 'curl pve ...',
          commandWithEnv: 'curl env pve ...',
          commandWithoutEnv: 'curl bare pve ...',
          expires: 1_900_000_000,
          tokenHint: 'set…123',
        }),
      ).rejects.toThrow('Invalid Proxmox setup script content type');

      vi.mocked(apiFetch).mockResolvedValueOnce(
        new Response('#!/bin/bash\necho bad', {
          status: 200,
          headers: {
            'Content-Type': 'text/x-shellscript; charset=utf-8',
          },
        }),
      );

      await expect(
        NodesAPI.downloadProxmoxSetupScript({
          setupToken: 'synthetic-setup-token',
          type: 'pve',
          host: 'https://pve.example:8006',
          url: 'https://pulse.example/base/api/setup-script?type=pve',
          downloadURL: 'https://pulse.example/base/api/setup-script?type=pve',
          scriptFileName: 'pulse-setup-pve.sh',
          command: 'curl pve ...',
          commandWithEnv: 'curl env pve ...',
          commandWithoutEnv: 'curl bare pve ...',
          expires: 1_900_000_000,
          tokenHint: 'set…123',
        }),
      ).rejects.toThrow('Invalid Proxmox setup script filename');
    });

    it('rejects setup-script downloads whose filename drifts from bootstrap metadata', async () => {
      vi.mocked(apiFetch).mockResolvedValueOnce(
        new Response('#!/bin/bash\necho bad', {
          status: 200,
          headers: {
            'Content-Type': 'text/x-shellscript; charset=utf-8',
            'Content-Disposition': 'attachment; filename="pulse-setup-pbs.sh"',
          },
        }),
      );

      await expect(
        NodesAPI.downloadProxmoxSetupScript({
          setupToken: 'synthetic-setup-token',
          type: 'pve',
          host: 'https://pve.example:8006',
          url: 'https://pulse.example/base/api/setup-script?type=pve',
          downloadURL: 'https://pulse.example/base/api/setup-script?type=pve',
          scriptFileName: 'pulse-setup-pve.sh',
          command: 'curl pve ...',
          commandWithEnv: 'curl env pve ...',
          commandWithoutEnv: 'curl bare pve ...',
          expires: 1_900_000_000,
          tokenHint: 'set…123',
        }),
      ).rejects.toThrow('Invalid Proxmox setup script filename');
    });
  });
});