fix(ui): complete VS Code bootstrap detection for directory startup (#2359)
This commit is contained in:
@@ -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