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.

4.2 KB
Raw Blame
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';

import {
  applyRequestedEntitlementProfile,
  buildBillingState,
  resolveEntitlementProfile,
  resolveEntitlementTarget,
} from './entitlement-bootstrap.mjs';

test('resolveEntitlementProfile infers multi-tenant from feature flag', () => {
  assert.deepEqual(
    resolveEntitlementProfile({ PULSE_MULTI_TENANT_ENABLED: 'true' }),
    { profile: 'multi-tenant', explicit: false },
  );
});

test('resolveEntitlementTarget prefers explicit file path in skip-docker mode', () => {
  assert.deepEqual(
    resolveEntitlementTarget({
      PULSE_E2E_SKIP_DOCKER: '1',
      PULSE_E2E_BILLING_STATE_PATH: '/tmp/pulse/billing.json',
    }),
    { kind: 'file', path: '/tmp/pulse/billing.json' },
  );
});

test('buildBillingState returns enterprise capabilities for multi-tenant profile', () => {
  const state = buildBillingState('multi-tenant');
  assert.equal(state.plan_version, 'enterprise_eval');
  assert.equal(state.subscription_state, 'active');
  assert.ok(state.capabilities.includes('multi_tenant'));
  assert.ok(state.capabilities.includes('rbac'));
});

test('general-purpose entitlement profiles keep commercial routes available', () => {
  for (const profile of ['multi-tenant', 'infra']) {
    const state = buildBillingState(profile);
    assert.ok(
      !state.capabilities.includes('white_label'),
      `${profile} must not opt the shared runtime into commercial suppression`,
    );
  }
});

test('applyRequestedEntitlementProfile writes a billing state file when a path is provided', async () => {
  const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'pulse-entitlement-'));
  const billingPath = path.join(dir, 'billing.json');

  const result = await applyRequestedEntitlementProfile({
    env: {
      PULSE_E2E_SKIP_DOCKER: '1',
      PULSE_E2E_ENTITLEMENT_PROFILE: 'multi-tenant',
      PULSE_E2E_BILLING_STATE_PATH: billingPath,
    },
    logger: { log() {}, warn() {} },
  });

  assert.equal(result.applied, true);
  const raw = JSON.parse(await fs.readFile(billingPath, 'utf8'));
  assert.equal(raw.plan_version, 'enterprise_eval');
  assert.ok(raw.capabilities.includes('multi_tenant'));
});

test('applyRequestedEntitlementProfile uses docker exec for managed docker runs', async () => {
  const calls = [];
  await applyRequestedEntitlementProfile({
    env: {
      PULSE_E2E_ENTITLEMENT_PROFILE: 'multi-tenant',
    },
    logger: { log() {}, warn() {} },
    run: async (command, args, options = {}) => {
      calls.push({ command, args, input: options.input });
    },
  });

  assert.equal(calls.length, 1);
  assert.equal(calls[0].command, 'docker');
  assert.deepEqual(calls[0].args.slice(0, 5), ['exec', '-i', 'pulse-test-server', 'sh', '-lc']);
  assert.match(calls[0].args[5], /\/data\/billing\.json/);
  assert.match(calls[0].input, /"multi_tenant"/);
});

test('applyRequestedEntitlementProfile warns instead of failing for inferred local live-instance runs', async () => {
  const warnings = [];
  const result = await applyRequestedEntitlementProfile({
    env: {
      PULSE_E2E_SKIP_DOCKER: '1',
      PULSE_MULTI_TENANT_ENABLED: 'true',
    },
    logger: {
      log() {},
      warn(message) {
        warnings.push(message);
      },
    },
  });

  assert.equal(result.applied, false);
  assert.equal(result.reason, 'no_target_configured');
  assert.equal(warnings.length, 1);
});

test('applyRequestedEntitlementProfile fails for explicit live-instance runs without a write target', async () => {
  await assert.rejects(
    () =>
      applyRequestedEntitlementProfile({
        env: {
          PULSE_E2E_SKIP_DOCKER: '1',
          PULSE_E2E_ENTITLEMENT_PROFILE: 'multi-tenant',
        },
        logger: { log() {}, warn() {} },
      }),
    /no entitlement write target/i,
  );
});

test('signed offline activation does not inject a billing profile', async () => {
  const result = await applyRequestedEntitlementProfile({
    env: { PULSE_E2E_OFFLINE_ACTIVATION_KEY: 'ephemeral', PULSE_MULTI_TENANT_ENABLED: 'true' },
    run: () => { throw new Error('must not write billing state'); },
  });
  assert.deepEqual(result, { applied: false, reason: 'signed_offline_activation' });
});