Skip to content

Commit 6364db9

Browse files
committed
test: login-utils for entitlment change detection and validity expiry utils
1 parent 3e506c1 commit 6364db9

File tree

4 files changed

+639
-98
lines changed

4 files changed

+639
-98
lines changed

src/services/login-service.js

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ define(function (require, exports, module) {
2828
require("./promotions");
2929

3030
const Metrics = require("utils/Metrics");
31+
const LoginUtils = require("./login-utils");
3132

3233
const MS_IN_DAY = 10 * 24 * 60 * 60 * 1000;
3334
const TEN_MINUTES = 10 * 60 * 1000;
@@ -142,102 +143,6 @@ define(function (require, exports, module) {
142143
}
143144
}
144145

145-
/**
146-
* Check if any validTill time has expired
147-
*/
148-
function validTillExpired(entitlements, lastRecordedEntitlement) {
149-
if (!entitlements) {
150-
return null;
151-
}
152-
153-
const now = Date.now();
154-
155-
function isNewlyExpired(validTill, lastValidTill) {
156-
return (
157-
validTill &&
158-
validTill < now && // expired now
159-
(!lastValidTill || lastValidTill >= now) // but wasn't expired before
160-
);
161-
}
162-
163-
// Check plan validTill
164-
if (entitlements.plan) {
165-
const validTill = entitlements.plan.validTill;
166-
const lastValidTill = (lastRecordedEntitlement && lastRecordedEntitlement.plan)
167-
? lastRecordedEntitlement.plan.validTill
168-
: null;
169-
170-
if (isNewlyExpired(validTill, lastValidTill)) {
171-
return entitlements.plan.name || brackets.config.main_pro_plan;
172-
}
173-
}
174-
175-
// Check entitlements validTill
176-
if (entitlements.entitlements) {
177-
for (const key in entitlements.entitlements) {
178-
const entitlement = entitlements.entitlements[key];
179-
if (!entitlement) {
180-
continue;
181-
}
182-
183-
const validTill = entitlement.validTill;
184-
const lastValidTill = (lastRecordedEntitlement &&
185-
lastRecordedEntitlement.entitlements &&
186-
lastRecordedEntitlement.entitlements[key])
187-
? lastRecordedEntitlement.entitlements[key].validTill
188-
: null;
189-
190-
if (isNewlyExpired(validTill, lastValidTill)) {
191-
return key;
192-
}
193-
}
194-
}
195-
196-
return null;
197-
}
198-
199-
200-
/**
201-
* Check if entitlements have changed from last recorded state
202-
*/
203-
function haveEntitlementsChanged(current, last) {
204-
if (!last && !current) {
205-
return false;
206-
}
207-
if ((!last && current) || (!current && last)) {
208-
return true;
209-
}
210-
if ((!last.entitlements && current.entitlements) || (current.entitlements && !last.entitlements)) {
211-
return true;
212-
}
213-
214-
// Check paidSubscriber changes
215-
const currentPaidSub = current.plan && current.plan.paidSubscriber;
216-
const lastPaidSub = last.plan && last.plan.paidSubscriber;
217-
if (currentPaidSub !== lastPaidSub) {
218-
return true;
219-
}
220-
221-
// Check plan name changes
222-
const currentPlanName = current.plan && current.plan.name;
223-
const lastPlanName = last.plan && last.plan.name;
224-
if (currentPlanName !== lastPlanName) {
225-
return true;
226-
}
227-
228-
// Check entitlement activations
229-
if (current.entitlements && last.entitlements) {
230-
for (const key of Object.keys(current.entitlements)) {
231-
const currentActivated = current.entitlements[key] && current.entitlements[key].activated;
232-
const lastActivated = last.entitlements[key] && last.entitlements[key].activated;
233-
if (currentActivated !== lastActivated) {
234-
return true;
235-
}
236-
}
237-
}
238-
239-
return false;
240-
}
241146

242147
/**
243148
* Start the 10-minute interval timer for monitoring entitlements
@@ -248,8 +153,8 @@ define(function (require, exports, module) {
248153
const current = await getEffectiveEntitlements(false); // Get effective entitlements
249154

250155
// Check if we need to refresh
251-
const expiredPlanName = validTillExpired(current, lastRecordedState);
252-
const hasChanged = haveEntitlementsChanged(current, lastRecordedState);
156+
const expiredPlanName = LoginUtils.validTillExpired(current, lastRecordedState);
157+
const hasChanged = LoginUtils.haveEntitlementsChanged(current, lastRecordedState);
253158

254159
if (expiredPlanName || hasChanged) {
255160
console.log(`Entitlements monitor detected changes, Expired: ${expiredPlanName},` +

src/services/login-utils.js

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* GNU AGPL-3.0 License
3+
*
4+
* Copyright (c) 2021 - present core.ai . All rights reserved.
5+
*
6+
* This program is free software: you can redistribute it and/or modify it under
7+
* the terms of the GNU Affero General Public License as published by the Free
8+
* Software Foundation, either version 3 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11+
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
16+
*
17+
*/
18+
19+
/**
20+
* Login Service Utilities
21+
*
22+
* This module contains utility functions for login service operations,
23+
* including entitlements expiration checking and change detection.
24+
*/
25+
26+
define(function (require, exports, module) {
27+
28+
/**
29+
* Check if any validTill time has expired
30+
*
31+
* @param {Object|null} entitlements - Current entitlements object
32+
* @param {Object|null} lastRecordedEntitlement - Previously recorded entitlements
33+
* @returns {string|null} - Name of expired plan/entitlement or null if none expired
34+
*/
35+
function validTillExpired(entitlements, lastRecordedEntitlement) {
36+
if (!entitlements) {
37+
return null;
38+
}
39+
40+
const now = Date.now();
41+
42+
function isNewlyExpired(validTill, lastValidTill) {
43+
return (
44+
validTill &&
45+
validTill < now && // expired now
46+
(!lastValidTill || lastValidTill >= now) // but wasn't expired before
47+
);
48+
}
49+
50+
// Check plan validTill
51+
if (entitlements.plan) {
52+
const validTill = entitlements.plan.validTill;
53+
const lastValidTill = (lastRecordedEntitlement && lastRecordedEntitlement.plan)
54+
? lastRecordedEntitlement.plan.validTill
55+
: null;
56+
57+
if (isNewlyExpired(validTill, lastValidTill)) {
58+
return entitlements.plan.name || brackets.config.main_pro_plan;
59+
}
60+
}
61+
62+
// Check entitlements validTill
63+
if (entitlements.entitlements) {
64+
for (const key in entitlements.entitlements) {
65+
const entitlement = entitlements.entitlements[key];
66+
if (!entitlement) {
67+
continue;
68+
}
69+
70+
const validTill = entitlement.validTill;
71+
const lastValidTill = (lastRecordedEntitlement &&
72+
lastRecordedEntitlement.entitlements &&
73+
lastRecordedEntitlement.entitlements[key])
74+
? lastRecordedEntitlement.entitlements[key].validTill
75+
: null;
76+
77+
if (isNewlyExpired(validTill, lastValidTill)) {
78+
return key;
79+
}
80+
}
81+
}
82+
83+
return null;
84+
}
85+
86+
/**
87+
* Check if entitlements have changed from last recorded state
88+
*
89+
* @param {Object|null} current - Current entitlements object
90+
* @param {Object|null} last - Last recorded entitlements object
91+
* @returns {boolean} - True if entitlements have changed, false otherwise
92+
*/
93+
function haveEntitlementsChanged(current, last) {
94+
if (!last && !current) {
95+
return false;
96+
}
97+
if ((!last && current) || (!current && last)) {
98+
return true;
99+
}
100+
if ((!last.entitlements && current.entitlements) || (!current.entitlements && last.entitlements)) {
101+
return true;
102+
}
103+
104+
// Check paidSubscriber changes
105+
const currentPaidSub = current.plan && current.plan.paidSubscriber;
106+
const lastPaidSub = last.plan && last.plan.paidSubscriber;
107+
if (currentPaidSub !== lastPaidSub) {
108+
return true;
109+
}
110+
111+
// Check plan name changes
112+
const currentPlanName = current.plan && current.plan.name;
113+
const lastPlanName = last.plan && last.plan.name;
114+
if (currentPlanName !== lastPlanName) {
115+
return true;
116+
}
117+
118+
// Check entitlement activations
119+
if (current.entitlements && last.entitlements) {
120+
for (const key of Object.keys(current.entitlements)) {
121+
const currentActivated = current.entitlements[key] && current.entitlements[key].activated;
122+
const lastActivated = last.entitlements[key] && last.entitlements[key].activated;
123+
if (currentActivated !== lastActivated) {
124+
return true;
125+
}
126+
}
127+
}
128+
129+
return false;
130+
}
131+
132+
// Export functions
133+
exports.validTillExpired = validTillExpired;
134+
exports.haveEntitlementsChanged = haveEntitlementsChanged;
135+
});

test/UnitTestSuite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ define(function (require, exports, module) {
120120
require("spec/spacing-auto-detect-integ-test");
121121
require("spec/LocalizationUtils-test");
122122
require("spec/ScrollTrackHandler-integ-test");
123+
require("spec/login-utils-test");
123124
// Integrated extension tests
124125
require("spec/Extn-InAppNotifications-integ-test");
125126
require("spec/Extn-RemoteFileAdapter-integ-test");

0 commit comments

Comments
 (0)