obsidian-testing-framework 0.2.4 → 0.3.0

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.
@@ -1,17 +1,21 @@
1
1
  import { _electron as electron } from "playwright";
2
2
  import { addConsoleListener } from "./internal-util.js";
3
+ import { Mutex } from "async-mutex";
3
4
  const singleton = {
4
5
  app: null,
5
6
  };
7
+ const mutex = new Mutex();
6
8
  export async function getOrCreateApp(obsidianPath, uriArg) {
7
9
  if (singleton.app) {
8
10
  return singleton.app;
9
11
  }
12
+ const release = await mutex.acquire();
10
13
  const app = await electron.launch({
11
14
  timeout: 60000,
12
15
  args: [obsidianPath, uriArg].filter((a) => !!a),
13
16
  });
14
17
  addConsoleListener(app);
15
18
  singleton.app = app;
19
+ await release();
16
20
  return app;
17
21
  }
package/lib/fixtures.d.ts CHANGED
@@ -6,4 +6,5 @@ export interface ObsidianTestFixtures {
6
6
  page: Page;
7
7
  obsidian: ObsidianTestingConfig;
8
8
  appHandle: JSHandle<App>;
9
+ vaultPoolDir: string;
9
10
  }
package/lib/index.d.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  export interface ObsidianTestingConfig {
2
2
  vault?: string;
3
3
  obsidianPath?: string;
4
- experimentalUseSingleton?: boolean;
4
+ concurrent?: false | "copy" | "symlink";
5
5
  }
6
6
  export declare const test: import("vitest").TestAPI<{
7
7
  page: import("playwright-core").Page;
8
8
  obsidian: ObsidianTestingConfig;
9
9
  electronApp: import("playwright-core").ElectronApplication;
10
10
  appHandle: import("playwright-core").JSHandle<import("obsidian").App>;
11
+ vaultPoolDir: string;
11
12
  }>;
package/lib/index.js CHANGED
@@ -1,15 +1,13 @@
1
1
  import { test as base, inject } from "vitest";
2
2
  import { _electron as electron } from "playwright";
3
3
  import path from "path";
4
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
4
+ import { cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmdirSync, writeFileSync, } from "fs";
5
5
  import { pageUtils, waitForIndexingComplete } from "./util.js";
6
6
  import { randomBytes } from "crypto";
7
7
  import { addConsoleListener, checkToy, getExe } from "./internal-util.js";
8
- import { getOrCreateApp } from "./electron-singleton.js";
9
- function generateVaultConfig(vault) {
10
- const vaultHash = randomBytes(8).toString("hex").toLocaleLowerCase();
8
+ import symlinkDir from "symlink-dir";
9
+ function findConfig() {
11
10
  let configLocation;
12
- console.log("vault is", vault, existsSync(vault));
13
11
  checkToy();
14
12
  if (process.platform == "win32") {
15
13
  configLocation = path.join(`${process.env.APPDATA}`, "Obsidian");
@@ -22,13 +20,26 @@ function generateVaultConfig(vault) {
22
20
  catch (e) { }
23
21
  }
24
22
  const obsidianConfigFile = path.join(configLocation, "obsidian.json");
23
+ return obsidianConfigFile;
24
+ }
25
+ function generateVaultConfig(vault, concurrent, vaultPoolDir) {
26
+ const vaultHash = randomBytes(8).toString("hex").toLocaleLowerCase();
27
+ let configLocation;
28
+ console.log("vault is", vault, existsSync(vault));
29
+ const obsidianConfigFile = findConfig();
25
30
  if (!existsSync(obsidianConfigFile)) {
26
31
  writeFileSync(obsidianConfigFile, JSON.stringify({ vaults: {} }));
27
32
  }
28
33
  const json = JSON.parse(readFileSync(obsidianConfigFile).toString());
34
+ if (concurrent === "symlink") {
35
+ symlinkDir.sync(vault, path.join(vaultPoolDir, vaultHash));
36
+ }
37
+ else if (concurrent === "copy") {
38
+ cpSync(vault, path.join(vaultPoolDir, vaultHash), { recursive: true });
39
+ }
29
40
  if (!Object.values(json.vaults).some((a) => a.path === vault)) {
30
41
  json.vaults[vaultHash] = {
31
- path: vault,
42
+ path: concurrent ? path.join(vaultPoolDir, vaultHash) : vault,
32
43
  ts: Date.now(),
33
44
  };
34
45
  writeFileSync(obsidianConfigFile, JSON.stringify(json));
@@ -41,34 +52,46 @@ function generateVaultConfig(vault) {
41
52
  }
42
53
  // @ts-ignore some error about a string type now having `undefined` as part of it's union
43
54
  export const test = base.extend({
44
- electronApp: [
55
+ vaultPoolDir: [
45
56
  async ({}, run) => {
57
+ const concurrent = inject("concurrent") ?? "copy";
58
+ if (!concurrent) {
59
+ await run("");
60
+ return;
61
+ }
62
+ const prefix = "obsidian-testing-framework-vault-pool-";
63
+ const vaultPoolDir = mkdtempSync(prefix);
64
+ await run(vaultPoolDir);
65
+ rmdirSync(vaultPoolDir, { recursive: true });
66
+ const configFile = findConfig();
67
+ const json = JSON.parse(readFileSync(configFile).toString());
68
+ const filtered = Object.fromEntries(Object.entries(json.vaults).filter(([, v]) => !v.path.includes(prefix)));
69
+ writeFileSync(configFile, JSON.stringify({ vaults: filtered }));
70
+ },
71
+ { scope: "worker", auto: true },
72
+ ],
73
+ electronApp: [
74
+ async ({ vaultPoolDir }, run) => {
46
75
  const obsidianPath = inject("obsidianPath") ?? undefined;
47
- const experimentalUseSingleton = inject("experimentalUseSingleton") ?? false;
48
76
  const vault = inject("vault");
77
+ const concurrent = inject("concurrent") ?? "copy";
49
78
  process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
50
79
  console.log("asar located at:", getExe(obsidianPath));
51
80
  let uriArg = "";
52
81
  if (vault) {
53
- let id = generateVaultConfig(vault);
82
+ let id = generateVaultConfig(vault, concurrent, vaultPoolDir);
54
83
  if (!!id) {
55
84
  uriArg = `obsidian://open?vault=${encodeURIComponent(id)}`;
56
85
  }
57
86
  }
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
- }
87
+ const electronApp = await electron.launch({
88
+ timeout: 60000,
89
+ args: [getExe(obsidianPath), uriArg].filter((a) => !!a),
90
+ });
91
+ addConsoleListener(electronApp);
67
92
  await electronApp.firstWindow();
68
93
  await run(electronApp);
69
- if (!experimentalUseSingleton) {
70
- await electronApp.close();
71
- }
94
+ await electronApp.close();
72
95
  },
73
96
  { auto: true },
74
97
  ],
package/package.json CHANGED
@@ -8,9 +8,11 @@
8
8
  "@codemirror/view": "^6.0.1",
9
9
  "@playwright/test": "^1.58.2",
10
10
  "asar": "^3.2.0",
11
+ "async-mutex": "^0.5.0",
11
12
  "electron": "^33.0.2",
12
13
  "obsidian": "latest",
13
14
  "playwright": "^1.58.2",
15
+ "symlink-dir": "^9.0.0",
14
16
  "tmp": "^0.2.3",
15
17
  "typescript": "^5.6.3",
16
18
  "xvfb-maybe": "^0.2.1"
@@ -25,7 +27,7 @@
25
27
  ".": "./lib/index.js"
26
28
  },
27
29
  "readme": "",
28
- "version": "0.2.4",
30
+ "version": "0.3.0",
29
31
  "main": "./lib/index.js",
30
32
  "typings": "./lib/index.d.ts",
31
33
  "repository": {