Skip to content

Commit 7749280

Browse files
authored
Transfer selection/starred status to renamed experiment (#4783)
1 parent d610333 commit 7749280

File tree

5 files changed

+56
-9
lines changed

5 files changed

+56
-9
lines changed

extension/src/experiments/commands/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,19 @@ export const getBranchExperimentCommand =
1919

2020
export const getRenameExperimentCommand =
2121
(experiments: WorkspaceExperiments) =>
22-
(cwd: string, oldName: string, newName: string) =>
23-
experiments.runCommand(AvailableCommands.EXP_RENAME, cwd, oldName, newName)
22+
async (cwd: string, oldName: string, newName: string) => {
23+
const output = await experiments.runCommand(
24+
AvailableCommands.EXP_RENAME,
25+
cwd,
26+
oldName,
27+
newName
28+
)
29+
if (!output) {
30+
return
31+
}
32+
const repository = experiments.getRepository(cwd)
33+
return repository.transferExperimentDetails(oldName, newName)
34+
}
2435

2536
const promptToAddStudioToken = async () => {
2637
const response = await Toast.askShowOrCloseOrNever(

extension/src/experiments/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,10 @@ export class Experiments extends BaseRepository<TableData> {
641641
this.notifyChanged()
642642
}
643643

644+
public transferExperimentDetails(oldName: string, newName: string) {
645+
return this.experiments.transferDetails(oldName, newName)
646+
}
647+
644648
protected sendInitialWebviewData() {
645649
return this.webviewMessages.sendWebviewMessage()
646650
}

extension/src/experiments/model/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ export class ExperimentsModel extends ModelWithPersistence {
213213
return this.coloredStatus[id]
214214
}
215215

216+
public transferDetails(oldName: string, newName: string) {
217+
const starred = this.starredExperiments[oldName]
218+
const selected = this.coloredStatus[oldName]
219+
220+
this.starredExperiments[newName] = starred
221+
this.coloredStatus[newName] = selected
222+
223+
delete this.starredExperiments[oldName]
224+
delete this.coloredStatus[oldName]
225+
226+
this.persistStatus()
227+
this.persistStars()
228+
}
229+
216230
public hasRunningExperiment() {
217231
return this.running.length > 0
218232
}

extension/src/experiments/workspace.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class WorkspaceExperiments extends BaseWorkspaceWebviews<
241241
}
242242

243243
public async getInputAndRun(
244-
runCommand: (...args: Args) => Promise<void> | void,
244+
runCommand: (...args: Args) => Promise<void | string>,
245245
title: Title,
246246
defaultValue: string,
247247
...args: Args
@@ -273,9 +273,10 @@ export class WorkspaceExperiments extends BaseWorkspaceWebviews<
273273
}
274274

275275
public runCommand(commandId: CommandId, cwd: string, ...args: Args) {
276-
return Toast.showOutput(
277-
this.internalCommands.executeCommand(commandId, cwd, ...args)
278-
)
276+
const output = this.internalCommands.executeCommand(commandId, cwd, ...args)
277+
278+
void Toast.showOutput(output)
279+
return output
279280
}
280281

281282
public createRepository(

extension/src/test/suite/experiments/index.test.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,13 @@ suite('Experiments Test Suite', () => {
562562
}).timeout(WEBVIEW_TEST_TIMEOUT)
563563

564564
it('should be able to handle a message to rename an experiment', async () => {
565-
const { mockMessageReceived } =
565+
const { mockMessageReceived, experimentsModel } =
566566
await stubWorkspaceGettersWebview(disposable)
567+
const transferDetailsSpy = spy(experimentsModel, 'transferDetails')
568+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
569+
const persistStatusSpy = spy(experimentsModel as any, 'persistStatus')
570+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
571+
const persistStarsSpy = spy(experimentsModel as any, 'persistStars')
567572

568573
const mockNewExperimentName = 'new-experiment-name'
569574
const inputEvent = getInputBoxEvent(mockNewExperimentName)
@@ -575,11 +580,15 @@ suite('Experiments Test Suite', () => {
575580
const mockRenameCalled = new Promise(resolve =>
576581
mockRenameExperiment.callsFake(() => {
577582
resolve(undefined)
578-
return Promise.resolve('Renamed experiments:')
583+
return Promise.resolve('Experiment renamed successfully')
579584
})
580585
)
581586

582-
const mockExperimentId = 'exp-e7a67'
587+
const mockExperimentId = 'exp-83425'
588+
const selectedColor = experimentsModel
589+
.getSelectedRevisions()
590+
.find(({ id }) => id === mockExperimentId)?.displayColor
591+
expect(selectedColor).not.to.be.undefined
583592

584593
mockMessageReceived.fire({
585594
payload: mockExperimentId,
@@ -593,6 +602,14 @@ suite('Experiments Test Suite', () => {
593602
mockExperimentId,
594603
mockNewExperimentName
595604
)
605+
expect(transferDetailsSpy).to.be.calledOnce
606+
expect(persistStatusSpy).to.be.calledOnce
607+
expect(persistStarsSpy).to.be.calledOnce
608+
609+
expect(
610+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
611+
(experimentsModel as any).coloredStatus[mockNewExperimentName]
612+
).to.equal(selectedColor)
596613
}).timeout(WEBVIEW_TEST_TIMEOUT)
597614

598615
it('should handle a message to show the logs of an experiment', async () => {

0 commit comments

Comments
 (0)