|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | +import { DisposableStore } from 'vs/base/common/lifecycle'; |
| 8 | +import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; |
| 9 | +import { Range } from 'vs/editor/common/core/range'; |
| 10 | +import { instantiateTestCodeEditor } from 'vs/editor/test/browser/testCodeEditor'; |
| 11 | +import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors'; |
| 12 | +import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection'; |
| 13 | +import { InteractiveEditorController, InteractiveEditorRunOptions, State } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorController'; |
| 14 | +import { IInteractiveEditorSessionService, InteractiveEditorSessionService } from 'vs/workbench/contrib/interactiveEditor/browser/interactiveEditorSession'; |
| 15 | +import { IInteractiveEditorService, InteractiveEditorResponseType } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditor'; |
| 16 | +import { InteractiveEditorServiceImpl } from 'vs/workbench/contrib/interactiveEditor/common/interactiveEditorServiceImpl'; |
| 17 | +import { workbenchInstantiationService } from 'vs/workbench/test/browser/workbenchTestServices'; |
| 18 | +import { MockContextKeyService } from 'vs/platform/keybinding/test/common/mockKeybindingService'; |
| 19 | +import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; |
| 20 | +import { TestInstantiationService } from 'vs/platform/instantiation/test/common/instantiationServiceMock'; |
| 21 | +import { IModelService } from 'vs/editor/common/services/model'; |
| 22 | +import { ITextModel } from 'vs/editor/common/model'; |
| 23 | +import { IEditorProgressService, IProgressRunner } from 'vs/platform/progress/common/progress'; |
| 24 | +import { mock } from 'vs/base/test/common/mock'; |
| 25 | +import { Emitter, Event } from 'vs/base/common/event'; |
| 26 | + |
| 27 | +suite('InteractiveEditorController', function () { |
| 28 | + |
| 29 | + class TestController extends InteractiveEditorController { |
| 30 | + |
| 31 | + private readonly _onDidChangeState = new Emitter<State>(); |
| 32 | + readonly onDidChangeState: Event<State> = this._onDidChangeState.event; |
| 33 | + |
| 34 | + readonly states: readonly State[] = []; |
| 35 | + |
| 36 | + protected override _nextState(state: State, options: InteractiveEditorRunOptions | undefined): Promise<void> { |
| 37 | + this._onDidChangeState.fire(state); |
| 38 | + (<State[]>this.states).push(state); |
| 39 | + return super._nextState(state, options); |
| 40 | + } |
| 41 | + |
| 42 | + override dispose() { |
| 43 | + super.dispose(); |
| 44 | + this._onDidChangeState.dispose(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const store = new DisposableStore(); |
| 49 | + let editor: ICodeEditor; |
| 50 | + let model: ITextModel; |
| 51 | + let ctrl: TestController; |
| 52 | + // let contextKeys: MockContextKeyService; |
| 53 | + let interactiveEditorService: InteractiveEditorServiceImpl; |
| 54 | + let instaService: TestInstantiationService; |
| 55 | + |
| 56 | + setup(function () { |
| 57 | + |
| 58 | + const contextKeyService = new MockContextKeyService(); |
| 59 | + interactiveEditorService = new InteractiveEditorServiceImpl(contextKeyService); |
| 60 | + |
| 61 | + const serviceCollection = new ServiceCollection( |
| 62 | + [IContextKeyService, contextKeyService], |
| 63 | + [IInteractiveEditorService, interactiveEditorService], |
| 64 | + [IInteractiveEditorSessionService, new SyncDescriptor(InteractiveEditorSessionService)], |
| 65 | + [IEditorProgressService, new class extends mock<IEditorProgressService>() { |
| 66 | + override show(total: unknown, delay?: unknown): IProgressRunner { |
| 67 | + return { |
| 68 | + total() { }, |
| 69 | + worked(value) { }, |
| 70 | + done() { }, |
| 71 | + }; |
| 72 | + } |
| 73 | + }] |
| 74 | + ); |
| 75 | + |
| 76 | + instaService = workbenchInstantiationService(undefined, store).createChild(serviceCollection); |
| 77 | + |
| 78 | + model = instaService.get(IModelService).createModel('Hello\nWorld\nHello Again\nHello World\n', null); |
| 79 | + editor = instantiateTestCodeEditor(instaService, model); |
| 80 | + |
| 81 | + store.add(interactiveEditorService.addProvider({ |
| 82 | + debugName: 'Unit Test', |
| 83 | + prepareInteractiveEditorSession() { |
| 84 | + return { |
| 85 | + id: Math.random() |
| 86 | + }; |
| 87 | + }, |
| 88 | + provideResponse(session, request) { |
| 89 | + return { |
| 90 | + type: InteractiveEditorResponseType.EditorEdit, |
| 91 | + id: Math.random(), |
| 92 | + edits: [{ |
| 93 | + range: new Range(1, 1, 1, 1), |
| 94 | + text: request.prompt |
| 95 | + }] |
| 96 | + }; |
| 97 | + } |
| 98 | + })); |
| 99 | + }); |
| 100 | + |
| 101 | + teardown(function () { |
| 102 | + editor.dispose(); |
| 103 | + model.dispose(); |
| 104 | + store.clear(); |
| 105 | + ctrl?.dispose(); |
| 106 | + }); |
| 107 | + |
| 108 | + test('creation, not showing anything', function () { |
| 109 | + ctrl = instaService.createInstance(TestController, editor); |
| 110 | + assert.ok(ctrl); |
| 111 | + assert.strictEqual(ctrl.getWidgetPosition(), undefined); |
| 112 | + }); |
| 113 | + |
| 114 | + test('run (show/hide)', async function () { |
| 115 | + ctrl = instaService.createInstance(TestController, editor); |
| 116 | + const run = ctrl.run({ message: 'Hello', autoSend: true }); |
| 117 | + |
| 118 | + await Event.toPromise(Event.filter(ctrl.onDidChangeState, e => e === State.SHOW_RESPONSE)); |
| 119 | + assert.ok(ctrl.getWidgetPosition() !== undefined); |
| 120 | + |
| 121 | + await ctrl.cancelSession(); |
| 122 | + |
| 123 | + await run; |
| 124 | + |
| 125 | + assert.ok(ctrl.getWidgetPosition() === undefined); |
| 126 | + }); |
| 127 | +}); |
0 commit comments