|
| 1 | +import { execSync, spawn } from 'node:child_process'; |
| 2 | +import { existsSync, mkdirSync, readdirSync, writeFileSync } from 'node:fs'; |
| 3 | +import { homedir } from 'node:os'; |
| 4 | +import { join, resolve } from 'node:path'; |
| 5 | +import { agentFromComputer } from '@midscene/computer'; |
| 6 | +import 'dotenv/config'; |
| 7 | + |
| 8 | +const sleep = (ms: number) => |
| 9 | + new Promise((resolve) => setTimeout(resolve, ms)); |
| 10 | + |
| 11 | +const DEMO_DIR = resolve(import.meta.dirname); |
| 12 | +const VAULT_DIR = join(DEMO_DIR, 'obsidian-vault'); |
| 13 | + |
| 14 | +/** |
| 15 | + * Locate the Obsidian binary. If only an AppImage is found, extract it first. |
| 16 | + * Returns the path to the executable. |
| 17 | + */ |
| 18 | +function findOrPrepareApp(): string { |
| 19 | + // Already extracted |
| 20 | + const extractedBin = join(DEMO_DIR, 'squashfs-root', 'obsidian'); |
| 21 | + if (existsSync(extractedBin)) { |
| 22 | + console.log('Using previously extracted Obsidian binary'); |
| 23 | + return extractedBin; |
| 24 | + } |
| 25 | + |
| 26 | + // Find AppImage in the demo directory |
| 27 | + const appImages = readdirSync(DEMO_DIR).filter((f) => |
| 28 | + f.endsWith('.AppImage'), |
| 29 | + ); |
| 30 | + if (appImages.length === 0) { |
| 31 | + throw new Error( |
| 32 | + 'No Obsidian AppImage found. Download it to the electron-demo directory first.', |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + const appImagePath = join(DEMO_DIR, appImages[0]); |
| 37 | + console.log(`Extracting ${appImages[0]} ...`); |
| 38 | + execSync(`chmod +x "${appImagePath}"`); |
| 39 | + execSync(`"${appImagePath}" --appimage-extract`, { cwd: DEMO_DIR }); |
| 40 | + console.log('Extraction complete'); |
| 41 | + |
| 42 | + if (!existsSync(extractedBin)) { |
| 43 | + throw new Error('Extraction succeeded but obsidian binary not found'); |
| 44 | + } |
| 45 | + return extractedBin; |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Pre-seed Obsidian config so it opens our vault directly (skipping the vault picker). |
| 50 | + */ |
| 51 | +function preseedVault(vaultDir: string): void { |
| 52 | + mkdirSync(vaultDir, { recursive: true }); |
| 53 | + |
| 54 | + const configDir = join(homedir(), '.config', 'obsidian'); |
| 55 | + mkdirSync(configDir, { recursive: true }); |
| 56 | + |
| 57 | + const obsidianJson = join(configDir, 'obsidian.json'); |
| 58 | + const vaultId = 'ci-test-vault'; |
| 59 | + const config = { |
| 60 | + vaults: { |
| 61 | + [vaultId]: { |
| 62 | + path: vaultDir, |
| 63 | + ts: Date.now(), |
| 64 | + open: true, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }; |
| 68 | + writeFileSync(obsidianJson, JSON.stringify(config, null, 2)); |
| 69 | + console.log(`Vault config written to ${obsidianJson}`); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Launch Obsidian as a detached child process with Electron-friendly flags. |
| 74 | + */ |
| 75 | +function launchApp( |
| 76 | + binaryPath: string, |
| 77 | + vaultDir: string, |
| 78 | +): ReturnType<typeof spawn> { |
| 79 | + const args = [ |
| 80 | + `--vault=${vaultDir}`, |
| 81 | + '--no-sandbox', |
| 82 | + '--disable-gpu', |
| 83 | + '--disable-dev-shm-usage', |
| 84 | + ]; |
| 85 | + console.log(`Launching: ${binaryPath} ${args.join(' ')}`); |
| 86 | + |
| 87 | + const child = spawn(binaryPath, args, { |
| 88 | + detached: true, |
| 89 | + stdio: 'ignore', |
| 90 | + env: { ...process.env, ELECTRON_DISABLE_SECURITY_WARNINGS: 'true' }, |
| 91 | + }); |
| 92 | + child.unref(); |
| 93 | + return child; |
| 94 | +} |
| 95 | + |
| 96 | +(async () => { |
| 97 | + // --- Prepare & launch Obsidian --- |
| 98 | + const binaryPath = findOrPrepareApp(); |
| 99 | + preseedVault(VAULT_DIR); |
| 100 | + |
| 101 | + const child = launchApp(binaryPath, VAULT_DIR); |
| 102 | + console.log(`Obsidian launched (pid: ${child.pid})`); |
| 103 | + |
| 104 | + // Give Obsidian time to start up |
| 105 | + await sleep(8000); |
| 106 | + |
| 107 | + // --- Connect Midscene agent --- |
| 108 | + const agent = await agentFromComputer({ |
| 109 | + aiActionContext: |
| 110 | + 'You are interacting with Obsidian, a note-taking desktop application. ' + |
| 111 | + 'If any dialog or popup appears, dismiss it by clicking the close button or pressing Escape.', |
| 112 | + }); |
| 113 | + |
| 114 | + try { |
| 115 | + // Wait for the main UI to appear |
| 116 | + await agent.aiWaitFor( |
| 117 | + 'Obsidian main window is visible with the editor area or vault view', |
| 118 | + { timeoutMs: 30000 }, |
| 119 | + ); |
| 120 | + console.log('Obsidian UI is ready'); |
| 121 | + |
| 122 | + // Dismiss any welcome dialogs / popups |
| 123 | + await agent.aiAct( |
| 124 | + 'If there is any popup, modal dialog, or "Trust author" prompt, close or dismiss it', |
| 125 | + ); |
| 126 | + await sleep(1000); |
| 127 | + |
| 128 | + // Create a new note |
| 129 | + await agent.aiAct('press Ctrl+N to create a new note'); |
| 130 | + await sleep(1500); |
| 131 | + |
| 132 | + // Type note content |
| 133 | + await agent.aiAct('type "Hello from Midscene CI" in the editor'); |
| 134 | + await sleep(1000); |
| 135 | + |
| 136 | + // Verify the note content |
| 137 | + const result = await agent.aiQuery( |
| 138 | + '{ content: string } — read the text content currently visible in the editor area', |
| 139 | + ); |
| 140 | + console.log('Editor content:', JSON.stringify(result)); |
| 141 | + |
| 142 | + if ( |
| 143 | + typeof result?.content === 'string' && |
| 144 | + result.content.includes('Midscene') |
| 145 | + ) { |
| 146 | + console.log('Content verification passed!'); |
| 147 | + } else { |
| 148 | + console.warn('Content verification: text may not match expected value'); |
| 149 | + } |
| 150 | + |
| 151 | + console.log('Electron demo completed successfully!'); |
| 152 | + } finally { |
| 153 | + // Cleanup: kill the Obsidian process |
| 154 | + if (child.pid) { |
| 155 | + try { |
| 156 | + process.kill(-child.pid, 'SIGTERM'); |
| 157 | + } catch { |
| 158 | + // Process may have already exited |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | +})(); |
0 commit comments