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.

11.8 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { installPortalApp } from './app';
import { createPortalRuntime } from './runtime';
import { createPortalStore } from './store';
import type { PortalBootstrapData } from './types';

function jsonResponse(payload: unknown, ok = true, status = ok ? 200 : 500) {
  return {
    ok,
    status,
    headers: new Headers({ 'content-type': 'application/json' }),
    json: vi.fn().mockResolvedValue(payload),
    text: vi.fn().mockResolvedValue(typeof payload === 'string' ? payload : JSON.stringify(payload)),
  };
}

async function flushAsync() {
  await Promise.resolve();
  await Promise.resolve();
  await Promise.resolve();
  await new Promise(function(resolve) {
    setTimeout(resolve, 0);
  });
}

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 app', function() {
  beforeEach(function() {
    document.body.innerHTML = `
      <div id="portal-user-info"></div>
      <div id="portal-app-root"></div>
      <div id="toast"></div>
    `;
    vi.restoreAllMocks();
  });

  it('refreshes authenticated startup bootstrap through the owned app entrypoint', async function() {
    var store = createPortalStore(bootstrapDefaults, {
      authenticated: true,
      email: '[email protected]',
      accounts: [],
    });

    var fetchMock = vi.fn().mockResolvedValue({
      ok: true,
      status: 200,
      json: async function() {
        return {
          authenticated: true,
          email: '[email protected]',
          accounts: [],
        };
      },
    });
    vi.stubGlobal('fetch', fetchMock);

    var app = installPortalApp({
      bootstrapDefaults: bootstrapDefaults,
      store: store,
    });

    await app.startupRefresh;

    expect(fetchMock).toHaveBeenCalledWith('/api/portal/bootstrap', {
      headers: { Accept: 'application/json' },
    });
    expect(store.getBootstrap().email).toBe('[email protected]');
  });

  it('falls back to anonymous bootstrap when startup refresh is unauthorized', async 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: [],
        },
      ],
    });

    vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
      ok: false,
      status: 401,
    }));

    var app = installPortalApp({
      bootstrapDefaults: bootstrapDefaults,
      store: store,
    });

    await app.startupRefresh;

    expect(store.getBootstrap().authenticated).toBe(false);
    expect(store.getBootstrap().email).toBe('');
    expect(store.getBootstrap().accounts).toEqual([]);
  });

  it('starts from the owned runtime factory instead of import-time globals', function() {
    document.body.innerHTML = `
      <script id="pulse-account-bootstrap" type="application/json">
        {"authenticated":true,"email":"[email protected]","public_site_url":"https://cloud.pulserelay.pro","accounts":[]}
      </script>
      <div id="portal-user-info"></div>
      <div id="portal-app-root"></div>
      <div id="toast"></div>
    `;

    var runtime = createPortalRuntime({
      authenticated: true,
      email: '[email protected]',
      public_site_url: 'https://cloud.pulserelay.pro',
      accounts: [],
    });

    expect(runtime.bootstrapDefaults.public_site_url).toBe('https://cloud.pulserelay.pro');
    expect(runtime.store.getBootstrap().authenticated).toBe(true);
    expect(runtime.store.getBootstrap().email).toBe('[email protected]');
  });

  it('preserves utility handoff intent across the signed-out to authenticated portal transition', function() {
    var runtime = createPortalRuntime(
      {
        authenticated: false,
        email: '',
        public_site_url: 'https://cloud.pulserelay.pro',
        accounts: [],
      },
      {
        email: '[email protected]',
        openBillingPanelID: 'retrieve-billing-panel',
        upgradePortalHandoffID: '',
        upgradeFeatureKey: '',
        upgradeTrialRequested: false,
      }
    );

    var app = installPortalApp({
      bootstrapDefaults: runtime.bootstrapDefaults,
      store: runtime.store,
    });

    expect((document.getElementById('portal-login-email') as HTMLInputElement | null)?.value).toBe('[email protected]');

    app.applyBootstrap({
      authenticated: true,
      email: '[email protected]',
      has_self_hosted_commercial: true,
      email_sign_in_available: true,
      provider_hosted_mode: false,
      accounts: [
        {
          id: 'acct_1',
          name: 'Acme MSP',
          kind: 'msp',
          kind_label: 'MSP',
          role: 'owner',
          can_manage: true,
          has_billing: true,
          members: [],
          workspaces: [],
        },
      ],
    });

    expect(document.getElementById('retrieve-billing-panel')?.classList.contains('visible')).toBe(true);
    expect((document.getElementById('retrieve-inline-email') as HTMLInputElement | null)?.value).toBe('[email protected]');
    expect(runtime.store.getBillingState().openBillingPanelID).toBe('retrieve-billing-panel');
  });

  it('completes the signed-out magic-link flow through the real shell and auth runtime', async function() {
    var store = createPortalStore(bootstrapDefaults, {
      authenticated: false,
      email: '',
      accounts: [],
    });
    var fetchMock = vi.fn().mockResolvedValue(jsonResponse({}));
    vi.stubGlobal('fetch', fetchMock);

    installPortalApp({
      bootstrapDefaults: bootstrapDefaults,
      store: store,
    });

    var emailInput = document.getElementById('portal-login-email') as HTMLInputElement | null;
    expect(emailInput).not.toBeNull();
    emailInput!.value = '[email protected]';
    emailInput!.dispatchEvent(new Event('input', { bubbles: true }));

    document.getElementById('portal-login-send')?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
    await flushAsync();

    expect(fetchMock).toHaveBeenCalledWith(
      '/api/public/magic-link/request',
      expect.objectContaining({
        method: 'POST',
        body: JSON.stringify({ email: '[email protected]', target: 'portal' }),
      })
    );
    expect(document.getElementById('portal-app-root')?.textContent).toContain('If that email is registered, a sign-in link is on the way.');
  });

  it('completes the retrieve-license flow through the real authenticated app shell', async function() {
    var accounts = [
      {
        id: 'acct_1',
        name: 'Acme MSP',
        kind: 'msp',
        kind_label: 'MSP',
        role: 'owner',
        can_manage: true,
        has_billing: true,
        members: [],
        workspaces: [],
      },
    ];
    var store = createPortalStore(bootstrapDefaults, {
      authenticated: true,
      email: '[email protected]',
      has_self_hosted_commercial: true,
      email_sign_in_available: true,
      provider_hosted_mode: false,
      accounts: accounts,
    });
    var fetchMock = vi.fn(async function(input: RequestInfo | URL) {
      var url = String(input);
      if (url === '/api/portal/bootstrap') {
        return jsonResponse({
          authenticated: true,
          email: '[email protected]',
          has_self_hosted_commercial: true,
          email_sign_in_available: true,
          provider_hosted_mode: false,
          accounts: accounts,
        });
      }
      if (url === '/api/portal/commercial/v1/retrieve-license/request') {
        return jsonResponse({});
      }
      if (url === '/api/portal/commercial/v1/retrieve-license') {
        return jsonResponse({
          license: {
            token: 'pulse_token_123',
            tier: 'Pro',
            issued_at: '2026-03-01T10:00:00Z',
            expires_at: null,
            email: '[email protected]',
          },
        });
      }
      throw new Error('Unexpected fetch: ' + url);
    });
    vi.stubGlobal('fetch', fetchMock);

    var app = installPortalApp({
      bootstrapDefaults: bootstrapDefaults,
      store: store,
    });
    await app.startupRefresh;

    document.getElementById('open-retrieve-billing')?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
    var retrievePanel = document.getElementById('retrieve-billing-panel');
    expect(retrievePanel?.classList.contains('visible')).toBe(true);

    var emailInput = document.getElementById('retrieve-inline-email') as HTMLInputElement | null;
    expect(emailInput?.value).toBe('[email protected]');

    document.getElementById('retrieve-inline-request')?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
    await flushAsync();

    var codeInput = document.getElementById('retrieve-inline-code') as HTMLInputElement | null;
    expect(codeInput).not.toBeNull();
    codeInput!.value = '123456';
    codeInput!.dispatchEvent(new Event('input', { bubbles: true }));
    document.getElementById('retrieve-inline-confirm')?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
    await flushAsync();

    expect(fetchMock.mock.calls).toContainEqual([
      '/api/portal/commercial/v1/retrieve-license/request',
      expect.objectContaining({
        method: 'POST',
        body: JSON.stringify({ email: '[email protected]' }),
      }),
    ]);
    expect(fetchMock.mock.calls).toContainEqual([
      '/api/portal/commercial/v1/retrieve-license',
      expect.objectContaining({
        method: 'POST',
        body: JSON.stringify({ email: '[email protected]', code: '123456' }),
      }),
    ]);
    expect((store.getBillingState().flows.retrieve.result as { token?: string } | null)?.token).toBe('pulse_token_123');
    expect((document.getElementById('retrieve-inline-result') as HTMLElement | null)?.hidden).toBe(false);
    expect((document.getElementById('retrieve-inline-copy') as HTMLButtonElement | null)?.hidden).toBe(false);
    expect(document.getElementById('retrieve-inline-status')?.textContent).toContain('License retrieved successfully.');
  });

  it('renders the hosted access roster from bootstrap through the real authenticated account shell', async function() {
    var accounts = [
      {
        id: 'acct_1',
        name: 'Acme MSP',
        kind: 'msp',
        kind_label: 'MSP',
        role: 'owner',
        can_manage: true,
        has_billing: true,
        members: [
          { email: '[email protected]', role: 'owner', user_id: 'u_1' },
        ],
        workspaces: [],
      },
    ];
    var store = createPortalStore(bootstrapDefaults, {
      authenticated: true,
      email: '[email protected]',
      accounts: accounts,
    });
    var fetchMock = vi.fn()
      .mockResolvedValueOnce(jsonResponse({
        authenticated: true,
        email: '[email protected]',
        accounts: accounts,
      }));
    vi.stubGlobal('fetch', fetchMock);

    var app = installPortalApp({
      bootstrapDefaults: bootstrapDefaults,
      store: store,
    });
    await app.startupRefresh;

    document.querySelector('[data-shell-action="activate-section"][data-shell-section="access"]')?.dispatchEvent(new MouseEvent('click', { bubbles: true }));
    await flushAsync();

    expect(fetchMock).toHaveBeenCalledTimes(1);
    expect(document.getElementById('access-section-acct_1')?.classList.contains('visible')).toBe(true);
    expect(document.getElementById('access-list-acct_1')?.textContent).toContain('[email protected]');
  });
});