fix(ui): complete VS Code bootstrap detection for directory startup (#2359)
This commit is contained in:
@@ -5,6 +5,7 @@ import type { DraftStarterRef } from '@/lib/draftStarters';
|
||||
import type { MobileKeyboardMode } from '@/lib/mobileKeyboardMode';
|
||||
import { getRuntimeApiBaseUrl, getRuntimeKey } from '@/lib/runtime-switch';
|
||||
import { getRegisteredRuntimeAPIs } from '@/contexts/runtimeAPIRegistry';
|
||||
import { isVSCodeBootstrapPresent } from '@/lib/vscodeBootstrap';
|
||||
|
||||
type ManagedRemoteTunnelPreset = {
|
||||
id: string;
|
||||
@@ -573,6 +574,12 @@ export const startDesktopWindowDrag = async (): Promise<boolean> => {
|
||||
};
|
||||
|
||||
export const isVSCodeRuntime = (): boolean => {
|
||||
// Prefer extension-host bootstrap config: it is injected in webview HTML
|
||||
// before any store module evaluates, so startup does not depend on
|
||||
// RuntimeAPIs registration order (see #2359).
|
||||
if (isVSCodeBootstrapPresent()) {
|
||||
return true;
|
||||
}
|
||||
const apis = getRegisteredRuntimeAPIs();
|
||||
return apis?.runtime?.isVSCode === true;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { afterEach, describe, expect, mock, test } from 'bun:test';
|
||||
|
||||
type RuntimeApisStub = { runtime?: { isVSCode?: boolean } } | null;
|
||||
|
||||
let registeredRuntimeApis: RuntimeApisStub = null;
|
||||
|
||||
mock.module('@/contexts/runtimeAPIRegistry', () => ({
|
||||
getRegisteredRuntimeAPIs: (): RuntimeApisStub => registeredRuntimeApis,
|
||||
}));
|
||||
|
||||
const { isVSCodeRuntime } = await import('./desktop');
|
||||
|
||||
describe('desktop isVSCodeRuntime bootstrap detection', () => {
|
||||
afterEach(() => {
|
||||
registeredRuntimeApis = null;
|
||||
delete (globalThis as { window?: unknown }).window;
|
||||
});
|
||||
|
||||
test('detects VS Code from bootstrap config before RuntimeAPIs register', () => {
|
||||
registeredRuntimeApis = null;
|
||||
(globalThis as { window: unknown }).window = {
|
||||
__VSCODE_CONFIG__: {
|
||||
workspaceFolder: '/Users/me/project-a',
|
||||
workspaceFolders: [{ name: 'project-a', path: '/Users/me/project-a' }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(isVSCodeRuntime()).toBe(true);
|
||||
});
|
||||
|
||||
test('falls back to registered RuntimeAPIs when bootstrap is absent', () => {
|
||||
registeredRuntimeApis = {
|
||||
runtime: { isVSCode: true },
|
||||
};
|
||||
(globalThis as { window: unknown }).window = {};
|
||||
|
||||
expect(isVSCodeRuntime()).toBe(true);
|
||||
});
|
||||
|
||||
test('does not classify an unregistered web runtime as VS Code', () => {
|
||||
registeredRuntimeApis = null;
|
||||
(globalThis as { window: unknown }).window = {};
|
||||
|
||||
expect(isVSCodeRuntime()).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { afterEach, describe, expect, test } from 'bun:test';
|
||||
import { getVSCodeBootstrapConfig, isVSCodeBootstrapPresent } from './vscodeBootstrap';
|
||||
|
||||
describe('VS Code bootstrap config', () => {
|
||||
afterEach(() => {
|
||||
delete (globalThis as { window?: unknown }).window;
|
||||
});
|
||||
|
||||
test('reads extension-host __VSCODE_CONFIG__ before RuntimeAPIs exist', () => {
|
||||
(globalThis as { window: unknown }).window = {
|
||||
__VSCODE_CONFIG__: {
|
||||
workspaceFolder: '/workspace/project-one',
|
||||
workspaceFolders: [{ name: 'project-one', path: '/workspace/project-one' }],
|
||||
},
|
||||
};
|
||||
|
||||
expect(getVSCodeBootstrapConfig()).toEqual({
|
||||
workspaceFolder: '/workspace/project-one',
|
||||
workspaceFolders: [{ name: 'project-one', path: '/workspace/project-one' }],
|
||||
});
|
||||
expect(isVSCodeBootstrapPresent()).toBe(true);
|
||||
});
|
||||
|
||||
test('treats missing window/bootstrap as not VS Code', () => {
|
||||
expect(getVSCodeBootstrapConfig()).toBeNull();
|
||||
expect(isVSCodeBootstrapPresent()).toBe(false);
|
||||
expect(isVSCodeBootstrapPresent(null)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Extension-host bootstrap config injected into the VS Code webview HTML
|
||||
* before any bundled module evaluates. Prefer this over RuntimeAPIs for
|
||||
* early VS Code detection during store module initialization.
|
||||
*/
|
||||
export interface VSCodeBootstrapConfig {
|
||||
workspaceFolder?: unknown;
|
||||
workspaceFolders?: unknown;
|
||||
}
|
||||
|
||||
export const getVSCodeBootstrapConfig = (): VSCodeBootstrapConfig | null => {
|
||||
if (typeof window === 'undefined') {
|
||||
return null;
|
||||
}
|
||||
return (window as unknown as { __VSCODE_CONFIG__?: VSCodeBootstrapConfig }).__VSCODE_CONFIG__ ?? null;
|
||||
};
|
||||
|
||||
export const isVSCodeBootstrapPresent = (
|
||||
bootstrapConfig: VSCodeBootstrapConfig | null = getVSCodeBootstrapConfig(),
|
||||
): boolean => Boolean(bootstrapConfig);
|
||||
@@ -2,6 +2,7 @@ import { create } from 'zustand';
|
||||
import { devtools } from 'zustand/middleware';
|
||||
import { opencodeClient } from '@/lib/opencode/client';
|
||||
import { getDesktopHomeDirectory, isVSCodeRuntime } from '@/lib/desktop';
|
||||
import { getVSCodeBootstrapConfig } from '@/lib/vscodeBootstrap';
|
||||
import { subscribeRuntimeEndpointChanged } from '@/lib/runtime-switch';
|
||||
import { updateDesktopSettings } from '@/lib/persistence';
|
||||
import { useFileSearchStore } from '@/stores/useFileSearchStore';
|
||||
@@ -227,7 +228,7 @@ const getVsCodeWorkspaceFolder = (): string | null => {
|
||||
if (!isVSCodeRuntime()) {
|
||||
return null;
|
||||
}
|
||||
const workspaceFolder = (window as unknown as { __VSCODE_CONFIG__?: { workspaceFolder?: unknown } }).__VSCODE_CONFIG__?.workspaceFolder;
|
||||
const workspaceFolder = getVSCodeBootstrapConfig()?.workspaceFolder;
|
||||
if (typeof workspaceFolder !== 'string' || workspaceFolder.trim().length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import type { RuntimeAPIs } from '@/lib/api/types';
|
||||
import { isVSCodeRuntime } from './vscodeRuntime';
|
||||
|
||||
describe('VS Code runtime detection', () => {
|
||||
@@ -9,6 +10,13 @@ describe('VS Code runtime detection', () => {
|
||||
})).toBe(true);
|
||||
});
|
||||
|
||||
test('uses registered runtime APIs when bootstrap is absent', () => {
|
||||
const runtimeApis = {
|
||||
runtime: { platform: 'vscode', isDesktop: false, isVSCode: true },
|
||||
} as RuntimeAPIs;
|
||||
expect(isVSCodeRuntime(runtimeApis, null)).toBe(true);
|
||||
});
|
||||
|
||||
test('does not classify an unregistered web runtime as VS Code', () => {
|
||||
expect(isVSCodeRuntime(null, null)).toBe(false);
|
||||
});
|
||||
|
||||
@@ -1,18 +1,14 @@
|
||||
import type { RuntimeAPIs } from '@/lib/api/types';
|
||||
import {
|
||||
getVSCodeBootstrapConfig,
|
||||
isVSCodeBootstrapPresent,
|
||||
type VSCodeBootstrapConfig,
|
||||
} from '@/lib/vscodeBootstrap';
|
||||
|
||||
export interface VSCodeBootstrapConfig {
|
||||
workspaceFolder?: unknown;
|
||||
workspaceFolders?: unknown;
|
||||
}
|
||||
|
||||
export const getVSCodeBootstrapConfig = (): VSCodeBootstrapConfig | null => {
|
||||
if (typeof window === 'undefined') {
|
||||
return null;
|
||||
}
|
||||
return (window as unknown as { __VSCODE_CONFIG__?: VSCodeBootstrapConfig }).__VSCODE_CONFIG__ ?? null;
|
||||
};
|
||||
export type { VSCodeBootstrapConfig };
|
||||
export { getVSCodeBootstrapConfig };
|
||||
|
||||
export const isVSCodeRuntime = (
|
||||
runtimeApis: RuntimeAPIs | null,
|
||||
bootstrapConfig = getVSCodeBootstrapConfig(),
|
||||
): boolean => Boolean(bootstrapConfig || runtimeApis?.runtime?.isVSCode);
|
||||
bootstrapConfig: VSCodeBootstrapConfig | null = getVSCodeBootstrapConfig(),
|
||||
): boolean => Boolean(isVSCodeBootstrapPresent(bootstrapConfig) || runtimeApis?.runtime?.isVSCode);
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { afterEach, describe, expect, mock, test } from 'bun:test';
|
||||
|
||||
/**
|
||||
* Integration-style coverage for #2359: store modules evaluate before
|
||||
* RuntimeAPIs registration, with only extension-host __VSCODE_CONFIG__ present
|
||||
* and a stale lastDirectory in storage.
|
||||
*/
|
||||
|
||||
const WORKSPACE = '/tmp/oc-ws-project-a';
|
||||
const STALE = '/tmp/oc-ws-other';
|
||||
|
||||
const storage = new Map<string, string>([
|
||||
['lastDirectory', STALE],
|
||||
['homeDirectory', STALE],
|
||||
]);
|
||||
|
||||
const installWindow = () => {
|
||||
(globalThis as { window: unknown }).window = {
|
||||
__VSCODE_CONFIG__: {
|
||||
workspaceFolder: WORKSPACE,
|
||||
workspaceFolders: [{ name: 'oc-ws-project-a', path: WORKSPACE }],
|
||||
},
|
||||
__OPENCHAMBER_HOME__: WORKSPACE,
|
||||
localStorage: {
|
||||
getItem: (key: string) => storage.get(key) ?? null,
|
||||
setItem: (key: string, value: string) => {
|
||||
storage.set(key, String(value));
|
||||
},
|
||||
removeItem: (key: string) => {
|
||||
storage.delete(key);
|
||||
},
|
||||
},
|
||||
matchMedia: () => ({ matches: false, addListener() {}, removeListener() {}, addEventListener() {}, removeEventListener() {} }),
|
||||
};
|
||||
(globalThis as { localStorage: unknown }).localStorage = (globalThis as { window: { localStorage: unknown } }).window.localStorage;
|
||||
};
|
||||
|
||||
mock.module('@/contexts/runtimeAPIRegistry', () => ({
|
||||
getRegisteredRuntimeAPIs: () => null,
|
||||
}));
|
||||
|
||||
mock.module('@/lib/opencode/client', () => ({
|
||||
opencodeClient: {
|
||||
setDirectory: () => undefined,
|
||||
getDirectory: () => WORKSPACE,
|
||||
getFilesystemHome: async () => WORKSPACE,
|
||||
getSystemInfo: async () => ({ homeDirectory: WORKSPACE }),
|
||||
},
|
||||
}));
|
||||
|
||||
mock.module('@/lib/persistence', () => ({
|
||||
updateDesktopSettings: async () => undefined,
|
||||
}));
|
||||
|
||||
mock.module('@/lib/runtime-switch', () => ({
|
||||
subscribeRuntimeEndpointChanged: () => () => undefined,
|
||||
getRuntimeApiBaseUrl: () => 'http://127.0.0.1:9',
|
||||
getRuntimeKey: () => 'test',
|
||||
}));
|
||||
|
||||
mock.module('@/stores/useFileSearchStore', () => ({
|
||||
useFileSearchStore: {
|
||||
getState: () => ({ clearCache: () => undefined }),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('VS Code store init before RuntimeAPIs (#2359)', () => {
|
||||
afterEach(() => {
|
||||
delete (globalThis as { window?: unknown }).window;
|
||||
delete (globalThis as { localStorage?: unknown }).localStorage;
|
||||
});
|
||||
|
||||
test('desktop isVSCodeRuntime prefers bootstrap config', async () => {
|
||||
installWindow();
|
||||
const { isVSCodeRuntime } = await import('@/lib/desktop');
|
||||
expect(isVSCodeRuntime()).toBe(true);
|
||||
});
|
||||
|
||||
test('projects helper derives workspace projects without RuntimeAPIs', async () => {
|
||||
installWindow();
|
||||
const { getVSCodeBootstrapConfig, isVSCodeRuntime } = await import('@/stores/utils/vscodeRuntime');
|
||||
const config = getVSCodeBootstrapConfig();
|
||||
expect(isVSCodeRuntime(null, config)).toBe(true);
|
||||
expect(config?.workspaceFolder).toBe(WORKSPACE);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user