diff --git a/.yarn/install-state.gz b/.yarn/install-state.gz index 65bb430..3dd177b 100644 Binary files a/.yarn/install-state.gz and b/.yarn/install-state.gz differ diff --git a/packages/obsidian-testing-framework/src/index.ts b/packages/obsidian-testing-framework/src/index.ts index f16c354..6408ea9 100644 --- a/packages/obsidian-testing-framework/src/index.ts +++ b/packages/obsidian-testing-framework/src/index.ts @@ -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,28 +40,48 @@ 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`), "{}"); } } const obsidianTestFixtures: Fixtures = { electronApp: [ - async ({}, run) => { + async ({ obsidian: { vault } }, run) => { process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = "true"; console.log("asar located at:", getExe()); + if (vault) generateVaultConfig(vault); const electronApp = await electron.launch({ timeout: 60000, args: [getExe()].filter((a) => !!a) as string[], @@ -79,13 +98,12 @@ const obsidianTestFixtures: Fixtures = { { 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");