Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Make desktop smoke detection and menu capture hermetic (PRD-9347) (#6238)

Andrew Mikofalvyd4b9456579ea
diff --git a/packages/desktop/src/main/index.ts b/packages/desktop/src/main/index.ts
index db8b3ba39..7865d377a 100644
--- a/packages/desktop/src/main/index.ts
+++ b/packages/desktop/src/main/index.ts
@@ -312,6 +312,7 @@ import { deriveInstanceUserDataDir } from './instance-isolation.ts';
import {
type EditorPresenceProbes,
registerIntegrationsSettings,
+ testPinnedSchemeHandlers,
} from './integrations-settings.ts';
import { handleAssetUpload } from './ipc/asset-upload.ts';
import {
@@ -5656,18 +5657,21 @@ function probeEditorPresence(): Promise<EditorPresenceProbes> {
}
async function probeEditorPresenceUncached(): Promise<EditorPresenceProbes> {
+ const pinnedSchemes = testPinnedSchemeHandlers(process.env);
const [cliOnPath, ...schemes] = await Promise.all([
resolveTerminalCliInstalledMap().catch(() => ({}) as Record<TerminalCli, boolean>),
...(['claude', 'codex', 'cursor', 'lmstudio'] as const).map((scheme) =>
- detectProtocolImpl(
- {
- platform: process.platform,
- getApplicationInfoForProtocol: (url) => app.getApplicationInfoForProtocol(url),
- },
- scheme,
- )
- .then((r) => r.installed)
- .catch(() => false),
+ pinnedSchemes !== null
+ ? Promise.resolve(pinnedSchemes.has(scheme))
+ : detectProtocolImpl(
+ {
+ platform: process.platform,
+ getApplicationInfoForProtocol: (url) => app.getApplicationInfoForProtocol(url),
+ },
+ scheme,
+ )
+ .then((r) => r.installed)
+ .catch(() => false),
),
]);
return {
diff --git a/packages/desktop/src/main/integrations-settings.test.ts b/packages/desktop/src/main/integrations-settings.test.ts
index 97bad673a..200c913d3 100644
--- a/packages/desktop/src/main/integrations-settings.test.ts
+++ b/packages/desktop/src/main/integrations-settings.test.ts
@@ -18,6 +18,7 @@ import {
type IntegrationsSkillsSurface,
type RegisterIntegrationsSettingsOpts,
registerIntegrationsSettings,
+ testPinnedSchemeHandlers,
} from './integrations-settings.ts';
import type { McpStatusMarker, McpWiringFsOps } from './mcp-wiring.ts';
@@ -213,6 +214,27 @@ describe('classifyEditorState', () => {
});
});
+describe('testPinnedSchemeHandlers', () => {
+ const smoke = { OK_DESKTOP_E2E_SMOKE: '1' };
+
+ test('an empty pin under smoke reports every scheme handler absent', () => {
+ expect(testPinnedSchemeHandlers({ ...smoke, OK_DESKTOP_TEST_SCHEME_HANDLERS: '' })).toEqual(
+ new Set(),
+ );
+ });
+
+ test('a pin lists the schemes reported present', () => {
+ expect(
+ testPinnedSchemeHandlers({ ...smoke, OK_DESKTOP_TEST_SCHEME_HANDLERS: ' codex, cursor ,' }),
+ ).toEqual(new Set(['codex', 'cursor']));
+ });
+
+ test('an unset pin, or one outside smoke, leaves the real probe in charge', () => {
+ expect(testPinnedSchemeHandlers(smoke)).toBeNull();
+ expect(testPinnedSchemeHandlers({ OK_DESKTOP_TEST_SCHEME_HANDLERS: '' })).toBeNull();
+ });
+});
+
describe('detectedEditorsFromProbes', () => {
const none = { cliOnPath: {}, schemeHandler: {} };
diff --git a/packages/desktop/src/main/integrations-settings.ts b/packages/desktop/src/main/integrations-settings.ts
index e5cf8662c..a86723eec 100644
--- a/packages/desktop/src/main/integrations-settings.ts
+++ b/packages/desktop/src/main/integrations-settings.ts
@@ -153,6 +153,20 @@ export function detectedEditorsFromProbes(probes: EditorPresenceProbes): Set<Mcp
return detected;
}
+export function testPinnedSchemeHandlers(
+ env: Readonly<Record<string, string | undefined>>,
+): ReadonlySet<string> | null {
+ if (env.OK_DESKTOP_E2E_SMOKE !== '1') return null;
+ const raw = env.OK_DESKTOP_TEST_SCHEME_HANDLERS;
+ if (raw === undefined) return null;
+ return new Set(
+ raw
+ .split(',')
+ .map((scheme) => scheme.trim())
+ .filter((scheme) => scheme.length > 0),
+ );
+}
+
export async function safeDetectedEditors(
probeEditorPresence: () => Promise<EditorPresenceProbes>,
): Promise<Set<McpWiringEditorId>> {
diff --git a/packages/desktop/tests/smoke/qa-create-new-extended.e2e.ts b/packages/desktop/tests/smoke/qa-create-new-extended.e2e.ts
index d86ba1a14..b9cbc7efc 100644
--- a/packages/desktop/tests/smoke/qa-create-new-extended.e2e.ts
+++ b/packages/desktop/tests/smoke/qa-create-new-extended.e2e.ts
@@ -10,9 +10,11 @@ import {
} from 'node:fs';
import { tmpdir } from 'node:os';
import { delimiter, join } from 'node:path';
+import { TERMINAL_CLIS } from '@inkeep/open-knowledge-core';
import type { ElectronApplication, Page } from '@playwright/test';
import { _electron as electron } from '@playwright/test';
import { configureTestGitRepository } from '../../../../test-support/configure-git-fixture.test-helper.ts';
+import { windowsPathKey } from '../../src/shared/windows-env.ts';
import { configureDesktopGitRepositories } from '../support/git-fixture.test-helper.ts';
import { typeProjectName } from './_helpers/create-new-dialog';
import { captureAppProcess, closeAppBounded } from './_helpers/electron-cleanup';
@@ -31,6 +33,28 @@ const TARGET = resolveDesktopTarget();
const DESKTOP_PRODUCT_NAME = '@inkeep/open-knowledge-desktop';
+const CLI_BINS = Object.values(TERMINAL_CLIS).map((cli) => cli.bin);
+const CLI_SUFFIXES = process.platform === 'win32' ? ['.exe', '.cmd', '.bat', '.ps1'] : [''];
+
+function holdsDetectableCli(dir: string): boolean {
+ return CLI_BINS.some((bin) => CLI_SUFFIXES.some((suffix) => existsSync(join(dir, bin + suffix))));
+}
+
+function hermeticPath(tmpHome: string): string {
+ const hostDirs = (process.env[windowsPathKey(process.env)] ?? '')
+ .split(delimiter)
+ .filter((dir) => dir.length > 0 && !holdsDetectableCli(dir));
+ return [join(tmpHome, 'bin'), ...hostDirs].join(delimiter);
+}
+
+function seedShellProfiles(tmpHome: string): void {
+ if (process.platform === 'win32') return;
+ const escaped = hermeticPath(tmpHome).replace(/["\\$`]/g, '\\$&');
+ for (const rc of ['.zprofile', '.zshrc', '.bash_profile', '.profile']) {
+ writeFileSync(join(tmpHome, rc), `export PATH="${escaped}"\n`);
+ }
+}
+
function seedCliOnPath(tmpHome: string, bin: string): void {
const binDir = join(tmpHome, 'bin');
mkdirSync(binDir, { recursive: true });
@@ -38,10 +62,6 @@ function seedCliOnPath(tmpHome: string, bin: string): void {
writeFileSync(join(binDir, `${bin}.cmd`), '@exit /b 0\r\n');
} else {
writeFileSync(join(binDir, bin), '#!/bin/sh\nexit 0\n', { mode: 0o755 });
- const pathLine = 'export PATH="$HOME/bin:$PATH"\n';
- for (const rc of ['.zprofile', '.zshrc', '.bash_profile', '.profile']) {
- writeFileSync(join(tmpHome, rc), pathLine);
- }
}
}
@@ -61,6 +81,7 @@ function seedTmpHome(prefix: string, stateOverride?: Record<string, unknown>): s
...stateOverride,
}),
);
+ seedShellProfiles(tmpHome);
return tmpHome;
}
@@ -77,9 +98,10 @@ async function launchApp(tmpHome: string, opts: LaunchOpts = {}): Promise<Electr
timeout: 30_000,
env: {
...process.env,
- PATH: `${join(tmpHome, 'bin')}${delimiter}${process.env.PATH ?? ''}`,
+ [windowsPathKey(process.env)]: hermeticPath(tmpHome),
...homeEnv(tmpHome),
OK_DESKTOP_E2E_SMOKE: '1',
+ OK_DESKTOP_TEST_SCHEME_HANDLERS: '',
...(opts.pickedParent !== undefined
? { OK_DESKTOP_TEST_PICKED_PATH: opts.pickedParent }
: {}),
diff --git a/packages/desktop/tests/smoke/report-bug.e2e.ts b/packages/desktop/tests/smoke/report-bug.e2e.ts
index 8c2565363..48bd057c5 100644
--- a/packages/desktop/tests/smoke/report-bug.e2e.ts
+++ b/packages/desktop/tests/smoke/report-bug.e2e.ts
@@ -103,6 +103,20 @@ async function readScreenshotPreview(dialog: Locator): Promise<string | null> {
return await img.getAttribute('src');
}
+const MENU_PAINT_CSS =
+ '[role="menu"], [role="menu"] * { animation: none !important; transition: none !important; }\n' +
+ '[role="menu"] { background: rgb(255, 0, 255) !important; opacity: 1 !important; ' +
+ 'transform: none !important; }\n' +
+ '[role="menu"] * { opacity: 0 !important; }\n';
+
+async function attachPreview(name: string, dataUrl: string): Promise<void> {
+ const base64 = dataUrl.slice(dataUrl.indexOf(',') + 1);
+ await test.info().attach(name, {
+ body: Buffer.from(base64, 'base64'),
+ contentType: 'image/png',
+ });
+}
+
function skipWhenHostCannotCapture(preview: string | null): asserts preview is string {
test.skip(
preview === null,
@@ -182,6 +196,12 @@ test.describe('Report-a-bug entry points', () => {
.getByRole('menu')
.filter({ has: page.getByRole('menuitem', { name: /rename/i }) });
await expect(contextMenu).toBeVisible({ timeout: 10_000 });
+ await page.addStyleTag({ content: MENU_PAINT_CSS });
+ await expect(contextMenu).toHaveCSS('background-color', 'rgb(255, 0, 255)');
+ await expect(contextMenu).toHaveCSS('opacity', '1');
+ await page.evaluate(
+ () => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))),
+ );
const menuBox = await contextMenu.boundingBox();
if (!menuBox) throw new Error('context menu reported no bounding box');
@@ -189,35 +209,35 @@ test.describe('Report-a-bug entry points', () => {
await expect(composeDialog).toBeVisible({ timeout: 10_000 });
const withMenu = await readScreenshotPreview(composeDialog);
if (withMenu === null) throw new Error('the second pass captured nothing while the first did');
- expect(withMenu).not.toEqual(withoutMenu);
+ await attachPreview('preview-without-menu', withoutMenu);
+ await attachPreview('preview-with-menu', withMenu);
- const diff = await app.evaluate(
+ const painted = await app.evaluate(
({ nativeImage }, images) => {
- const a = nativeImage.createFromDataURL(images.a).toBitmap();
- const b = nativeImage.createFromDataURL(images.b).toBitmap();
- if (a.length !== b.length || a.length === 0) {
- return { comparable: false, changedPixels: 0, totalPixels: 0 };
- }
- let changedPixels = 0;
- for (let i = 0; i < a.length; i += 4) {
- if (a[i] !== b[i] || a[i + 1] !== b[i + 1] || a[i + 2] !== b[i + 2]) changedPixels += 1;
- }
- return { comparable: true, changedPixels, totalPixels: a.length / 4 };
+ const fraction = (dataUrl: string): number => {
+ const bitmap = nativeImage.createFromDataURL(dataUrl).toBitmap();
+ let matched = 0;
+ for (let i = 0; i < bitmap.length; i += 4) {
+ if (bitmap[i] > 200 && bitmap[i + 1] < 80 && bitmap[i + 2] > 200) matched += 1;
+ }
+ return bitmap.length === 0 ? 0 : matched / (bitmap.length / 4);
+ };
+ return { without: fraction(images.without), with: fraction(images.with) };
},
- { a: withoutMenu, b: withMenu },
+ { without: withoutMenu, with: withMenu },
);
- expect(diff.comparable, 'the two previews did not decode to comparable bitmaps').toBe(true);
const viewport = await page.evaluate(() => ({
width: window.innerWidth,
height: window.innerHeight,
}));
const menuFraction = (menuBox.width * menuBox.height) / (viewport.width * viewport.height);
- const changedFraction = diff.changedPixels / diff.totalPixels;
- expect(
- changedFraction,
- `changed ${(changedFraction * 100).toFixed(3)}% of the preview; the menu covers ` +
- `${(menuFraction * 100).toFixed(3)}% of the viewport`,
- ).toBeGreaterThan(menuFraction * 0.25);
+ const detail =
+ `the menu covers ${(menuFraction * 100).toFixed(3)}% of the viewport; its paint covers ` +
+ `${(painted.with * 100).toFixed(3)}% of the preview with it open and ` +
+ `${(painted.without * 100).toFixed(3)}% without`;
+ expect(painted.without, detail).toBeLessThan(menuFraction * 0.1);
+ expect(painted.with, detail).toBeGreaterThan(menuFraction * 0.5);
+ expect(painted.with, detail).toBeLessThan(menuFraction * 1.1);
});
});