mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-10 10:49:35 +00:00
fix(licensing): resolve Admiral variant detection and lifetime license handling (#376)
* fix(licensing): resolve Admiral variant detection and lifetime license handling The Lemon Squeezy variant name for Admiral licenses contains "Admiral" (not "Team"), but getVariant() only checked for "team" and "personal". This caused Admiral licenses to be misidentified as Skipper, locking all Admiral-exclusive features. - Map "admiral" variant names to internal "team" value, "skipper" to "personal" - Add isLifetime field to LicenseInfo API response - Hide "Manage Subscription" button for lifetime licenses (no billing portal) - Show "Duration: Lifetime" instead of empty renewal date - Hide upgrade cards for active Admiral users - Add 23 unit tests covering variant resolution, tier computation, and lifetime detection - Add troubleshooting entries for wrong tier label, locked features, and billing portal errors * fix(licensing): address code review findings - Fix nested ternary in LicenseSection JSX; restore conditional rendering to avoid showing an empty "N/A" row for non-subscription states - Clean up test file: use shared svc variable, remove redundant comments, add trialDaysRemaining assertions, rename describe block
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
/**
|
||||
* Tests for LicenseService: variant resolution, tier computation, lifetime detection,
|
||||
* and getLicenseInfo() output across all license states.
|
||||
*/
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
||||
import { setupTestDb, cleanupTestDb } from './helpers/setupTestDb';
|
||||
|
||||
let tmpDir: string;
|
||||
let svc: import('../services/LicenseService').LicenseService;
|
||||
let DatabaseService: typeof import('../services/DatabaseService').DatabaseService;
|
||||
|
||||
beforeAll(async () => {
|
||||
tmpDir = await setupTestDb();
|
||||
const licMod = await import('../services/LicenseService');
|
||||
svc = licMod.LicenseService.getInstance();
|
||||
({ DatabaseService } = await import('../services/DatabaseService'));
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
cleanupTestDb(tmpDir);
|
||||
});
|
||||
|
||||
function setLicenseState(overrides: Record<string, string>) {
|
||||
const db = DatabaseService.getInstance();
|
||||
const keys = [
|
||||
'license_status', 'license_key', 'license_valid_until',
|
||||
'license_last_validated', 'license_customer_name',
|
||||
'license_product_name', 'license_variant_name',
|
||||
'billing_portal_url', 'billing_portal_expires',
|
||||
];
|
||||
for (const key of keys) {
|
||||
db.setSystemState(key, '');
|
||||
}
|
||||
for (const [key, value] of Object.entries(overrides)) {
|
||||
db.setSystemState(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
describe('LicenseService.getVariant()', () => {
|
||||
it('returns "personal" for trial licenses', () => {
|
||||
setLicenseState({ license_status: 'trial' });
|
||||
expect(svc.getVariant()).toBe('personal');
|
||||
});
|
||||
|
||||
it('returns null when no variant name is stored', () => {
|
||||
setLicenseState({ license_status: 'active' });
|
||||
expect(svc.getVariant()).toBeNull();
|
||||
});
|
||||
|
||||
it('maps "Team" variant name to "team"', () => {
|
||||
setLicenseState({ license_status: 'active', license_variant_name: 'Team' });
|
||||
expect(svc.getVariant()).toBe('team');
|
||||
});
|
||||
|
||||
it('maps "Personal" variant name to "personal"', () => {
|
||||
setLicenseState({ license_status: 'active', license_variant_name: 'Personal' });
|
||||
expect(svc.getVariant()).toBe('personal');
|
||||
});
|
||||
|
||||
it('maps "Admiral" variant name to "team"', () => {
|
||||
setLicenseState({ license_status: 'active', license_variant_name: 'Admiral' });
|
||||
expect(svc.getVariant()).toBe('team');
|
||||
});
|
||||
|
||||
it('maps "Admiral Lifetime" variant name to "team"', () => {
|
||||
setLicenseState({ license_status: 'active', license_variant_name: 'Admiral Lifetime' });
|
||||
expect(svc.getVariant()).toBe('team');
|
||||
});
|
||||
|
||||
it('maps "Skipper" variant name to "personal"', () => {
|
||||
setLicenseState({ license_status: 'active', license_variant_name: 'Skipper' });
|
||||
expect(svc.getVariant()).toBe('personal');
|
||||
});
|
||||
|
||||
it('maps "Skipper Lifetime" variant name to "personal"', () => {
|
||||
setLicenseState({ license_status: 'active', license_variant_name: 'Skipper Lifetime' });
|
||||
expect(svc.getVariant()).toBe('personal');
|
||||
});
|
||||
|
||||
it('defaults unknown variant names to "personal"', () => {
|
||||
setLicenseState({ license_status: 'active', license_variant_name: 'Unknown Variant' });
|
||||
expect(svc.getVariant()).toBe('personal');
|
||||
});
|
||||
});
|
||||
|
||||
describe('LicenseService.getTier()', () => {
|
||||
it('returns "community" when no status is set', () => {
|
||||
setLicenseState({});
|
||||
// initialize() sets trial on first boot; override to test the empty-status path
|
||||
DatabaseService.getInstance().setSystemState('license_status', '');
|
||||
expect(svc.getTier()).toBe('community');
|
||||
});
|
||||
|
||||
it('returns "community" for community status', () => {
|
||||
setLicenseState({ license_status: 'community' });
|
||||
expect(svc.getTier()).toBe('community');
|
||||
});
|
||||
|
||||
it('returns "community" for expired status', () => {
|
||||
setLicenseState({ license_status: 'expired' });
|
||||
expect(svc.getTier()).toBe('community');
|
||||
});
|
||||
|
||||
it('returns "community" for disabled status', () => {
|
||||
setLicenseState({ license_status: 'disabled' });
|
||||
expect(svc.getTier()).toBe('community');
|
||||
});
|
||||
|
||||
it('returns "paid" for active status with valid license', () => {
|
||||
setLicenseState({
|
||||
license_status: 'active',
|
||||
license_last_validated: Date.now().toString(),
|
||||
});
|
||||
expect(svc.getTier()).toBe('paid');
|
||||
});
|
||||
|
||||
it('returns "paid" for active trial', () => {
|
||||
const future = new Date();
|
||||
future.setDate(future.getDate() + 7);
|
||||
setLicenseState({
|
||||
license_status: 'trial',
|
||||
license_valid_until: future.toISOString(),
|
||||
});
|
||||
expect(svc.getTier()).toBe('paid');
|
||||
});
|
||||
|
||||
it('returns "community" for expired trial', () => {
|
||||
const past = new Date();
|
||||
past.setDate(past.getDate() - 1);
|
||||
setLicenseState({
|
||||
license_status: 'trial',
|
||||
license_valid_until: past.toISOString(),
|
||||
});
|
||||
expect(svc.getTier()).toBe('community');
|
||||
});
|
||||
|
||||
it('returns "paid" for lifetime license (no expiry)', () => {
|
||||
setLicenseState({
|
||||
license_status: 'active',
|
||||
license_key: 'test-key-1234',
|
||||
license_last_validated: Date.now().toString(),
|
||||
});
|
||||
expect(svc.getTier()).toBe('paid');
|
||||
});
|
||||
});
|
||||
|
||||
describe('LicenseService.getLicenseInfo() - isLifetime', () => {
|
||||
it('sets isLifetime=true for active license with key and no expiry', () => {
|
||||
setLicenseState({
|
||||
license_status: 'active',
|
||||
license_key: 'test-key-1234',
|
||||
license_last_validated: Date.now().toString(),
|
||||
});
|
||||
const info = svc.getLicenseInfo();
|
||||
expect(info.isLifetime).toBe(true);
|
||||
expect(info.trialDaysRemaining).toBeNull();
|
||||
});
|
||||
|
||||
it('sets isLifetime=false for active subscription with expiry', () => {
|
||||
const future = new Date();
|
||||
future.setDate(future.getDate() + 30);
|
||||
setLicenseState({
|
||||
license_status: 'active',
|
||||
license_key: 'test-key-1234',
|
||||
license_valid_until: future.toISOString(),
|
||||
license_last_validated: Date.now().toString(),
|
||||
});
|
||||
const info = svc.getLicenseInfo();
|
||||
expect(info.isLifetime).toBe(false);
|
||||
expect(info.trialDaysRemaining).toBeNull();
|
||||
});
|
||||
|
||||
it('sets isLifetime=false for trial licenses', () => {
|
||||
const future = new Date();
|
||||
future.setDate(future.getDate() + 14);
|
||||
setLicenseState({
|
||||
license_status: 'trial',
|
||||
license_valid_until: future.toISOString(),
|
||||
});
|
||||
const info = svc.getLicenseInfo();
|
||||
expect(info.isLifetime).toBe(false);
|
||||
expect(info.trialDaysRemaining).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('sets isLifetime=false for community status', () => {
|
||||
setLicenseState({ license_status: 'community' });
|
||||
const info = svc.getLicenseInfo();
|
||||
expect(info.isLifetime).toBe(false);
|
||||
expect(info.trialDaysRemaining).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('LicenseService.getLicenseInfo() - full scenarios', () => {
|
||||
it('returns correct info for an Admiral lifetime license', () => {
|
||||
setLicenseState({
|
||||
license_status: 'active',
|
||||
license_key: 'ABCD-EFGH-IJKL-MN5D',
|
||||
license_variant_name: 'Admiral Lifetime',
|
||||
license_customer_name: 'Test User',
|
||||
license_product_name: 'Sencho Admiral',
|
||||
license_last_validated: Date.now().toString(),
|
||||
});
|
||||
const info = svc.getLicenseInfo();
|
||||
expect(info.tier).toBe('paid');
|
||||
expect(info.status).toBe('active');
|
||||
expect(info.variant).toBe('team');
|
||||
expect(info.isLifetime).toBe(true);
|
||||
expect(info.trialDaysRemaining).toBeNull();
|
||||
expect(info.customerName).toBe('Test User');
|
||||
expect(info.productName).toBe('Sencho Admiral');
|
||||
expect(info.maskedKey).toBe('****-****-****-MN5D');
|
||||
});
|
||||
|
||||
it('returns correct info for a Skipper subscription', () => {
|
||||
const future = new Date();
|
||||
future.setDate(future.getDate() + 30);
|
||||
setLicenseState({
|
||||
license_status: 'active',
|
||||
license_key: 'ABCD-EFGH-IJKL-SK5D',
|
||||
license_variant_name: 'Skipper Monthly',
|
||||
license_customer_name: 'Another User',
|
||||
license_product_name: 'Sencho Skipper',
|
||||
license_valid_until: future.toISOString(),
|
||||
license_last_validated: Date.now().toString(),
|
||||
});
|
||||
const info = svc.getLicenseInfo();
|
||||
expect(info.tier).toBe('paid');
|
||||
expect(info.status).toBe('active');
|
||||
expect(info.variant).toBe('personal');
|
||||
expect(info.isLifetime).toBe(false);
|
||||
expect(info.trialDaysRemaining).toBeNull();
|
||||
expect(info.customerName).toBe('Another User');
|
||||
});
|
||||
});
|
||||
@@ -33,6 +33,7 @@ export interface LicenseInfo {
|
||||
trialDaysRemaining: number | null;
|
||||
instanceId: string;
|
||||
portalUrl: string | null;
|
||||
isLifetime: boolean;
|
||||
}
|
||||
|
||||
/** Seat limits per variant. null = unlimited. */
|
||||
@@ -194,7 +195,9 @@ export class LicenseService {
|
||||
|
||||
/**
|
||||
* Get the license variant (personal or team) from stored metadata.
|
||||
* Trial licenses default to "personal" - Admiral features require an Admiral license.
|
||||
* Trial licenses default to "personal"; Admiral features require an Admiral license.
|
||||
* Maps Lemon Squeezy variant names (which may use brand names like "Admiral"
|
||||
* or "Skipper") to the internal 'team' or 'personal' values.
|
||||
*/
|
||||
public getVariant(): LicenseVariant {
|
||||
const db = DatabaseService.getInstance();
|
||||
@@ -203,8 +206,8 @@ export class LicenseService {
|
||||
const variantName = db.getSystemState('license_variant_name');
|
||||
if (!variantName) return null;
|
||||
const lower = variantName.toLowerCase();
|
||||
if (lower.includes('team')) return 'team';
|
||||
if (lower.includes('personal')) return 'personal';
|
||||
if (lower.includes('team') || lower.includes('admiral')) return 'team';
|
||||
if (lower.includes('personal') || lower.includes('skipper')) return 'personal';
|
||||
return 'personal'; // default activated licenses to personal
|
||||
}
|
||||
|
||||
@@ -233,6 +236,9 @@ export class LicenseService {
|
||||
trialDaysRemaining = Math.max(0, Math.ceil(remaining));
|
||||
}
|
||||
|
||||
// Lifetime license: active with a stored key but no expiry date
|
||||
const isLifetime = status === 'active' && !!key && !validUntil;
|
||||
|
||||
return {
|
||||
tier: this.getTier(),
|
||||
status,
|
||||
@@ -244,6 +250,7 @@ export class LicenseService {
|
||||
trialDaysRemaining,
|
||||
instanceId,
|
||||
portalUrl: db.getSystemState('billing_portal_url') || db.getSystemState('customer_portal_url') || null,
|
||||
isLifetime,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user