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

import { createAnonymousBootstrap, createPortalStore, normalizeBootstrap } from './store';
import type { PortalBootstrapData } from './types';

const bootstrapDefaults: Omit<PortalBootstrapData, 'authenticated' | 'email' | 'accounts'> = {
  has_self_hosted_commercial: false,
  email_sign_in_available: true,
  provider_hosted_mode: false,
  public_site_url: 'https://pulserelay.pro',
  support_email: '[email protected]',
  commercial_api_base_url: '/api/portal/commercial',
  portal_path: '/portal',
  bootstrap_path: '/api/portal/bootstrap',
  magic_link_request_path: '/api/public/magic-link/request',
  signup_path: '/signup',
  logout_path: '/auth/logout',
  account_api_base_path: '/api/accounts',
  portal_api_base_path: '/api/portal',
};

describe('portal store', function() {
  it('normalizes anonymous bootstrap state from defaults', function() {
    var bootstrap = createAnonymousBootstrap(bootstrapDefaults, {
      signup_path: '/join',
    });

    expect(bootstrap.authenticated).toBe(false);
    expect(bootstrap.email).toBe('');
    expect(bootstrap.signup_path).toBe('/join');
    expect(bootstrap.accounts).toEqual([]);
  });

  it('normalizes partial bootstrap payloads into tracked account state', function() {
    var bootstrap = normalizeBootstrap(bootstrapDefaults, {
      authenticated: true,
      email: '[email protected]',
      accounts: [
        {
          id: 'acct_1',
          name: 'Acme MSP',
          kind: 'msp',
          kind_label: 'MSP',
          role: 'owner',
          can_manage: true,
          has_billing: true,
          members: [],
          workspaces: [],
        },
      ],
    });

    expect(bootstrap.authenticated).toBe(true);
    expect(bootstrap.email).toBe('[email protected]');
    expect(bootstrap.accounts).toHaveLength(1);
    expect(bootstrap.accounts[0].setup_templates).toEqual([]);
    expect(bootstrap.portal_path).toBe('/portal');
  });

  it('defaults authenticated hosted accounts to the workspaces section', function() {
    var store = createPortalStore(bootstrapDefaults, {
      authenticated: true,
      email: '[email protected]',
      accounts: [
        {
          id: 'acct_1',
          name: 'Acme MSP',
          kind: 'msp',
          kind_label: 'MSP',
          role: 'owner',
          can_manage: true,
          has_billing: true,
          members: [],
          workspaces: [],
        },
      ],
    });

    expect(store.getShellState().activeSection).toBe('workspaces');
  });

  it('defaults authenticated self-hosted-only accounts to the billing section', function() {
    var store = createPortalStore(bootstrapDefaults, {
      authenticated: true,
      email: '[email protected]',
      has_self_hosted_commercial: true,
      email_sign_in_available: true,
      provider_hosted_mode: false,
      accounts: [],
    });

    expect(store.getShellState().activeSection).toBe('billing');
  });

  it('publishes bootstrap changes through a subscription boundary', function() {
    var store = createPortalStore(bootstrapDefaults, null);
    var listener = vi.fn();
    var unsubscribe = store.subscribeBootstrap(listener);

    store.setBootstrap({
      authenticated: true,
      email: '[email protected]',
    });

    expect(listener).toHaveBeenCalledTimes(1);
    expect(store.getBootstrap().authenticated).toBe(true);
    expect(store.getBootstrap().email).toBe('[email protected]');

    unsubscribe();
    store.setBootstrap({
      authenticated: false,
      email: '',
    });

    expect(listener).toHaveBeenCalledTimes(1);
  });

  it('seeds hosted access state from the bootstrap roster snapshot', function() {
    var store = createPortalStore(bootstrapDefaults, {
      authenticated: true,
      email: '[email protected]',
      accounts: [
        {
          id: 'acct_1',
          name: 'Acme MSP',
          kind: 'msp',
          kind_label: 'MSP',
          role: 'owner',
          can_manage: true,
          has_billing: true,
          members: [
            { subject_id: 'u_1', email: '[email protected]', role: 'owner', user_id: 'u_1', state: 'active', created_at: '2026-03-28T10:00:00Z' },
          ],
          workspaces: [],
        },
      ],
    });

    var entry = store.getAccountState().byAccountID.acct_1;
    expect(entry.accessQuery.status).toBe('ready');
    expect(entry.accessQuery.error).toBe('');
    expect(entry.accessQuery.data).toEqual([
      { subject_id: 'u_1', email: '[email protected]', role: 'owner', user_id: 'u_1', state: 'active', created_at: '2026-03-28T10:00:00Z' },
    ]);

    store.setBootstrap({
      authenticated: true,
      email: '[email protected]',
      accounts: [
        {
          id: 'acct_1',
          name: 'Acme MSP',
          kind: 'msp',
          kind_label: 'MSP',
          role: 'owner',
          can_manage: true,
          has_billing: true,
          members: [
            { subject_id: 'u_2', email: '[email protected]', role: 'admin', user_id: 'u_2', state: 'active', created_at: '2026-03-28T11:00:00Z' },
          ],
          workspaces: [],
        },
      ],
    });

    expect(store.getAccountState().byAccountID.acct_1.accessQuery.data).toEqual([
      { subject_id: 'u_2', email: '[email protected]', role: 'admin', user_id: 'u_2', state: 'active', created_at: '2026-03-28T11:00:00Z' },
    ]);
  });

  it('keeps login and service state in the same owned store with selective notifications', function() {
    var store = createPortalStore(bootstrapDefaults, null);
    var loginListener = vi.fn();
    var billingListener = vi.fn();

    store.subscribeLogin(loginListener);
    store.subscribeBilling(billingListener);

    store.updateLoginState(function(loginState) {
      loginState.emailValue = '[email protected]';
    }, { notify: false });
    store.updateBillingState(function(billingState) {
      billingState.openBillingPanelID = 'retrieve-billing-panel';
    });

    expect(store.getLoginState().emailValue).toBe('[email protected]');
    expect(store.getBillingState().openBillingPanelID).toBe('retrieve-billing-panel');
    expect(loginListener).not.toHaveBeenCalled();
    expect(billingListener).toHaveBeenCalledTimes(1);
  });
});