Skip to content

Commit 2a81d8e

Browse files
committed
fix: test suite init with new constants
1 parent f5e4da2 commit 2a81d8e

4 files changed

Lines changed: 75 additions & 9 deletions

File tree

src/index.html

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"\n\nYou will now be redirected to phcode.dev.");
7979
window.location = "https://phcode.dev";
8080
}
81-
if(window.electronAPI) {
81+
if(window.electronAppAPI) {
8282
window.__ELECTRON__ = true;
8383
}
8484
if(location.href.startsWith("tauri://") || location.href.startsWith('https://tauri.localhost')){
@@ -103,7 +103,7 @@
103103
// develop phoenix inside phoenix.
104104
// This will throw an error if the live preview iframe and parent are cross-origin which is case in phcode
105105
canAccessNativeShell = window.top.window.__TAURI__;
106-
canAccessNativeShell = window.top.window.electronAPI;
106+
canAccessNativeShell = window.top.window.electronAppAPI;
107107
canAccessNativeShell = true;
108108
} catch (e) {
109109
// Cross-origin, exception caught, isSameOrigin remains false
@@ -167,13 +167,14 @@
167167
}
168168
// fs special handling end
169169
}
170-
// linux electron api iframe injection for tests
170+
// electron api iframe injection for tests
171171
if(canAccessNativeShell && window.parent.window !== window && window.top.window.__ELECTRON__) {
172172
// This is only intended to be used for in tests where phoenix is loaded inside iframes.
173-
// this means that we are loaded in an iframe in the specrunner. Tauri doesnt expose tauri APIs
174-
// in its iframes, so we directly use the top most windows tauri api object for tests to run properly.
175-
console.warn("Phoenix is loaded in iframe, attempting to use electron APIs from window.top.electronAPI");
176-
window.electronAPI = window.top.window.electronAPI;
173+
// this means that we are loaded in an iframe in the specrunner. Electron doesnt expose APIs
174+
// in its iframes, so we directly use the top most windows electron api objects for tests to run properly.
175+
console.warn("Phoenix is loaded in iframe, attempting to use electron APIs from window.top");
176+
window.electronAppAPI = window.top.window.electronAppAPI;
177+
window.electronFSAPI = window.top.window.electronFSAPI;
177178
}
178179
if(window.__TAURI__ || window.__ELECTRON__) {
179180
window.__IS_NATIVE_SHELL__ = true;

src/phoenix/init_vfs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function _getNativeAssetUrl(fullFilePath) {
4848
}
4949
return null;
5050
}
51+
// todo electron
5152
return null;
5253
}
5354

test/SpecRunner.html

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
throw new Error(errorMessage);
4646
}
4747
console.warn('Make sure to run this following command before starting tests : npm run build ');
48+
if(window.electronAppAPI) {
49+
window.__ELECTRON__ = true;
50+
}
51+
if(window.__TAURI__ || window.__ELECTRON__) {
52+
window.__IS_NATIVE_SHELL__ = true;
53+
}
4854
if(window.__TAURI__) {
4955
window.testRunnerLogToConsole = function (...args) {
5056
const message = args.join(' ');
@@ -60,6 +66,17 @@
6066
}
6167
testRunnerLogToConsole(`Tauri test reporters attached.`);
6268
}
69+
if(window.__ELECTRON__) {
70+
window.testRunnerLogToConsole = function (...args) {
71+
const message = args.join(' ');
72+
window.electronAppAPI.consoleLog(message);
73+
};
74+
window.testRunnerErrorToConsole = function (...args) {
75+
const message = args.join(' ');
76+
window.electronAppAPI.consoleLog(`ERROR: ${message}`);
77+
}
78+
testRunnerLogToConsole(`Electron test reporters attached.`);
79+
}
6380

6481
// in playwright tests, testRunnerLogToConsole is injected by playwright runners
6582

@@ -143,6 +160,52 @@
143160
}
144161
setupTauriBootVars();
145162
}
163+
if(window.__ELECTRON__) {
164+
function setupElectronBootVars() {
165+
// this is used by storage.js (window.PhStore) to restore our persistent storage layer in electron.
166+
window._tauriStorageRestorePromise = window.electronFSAPI.appLocalDataDir()
167+
.then(appDataDir => {
168+
const sep = window.electronFSAPI.path.sep;
169+
const storagePath = `${appDataDir}${appDataDir.endsWith(sep) ? "" : sep}storageDB${sep}storageDBDump.json`;
170+
return window.electronFSAPI.fsReadFile(storagePath);
171+
})
172+
.then(data => {
173+
if(data && data.__fsError) {
174+
throw new Error(data.message);
175+
}
176+
return data;
177+
})
178+
.catch(err => {
179+
console.error("First boot detected or Failed to init storage from cache." +
180+
" If first boot, ignore this error", err);
181+
});
182+
const appNamePromise = window.electronAppAPI.getAppName();
183+
// for running tests, the user document dir is set to app data dir as we dont want to
184+
// corrupt user documents dir for tests
185+
const documentDirPromise = window.electronFSAPI.appLocalDataDir();
186+
const appLocalDirPromise = window.electronFSAPI.appLocalDataDir();
187+
const tempDirPromise = window.electronFSAPI.tempDir();
188+
window._tauriBootVars = {};
189+
const tauriBootStartTime = Date.now();
190+
window._tauriBootVarsPromise = Promise.all([appNamePromise, documentDirPromise,
191+
appLocalDirPromise, tempDirPromise])
192+
.then((results) => {
193+
window._tauriBootVars.appname = results[0];
194+
// For tests, documents dir is localAppDataDir/documents to keep user documents garbage free for tests
195+
window._tauriBootVars.documentDir = results[1];
196+
const sep = window.electronFSAPI.path.sep;
197+
if(!window._tauriBootVars.documentDir.endsWith(sep)){
198+
window._tauriBootVars.documentDir = window._tauriBootVars.documentDir + sep;
199+
}
200+
window._tauriBootVars.documentDir = `${window._tauriBootVars.documentDir}documents${sep}`;
201+
//Documents dir special case for tests
202+
window._tauriBootVars.appLocalDir = results[2];
203+
window._tauriBootVars.tempDir = results[3];
204+
window._tauriBootVars.bootstrapTime = Date.now() - tauriBootStartTime;
205+
});
206+
}
207+
setupElectronBootVars();
208+
}
146209
</script>
147210

148211
<!-- Import the phoenix browser virtual file system -->
@@ -211,6 +274,7 @@
211274
isDeskTop: !_mobileAndTabletCheck() && !_mobileCheck(),
212275
isChromeOS: /CrOS/.test(navigator.userAgent),
213276
isTauri: !!window.__TAURI__,
277+
isElectron: !!window.__ELECTRON__,
214278
mobile: {
215279
isAndroid: (navigator.userAgent.match(/Android/i) !== null),
216280
isIos: (navigator.userAgent.match(/iPhone|iPad|iPod/i) !== null),
@@ -290,7 +354,7 @@
290354
'https://create.phcode.dev': true
291355
}
292356
};
293-
window.Phoenix.isNativeApp = window.Phoenix.browser.isTauri;
357+
window.Phoenix.isNativeApp = window.__IS_NATIVE_SHELL__;
294358
window.Phoenix.TRUSTED_ORIGINS[location.origin] = true;
295359
Phoenix.isSupportedBrowser = Phoenix.isNativeApp ||
296360
(Phoenix.browser.isDeskTop && ("serviceWorker" in navigator));

test/spec/LiveDevelopmentMultiBrowser-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ define(function (require, exports, module) {
9292
// we have to popout a new window and cant use the embedded iframe for live preview integ tests
9393
// as Firefox sandbox prevents service worker access from nexted iframes.
9494
// In tauri, we use node server, so this limitation doesn't apply in tauri, and we stick to iframes.
95-
const useWindowInsteadOfIframe = (Phoenix.browser.desktop.isFirefox && !window.__TAURI__);
95+
const useWindowInsteadOfIframe = Phoenix.browser.desktop.isFirefox;
9696
testWindow = await SpecRunnerUtils.createTestWindowAndRun({
9797
forceReload: false, useWindowInsteadOfIframe
9898
});

0 commit comments

Comments
 (0)