Skip to content

Commit f916795

Browse files
ergunshDevtools-frontend LUCI CQ
authored andcommitted
Reland "[PatchWidget] Handle discard and saveAll actions"
This reverts commit 6b151b0. Reason for revert: Relanding after fixing test failures (We weren't setting the selected workspace in the test, and weren't resetting `workspaceDiff` before tests) Original change's description: > Revert "[PatchWidget] Handle `discard` and `saveAll` actions" > > This reverts commit 34e26e6. > > Reason for revert: Waterfall turned red with this CL. > > Original change's description: > > [PatchWidget] Handle `discard` and `saveAll` actions > > > > Fixed: 399559699 > > Change-Id: I14073f0e3b8107ff4b632c0b87c7c90aeec1feab > > Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6348560 > > Reviewed-by: Wolfgang Beyer <[email protected]> > > Reviewed-by: Alex Rudenko <[email protected]> > > Commit-Queue: Ergün Erdoğmuş <[email protected]> > > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Change-Id: I9ededca126a5c3c2350f6df87e1853e4ec82f571 > Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6357069 > Commit-Queue: Rubber Stamper <[email protected]> > Bot-Commit: Rubber Stamper <[email protected]> > Auto-Submit: Philip Pfaffe <[email protected]> Bug: 399559699 Change-Id: I08a1117ee6a01377421ec85917bf2fc85fc0d7a4 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6357070 Reviewed-by: Alex Rudenko <[email protected]> Commit-Queue: Ergün Erdoğmuş <[email protected]>
1 parent bb89126 commit f916795

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

front_end/models/workspace_diff/WorkspaceDiff.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ export interface UISourceCodeDiffEventTypes {
304304

305305
let workspaceDiffImplInstance: WorkspaceDiffImpl|null = null;
306306

307-
export function workspaceDiff(): WorkspaceDiffImpl {
308-
if (!workspaceDiffImplInstance) {
307+
export function workspaceDiff({forceNew}: {forceNew?: boolean} = {}): WorkspaceDiffImpl {
308+
if (!workspaceDiffImplInstance || forceNew) {
309309
workspaceDiffImplInstance = new WorkspaceDiffImpl(Workspace.Workspace.WorkspaceImpl.instance());
310310
}
311311
return workspaceDiffImplInstance;

front_end/panels/ai_assistance/PatchWidget.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as PanelCommon from '../../panels/common/common.js';
99
import {
1010
cleanup,
1111
createPatchWidget,
12+
createPatchWidgetWithDiffView,
1213
createTestFilesystem,
1314
initializePersistenceImplForTests,
1415
mockAidaClient,
@@ -176,4 +177,51 @@ Files:
176177
assert.strictEqual(input.projectName, 'test2');
177178
});
178179
});
180+
181+
describe('diff view', () => {
182+
let uiSourceCode: Workspace.UISourceCode.UISourceCode;
183+
let commitWorkingCopyStub:
184+
sinon.SinonStub<Parameters<typeof Workspace.UISourceCode.UISourceCode.prototype.commitWorkingCopy>>;
185+
let resetWorkingCopyStub:
186+
sinon.SinonStub<Parameters<typeof Workspace.UISourceCode.UISourceCode.prototype.resetWorkingCopy>>;
187+
188+
beforeEach(() => {
189+
uiSourceCode = createTestFilesystem('file://test').uiSourceCode;
190+
Common.Settings.Settings.instance().createSetting('ai-assistance-patching-selected-project-id', 'file://test');
191+
updateHostConfig({
192+
devToolsFreestyler: {
193+
enabled: true,
194+
patching: true,
195+
},
196+
});
197+
198+
commitWorkingCopyStub =
199+
sinon.stub(Workspace.UISourceCode.UISourceCode.prototype, 'commitWorkingCopy').callThrough();
200+
resetWorkingCopyStub =
201+
sinon.stub(Workspace.UISourceCode.UISourceCode.prototype, 'resetWorkingCopy').callThrough();
202+
});
203+
204+
it('save all should commit the working copy of the changed UI codes to the disk and render savedToDisk view',
205+
async () => {
206+
const {view} = await createPatchWidgetWithDiffView();
207+
uiSourceCode.setWorkingCopy('working copy');
208+
209+
view.input.onSaveAll();
210+
const nextInput = await view.nextInput;
211+
212+
assert.isTrue(nextInput.savedToDisk);
213+
assert.isTrue(commitWorkingCopyStub.called, 'Expected commitWorkingCopy to be called but it is not called');
214+
});
215+
216+
it('discard should discard the working copy and render the view without patchSuggestion', async () => {
217+
const {view} = await createPatchWidgetWithDiffView();
218+
uiSourceCode.setWorkingCopy('working copy');
219+
220+
view.input.onDiscard();
221+
const nextInput = await view.nextInput;
222+
223+
assert.notExists(nextInput.patchSuggestion);
224+
assert.isTrue(resetWorkingCopyStub.called, 'Expected resetWorkingCopy to be called but it is not called');
225+
});
226+
});
179227
});

front_end/panels/ai_assistance/PatchWidget.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,21 @@ ${processedFiles.map(filename => `* ${filename}`).join('\n')}`;
501501
}
502502

503503
#onDiscard(): void {
504-
// TODO: Remove changes from the working copies as well.
504+
this.#workspaceDiff.modifiedUISourceCodes().forEach(modifiedUISourceCode => {
505+
modifiedUISourceCode.resetWorkingCopy();
506+
});
507+
505508
this.#patchSuggestion = undefined;
506509
this.#patchSources = undefined;
507510
this.requestUpdate();
508511
}
509512

510513
#onSaveAll(): void {
511-
// TODO: Handle saving all the files.
514+
// TODO: What should we do for the inspector stylesheet?
515+
this.#workspaceDiff.modifiedUISourceCodes().forEach(modifiedUISourceCode => {
516+
modifiedUISourceCode.commitWorkingCopy();
517+
});
518+
512519
this.#savedToDisk = true;
513520
this.requestUpdate();
514521
}

front_end/testing/AiAssistanceHelpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Breakpoints from '../models/breakpoints/breakpoints.js';
1212
import * as Logs from '../models/logs/logs.js';
1313
import * as Persistence from '../models/persistence/persistence.js';
1414
import * as Workspace from '../models/workspace/workspace.js';
15+
import * as WorkspaceDiff from '../models/workspace_diff/workspace_diff.js';
1516
import * as AiAssistance from '../panels/ai_assistance/ai_assistance.js';
1617

1718
import {findMenuItemWithLabel, getMenu} from './ContextMenuHelpers.js';
@@ -229,6 +230,16 @@ export async function createPatchWidget(options?: {
229230
};
230231
}
231232

233+
export async function createPatchWidgetWithDiffView() {
234+
const {view, panel, aidaClient} =
235+
await createPatchWidget({aidaClient: mockAidaClient([[{explanation: 'patch applied'}]])});
236+
panel.changeSummary = 'body { background-color: red; }';
237+
view.input.onApplyToWorkspace();
238+
assert.exists((await view.nextInput).patchSuggestion);
239+
240+
return {panel, view, aidaClient};
241+
}
242+
232243
export function initializePersistenceImplForTests(): void {
233244
const workspace = Workspace.Workspace.WorkspaceImpl.instance({forceNew: true});
234245
const debuggerWorkspaceBinding = Bindings.DebuggerWorkspaceBinding.DebuggerWorkspaceBinding.instance({
@@ -244,6 +255,7 @@ export function initializePersistenceImplForTests(): void {
244255
debuggerWorkspaceBinding,
245256
});
246257
Persistence.Persistence.PersistenceImpl.instance({forceNew: true, workspace, breakpointManager});
258+
WorkspaceDiff.WorkspaceDiff.workspaceDiff({forceNew: true});
247259
}
248260

249261
export function cleanup() {

0 commit comments

Comments
 (0)