|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execFileSync } from 'child_process'; |
| 4 | +import { |
| 5 | + copyFileSync, |
| 6 | + existsSync, |
| 7 | + mkdirSync, |
| 8 | + mkdtempSync, |
| 9 | + readFileSync, |
| 10 | + rmSync, |
| 11 | + writeFileSync, |
| 12 | +} from 'fs'; |
| 13 | +import os from 'os'; |
| 14 | +import path from 'path'; |
| 15 | +import process from 'process'; |
| 16 | +import { fileURLToPath } from 'url'; |
| 17 | + |
| 18 | +const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); |
| 19 | +const DEBUG_PORT = Number(process.env.OBSIDIAN_DEBUG_PORT || 9222); |
| 20 | +const DEBUG_ORIGIN = `http://127.0.0.1:${DEBUG_PORT}`; |
| 21 | +const PLUGIN_ID = 'qoderian'; |
| 22 | +const DROP_MARKER = 'QODERIAN_E2E_FILE_CONTENT_MUST_NOT_BE_PASTED'; |
| 23 | + |
| 24 | +function loadLocalEnvironment() { |
| 25 | + const envPath = path.join(ROOT, '.env.local'); |
| 26 | + if (!existsSync(envPath)) return; |
| 27 | + |
| 28 | + for (const line of readFileSync(envPath, 'utf8').split(/\r?\n/)) { |
| 29 | + const match = line.match(/^([^#=]+)=["']?(.+?)["']?$/); |
| 30 | + if (match && !process.env[match[1].trim()]) { |
| 31 | + process.env[match[1].trim()] = match[2].trim(); |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function deployPlugin(vaultPath) { |
| 37 | + execFileSync(process.execPath, ['scripts/build.mjs', 'production'], { |
| 38 | + cwd: ROOT, |
| 39 | + stdio: 'inherit', |
| 40 | + }); |
| 41 | + |
| 42 | + const pluginDir = path.join(vaultPath, '.obsidian', 'plugins', PLUGIN_ID); |
| 43 | + if (!existsSync(pluginDir)) { |
| 44 | + throw new Error(`Qoderian plugin directory does not exist: ${pluginDir}`); |
| 45 | + } |
| 46 | + for (const name of ['main.js', 'manifest.json', 'styles.css']) { |
| 47 | + copyFileSync(path.join(ROOT, name), path.join(pluginDir, name)); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +class CdpSession { |
| 52 | + constructor(url) { |
| 53 | + this.url = url; |
| 54 | + this.nextId = 1; |
| 55 | + this.pending = new Map(); |
| 56 | + } |
| 57 | + |
| 58 | + async connect() { |
| 59 | + this.socket = new WebSocket(this.url); |
| 60 | + await new Promise((resolve, reject) => { |
| 61 | + this.socket.addEventListener('open', resolve, { once: true }); |
| 62 | + this.socket.addEventListener('error', reject, { once: true }); |
| 63 | + }); |
| 64 | + this.socket.addEventListener('message', (event) => { |
| 65 | + const message = JSON.parse(event.data); |
| 66 | + if (!message.id) return; |
| 67 | + const pending = this.pending.get(message.id); |
| 68 | + if (!pending) return; |
| 69 | + this.pending.delete(message.id); |
| 70 | + if (message.error) { |
| 71 | + pending.reject(new Error(`${pending.method}: ${message.error.message}`)); |
| 72 | + } else { |
| 73 | + pending.resolve(message.result); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + send(method, params = {}) { |
| 79 | + const id = this.nextId++; |
| 80 | + return new Promise((resolve, reject) => { |
| 81 | + this.pending.set(id, { method, resolve, reject }); |
| 82 | + this.socket.send(JSON.stringify({ id, method, params })); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + async evaluate(expression, awaitPromise = false) { |
| 87 | + const result = await this.send('Runtime.evaluate', { |
| 88 | + expression, |
| 89 | + awaitPromise, |
| 90 | + returnByValue: true, |
| 91 | + }); |
| 92 | + if (result.exceptionDetails) { |
| 93 | + throw new Error(result.exceptionDetails.exception?.description || 'Renderer evaluation failed'); |
| 94 | + } |
| 95 | + return result.result.value; |
| 96 | + } |
| 97 | + |
| 98 | + close() { |
| 99 | + this.socket.close(); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +async function retry(description, action, timeoutMs = 15_000) { |
| 104 | + const deadline = Date.now() + timeoutMs; |
| 105 | + let lastError; |
| 106 | + while (Date.now() < deadline) { |
| 107 | + try { |
| 108 | + const value = await action(); |
| 109 | + if (value) return value; |
| 110 | + } catch (error) { |
| 111 | + lastError = error; |
| 112 | + } |
| 113 | + await new Promise(resolve => setTimeout(resolve, 150)); |
| 114 | + } |
| 115 | + throw new Error(`${description} timed out${lastError ? `: ${lastError.message}` : ''}`); |
| 116 | +} |
| 117 | + |
| 118 | +async function findVaultTarget(vaultName) { |
| 119 | + const targets = await retry('Obsidian DevTools endpoint', async () => { |
| 120 | + const response = await fetch(`${DEBUG_ORIGIN}/json/list`); |
| 121 | + if (!response.ok) return null; |
| 122 | + return response.json(); |
| 123 | + }); |
| 124 | + const target = targets.find(candidate => |
| 125 | + candidate.type === 'page' |
| 126 | + && candidate.url === 'app://obsidian.md/index.html' |
| 127 | + && candidate.title.includes(vaultName) |
| 128 | + ); |
| 129 | + if (!target) { |
| 130 | + throw new Error(`No Obsidian window for vault "${vaultName}" was found on ${DEBUG_ORIGIN}`); |
| 131 | + } |
| 132 | + return target; |
| 133 | +} |
| 134 | + |
| 135 | +function visibleComposerStateExpression() { |
| 136 | + return `(() => { |
| 137 | + const wrapper = [...document.querySelectorAll('.qoderian-input-wrapper')] |
| 138 | + .find(element => element.getBoundingClientRect().width > 0); |
| 139 | + if (!wrapper) return null; |
| 140 | + const rect = wrapper.getBoundingClientRect(); |
| 141 | + return { |
| 142 | + x: Math.round(rect.x + rect.width / 2), |
| 143 | + y: Math.round(rect.y + rect.height / 2), |
| 144 | + sourceValue: wrapper.querySelector('textarea.qoderian-input')?.value ?? '', |
| 145 | + editorText: wrapper.querySelector('.cm-content')?.textContent ?? '', |
| 146 | + }; |
| 147 | + })()`; |
| 148 | +} |
| 149 | + |
| 150 | +async function run() { |
| 151 | + loadLocalEnvironment(); |
| 152 | + const vaultPath = process.env.OBSIDIAN_VAULT; |
| 153 | + if (!vaultPath || !existsSync(vaultPath)) { |
| 154 | + throw new Error('Set OBSIDIAN_VAULT in .env.local to an existing test vault.'); |
| 155 | + } |
| 156 | + |
| 157 | + deployPlugin(vaultPath); |
| 158 | + |
| 159 | + const target = await findVaultTarget(path.basename(vaultPath)); |
| 160 | + const cdp = new CdpSession(target.webSocketDebuggerUrl); |
| 161 | + await cdp.connect(); |
| 162 | + |
| 163 | + const tempDir = mkdtempSync(path.join(os.tmpdir(), 'qoderian-file-drop-e2e-')); |
| 164 | + const isFolder = process.env.QODERIAN_E2E_DROP_KIND !== 'file'; |
| 165 | + const droppedPath = path.join(tempDir, isFolder ? '外部资料 folder' : 'external-context.txt'); |
| 166 | + if (isFolder) mkdirSync(droppedPath); |
| 167 | + else writeFileSync(droppedPath, DROP_MARKER, 'utf8'); |
| 168 | + |
| 169 | + try { |
| 170 | + await cdp.evaluate(`(async () => { |
| 171 | + const plugin = app.plugins.getPlugin(${JSON.stringify(PLUGIN_ID)}); |
| 172 | + const contexts = new Map([...(plugin?.getView()?.getTabManager()?.tabs ?? [])] |
| 173 | + .map(([id, tab]) => [id, tab.ui.externalContextSelector?.getExternalContexts() ?? []])); |
| 174 | + await app.plugins.disablePlugin(${JSON.stringify(PLUGIN_ID)}); |
| 175 | + if (window.__qoderianE2EDragOverObserver) { |
| 176 | + window.removeEventListener('dragover', window.__qoderianE2EDragOverObserver, true); |
| 177 | + } |
| 178 | + if (window.__qoderianE2EHostBlocker) { |
| 179 | + document.removeEventListener('dragover', window.__qoderianE2EHostBlocker, true); |
| 180 | + } |
| 181 | + window.__qoderianE2EDragOverResults = []; |
| 182 | + window.__qoderianE2EDragOverObserver = event => { |
| 183 | + const reachesComposer = event.composedPath() |
| 184 | + .some(element => element?.classList?.contains('qoderian-input-wrapper')); |
| 185 | + const result = { |
| 186 | + accepted: false, |
| 187 | + reachesComposer, |
| 188 | + types: [...(event.dataTransfer?.types ?? [])], |
| 189 | + hostBlocked: false, |
| 190 | + }; |
| 191 | + window.__qoderianE2EDragOverResults.push(result); |
| 192 | + const preventDefault = event.preventDefault.bind(event); |
| 193 | + event.preventDefault = () => { |
| 194 | + result.accepted = true; |
| 195 | + preventDefault(); |
| 196 | + }; |
| 197 | + }; |
| 198 | + window.__qoderianE2EHostBlocker = event => { |
| 199 | + if (event.composedPath().some(element => element?.classList?.contains('qoderian-input-wrapper'))) { |
| 200 | + window.__qoderianE2EDragOverResults.push({ |
| 201 | + accepted: event.defaultPrevented, |
| 202 | + reachesComposer: true, |
| 203 | + types: [...(event.dataTransfer?.types ?? [])], |
| 204 | + hostBlocked: true, |
| 205 | + }); |
| 206 | + event.stopImmediatePropagation(); |
| 207 | + } |
| 208 | + }; |
| 209 | + window.addEventListener('dragover', window.__qoderianE2EDragOverObserver, true); |
| 210 | + await app.plugins.enablePlugin(${JSON.stringify(PLUGIN_ID)}); |
| 211 | + await app.commands.executeCommandById(${JSON.stringify(`${PLUGIN_ID}:open-view`)}); |
| 212 | + for (const [id, tab] of app.plugins.getPlugin(${JSON.stringify(PLUGIN_ID)}).getView().getTabManager().tabs) { |
| 213 | + if (contexts.has(id)) tab.ui.externalContextSelector?.setExternalContexts(contexts.get(id)); |
| 214 | + } |
| 215 | + document.addEventListener('dragover', window.__qoderianE2EHostBlocker, true); |
| 216 | + return true; |
| 217 | + })()`, true); |
| 218 | + |
| 219 | + const before = await retry('visible Qoderian composer', () => |
| 220 | + cdp.evaluate(visibleComposerStateExpression())); |
| 221 | + |
| 222 | + const dragData = { |
| 223 | + items: [{ mimeType: 'text/plain', data: DROP_MARKER }], |
| 224 | + files: [droppedPath], |
| 225 | + dragOperationsMask: 1, |
| 226 | + }; |
| 227 | + for (const type of ['dragEnter', 'dragOver', 'drop']) { |
| 228 | + await cdp.send('Input.dispatchDragEvent', { |
| 229 | + type, |
| 230 | + x: before.x, |
| 231 | + y: before.y, |
| 232 | + data: dragData, |
| 233 | + }); |
| 234 | + } |
| 235 | + |
| 236 | + const after = await retry('dropped file to appear as external context', async () => { |
| 237 | + const state = await cdp.evaluate(`(() => { |
| 238 | + const path = ${JSON.stringify(droppedPath)}; |
| 239 | + const context = [...document.querySelectorAll('.qoderian-external-context-text')] |
| 240 | + .find(element => element.getAttribute('title') === path); |
| 241 | + const composer = (${visibleComposerStateExpression()}); |
| 242 | + const notice = [...document.querySelectorAll('.notice')] |
| 243 | + .some(element => element.textContent.includes(path)); |
| 244 | + const badge = context?.closest('.qoderian-external-context-selector') |
| 245 | + ?.querySelector('.qoderian-external-context-badge.visible'); |
| 246 | + return context && composer && notice && badge |
| 247 | + ? { composer, contextTitle: context.getAttribute('title') } : null; |
| 248 | + })()`); |
| 249 | + return state; |
| 250 | + }); |
| 251 | + |
| 252 | + const dragOverResults = await cdp.evaluate( |
| 253 | + `window.__qoderianE2EDragOverResults ?? []`, |
| 254 | + ); |
| 255 | + const dragOverWasAccepted = dragOverResults.some(result => result.accepted); |
| 256 | + if (!dragOverWasAccepted) { |
| 257 | + throw new Error( |
| 258 | + `The window capture handler did not accept native dragover before the host intercepted it: ${JSON.stringify(dragOverResults)}`, |
| 259 | + ); |
| 260 | + } |
| 261 | + |
| 262 | + if (after.composer.sourceValue !== before.sourceValue |
| 263 | + || after.composer.editorText !== before.editorText |
| 264 | + || after.composer.sourceValue.includes(DROP_MARKER) |
| 265 | + || after.composer.editorText.includes(DROP_MARKER)) { |
| 266 | + throw new Error('The dropped file content was inserted into the composer.'); |
| 267 | + } |
| 268 | + |
| 269 | + console.log(`PASS protocol-injected ${isFolder ? 'folder' : 'file'} drop -> external context: ${after.contextTitle}`); |
| 270 | + console.log('PASS added path shown in a notice and context count badge'); |
| 271 | + console.log('PASS dragover accepted before simulated host interception'); |
| 272 | + console.log('PASS dropped file content was not pasted into the composer'); |
| 273 | + } finally { |
| 274 | + try { |
| 275 | + await cdp.evaluate(`(() => { |
| 276 | + const path = ${JSON.stringify(droppedPath)}; |
| 277 | + const text = [...document.querySelectorAll('.qoderian-external-context-text')] |
| 278 | + .find(element => element.getAttribute('title') === path); |
| 279 | + text?.closest('.qoderian-external-context-item') |
| 280 | + ?.querySelector('.qoderian-external-context-remove')?.click(); |
| 281 | + if (window.__qoderianE2EDragOverObserver) { |
| 282 | + window.removeEventListener('dragover', window.__qoderianE2EDragOverObserver, true); |
| 283 | + } |
| 284 | + if (window.__qoderianE2EHostBlocker) { |
| 285 | + document.removeEventListener('dragover', window.__qoderianE2EHostBlocker, true); |
| 286 | + } |
| 287 | + delete window.__qoderianE2EDragOverObserver; |
| 288 | + delete window.__qoderianE2EHostBlocker; |
| 289 | + delete window.__qoderianE2EDragOverResults; |
| 290 | + return true; |
| 291 | + })()`); |
| 292 | + } catch { |
| 293 | + // Best-effort UI cleanup; the temporary file is always removed below. |
| 294 | + } |
| 295 | + cdp.close(); |
| 296 | + rmSync(tempDir, { recursive: true, force: true }); |
| 297 | + } |
| 298 | +} |
| 299 | + |
| 300 | +run().catch((error) => { |
| 301 | + console.error(`E2E FAILED: ${error.message}`); |
| 302 | + console.error(`Launch Obsidian with --remote-debugging-port=${DEBUG_PORT} and open the OBSIDIAN_VAULT test vault.`); |
| 303 | + process.exit(1); |
| 304 | +}); |
0 commit comments