diff --git a/src/command/utility/rchash.ts b/src/command/utility/rchash.ts index 5dd13192..be47d345 100644 --- a/src/command/utility/rchash.ts +++ b/src/command/utility/rchash.ts @@ -1,5 +1,5 @@ import { System } from 'cafe-utility' -import { LeafCommand } from 'furious-commander' +import { LeafCommand, Option } from 'furious-commander' import { createSpinner } from '../../utils/spinner' import { createKeyValue } from '../../utils/text' import { RootCommand } from '../root-command' @@ -9,12 +9,23 @@ export class Rchash extends RootCommand implements LeafCommand { public readonly description = 'Check reserve sampling duration' + @Option({ + key: 'depth', + type: 'number', + minimum: 0, + maximum: 32, + description: 'Depth to use for sampling (default: committedDepth from node status)', + }) + public depth!: number + public async run(): Promise { super.init() const addresses = await this.bee.getNodeAddresses() - const topology = await this.bee.getTopology() + const status = await this.bee.getStatus() + const depth = this.depth ?? status.committedDepth + const anchor = addresses.overlay.toHex().slice(0, Math.max(2, Math.ceil(depth / 8) * 2)) let stillRunning = true - const promise = this.bee.rchash(topology.depth, addresses.overlay.toHex(), addresses.overlay.toHex()) + const promise = this.bee.rchash(depth, anchor, anchor) promise.finally(() => { stillRunning = false }) diff --git a/test/command/rchash.spec.ts b/test/command/rchash.spec.ts new file mode 100644 index 00000000..78bb6fb3 --- /dev/null +++ b/test/command/rchash.spec.ts @@ -0,0 +1,36 @@ +import { Bee } from '@ethersphere/bee-js' +import { System } from 'cafe-utility' +import { toMatchLinesInOrder } from '../custom-matcher' +import { describeCommand, invokeTestCli } from '../utility' + +expect.extend({ + toMatchLinesInOrder, +}) + +describeCommand('Test Utility rchash command', ({ consoleMessages }) => { + it('should print reserve sampling duration', async () => { + await System.waitFor( + async () => { + const bee = new Bee('http://localhost:1633') + const status = await bee.getStatus() + return status.isWarmingUp === false + }, + { attempts: 300, waitMillis: 1000 }, + ) + await invokeTestCli(['utility', 'rchash']) + expect(consoleMessages).toMatchLinesInOrder([['Reserve sampling duration']]) + }) + + it('should print reserve sampling duration with custom depth', async () => { + await System.waitFor( + async () => { + const bee = new Bee('http://localhost:1633') + const status = await bee.getStatus() + return status.isWarmingUp === false + }, + { attempts: 300, waitMillis: 1000 }, + ) + await invokeTestCli(['utility', 'rchash', '--depth', '2']) + expect(consoleMessages).toMatchLinesInOrder([['Reserve sampling duration']]) + }) +})