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.

17.2 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import type { LicenseRuntimeCapabilityBlock, LicenseRuntimeIdentity } from '@/api/license';

import {
  PATROL_ALERT_ANALYSIS_FEATURE_KEY,
  PATROL_AUTONOMY_FEATURE_KEY,
  PATROL_AUTONOMY_RUNTIME_REQUIRED_REASON,
  getPatrolAlertAnalysisAvailabilityPresentation,
  getPatrolAutonomyAvailabilityPresentation,
} from '../patrolAutonomyAvailability';
import type { UpgradeDestination } from '@/utils/upgradeNavigation';

const planDestination: UpgradeDestination = {
  href: '/plans',
  external: false,
  hardNavigation: false,
  newTab: false,
  preserveOpener: false,
};

const externalDestination = (href: string): UpgradeDestination => ({
  href,
  external: true,
  hardNavigation: true,
  newTab: true,
  preserveOpener: false,
});

const internalDestination = (href: string): UpgradeDestination => ({
  href,
  external: false,
  hardNavigation: false,
  newTab: false,
  preserveOpener: false,
});

const runtimeBlock = (
  overrides: Partial<LicenseRuntimeCapabilityBlock> = {},
): LicenseRuntimeCapabilityBlock => ({
  key: PATROL_AUTONOMY_FEATURE_KEY,
  reason: PATROL_AUTONOMY_RUNTIME_REQUIRED_REASON,
  ...overrides,
});

const runtime = (overrides: Partial<LicenseRuntimeIdentity> = {}): LicenseRuntimeIdentity => ({
  build: 'community-1',
  label: 'Community runtime',
  ...overrides,
});

describe('patrolAutonomyAvailability', () => {
  describe('exported constants', () => {
    it('exposes the ai_autofix feature key', () => {
      expect(PATROL_AUTONOMY_FEATURE_KEY).toBe('ai_autofix');
    });

    it('exposes the ai_alerts feature key', () => {
      expect(PATROL_ALERT_ANALYSIS_FEATURE_KEY).toBe('ai_alerts');
    });

    it('exposes the paid_runtime_required reason', () => {
      expect(PATROL_AUTONOMY_RUNTIME_REQUIRED_REASON).toBe('paid_runtime_required');
    });
  });

  describe('getPatrolAutonomyAvailabilityPresentation', () => {
    it('is available when autoFix is not locked, ignoring every other field', () => {
      const result = getPatrolAutonomyAvailabilityPresentation({
        autoFixLocked: false,
        runtimeCapabilityBlock: runtimeBlock(),
        runtime: runtime(),
        commercialSurfacesHidden: true,
        planUpgradeDestination: planDestination,
      });

      expect(result).toEqual({
        kind: 'available',
        locked: false,
        title: 'Patrol mode available',
        body: 'Choose the mode for this install.',
      });
    });

    it('is available when autoFixLocked is falsy', () => {
      const result = getPatrolAutonomyAvailabilityPresentation({
        autoFixLocked: undefined as unknown as boolean,
        planUpgradeDestination: planDestination,
      });
      expect(result.kind).toBe('available');
      expect(result.locked).toBe(false);
    });

    it.each([
      [
        'commercialSurfacesHidden beats runtime_locked',
        {
          autoFixLocked: true,
          runtimeCapabilityBlock: runtimeBlock(),
          runtime: runtime(),
          commercialSurfacesHidden: true,
          upgradePromptsHidden: true,
          planUpgradeDestination: planDestination,
        },
        'plan_locked',
      ],
      [
        'runtime_locked when commercial surfaces are visible',
        {
          autoFixLocked: true,
          runtimeCapabilityBlock: runtimeBlock(),
          runtime: runtime(),
          upgradePromptsHidden: true,
          planUpgradeDestination: planDestination,
        },
        'runtime_locked',
      ],
      [
        'plan_locked when commercialSurfacesHidden and no runtime block',
        {
          autoFixLocked: true,
          commercialSurfacesHidden: true,
          planUpgradeDestination: planDestination,
        },
        'plan_locked',
      ],
      [
        'plan_locked by default when locked but no block and surfaces visible',
        {
          autoFixLocked: true,
          planUpgradeDestination: planDestination,
        },
        'plan_locked',
      ],
    ])('resolves kind precedence: %s', (_label, input, expectedKind) => {
      expect(getPatrolAutonomyAvailabilityPresentation(input).kind).toBe(expectedKind);
    });

    describe('runtime_locked presentation', () => {
      it('builds the runtime-locked surface with the runtime label in the body', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          runtimeCapabilityBlock: runtimeBlock({ action_url: 'https://pro.example/downloads' }),
          runtime: runtime({ label: 'Community runtime' }),
          planUpgradeDestination: planDestination,
        });

        expect(result).toEqual({
          kind: 'runtime_locked',
          locked: true,
          title: 'Pulse Pro runtime required',
          body: 'This install is running Community runtime. Install the Pulse Pro runtime to use Patrol modes.',
          actionLabel: 'Open Pro downloads',
          destination: externalDestination('https://pro.example/downloads'),
        });
      });

      it('uses the runtime download_url when the block has no action_url', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          runtimeCapabilityBlock: runtimeBlock(),
          runtime: runtime({ download_url: '/downloads/pro' }),
          planUpgradeDestination: planDestination,
        });

        expect(result.actionLabel).toBe('Open Pro downloads');
        expect(result.destination).toEqual(internalDestination('/downloads/pro'));
      });

      it('prefers the block action_url over the runtime download_url', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          runtimeCapabilityBlock: runtimeBlock({ action_url: 'https://block.example/dl' }),
          runtime: runtime({ download_url: 'https://runtime.example/dl' }),
          planUpgradeDestination: planDestination,
        });

        expect(result.destination).toEqual(externalDestination('https://block.example/dl'));
      });

      it('trims whitespace on the action_url before resolving', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          runtimeCapabilityBlock: runtimeBlock({ action_url: '  https://pro.example/downloads  ' }),
          runtime: runtime(),
          planUpgradeDestination: planDestination,
        });

        expect(result.destination).toEqual(externalDestination('https://pro.example/downloads'));
      });

      it('falls back to the plan upgrade destination when no runtime download source exists', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          runtimeCapabilityBlock: runtimeBlock(),
          runtime: runtime(),
          planUpgradeDestination: planDestination,
        });

        expect(result.destination).toBe(planDestination);
      });

      it('falls back to the plan destination when action_url and download_url are blank', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          runtimeCapabilityBlock: runtimeBlock({ action_url: '   ' }),
          runtime: runtime({ download_url: '   ' }),
          planUpgradeDestination: planDestination,
        });

        expect(result.destination).toBe(planDestination);
      });

      it('omits the action label and destination when upgrade prompts are hidden', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          upgradePromptsHidden: true,
          runtimeCapabilityBlock: runtimeBlock({ action_url: 'https://pro.example/downloads' }),
          runtime: runtime(),
          planUpgradeDestination: planDestination,
        });

        expect(result).toEqual({
          kind: 'runtime_locked',
          locked: true,
          title: 'Pulse Pro runtime required',
          body: 'This install is running Community runtime. Install the Pulse Pro runtime to use Patrol modes.',
        });
        expect(result).not.toHaveProperty('actionLabel');
        expect(result).not.toHaveProperty('destination');
      });

      it('falls back to the "this runtime" label when the runtime label is blank', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          upgradePromptsHidden: true,
          runtimeCapabilityBlock: runtimeBlock(),
          runtime: runtime({ label: '   ' }),
          planUpgradeDestination: planDestination,
        });

        expect(result.body).toBe(
          'This install is running this runtime. Install the Pulse Pro runtime to use Patrol modes.',
        );
      });

      it('falls back to the "this runtime" label when runtime is undefined', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          upgradePromptsHidden: true,
          runtimeCapabilityBlock: runtimeBlock(),
          planUpgradeDestination: planDestination,
        });

        expect(result.body).toBe(
          'This install is running this runtime. Install the Pulse Pro runtime to use Patrol modes.',
        );
      });

      it('trims the runtime label before interpolating it', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          upgradePromptsHidden: true,
          runtimeCapabilityBlock: runtimeBlock(),
          runtime: runtime({ label: '  Pulse Pro  ' }),
          planUpgradeDestination: planDestination,
        });

        expect(result.body).toBe(
          'This install is running Pulse Pro. Install the Pulse Pro runtime to use Patrol modes.',
        );
      });

      it('does not treat a non-matching capability reason as runtime locked', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          runtimeCapabilityBlock: runtimeBlock({ reason: 'some_other_reason' }),
          runtime: runtime(),
          planUpgradeDestination: planDestination,
        });

        expect(result.kind).toBe('plan_locked');
        expect(result.actionLabel).toBe('Plans & Billing');
      });
    });

    describe('plan_locked presentation', () => {
      it('shows the Plans & Billing action when surfaces are visible', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          planUpgradeDestination: planDestination,
        });

        expect(result).toEqual({
          kind: 'plan_locked',
          locked: true,
          title: 'Watch only',
          body: 'This install watches infrastructure and shows issues.',
          actionLabel: 'Plans & Billing',
          destination: planDestination,
        });
      });

      it('omits the action when upgrade prompts are hidden', () => {
        const result = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          upgradePromptsHidden: true,
          planUpgradeDestination: planDestination,
        });

        expect(result).toEqual({
          kind: 'plan_locked',
          locked: true,
          title: 'Watch only',
          body: 'This install watches infrastructure and shows issues.',
        });
        expect(result).not.toHaveProperty('actionLabel');
        expect(result).not.toHaveProperty('destination');
      });

      it('omits the action when commercial surfaces are hidden, regardless of upgrade prompt visibility', () => {
        const withPromptsVisible = getPatrolAutonomyAvailabilityPresentation({
          autoFixLocked: true,
          commercialSurfacesHidden: true,
          planUpgradeDestination: planDestination,
        });

        expect(withPromptsVisible).toEqual({
          kind: 'plan_locked',
          locked: true,
          title: 'Watch only',
          body: 'This install watches infrastructure and shows issues.',
        });
        expect(withPromptsVisible).not.toHaveProperty('actionLabel');
        expect(withPromptsVisible).not.toHaveProperty('destination');
      });
    });
  });

  describe('getPatrolAlertAnalysisAvailabilityPresentation', () => {
    const alertRuntimeBlock = runtimeBlock({ key: PATROL_ALERT_ANALYSIS_FEATURE_KEY });

    it('is available when alert analysis is not locked, ignoring every other field', () => {
      expect(
        getPatrolAlertAnalysisAvailabilityPresentation({
          alertAnalysisLocked: false,
          runtimeCapabilityBlock: alertRuntimeBlock,
          commercialSurfacesHidden: true,
          planUpgradeDestination: planDestination,
        }),
      ).toEqual({
        kind: 'available',
        locked: false,
        title: 'Container update risk available',
        body: 'Assess risk when container-update alerts fire.',
      });
    });

    it('explains a plan lock and offers Plans & Billing when surfaces are visible', () => {
      expect(
        getPatrolAlertAnalysisAvailabilityPresentation({
          alertAnalysisLocked: true,
          planUpgradeDestination: planDestination,
        }),
      ).toEqual({
        kind: 'plan_locked',
        locked: true,
        title: 'Higher license plan required',
        body: "This install's plan does not include container update risk.",
        actionLabel: 'Plans & Billing',
        destination: planDestination,
      });
    });

    it('keeps the plan explanation but drops the action when upgrade prompts are hidden', () => {
      const result = getPatrolAlertAnalysisAvailabilityPresentation({
        alertAnalysisLocked: true,
        upgradePromptsHidden: true,
        planUpgradeDestination: planDestination,
      });

      expect(result).toEqual({
        kind: 'plan_locked',
        locked: true,
        title: 'Higher license plan required',
        body: "This install's plan does not include container update risk.",
      });
      expect(result).not.toHaveProperty('actionLabel');
      expect(result).not.toHaveProperty('destination');
    });

    it('uses neutral copy with no action when commercial surfaces are hidden', () => {
      const result = getPatrolAlertAnalysisAvailabilityPresentation({
        alertAnalysisLocked: true,
        commercialSurfacesHidden: true,
        planUpgradeDestination: planDestination,
      });

      expect(result).toEqual({
        kind: 'plan_locked',
        locked: true,
        title: 'Not available',
        body: 'This install does not include container update risk.',
      });
      expect(`${result.title} ${result.body}`).not.toMatch(/plan|Pro\b/i);
    });

    it('shares the canonical runtime lock with Patrol mode', () => {
      const input = {
        runtimeCapabilityBlock: runtimeBlock({
          key: PATROL_ALERT_ANALYSIS_FEATURE_KEY,
          action_url: 'https://pro.example/downloads',
        }),
        runtime: runtime({ label: 'Pulse Community runtime' }),
        planUpgradeDestination: planDestination,
      };
      const alertAnalysis = getPatrolAlertAnalysisAvailabilityPresentation({
        ...input,
        alertAnalysisLocked: true,
      });
      const patrolMode = getPatrolAutonomyAvailabilityPresentation({
        ...input,
        autoFixLocked: true,
      });

      expect(alertAnalysis).toEqual({
        kind: 'runtime_locked',
        locked: true,
        title: 'Pulse Pro runtime required',
        body: 'This install is running Pulse Community runtime. Install the Pulse Pro runtime to use container update risk.',
        actionLabel: 'Open Pro downloads',
        destination: externalDestination('https://pro.example/downloads'),
      });
      expect(alertAnalysis.title).toBe(patrolMode.title);
      expect(alertAnalysis.actionLabel).toBe(patrolMode.actionLabel);
      expect(alertAnalysis.destination).toEqual(patrolMode.destination);
    });

    it('keeps Pro runtime wording out of hidden-commercial sessions for both capabilities', () => {
      const input = {
        commercialSurfacesHidden: true,
        upgradePromptsHidden: true,
        runtimeCapabilityBlock: runtimeBlock({ action_url: 'https://pro.example/downloads' }),
        runtime: runtime({ label: 'Pulse Community runtime' }),
        planUpgradeDestination: planDestination,
      };
      const alertAnalysis = getPatrolAlertAnalysisAvailabilityPresentation({
        ...input,
        alertAnalysisLocked: true,
      });
      const patrolMode = getPatrolAutonomyAvailabilityPresentation({
        ...input,
        autoFixLocked: true,
      });

      expect(alertAnalysis).toEqual({
        kind: 'plan_locked',
        locked: true,
        title: 'Not available',
        body: 'This install does not include container update risk.',
      });
      expect(patrolMode).toEqual({
        kind: 'plan_locked',
        locked: true,
        title: 'Watch only',
        body: 'This install watches infrastructure and shows issues.',
      });
      for (const result of [alertAnalysis, patrolMode]) {
        expect(`${result.title} ${result.body}`).not.toMatch(/\bPro\b|runtime|plan/i);
      }
    });

    it('omits the runtime download action when upgrade prompts are hidden', () => {
      const result = getPatrolAlertAnalysisAvailabilityPresentation({
        alertAnalysisLocked: true,
        upgradePromptsHidden: true,
        runtimeCapabilityBlock: alertRuntimeBlock,
        runtime: runtime(),
        planUpgradeDestination: planDestination,
      });

      expect(result.kind).toBe('runtime_locked');
      expect(result).not.toHaveProperty('actionLabel');
      expect(result).not.toHaveProperty('destination');
    });
  });
});