diff --git a/src/command/stamp/index.ts b/src/command/stamp/index.ts index f38e0f1c..2896c0dc 100644 --- a/src/command/stamp/index.ts +++ b/src/command/stamp/index.ts @@ -4,6 +4,7 @@ import { Create } from './create' import { Dilute } from './dilute' import { Extend } from './extend' import { List } from './list' +import { Rename } from './rename' import { Show } from './show' import { Topup } from './topup' @@ -12,5 +13,5 @@ export class Stamp implements GroupCommand { public readonly description = 'Buy, list and show postage stamps' - public subCommandClasses = [List, Create, Extend, Buy, Show, Dilute, Topup] + public subCommandClasses = [List, Create, Extend, Buy, Show, Dilute, Topup, Rename] } diff --git a/src/command/stamp/rename.ts b/src/command/stamp/rename.ts new file mode 100644 index 00000000..5434f8f8 --- /dev/null +++ b/src/command/stamp/rename.ts @@ -0,0 +1,33 @@ +import { Argument, LeafCommand, Option } from 'furious-commander' +import { pickStamp } from '../../service/stamp' +import { stampProperties } from '../../utils/option' +import { StampCommand } from './stamp-command' + +export class Rename extends StampCommand implements LeafCommand { + public readonly name = 'rename' + + public readonly description = 'Update the label of a postage stamp' + + @Argument({ key: 'label', description: 'New label for the postage stamp' }) + public label!: string + + @Option(stampProperties) + public stamp!: string + + public async run(): Promise { + super.init() + + if (!this.stamp) { + this.stamp = await pickStamp(this.bee, this.console) + } + + if (!this.label) { + this.label = await this.console.askForValue('Please provide a new label for the postage stamp:') + } + + await this.bee.updatePostageBatchLabel(this.stamp, this.label) + + this.console.log(`Postage stamp ${this.stamp} has been successfully renamed to '${this.label}'`) + this.console.log(`Check it later with swarm-cli stamp show ${this.stamp}`) + } +} diff --git a/test/prompt/stamp-prompt.spec.ts b/test/prompt/stamp-prompt.spec.ts index e4641c85..836d1b49 100644 --- a/test/prompt/stamp-prompt.spec.ts +++ b/test/prompt/stamp-prompt.spec.ts @@ -1,20 +1,38 @@ import chalk from 'chalk' import inquirer from 'inquirer' -import { describeCommand, invokeTestCli } from '../utility' import { createKeyValue } from '../../src/utils/text' +import { describeCommand, invokeTestCli } from '../utility' +import { getStampOption } from '../utility/stamp' + +describeCommand('Postage stamp prompt', ({ consoleMessages, getNthLastMessage }) => { + describe('Price estimation', () => { + it('stamp buy should prompt for price confirmation', async () => { + jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ value: false }) + await invokeTestCli(['stamp', 'buy', '--depth', '24', '--amount', '596046400']) + expect(consoleMessages[0]).toBe(createKeyValue('Estimated cost', '0.9999999198822400 xBZZ')) + expect(consoleMessages[1]).toBe(createKeyValue('Estimated capacity', '40.084 GB')) + expect(consoleMessages[2]).toBe(createKeyValue('Estimated TTL', '34 hours')) + expect(inquirer.prompt).toHaveBeenCalledWith({ + message: 'Confirm the purchase', + name: 'value', + prefix: chalk.bold.cyan('?'), + type: 'confirm', + }) + }) + }) -describeCommand('Postage stamp price estimation prompt', ({ consoleMessages }) => { - it('stamp buy should prompt for price confirmation', async () => { - jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ value: false }) - await invokeTestCli(['stamp', 'buy', '--depth', '24', '--amount', '596046400']) - expect(consoleMessages[0]).toBe(createKeyValue('Estimated cost', '0.9999999198822400 xBZZ')) - expect(consoleMessages[1]).toBe(createKeyValue('Estimated capacity', '40.084 GB')) - expect(consoleMessages[2]).toBe(createKeyValue('Estimated TTL', '34 hours')) - expect(inquirer.prompt).toHaveBeenCalledWith({ - message: 'Confirm the purchase', - name: 'value', - prefix: chalk.bold.cyan('?'), - type: 'confirm', + describe('Rename postage stamp', () => { + it('should prompt for new name', async () => { + jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ value: 'new-stamp-name' }) + await invokeTestCli(['stamp', 'rename', ...getStampOption()]) + expect(getNthLastMessage(2)).toContain( + `Postage stamp ${getStampOption()[1]} has been successfully renamed to 'new-stamp-name'`, + ) + expect(inquirer.prompt).toHaveBeenCalledWith({ + message: 'Please provide a new label for the postage stamp:', + name: 'value', + prefix: chalk.bold.cyan('?'), + }) }) }) })