Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/command/stamp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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]
}
33 changes: 33 additions & 0 deletions src/command/stamp/rename.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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}`)
}
}
44 changes: 31 additions & 13 deletions test/prompt/stamp-prompt.spec.ts
Original file line number Diff line number Diff line change
@@ -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('?'),
})
})
})
})
Loading