Skip to content

Commit fcc163b

Browse files
committed
test: pro promotions integ tests for non logged in case
1 parent 7dc2a5a commit fcc163b

File tree

6 files changed

+391
-11
lines changed

6 files changed

+391
-11
lines changed

src/services/login-browser.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ define(function (require, exports, module) {
100100
const ERR_INVALID = "invalid";
101101
const ERR_NOT_LOGGED_IN = "not_logged_in";
102102

103+
// save a copy of window.fetch so that extensions wont tamper with it.
104+
let fetchFn = window.fetch;
105+
103106
/**
104107
* Resolve browser session using cookies
105108
* @return {Promise<Object>} A promise resolving to user profile or error object
@@ -110,7 +113,7 @@ define(function (require, exports, module) {
110113
return {err: ERR_RETRY_LATER};
111114
}
112115
try {
113-
const response = await fetch(resolveURL, {
116+
const response = await fetchFn(resolveURL, {
114117
method: 'GET',
115118
credentials: 'include', // Include cookies
116119
headers: {
@@ -316,7 +319,7 @@ define(function (require, exports, module) {
316319
async function signOutBrowser() {
317320
const logoutURL = `${_getAccountBaseURL()}/signOut`;
318321
try {
319-
const response = await fetch(logoutURL, {
322+
const response = await fetchFn(logoutURL, {
320323
method: 'POST',
321324
credentials: 'include', // Include cookies
322325
headers: {

src/services/login-desktop.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ define(function (require, exports, module) {
4646
let userProfile = null;
4747
let isLoggedInUser = false;
4848

49+
// save a copy of window.fetch so that extensions wont tamper with it.
50+
let fetchFn = window.fetch;
51+
4952
// just used as trigger to notify different windows about user profile changes
5053
const PREF_USER_PROFILE_VERSION = "userProfileVersion";
5154

@@ -98,7 +101,7 @@ define(function (require, exports, module) {
98101
return {err: ERR_RETRY_LATER};
99102
}
100103
try {
101-
const response = await fetch(resolveURL);
104+
const response = await fetchFn(resolveURL);
102105
if (response.status === 400 || response.status === 404) {
103106
// 404 api key not found and 400 Bad Request, eg: verification code mismatch
104107
return {err: ERR_INVALID};
@@ -192,7 +195,7 @@ define(function (require, exports, module) {
192195
const resolveURL = `${Phoenix.config.account_url}getAppAuthSession?autoAuthPort=${authPortURL}&appName=${appName}`;
193196
// {"isSuccess":true,"appSessionID":"a uuid...","validationCode":"SWXP07"}
194197
try {
195-
const response = await fetch(resolveURL);
198+
const response = await fetchFn(resolveURL);
196199
if (response.ok) {
197200
const {appSessionID, validationCode} = await response.json();
198201
if(!appSessionID || !validationCode) {
@@ -345,7 +348,7 @@ define(function (require, exports, module) {
345348
appSessionID: userProfile.apiKey
346349
};
347350

348-
const response = await fetch(resolveURL, {
351+
const response = await fetchFn(resolveURL, {
349352
method: 'POST',
350353
headers: {
351354
'Content-Type': 'application/json'

src/services/login-service.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ define(function (require, exports, module) {
3333
const MS_IN_DAY = 10 * 24 * 60 * 60 * 1000;
3434
const TEN_MINUTES = 10 * 60 * 1000;
3535

36+
// save a copy of window.fetch so that extensions wont tamper with it.
37+
let fetchFn = window.fetch;
38+
3639
const KernalModeTrust = window.KernalModeTrust;
3740
if(!KernalModeTrust){
3841
// integrated extensions will have access to kernal mode, but not external extensions
@@ -107,7 +110,7 @@ define(function (require, exports, module) {
107110
fetchOptions.credentials = 'include';
108111
}
109112

110-
const response = await fetch(url, fetchOptions);
113+
const response = await fetchFn(url, fetchOptions);
111114

112115
if (response.ok) {
113116
const result = await response.json();

src/services/promotions.js

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* Provides loginless pro trials
2626
*
2727
* - First install: 30-day trial on first usage
28-
* - Subsequent versions: 3-day trial (or remaining from 30-day if still valid)
28+
* - Subsequent versions: 7-day trial (or remaining from 30-day if still valid)
2929
* - Older versions: No new trial, but existing 30-day trial remains valid
3030
*/
3131

@@ -36,6 +36,7 @@ define(function (require, exports, module) {
3636
semver = require("thirdparty/semver.browser"),
3737
ProDialogs = require("./pro-dialogs");
3838

39+
let dateNowFn = Date.now;
3940
const KernalModeTrust = window.KernalModeTrust;
4041
if (!KernalModeTrust) {
4142
throw new Error("Promotions service requires access to KernalModeTrust. Cannot boot without trust ring");
@@ -47,7 +48,7 @@ define(function (require, exports, module) {
4748
const EVENT_PRO_UPGRADE_ON_INSTALL = "pro_upgrade_on_install";
4849
const TRIAL_POLL_MS = 10 * 1000; // 10 seconds after start, we assign a free trial if possible
4950
const FIRST_INSTALL_TRIAL_DAYS = 30;
50-
const SUBSEQUENT_TRIAL_DAYS = 3;
51+
const SUBSEQUENT_TRIAL_DAYS = 7;
5152
const MS_PER_DAY = 24 * 60 * 60 * 1000;
5253

5354
/**
@@ -151,7 +152,7 @@ define(function (require, exports, module) {
151152
* Calculate remaining trial days from end date
152153
*/
153154
function _calculateRemainingTrialDays(existingTrialData) {
154-
const now = Date.now();
155+
const now = dateNowFn();
155156
const trialEndDate = existingTrialData.endDate;
156157

157158
// Calculate days remaining until trial ends
@@ -209,7 +210,7 @@ define(function (require, exports, module) {
209210

210211
let trialDays = FIRST_INSTALL_TRIAL_DAYS;
211212
let endDate;
212-
const now = Date.now();
213+
const now = dateNowFn();
213214
let metricString = `${currentVersion.replaceAll(".", "_")}`; // 3.1.0 -> 3_1_0
214215

215216
if (existingTrialData) {
@@ -250,7 +251,7 @@ define(function (require, exports, module) {
250251
endDate = existingTrialData.endDate;
251252
metricString = `nD_${metricString}_upgrade`;
252253
} else {
253-
// Newer version with shorter existing trial - give 3 days
254+
// Newer version with shorter existing trial - give 7 days
254255
console.log(`Newer version - granting ${SUBSEQUENT_TRIAL_DAYS} days trial`);
255256
trialDays = SUBSEQUENT_TRIAL_DAYS;
256257
endDate = now + (trialDays * MS_PER_DAY);
@@ -328,5 +329,44 @@ define(function (require, exports, module) {
328329
LoginService.getProTrialDaysRemaining = getProTrialDaysRemaining;
329330
LoginService.EVENT_PRO_UPGRADE_ON_INSTALL = EVENT_PRO_UPGRADE_ON_INSTALL;
330331

332+
// Test-only exports for integration testing
333+
if (Phoenix.isTestWindow) {
334+
window._test_login_exports = {
335+
LoginService: LoginService,
336+
ProDialogs: ProDialogs,
337+
_getTrialData: _getTrialData,
338+
_setTrialData: _setTrialData,
339+
_cleanTrialData: async function() {
340+
try {
341+
if (Phoenix.isNativeApp) {
342+
await KernalModeTrust.removeCredential(KernalModeTrust.CRED_KEY_ENTITLEMENTS);
343+
} else {
344+
const filePath = Phoenix.app.getApplicationSupportDirectory() + "entitlements_granted.json";
345+
return new Promise((resolve) => {
346+
window.fs.unlink(filePath, () => {
347+
// Always resolve, ignore errors since file might not exist
348+
resolve();
349+
});
350+
});
351+
}
352+
} catch (error) {
353+
// Ignore cleanup errors
354+
console.log("Trial data cleanup completed (ignoring errors)");
355+
}
356+
},
357+
activateProTrial: activateProTrial,
358+
getProTrialDaysRemaining: getProTrialDaysRemaining,
359+
setDateNowFn: function _setDdateNowFn(fn) {
360+
dateNowFn = fn;
361+
},
362+
EVENT_PRO_UPGRADE_ON_INSTALL: EVENT_PRO_UPGRADE_ON_INSTALL,
363+
TRIAL_CONSTANTS: {
364+
FIRST_INSTALL_TRIAL_DAYS,
365+
SUBSEQUENT_TRIAL_DAYS,
366+
MS_PER_DAY
367+
}
368+
};
369+
}
370+
331371
// no public exports to prevent extension tampering
332372
});

test/UnitTestSuite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ define(function (require, exports, module) {
118118
require("spec/TaskManager-integ-test");
119119
require("spec/Generic-integ-test");
120120
require("spec/spacing-auto-detect-integ-test");
121+
require("spec/promotions-integ-test");
121122
require("spec/LocalizationUtils-test");
122123
require("spec/ScrollTrackHandler-integ-test");
123124
require("spec/login-utils-test");

0 commit comments

Comments
 (0)