Add period-based license keys alongside fixed expiry dates.
New keys default to validDays with countdown starting on first activation; existing expiresAtSec keys behave unchanged. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import {
|
||||
DEFAULT_VALID_DAYS,
|
||||
ensurePeriodActivated,
|
||||
enrichLicenseEntry,
|
||||
getEffectiveExpSec,
|
||||
isFixedKey,
|
||||
isPeriodKey,
|
||||
parseProductKeyExpiry,
|
||||
resolveTokenExp,
|
||||
SECONDS_PER_DAY,
|
||||
} from '../lib/licenseLogic.mjs';
|
||||
|
||||
void test('parseProductKeyExpiry defaults to period', () => {
|
||||
const r = parseProductKeyExpiry({});
|
||||
assert.equal(r.ok, true);
|
||||
assert.equal(r.validDays, DEFAULT_VALID_DAYS);
|
||||
});
|
||||
|
||||
void test('parseProductKeyExpiry rejects conflicting expiry fields', () => {
|
||||
const r = parseProductKeyExpiry({ expiresAtSec: 100, validDays: 30 });
|
||||
assert.equal(r.ok, false);
|
||||
assert.equal(r.error, 'conflicting_expiry');
|
||||
});
|
||||
|
||||
void test('ensurePeriodActivated sets exp once for period keys', () => {
|
||||
const pk = { validDays: 10 };
|
||||
const first = ensurePeriodActivated(pk, 1000);
|
||||
assert.equal(first.changed, true);
|
||||
assert.equal(first.exp, 1000 + 10 * SECONDS_PER_DAY);
|
||||
assert.equal(pk.activatedAtSec, 1000);
|
||||
|
||||
const second = ensurePeriodActivated(pk, 5000);
|
||||
assert.equal(second.changed, false);
|
||||
assert.equal(second.exp, first.exp);
|
||||
});
|
||||
|
||||
void test('resolveTokenExp for fixed and period keys', () => {
|
||||
const fixed = { expiresAtSec: 9999 };
|
||||
assert.equal(resolveTokenExp(fixed, 100), 9999);
|
||||
assert.ok(isFixedKey(fixed));
|
||||
|
||||
const period = { validDays: 7, licenseExpSec: 7000 };
|
||||
assert.equal(resolveTokenExp(period, 100), 7000);
|
||||
assert.ok(isPeriodKey(period));
|
||||
});
|
||||
|
||||
void test('enrichLicenseEntry exposes expiry metadata', () => {
|
||||
const fixed = enrichLicenseEntry(
|
||||
{ key: 'k', sub: 's', pid: 'p', maxDevices: 1, expiresAtSec: 123 },
|
||||
[],
|
||||
false,
|
||||
);
|
||||
assert.equal(fixed.expiryMode, 'fixed');
|
||||
assert.equal(fixed.effectiveExpSec, 123);
|
||||
|
||||
const period = enrichLicenseEntry(
|
||||
{ key: 'k', sub: 's', pid: 'p', maxDevices: 1, validDays: 30 },
|
||||
[],
|
||||
false,
|
||||
);
|
||||
assert.equal(period.expiryMode, 'period');
|
||||
assert.equal(period.effectiveExpSec, null);
|
||||
assert.equal(getEffectiveExpSec({ validDays: 30, licenseExpSec: 555 }), 555);
|
||||
});
|
||||
Reference in New Issue
Block a user