|
| 1 | +// Copyright 2025 The Chromium Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as Common from '../../../core/common/common.js'; |
| 6 | +import * as Host from '../../../core/host/host.js'; |
| 7 | +import * as PanelCommon from '../../../panels/common/common.js'; |
| 8 | +import {renderElementIntoDOM} from '../../../testing/DOMHelpers.js'; |
| 9 | +import {describeWithEnvironment, updateHostConfig} from '../../../testing/EnvironmentHelpers.js'; |
| 10 | +import * as CodeMirror from '../../../third_party/codemirror.next/codemirror.next.js'; |
| 11 | + |
| 12 | +import {AiCodeGenerationProvider, TextEditor} from './text_editor.js'; |
| 13 | + |
| 14 | +function createEditorWithProvider(doc: string): |
| 15 | + {editor: TextEditor.TextEditor, provider: AiCodeGenerationProvider.AiCodeGenerationProvider} { |
| 16 | + const provider = AiCodeGenerationProvider.AiCodeGenerationProvider.createInstance(); |
| 17 | + const editor = new TextEditor.TextEditor( |
| 18 | + CodeMirror.EditorState.create({ |
| 19 | + doc, |
| 20 | + extensions: [ |
| 21 | + provider.extension(), |
| 22 | + ], |
| 23 | + }), |
| 24 | + ); |
| 25 | + renderElementIntoDOM(editor); |
| 26 | + provider.editorInitialized(editor); |
| 27 | + return {editor, provider}; |
| 28 | +} |
| 29 | + |
| 30 | +describeWithEnvironment('AiCodeGenerationProvider', () => { |
| 31 | + let clock: sinon.SinonFakeTimers; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + clock = sinon.useFakeTimers(); |
| 35 | + updateHostConfig({ |
| 36 | + devToolsAiCodeGeneration: { |
| 37 | + enabled: true, |
| 38 | + }, |
| 39 | + aidaAvailability: { |
| 40 | + enabled: true, |
| 41 | + blockedByAge: false, |
| 42 | + blockedByGeo: false, |
| 43 | + } |
| 44 | + }); |
| 45 | + sinon.stub(Host.AidaClient.AidaClient, 'checkAccessPreconditions') |
| 46 | + .resolves(Host.AidaClient.AidaAccessPreconditions.AVAILABLE); |
| 47 | + sinon.stub(Host.AidaClient.HostConfigTracker, 'instance').returns({ |
| 48 | + addEventListener: () => {}, |
| 49 | + removeEventListener: () => {}, |
| 50 | + dispose: () => {}, |
| 51 | + } as unknown as Host.AidaClient.HostConfigTracker); |
| 52 | + Common.Settings.Settings.instance().settingForTest('ai-code-completion-enabled').set(true); |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + clock.restore(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('does not create a provider when the feature is disabled', () => { |
| 60 | + updateHostConfig({ |
| 61 | + devToolsAiCodeGeneration: { |
| 62 | + enabled: false, |
| 63 | + }, |
| 64 | + }); |
| 65 | + assert.throws(() => createEditorWithProvider(''), 'AI code generation feature is not enabled.'); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('Teaser decoration', () => { |
| 69 | + it('shows teaser when cursor is at the end of a comment line', async () => { |
| 70 | + const {editor, provider} = createEditorWithProvider('// Hello'); |
| 71 | + editor.dispatch({selection: {anchor: 8}}); |
| 72 | + await clock.tickAsync(0); |
| 73 | + assert.isNotNull(editor.editor.dom.querySelector('.cm-placeholder')); |
| 74 | + provider.dispose(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('hides teaser when cursor is not at the end of the line', async () => { |
| 78 | + const {editor, provider} = createEditorWithProvider('// Hello'); |
| 79 | + editor.dispatch({selection: {anchor: 5}}); |
| 80 | + await clock.tickAsync(0); |
| 81 | + assert.isNull(editor.editor.dom.querySelector('.cm-placeholder')); |
| 82 | + provider.dispose(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('hides teaser when the line is not a comment', async () => { |
| 86 | + const {editor, provider} = createEditorWithProvider('console'); |
| 87 | + editor.dispatch({selection: {anchor: 7}}); |
| 88 | + await clock.tickAsync(0); |
| 89 | + assert.isNull(editor.editor.dom.querySelector('.cm-placeholder')); |
| 90 | + provider.dispose(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('hides teaser when mode is DISMISSED', async () => { |
| 94 | + const {editor, provider} = createEditorWithProvider('// Hello'); |
| 95 | + editor.dispatch({selection: {anchor: 8}}); |
| 96 | + await clock.tickAsync(0); |
| 97 | + assert.isNotNull(editor.editor.dom.querySelector('.cm-placeholder')); |
| 98 | + |
| 99 | + editor.dispatch({ |
| 100 | + effects: AiCodeGenerationProvider.setAiCodeGenerationTeaserMode.of( |
| 101 | + AiCodeGenerationProvider.AiCodeGenerationTeaserMode.DISMISSED) |
| 102 | + }); |
| 103 | + await clock.tickAsync(0); |
| 104 | + assert.isNull(editor.editor.dom.querySelector('.cm-placeholder')); |
| 105 | + provider.dispose(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('shows teaser again after a document change', async () => { |
| 109 | + const {editor, provider} = createEditorWithProvider('// Hello'); |
| 110 | + editor.dispatch({selection: {anchor: 8}}); |
| 111 | + await clock.tickAsync(0); |
| 112 | + assert.isNotNull(editor.editor.dom.querySelector('.cm-placeholder')); |
| 113 | + |
| 114 | + editor.dispatch({ |
| 115 | + effects: AiCodeGenerationProvider.setAiCodeGenerationTeaserMode.of( |
| 116 | + AiCodeGenerationProvider.AiCodeGenerationTeaserMode.DISMISSED) |
| 117 | + }); |
| 118 | + await clock.tickAsync(0); |
| 119 | + assert.isNull(editor.editor.dom.querySelector('.cm-placeholder')); |
| 120 | + |
| 121 | + editor.dispatch({changes: {from: 8, insert: 'W'}, selection: {anchor: 9}}); |
| 122 | + await clock.tickAsync(0); |
| 123 | + assert.isNotNull(editor.editor.dom.querySelector('.cm-placeholder')); |
| 124 | + provider.dispose(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('Editor keymap', () => { |
| 129 | + it('dismisses teaser on Escape when loading', async () => { |
| 130 | + const {editor, provider} = createEditorWithProvider('// Hello'); |
| 131 | + sinon.stub(PanelCommon.AiCodeGenerationTeaser.prototype, 'loading').value(true); |
| 132 | + sinon.stub(PanelCommon.AiCodeGenerationTeaser.prototype, 'isShowing').returns(true); |
| 133 | + editor.dispatch({selection: {anchor: 8}}); |
| 134 | + await clock.tickAsync(0); |
| 135 | + |
| 136 | + const dispatchSpy = sinon.spy(editor, 'dispatch'); |
| 137 | + editor.editor.contentDOM.dispatchEvent(new KeyboardEvent('keydown', {key: 'Escape'})); |
| 138 | + |
| 139 | + sinon.assert.calledOnce(dispatchSpy); |
| 140 | + sinon.assert.calledWith(dispatchSpy, { |
| 141 | + effects: AiCodeGenerationProvider.setAiCodeGenerationTeaserMode.of( |
| 142 | + AiCodeGenerationProvider.AiCodeGenerationTeaserMode.DISMISSED) |
| 143 | + }); |
| 144 | + provider.dispose(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('triggers loading state on Ctrl+I', async () => { |
| 148 | + const {editor, provider} = createEditorWithProvider('// Hello'); |
| 149 | + const generationTeaser = sinon.spy(PanelCommon.AiCodeGenerationTeaser.prototype, 'loading', ['set']); |
| 150 | + sinon.stub(PanelCommon.AiCodeGenerationTeaser.prototype, 'isShowing').returns(true); |
| 151 | + editor.dispatch({selection: {anchor: 8}}); |
| 152 | + await clock.tickAsync(0); |
| 153 | + |
| 154 | + const event = new KeyboardEvent('keydown', { |
| 155 | + key: 'i', |
| 156 | + ctrlKey: Host.Platform.isMac() ? false : true, |
| 157 | + metaKey: Host.Platform.isMac() ? true : false, |
| 158 | + }); |
| 159 | + editor.editor.contentDOM.dispatchEvent(event); |
| 160 | + |
| 161 | + sinon.assert.calledOnce(generationTeaser.set); |
| 162 | + sinon.assert.calledWith(generationTeaser.set, true); |
| 163 | + provider.dispose(); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments