Compare commits

...

2 Commits

Author SHA1 Message Date
e59bdd6af3
rethink vault opening
Some checks failed
Playwright Tests / test (push) Failing after 2m21s
2024-10-23 18:07:10 -04:00
76e14fe125
rethink vault opening 2024-10-23 18:04:08 -04:00
2 changed files with 52 additions and 23 deletions

BIN
.yarn/install-state.gz vendored

Binary file not shown.

@ -3,16 +3,17 @@ import { _electron as electron } from "playwright";
import { Fixtures } from "@playwright/test"; import { Fixtures } from "@playwright/test";
import path from "path"; import path from "path";
import { ObsidianTestFixtures } from "./fixtures.js"; import { ObsidianTestFixtures } from "./fixtures.js";
import { existsSync } from "fs"; import { existsSync, readFileSync, writeFileSync } from "fs";
import { getApp, waitForIndexingComplete } from "./util.js"; import { getApp, waitForIndexingComplete } from "./util.js";
import { execSync } from "child_process"; import { execSync } from "child_process";
import { Page } from "playwright"; import { randomBytes } from "crypto";
export interface ObsidianTestingConfig { export interface ObsidianTestingConfig {
vault?: string; vault?: string;
} }
export function getExe(): string { export function getExe(): string {
checkToy();
if (process.platform == "win32") { if (process.platform == "win32") {
return path.join( return path.join(
process.env.LOCALAPPDATA!, process.env.LOCALAPPDATA!,
@ -21,9 +22,7 @@ export function getExe(): string {
"app.asar" "app.asar"
); );
} }
if (process.platform == "darwin") {
throw new Error("use a non-toy operating system, dumbass");
}
const possibleDirs = [ const possibleDirs = [
"/opt/Obsidian", "/opt/Obsidian",
"/usr/lib/Obsidian", "/usr/lib/Obsidian",
@ -41,31 +40,62 @@ export function getExe(): string {
return ""; return "";
} }
async function selectNewVault(page: Page, vaultPath: string) { function checkToy() {
try { if (process.platform == "darwin") {
let btn = await page.locator("button", { hasText: "Open" }); throw new Error("use a non-toy operating system, dumbass");
const promise = page.waitForEvent("filechooser"); }
await btn.click(); }
const chooser = await promise;
let el = chooser.element(); function generateVaultConfig(vault: string) {
console.log("chooser el", el); const vaultHash = randomBytes(16).toString("hex").toLocaleLowerCase();
el.setInputFiles(vaultPath); let configLocation;
} catch (e) { checkToy();
console.error(e); if (process.platform == "win32") {
console.debug( configLocation = path.join(`${process.env.LOCALAPPDATA}`, "Obsidian");
"congratulations, you don't have to choose a vault manually!" } else {
); configLocation = path.join(`${process.env.XDG_CONFIG_HOME}`, "obsidian");
}
const obsidianConfigFile = path.join(configLocation, "obsidian.json");
const json: {
vaults: {
[key: string]: {
path: string;
ts: number;
open?: boolean;
};
};
} = JSON.parse(readFileSync(obsidianConfigFile).toString());
if (!Object.values(json.vaults).some((a) => a.path === vault)) {
json.vaults[vaultHash] = {
path: vault,
ts: Date.now(),
};
writeFileSync(obsidianConfigFile, JSON.stringify(json));
writeFileSync(path.join(configLocation, `${vaultHash}.json`), "{}");
return vaultHash;
} else {
return "";
} }
} }
const obsidianTestFixtures: Fixtures<ObsidianTestFixtures> = { const obsidianTestFixtures: Fixtures<ObsidianTestFixtures> = {
electronApp: [ electronApp: [
async ({}, run) => { async ({ obsidian: { vault } }, run) => {
process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true"; process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true";
console.log("asar located at:", getExe()); console.log("asar located at:", getExe());
let uriArg = "";
if (vault) {
let id = generateVaultConfig(vault);
if(!!id) {
uriArg = `obsidian://open?vault=${encodeURIComponent(id)}`
}
}
const electronApp = await electron.launch({ const electronApp = await electron.launch({
timeout: 60000, timeout: 60000,
args: [getExe()].filter((a) => !!a) as string[], args: [getExe(), uriArg].filter((a) => !!a) as string[],
}); });
electronApp.on("console", async (msg) => { electronApp.on("console", async (msg) => {
console.log( console.log(
@ -79,13 +109,12 @@ const obsidianTestFixtures: Fixtures<ObsidianTestFixtures> = {
{ timeout: 60000 }, { timeout: 60000 },
], ],
page: [ page: [
async ({ electronApp, obsidian: { vault } }, run) => { async ({ electronApp }, run) => {
const windows = electronApp.windows(); const windows = electronApp.windows();
// console.log("windows", windows); // console.log("windows", windows);
let page = windows[windows.length - 1]!; let page = windows[windows.length - 1]!;
await page.waitForEvent("load"); await page.waitForEvent("load");
await page.waitForLoadState("domcontentloaded"); await page.waitForLoadState("domcontentloaded");
await selectNewVault(page, vault!);
page = electronApp.windows()[electronApp.windows().length - 1]!; page = electronApp.windows()[electronApp.windows().length - 1]!;
await page.waitForEvent("load"); await page.waitForEvent("load");
await page.waitForLoadState("domcontentloaded"); await page.waitForLoadState("domcontentloaded");