obsidian-testing-framework 0.1.9 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/electron-singleton.d.ts +2 -0
- package/lib/electron-singleton.js +17 -0
- package/lib/index.d.ts +1 -1
- package/lib/index.js +21 -45
- package/lib/internal-util.d.ts +4 -0
- package/lib/internal-util.js +39 -0
- package/package.json +1 -1
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { _electron as electron } from "playwright";
|
|
2
|
+
import { addConsoleListener } from "./internal-util";
|
|
3
|
+
const singleton = {
|
|
4
|
+
app: null,
|
|
5
|
+
};
|
|
6
|
+
export async function getOrCreateApp(obsidianPath, uriArg) {
|
|
7
|
+
if (singleton.app) {
|
|
8
|
+
return singleton.app;
|
|
9
|
+
}
|
|
10
|
+
const app = await electron.launch({
|
|
11
|
+
timeout: 60000,
|
|
12
|
+
args: [obsidianPath, uriArg].filter((a) => !!a),
|
|
13
|
+
});
|
|
14
|
+
addConsoleListener(app);
|
|
15
|
+
singleton.app = app;
|
|
16
|
+
return app;
|
|
17
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export interface ObsidianTestingConfig {
|
|
2
2
|
vault?: string;
|
|
3
3
|
obsidianPath?: string;
|
|
4
|
+
experimentalUseSingleton?: boolean;
|
|
4
5
|
}
|
|
5
|
-
export declare function getExe(obsidianPath?: string): string;
|
|
6
6
|
export declare const test: import("vitest").TestAPI<{
|
|
7
7
|
page: import("playwright-core").Page;
|
|
8
8
|
obsidian: ObsidianTestingConfig;
|
package/lib/index.js
CHANGED
|
@@ -4,38 +4,8 @@ import path from "path";
|
|
|
4
4
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
5
5
|
import { pageUtils, waitForIndexingComplete } from "./util.js";
|
|
6
6
|
import { randomBytes } from "crypto";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (obsidianPath) {
|
|
10
|
-
return path.join(obsidianPath, "Resources", "app.asar");
|
|
11
|
-
}
|
|
12
|
-
if (process.platform == "win32") {
|
|
13
|
-
const p = path.join(process.env.LOCALAPPDATA, "Obsidian", "Resources", "app.asar");
|
|
14
|
-
if (existsSync(p)) {
|
|
15
|
-
return p;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
const possibleDirs = [
|
|
19
|
-
"/opt/Obsidian",
|
|
20
|
-
"/usr/lib/Obsidian",
|
|
21
|
-
"/opt/obsidian",
|
|
22
|
-
"/usr/lib/obsidian",
|
|
23
|
-
"/var/lib/flatpak/app/md.obsidian.Obsidian/current/active/files",
|
|
24
|
-
"/snap/obsidian/current",
|
|
25
|
-
];
|
|
26
|
-
for (let i = 0; i < possibleDirs.length; i++) {
|
|
27
|
-
if (existsSync(possibleDirs[i])) {
|
|
28
|
-
// console.log(execSync(`ls -l ${possibleDirs[i]}`).toString());
|
|
29
|
-
return path.join(possibleDirs[i], "resources", "app.asar");
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return "";
|
|
33
|
-
}
|
|
34
|
-
function checkToy() {
|
|
35
|
-
if (process.platform == "darwin") {
|
|
36
|
-
throw new Error("use a non-toy operating system, dumbass");
|
|
37
|
-
}
|
|
38
|
-
}
|
|
7
|
+
import { addConsoleListener, checkToy, getExe } from "./internal-util.js";
|
|
8
|
+
import { getOrCreateApp } from "./electron-singleton.js";
|
|
39
9
|
function generateVaultConfig(vault) {
|
|
40
10
|
const vaultHash = randomBytes(8).toString("hex").toLocaleLowerCase();
|
|
41
11
|
let configLocation;
|
|
@@ -66,7 +36,7 @@ function generateVaultConfig(vault) {
|
|
|
66
36
|
return vaultHash;
|
|
67
37
|
}
|
|
68
38
|
else {
|
|
69
|
-
return Object.entries(json.vaults).find(a => a[1].path === vault)[0];
|
|
39
|
+
return Object.entries(json.vaults).find((a) => a[1].path === vault)[0];
|
|
70
40
|
}
|
|
71
41
|
}
|
|
72
42
|
// @ts-ignore some error about a string type now having `undefined` as part of it's union
|
|
@@ -74,7 +44,7 @@ export const test = base.extend({
|
|
|
74
44
|
electronApp: [
|
|
75
45
|
async ({ obsidian }, run) => {
|
|
76
46
|
console.log("obsidian", obsidian);
|
|
77
|
-
const { obsidianPath = undefined } = obsidian ?? {};
|
|
47
|
+
const { obsidianPath = undefined, experimentalUseSingleton = false } = obsidian ?? {};
|
|
78
48
|
const vault = inject("vault");
|
|
79
49
|
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
|
|
80
50
|
console.log("asar located at:", getExe(obsidianPath));
|
|
@@ -85,17 +55,22 @@ export const test = base.extend({
|
|
|
85
55
|
uriArg = `obsidian://open?vault=${encodeURIComponent(id)}`;
|
|
86
56
|
}
|
|
87
57
|
}
|
|
88
|
-
const electronApp =
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
58
|
+
const electronApp = experimentalUseSingleton
|
|
59
|
+
? await getOrCreateApp(getExe(obsidianPath), !!uriArg ? uriArg : undefined)
|
|
60
|
+
: await electron.launch({
|
|
61
|
+
timeout: 60000,
|
|
62
|
+
args: [getExe(obsidianPath), uriArg].filter((a) => !!a),
|
|
63
|
+
});
|
|
64
|
+
if (!experimentalUseSingleton) {
|
|
65
|
+
addConsoleListener(electronApp);
|
|
66
|
+
}
|
|
95
67
|
await electronApp.firstWindow();
|
|
96
68
|
await run(electronApp);
|
|
97
|
-
|
|
98
|
-
|
|
69
|
+
if (!experimentalUseSingleton) {
|
|
70
|
+
await electronApp.close();
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
{ auto: true },
|
|
99
74
|
],
|
|
100
75
|
page: [
|
|
101
76
|
async ({ electronApp }, run) => {
|
|
@@ -112,7 +87,7 @@ export const test = base.extend({
|
|
|
112
87
|
for (let fn of Object.entries(pageUtils)) {
|
|
113
88
|
await page.exposeFunction(fn[0], fn[1]);
|
|
114
89
|
}
|
|
115
|
-
page.on("pageerror", exc => {
|
|
90
|
+
page.on("pageerror", (exc) => {
|
|
116
91
|
console.error("EXCEPTION");
|
|
117
92
|
console.error(exc);
|
|
118
93
|
});
|
|
@@ -120,6 +95,7 @@ export const test = base.extend({
|
|
|
120
95
|
console.log(...(await Promise.all(msg.args().map((a) => a.jsonValue()))));
|
|
121
96
|
});
|
|
122
97
|
await run(page);
|
|
123
|
-
},
|
|
98
|
+
},
|
|
99
|
+
{ auto: true },
|
|
124
100
|
],
|
|
125
101
|
});
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
import { existsSync } from "fs";
|
|
3
|
+
export function checkToy() {
|
|
4
|
+
if (process.platform == "darwin") {
|
|
5
|
+
throw new Error("use a non-toy operating system, dumbass");
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
export function getExe(obsidianPath) {
|
|
9
|
+
checkToy();
|
|
10
|
+
if (obsidianPath) {
|
|
11
|
+
return path.join(obsidianPath, "Resources", "app.asar");
|
|
12
|
+
}
|
|
13
|
+
if (process.platform == "win32") {
|
|
14
|
+
const p = path.join(process.env.LOCALAPPDATA, "Obsidian", "Resources", "app.asar");
|
|
15
|
+
if (existsSync(p)) {
|
|
16
|
+
return p;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
const possibleDirs = [
|
|
20
|
+
"/opt/Obsidian",
|
|
21
|
+
"/usr/lib/Obsidian",
|
|
22
|
+
"/opt/obsidian",
|
|
23
|
+
"/usr/lib/obsidian",
|
|
24
|
+
"/var/lib/flatpak/app/md.obsidian.Obsidian/current/active/files",
|
|
25
|
+
"/snap/obsidian/current",
|
|
26
|
+
];
|
|
27
|
+
for (let i = 0; i < possibleDirs.length; i++) {
|
|
28
|
+
if (existsSync(possibleDirs[i])) {
|
|
29
|
+
// console.log(execSync(`ls -l ${possibleDirs[i]}`).toString());
|
|
30
|
+
return path.join(possibleDirs[i], "resources", "app.asar");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return "";
|
|
34
|
+
}
|
|
35
|
+
export function addConsoleListener(app) {
|
|
36
|
+
app.on("console", async (msg) => {
|
|
37
|
+
console.log(...(await Promise.all(msg.args().map((a) => a.jsonValue()))));
|
|
38
|
+
});
|
|
39
|
+
}
|