Skip to content

Commit e2d8fe6

Browse files
slapec93Gergely Békési
andauthored
feat: rename command for stamp (#772)
* feat: rename command for stamp * fix: specs --------- Co-authored-by: Gergely Békési <gergely.bekesi@ethswarm.org>
1 parent 33bd9ed commit e2d8fe6

3 files changed

Lines changed: 66 additions & 14 deletions

File tree

src/command/stamp/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Create } from './create'
44
import { Dilute } from './dilute'
55
import { Extend } from './extend'
66
import { List } from './list'
7+
import { Rename } from './rename'
78
import { Show } from './show'
89
import { Topup } from './topup'
910

@@ -12,5 +13,5 @@ export class Stamp implements GroupCommand {
1213

1314
public readonly description = 'Buy, list and show postage stamps'
1415

15-
public subCommandClasses = [List, Create, Extend, Buy, Show, Dilute, Topup]
16+
public subCommandClasses = [List, Create, Extend, Buy, Show, Dilute, Topup, Rename]
1617
}

src/command/stamp/rename.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Argument, LeafCommand, Option } from 'furious-commander'
2+
import { pickStamp } from '../../service/stamp'
3+
import { stampProperties } from '../../utils/option'
4+
import { StampCommand } from './stamp-command'
5+
6+
export class Rename extends StampCommand implements LeafCommand {
7+
public readonly name = 'rename'
8+
9+
public readonly description = 'Update the label of a postage stamp'
10+
11+
@Argument({ key: 'label', description: 'New label for the postage stamp' })
12+
public label!: string
13+
14+
@Option(stampProperties)
15+
public stamp!: string
16+
17+
public async run(): Promise<void> {
18+
super.init()
19+
20+
if (!this.stamp) {
21+
this.stamp = await pickStamp(this.bee, this.console)
22+
}
23+
24+
if (!this.label) {
25+
this.label = await this.console.askForValue('Please provide a new label for the postage stamp:')
26+
}
27+
28+
await this.bee.updatePostageBatchLabel(this.stamp, this.label)
29+
30+
this.console.log(`Postage stamp ${this.stamp} has been successfully renamed to '${this.label}'`)
31+
this.console.log(`Check it later with swarm-cli stamp show ${this.stamp}`)
32+
}
33+
}

test/prompt/stamp-prompt.spec.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,38 @@
11
import chalk from 'chalk'
22
import inquirer from 'inquirer'
3-
import { describeCommand, invokeTestCli } from '../utility'
43
import { createKeyValue } from '../../src/utils/text'
4+
import { describeCommand, invokeTestCli } from '../utility'
5+
import { getStampOption } from '../utility/stamp'
6+
7+
describeCommand('Postage stamp prompt', ({ consoleMessages, getNthLastMessage }) => {
8+
describe('Price estimation', () => {
9+
it('stamp buy should prompt for price confirmation', async () => {
10+
jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ value: false })
11+
await invokeTestCli(['stamp', 'buy', '--depth', '24', '--amount', '596046400'])
12+
expect(consoleMessages[0]).toBe(createKeyValue('Estimated cost', '0.9999999198822400 xBZZ'))
13+
expect(consoleMessages[1]).toBe(createKeyValue('Estimated capacity', '40.084 GB'))
14+
expect(consoleMessages[2]).toBe(createKeyValue('Estimated TTL', '34 hours'))
15+
expect(inquirer.prompt).toHaveBeenCalledWith({
16+
message: 'Confirm the purchase',
17+
name: 'value',
18+
prefix: chalk.bold.cyan('?'),
19+
type: 'confirm',
20+
})
21+
})
22+
})
523

6-
describeCommand('Postage stamp price estimation prompt', ({ consoleMessages }) => {
7-
it('stamp buy should prompt for price confirmation', async () => {
8-
jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ value: false })
9-
await invokeTestCli(['stamp', 'buy', '--depth', '24', '--amount', '596046400'])
10-
expect(consoleMessages[0]).toBe(createKeyValue('Estimated cost', '0.9999999198822400 xBZZ'))
11-
expect(consoleMessages[1]).toBe(createKeyValue('Estimated capacity', '40.084 GB'))
12-
expect(consoleMessages[2]).toBe(createKeyValue('Estimated TTL', '34 hours'))
13-
expect(inquirer.prompt).toHaveBeenCalledWith({
14-
message: 'Confirm the purchase',
15-
name: 'value',
16-
prefix: chalk.bold.cyan('?'),
17-
type: 'confirm',
24+
describe('Rename postage stamp', () => {
25+
it('should prompt for new name', async () => {
26+
jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ value: 'new-stamp-name' })
27+
await invokeTestCli(['stamp', 'rename', ...getStampOption()])
28+
expect(getNthLastMessage(2)).toContain(
29+
`Postage stamp ${getStampOption()[1]} has been successfully renamed to 'new-stamp-name'`,
30+
)
31+
expect(inquirer.prompt).toHaveBeenCalledWith({
32+
message: 'Please provide a new label for the postage stamp:',
33+
name: 'value',
34+
prefix: chalk.bold.cyan('?'),
35+
})
1836
})
1937
})
2038
})

0 commit comments

Comments
 (0)