Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions packages/debug/__tests__/browser/debug-session.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Disposable, Emitter } from '@opensumi/ide-core-common';
import { DebugSession } from '@opensumi/ide-debug/lib/browser/debug-session';

import type { DebugSessionOptions } from '@opensumi/ide-debug/lib/common';

const createSession = () => {
const connection = {
disposed: false,
onRequest: jest.fn(),
on: jest.fn(() => Disposable.create(() => {})),
onDidCustomEvent: new Emitter<any>().event,
dispose: jest.fn(),
};
const breakpointManager = {
breakpointsEnabled: false,
onDidChangeBreakpoints: jest.fn(() => Disposable.create(() => {})),
onDidChangeExceptionsBreakpoints: jest.fn(() => Disposable.create(() => {})),
clearAllStatus: jest.fn(),
};
const options: DebugSessionOptions = {
configuration: {
name: 'test',
type: 'node',
request: 'launch',
},
index: 0,
};

return new DebugSession(
'session-1',
options,
connection as any,
{} as any,
{} as any,
breakpointManager as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
);
};

describe('DebugSession evaluate', () => {
it('fires variable change for repl evaluation', async () => {
const session = createSession();
session.sendRequest = jest.fn().mockResolvedValue({
body: {
result: '1',
},
});
const onVariableChange = jest.fn();

session.onVariableChange(onVariableChange);
await session.evaluate('t = 1', 'repl');

expect(onVariableChange).toHaveBeenCalledTimes(1);
});

it('does not fire variable change for watch evaluation', async () => {
const session = createSession();
session.sendRequest = jest.fn().mockResolvedValue({
body: {
result: '1',
},
});
const onVariableChange = jest.fn();

session.onVariableChange(onVariableChange);
await session.evaluate('t', 'watch');

expect(onVariableChange).not.toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { DebugContextKey } from './../../../../src/browser/contextkeys/debug-con
describe('Debug Variables Tree Model', () => {
const mockInjector = createBrowserInjector([]);
let debugVariablesModelService: DebugVariablesModelService;
let viewModelChangeListener: (() => void | Promise<void>) | undefined;
let variableChangeListener: (() => void | Promise<void>) | undefined;
const mockDebugHoverSource = {
onDidChange: jest.fn(() => Disposable.create(() => {})),
} as any;
Expand Down Expand Up @@ -54,7 +56,11 @@ describe('Debug Variables Tree Model', () => {
} as any;

const mockDebugViewModel = {
onDidChange: jest.fn(),
currentSession: undefined as any,
onDidChange: jest.fn((listener) => {
viewModelChangeListener = listener;
return Disposable.create(() => {});
}),
};

beforeAll(() => {
Expand Down Expand Up @@ -113,6 +119,7 @@ describe('Debug Variables Tree Model', () => {
expect(typeof debugVariablesModelService.dispose).toBe('function');
expect(typeof debugVariablesModelService.onDidUpdateTreeModel).toBe('function');
expect(typeof debugVariablesModelService.initTreeModel).toBe('function');
expect(typeof debugVariablesModelService.refresh).toBe('function');
expect(typeof debugVariablesModelService.initDecorations).toBe('function');
expect(typeof debugVariablesModelService.activeNodeDecoration).toBe('function');
expect(typeof debugVariablesModelService.activeNodeActivedDecoration).toBe('function');
Expand All @@ -134,6 +141,79 @@ describe('Debug Variables Tree Model', () => {
expect(mockDebugViewModel.onDidChange).toHaveBeenCalledTimes(1);
});

it('refreshes when current session variables change', async () => {
const mockSession = {
on: jest.fn(),
onVariableChange: jest.fn((listener) => {
variableChangeListener = listener;
return Disposable.create(() => {});
}),
} as any;
const refreshSpy = jest.spyOn(debugVariablesModelService, 'refresh').mockResolvedValue();

mockDebugViewModel.currentSession = mockSession;
await viewModelChangeListener?.();
await variableChangeListener?.();

expect(mockSession.onVariableChange).toHaveBeenCalledTimes(1);
expect(refreshSpy).toHaveBeenCalledTimes(1);
});

it('restores expanded Locals and Globals from cached scope state', async () => {
const localsRawScope = { name: 'Locals', expensive: false };
const globalsRawScope = { name: 'Globals', expensive: false };
const oldLocalsScope = {
expanded: true,
variablesReference: 1,
getRawScope: () => localsRawScope,
};
const oldGlobalsScope = {
expanded: true,
variablesReference: 2,
getRawScope: () => globalsRawScope,
};
const refreshedLocalsScope = {
expanded: false,
variablesReference: 101,
children: [],
setExpanded: jest.fn(async () => {
refreshedLocalsScope.expanded = true;
}),
getRawScope: () => ({ name: 'Locals', expensive: false }),
};
const refreshedGlobalsScope = {
expanded: false,
variablesReference: 102,
children: [],
setExpanded: jest.fn(async () => {
refreshedGlobalsScope.expanded = true;
}),
getRawScope: () => ({ name: 'Globals', expensive: false }),
};
const refreshedClosureScope = {
expanded: false,
variablesReference: 103,
children: [],
setExpanded: jest.fn(async () => {
refreshedClosureScope.expanded = true;
}),
getRawScope: () => ({ name: 'Closure', expensive: false }),
};

(debugVariablesModelService as any).keepExpandedScopesModel.set(oldLocalsScope);
(debugVariablesModelService as any).keepExpandedScopesModel.set(oldGlobalsScope);

await (debugVariablesModelService as any).restoreExpandedScopes([
refreshedLocalsScope,
refreshedGlobalsScope,
refreshedClosureScope,
]);

expect(refreshedLocalsScope.setExpanded).toHaveBeenCalledTimes(1);
expect(refreshedGlobalsScope.setExpanded).toHaveBeenCalledTimes(1);
expect(refreshedClosureScope.setExpanded).not.toHaveBeenCalled();
});

it('initTreeModel method should be work', () => {
const mockSession = {
on: jest.fn(),
Expand Down
3 changes: 3 additions & 0 deletions packages/debug/src/browser/debug-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,9 @@ export class DebugSession implements IDebugSession {

// 在 VS Code JavaScript Debugger 中,如果一个表达式取值为 `undefined`, 这里将不会返回结果
const response = await this.sendRequest('evaluate', { expression, frameId, context });
if (context === 'repl') {
this._onVariableChange.fire();
}
return response.body;
}

Expand Down
Loading
Loading