|
| 1 | +/** |
| 2 | + * Copyright 2026 Arm Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +// generated with AI |
| 17 | + |
| 18 | +import * as vscode from 'vscode'; |
| 19 | +import { SourceFileHighlighting } from './source-file-highlighting'; |
| 20 | +import { debugSessionFactory, extensionContextFactory } from '../../__test__/vscode.factory'; |
| 21 | + |
| 22 | +type ActiveDebugSessionListener = (session: vscode.DebugSession | undefined) => void; |
| 23 | +type ActiveTextEditorListener = (editor: vscode.TextEditor | undefined) => void; |
| 24 | + |
| 25 | +function makeDocument(options: { |
| 26 | + fileName?: string; |
| 27 | + lineCount?: number; |
| 28 | + scheme?: string; |
| 29 | +} = {}): vscode.TextDocument { |
| 30 | + const fileName = options.fileName ?? '/workspace/source/main.c'; |
| 31 | + const uri = options.scheme === 'file' || options.scheme === undefined |
| 32 | + ? vscode.Uri.file(fileName) |
| 33 | + : { ...vscode.Uri.file(fileName), scheme: options.scheme }; |
| 34 | + return { |
| 35 | + fileName, |
| 36 | + uri, |
| 37 | + lineCount: options.lineCount ?? 12 |
| 38 | + } as vscode.TextDocument; |
| 39 | +} |
| 40 | + |
| 41 | +function makeEditor(document: vscode.TextDocument = makeDocument()): jest.Mocked<vscode.TextEditor> { |
| 42 | + return { |
| 43 | + document, |
| 44 | + setDecorations: jest.fn() |
| 45 | + } as unknown as jest.Mocked<vscode.TextEditor>; |
| 46 | +} |
| 47 | + |
| 48 | +async function waitForAsyncCallbacks(): Promise<void> { |
| 49 | + await Promise.resolve(); |
| 50 | + await Promise.resolve(); |
| 51 | +} |
| 52 | + |
| 53 | +describe('SourceFileHighlighting', () => { |
| 54 | + let activeDebugSessionListener: ActiveDebugSessionListener; |
| 55 | + let activeTextEditorListener: ActiveTextEditorListener; |
| 56 | + let debugSessionDisposable: vscode.Disposable; |
| 57 | + let editorDisposable: vscode.Disposable; |
| 58 | + |
| 59 | + beforeEach(() => { |
| 60 | + jest.clearAllMocks(); |
| 61 | + debugSessionDisposable = { dispose: jest.fn() }; |
| 62 | + editorDisposable = { dispose: jest.fn() }; |
| 63 | + (vscode.debug.onDidChangeActiveDebugSession as jest.Mock).mockImplementation((listener: ActiveDebugSessionListener) => { |
| 64 | + activeDebugSessionListener = listener; |
| 65 | + return debugSessionDisposable; |
| 66 | + }); |
| 67 | + (vscode.window.onDidChangeActiveTextEditor as jest.Mock).mockImplementation((listener: ActiveTextEditorListener) => { |
| 68 | + activeTextEditorListener = listener; |
| 69 | + return editorDisposable; |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + it('registers active debug session and active editor listeners', () => { |
| 74 | + const context = extensionContextFactory(); |
| 75 | + const sourceFileHighlighting = new SourceFileHighlighting(context); |
| 76 | + |
| 77 | + sourceFileHighlighting.activate(); |
| 78 | + |
| 79 | + expect(vscode.debug.onDidChangeActiveDebugSession).toHaveBeenCalledWith(expect.any(Function)); |
| 80 | + expect(vscode.window.onDidChangeActiveTextEditor).toHaveBeenCalledWith(expect.any(Function)); |
| 81 | + expect(context.subscriptions).toContain(debugSessionDisposable); |
| 82 | + }); |
| 83 | + |
| 84 | + it('requests breakpoint locations for the active file and highlights unique executable lines', async () => { |
| 85 | + const context = extensionContextFactory(); |
| 86 | + const sourceFileHighlighting = new SourceFileHighlighting(context); |
| 87 | + const debugSession = debugSessionFactory({ name: 'test-session', type: 'gdbtarget', request: 'launch' }); |
| 88 | + (debugSession.customRequest as jest.Mock).mockResolvedValueOnce({ |
| 89 | + breakpoints: [ |
| 90 | + { line: 2 }, |
| 91 | + { line: 7 }, |
| 92 | + { line: 2 } |
| 93 | + ] |
| 94 | + }); |
| 95 | + const editor = makeEditor(makeDocument({ fileName: '/workspace/source/main.c', lineCount: 20 })); |
| 96 | + |
| 97 | + sourceFileHighlighting.activate(); |
| 98 | + activeDebugSessionListener(debugSession); |
| 99 | + activeTextEditorListener(editor); |
| 100 | + await waitForAsyncCallbacks(); |
| 101 | + |
| 102 | + expect(debugSession.customRequest).toHaveBeenCalledWith('breakpointLocations', { |
| 103 | + source: { path: '/workspace/source/main.c' }, |
| 104 | + line: 1, |
| 105 | + endLine: 20 |
| 106 | + }); |
| 107 | + expect(editor.setDecorations).toHaveBeenCalledTimes(1); |
| 108 | + expect(editor.setDecorations).toHaveBeenCalledWith( |
| 109 | + (vscode.window.createTextEditorDecorationType as jest.Mock).mock.results[0].value, |
| 110 | + [ |
| 111 | + { range: new vscode.Range(1, 0, 1, 0) }, |
| 112 | + { range: new vscode.Range(6, 0, 6, 0) } |
| 113 | + ] |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('does not request breakpoint locations before an active debug session exists', async () => { |
| 118 | + const context = extensionContextFactory(); |
| 119 | + const sourceFileHighlighting = new SourceFileHighlighting(context); |
| 120 | + const debugSession = debugSessionFactory({ name: 'test-session', type: 'gdbtarget', request: 'launch' }); |
| 121 | + const editor = makeEditor(); |
| 122 | + |
| 123 | + sourceFileHighlighting.activate(); |
| 124 | + activeTextEditorListener(editor); |
| 125 | + await waitForAsyncCallbacks(); |
| 126 | + |
| 127 | + expect(debugSession.customRequest).not.toHaveBeenCalled(); |
| 128 | + expect(editor.setDecorations).not.toHaveBeenCalled(); |
| 129 | + }); |
| 130 | + |
| 131 | + it('does not request breakpoint locations for non-file editors', async () => { |
| 132 | + const context = extensionContextFactory(); |
| 133 | + const sourceFileHighlighting = new SourceFileHighlighting(context); |
| 134 | + const debugSession = debugSessionFactory({ name: 'test-session', type: 'gdbtarget', request: 'launch' }); |
| 135 | + const editor = makeEditor(makeDocument({ scheme: 'untitled' })); |
| 136 | + |
| 137 | + sourceFileHighlighting.activate(); |
| 138 | + activeDebugSessionListener(debugSession); |
| 139 | + activeTextEditorListener(editor); |
| 140 | + await waitForAsyncCallbacks(); |
| 141 | + |
| 142 | + expect(debugSession.customRequest).not.toHaveBeenCalled(); |
| 143 | + expect(editor.setDecorations).not.toHaveBeenCalled(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('does not decorate when the adapter returns no breakpoint locations', async () => { |
| 147 | + const context = extensionContextFactory(); |
| 148 | + const sourceFileHighlighting = new SourceFileHighlighting(context); |
| 149 | + const debugSession = debugSessionFactory({ name: 'test-session', type: 'gdbtarget', request: 'launch' }); |
| 150 | + (debugSession.customRequest as jest.Mock).mockResolvedValueOnce(undefined); |
| 151 | + const editor = makeEditor(); |
| 152 | + |
| 153 | + sourceFileHighlighting.activate(); |
| 154 | + activeDebugSessionListener(debugSession); |
| 155 | + activeTextEditorListener(editor); |
| 156 | + await waitForAsyncCallbacks(); |
| 157 | + |
| 158 | + expect(debugSession.customRequest).toHaveBeenCalledWith('breakpointLocations', { |
| 159 | + source: { path: '/workspace/source/main.c' }, |
| 160 | + line: 1, |
| 161 | + endLine: 12 |
| 162 | + }); |
| 163 | + expect(editor.setDecorations).not.toHaveBeenCalled(); |
| 164 | + }); |
| 165 | +}); |
0 commit comments