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.8 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import type { UIEmailConfig } from '../types';

import { buildEmailConfigPayload } from '../alertDestinationsModel';

// ---------------------------------------------------------------------------
// Fixture — minimal valid UIEmailConfig.  Tests override only the fields
// relevant to the branch under examination.
// ---------------------------------------------------------------------------

function makeUIEmailConfig(overrides: Partial<UIEmailConfig> = {}): UIEmailConfig {
  return {
    enabled: true,
    provider: 'smtp',
    server: 'smtp.internal',
    port: 587,
    username: '[email protected]',
    password: 'secret',
    from: '[email protected]',
    to: ['[email protected]'],
    tls: true,
    startTLS: true,
    rateLimit: 0,
    ...overrides,
  };
}

describe('buildEmailConfigPayload — branch coverage (batch 2)', () => {
  // -----------------------------------------------------------------------
  // to: .map(trim) / .filter(length > 0)
  //   - filter true  arm: entry.length > 0  → element kept
  //   - filter false arm: entry.length === 0 → element dropped
  //   - map  no-op  arm: already-trimmed string
  //   - map  strip  arm: leading/trailing/whitespace removed
  //   - zero-iteration path: empty input array
  // -----------------------------------------------------------------------
  describe('to array transformation', () => {
    it('returns an empty to array when the input to list is empty', () => {
      const result = buildEmailConfigPayload(makeUIEmailConfig({ to: [] }));
      expect(result.to).toStrictEqual([]);
    });

    it('drops every recipient when all entries are whitespace-only', () => {
      // Each entry is non-empty before trim but becomes '' after trim,
      // exercising the `entry.length > 0 === false` filter arm for
      // strings that only become empty *after* trimming.
      const result = buildEmailConfigPayload(makeUIEmailConfig({ to: ['   ', '\t', '\n'] }));
      expect(result.to).toStrictEqual([]);
    });

    it('strips leading-only, trailing-only, and tab-padded whitespace independently', () => {
      const result = buildEmailConfigPayload(
        makeUIEmailConfig({
          to: ['  [email protected]', '[email protected]  ', '\t[email protected]\t'],
        }),
      );
      expect(result.to).toStrictEqual(['[email protected]', '[email protected]', '[email protected]']);
    });

    it('preserves internal whitespace within recipient entries', () => {
      const result = buildEmailConfigPayload(makeUIEmailConfig({ to: ['  alice [email protected]  '] }));
      expect(result.to).toStrictEqual(['alice [email protected]']);
    });

    it('keeps a single already-trimmed entry without modification', () => {
      const result = buildEmailConfigPayload(makeUIEmailConfig({ to: ['[email protected]'] }));
      expect(result.to).toStrictEqual(['[email protected]']);
    });

    it('interleaves kept and dropped entries preserving original order', () => {
      const result = buildEmailConfigPayload(
        makeUIEmailConfig({
          to: ['', '[email protected]', '   ', '  [email protected]  ', ''],
        }),
      );
      expect(result.to).toStrictEqual(['[email protected]', '[email protected]']);
    });
  });

  // -----------------------------------------------------------------------
  // Exact payload shape — toStrictEqual verifies that unsupported fields
  // (replyTo, maxRetries, retryDelay) stay absent from the
  // output and that falsy scalar values pass through unchanged.
  // -----------------------------------------------------------------------
  describe('payload shape', () => {
    it('produces the exact EmailConfig shape with all falsy values preserved', () => {
      const result = buildEmailConfigPayload(
        makeUIEmailConfig({
          enabled: false,
          provider: 'ses',
          server: 'email.example.com',
          port: 0,
          username: '[email protected]',
          password: '',
          from: '[email protected]',
          to: ['[email protected]'],
          tls: false,
          startTLS: false,
          rateLimit: 100,
        }),
      );
      expect(result).toStrictEqual({
        enabled: false,
        provider: 'ses',
        server: 'email.example.com',
        port: 0,
        username: '[email protected]',
        password: '',
        from: '[email protected]',
        to: ['[email protected]'],
        tls: false,
        startTLS: false,
        rateLimit: 100,
        minimumSeverity: 'all',
      });
    });

    it('includes the rate limit already supported by the email API', () => {
      const result = buildEmailConfigPayload(makeUIEmailConfig({ rateLimit: 100 }));
      expect(result).toHaveProperty('rateLimit', 100);
    });

    it('does not include replyTo, maxRetries, or retryDelay in the payload', () => {
      const staleDraft = {
        ...makeUIEmailConfig(),
        replyTo: '[email protected]',
        maxRetries: 10,
        retryDelay: 300,
      };
      const result = buildEmailConfigPayload(staleDraft);
      expect(result).not.toHaveProperty('replyTo');
      expect(result).not.toHaveProperty('maxRetries');
      expect(result).not.toHaveProperty('retryDelay');
    });
  });

  // -----------------------------------------------------------------------
  // Scalar passthrough — verify individual fields are forwarded verbatim,
  // including edge values (zero port, empty password, empty from/server).
  // -----------------------------------------------------------------------
  describe('scalar passthrough', () => {
    it('forwards a zero port unchanged', () => {
      const result = buildEmailConfigPayload(makeUIEmailConfig({ port: 0 }));
      expect(result.port).toBe(0);
    });

    it('forwards an empty password string unchanged', () => {
      const result = buildEmailConfigPayload(makeUIEmailConfig({ password: '' }));
      expect(result.password).toBe('');
    });

    it('forwards an empty from address unchanged', () => {
      const result = buildEmailConfigPayload(makeUIEmailConfig({ from: '' }));
      expect(result.from).toBe('');
    });

    it('forwards an empty server string unchanged', () => {
      const result = buildEmailConfigPayload(makeUIEmailConfig({ server: '' }));
      expect(result.server).toBe('');
    });

    it('forwards enabled, tls, and startTLS false values unchanged', () => {
      const result = buildEmailConfigPayload(
        makeUIEmailConfig({ enabled: false, tls: false, startTLS: false }),
      );
      expect(result.enabled).toBe(false);
      expect(result.tls).toBe(false);
      expect(result.startTLS).toBe(false);
    });

    it('forwards enabled, tls, and startTLS true values unchanged', () => {
      const result = buildEmailConfigPayload(
        makeUIEmailConfig({ enabled: true, tls: true, startTLS: true }),
      );
      expect(result.enabled).toBe(true);
      expect(result.tls).toBe(true);
      expect(result.startTLS).toBe(true);
    });
  });
});