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