Skip to content
Open
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
10 changes: 5 additions & 5 deletions .github/workflows/tests-rc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ jobs:

- name: Deposit to chequebook
run: |
swarm-cli cheque deposit 100000000000000000
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:11633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:21633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:31633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:41633
swarm-cli cheque deposit 10
swarm-cli cheque deposit 10 --bee-api-url http://localhost:11633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:21633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:31633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:41633

- name: Print swarm-cli status
continue-on-error: true
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ jobs:

- name: Deposit to chequebook
run: |
swarm-cli cheque deposit 100000000000000000
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:11633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:21633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:31633
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:41633
swarm-cli cheque deposit 10
swarm-cli cheque deposit 10 --bee-api-url http://localhost:11633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:21633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:31633
swarm-cli cheque deposit 10 --bee-api-url http://localhost:41633

- name: Print swarm-cli status
continue-on-error: true
Expand Down
112 changes: 81 additions & 31 deletions test/command/stamp.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { System, Types } from 'cafe-utility'
import { Types } from 'cafe-utility'
import inquirer from 'inquirer'
import { Buy } from '../../src/command/stamp/buy'
import { toMatchLinesInOrder } from '../custom-matcher'
import { describeCommand, invokeTestCli } from '../utility'
import { BatchId, Bee } from '@ethersphere/bee-js'
import { randomBytes } from 'crypto'

expect.extend({
toMatchLinesInOrder,
Expand All @@ -11,6 +13,44 @@ expect.extend({
describeCommand(
'Test Stamp command',
({ consoleMessages, getLastMessage, getNthLastMessage, hasMessageContaining }) => {
const fakeBatchIdHash = randomBytes(32).toString('hex')

afterEach(() => {
jest.clearAllMocks()
})

describe('e2e', () => {
it('should buy stamp with right capacity and properties', async () => {
const execution = await invokeTestCli([
'stamp',
'buy',
'--depth',
'20',
'--amount',
'555m',
'--label',
'Alice',
'--gas-price',
'100_000_000',
'--wait-usable',
'--yes',
])
const command = execution.runnable as Buy
expect(command.yes).toBe(true)

const id = command.postageBatchId
await invokeTestCli(['stamp', 'show', id.toHex(), '--verbose'])
const pattern = [
['Type', 'Immutable'],
['Stamp ID', id.toHex()],
['Label', 'Alice'],
['Depth', '20'],
['Usable', 'true'],
]
expect(consoleMessages).toMatchLinesInOrder(pattern)
})
})

it('should list stamps', async () => {
await invokeTestCli(['stamp', 'list'])
const pattern = [['Stamp ID'], ['Usage'], ['Capacity'], ['TTL']]
Expand All @@ -26,23 +66,28 @@ describeCommand(
it('should not allow buying stamp with amount 0', async () => {
await invokeTestCli(['stamp', 'buy', '--amount', '0', '--depth', '20'])
expect(getLastMessage()).toContain('[amount] must be at least 1')
await System.sleepMillis(11_000)
})

it('should not allow buying stamp with depth 16', async () => {
await invokeTestCli(['stamp', 'buy', '--amount', '1', '--depth', '16'])
expect(getLastMessage()).toContain('[depth] must be at least 17')
await System.sleepMillis(11_000)
})

it('should buy stamp', async () => {
const createPostageBatch = jest
.spyOn(Bee.prototype, 'createPostageBatch')
.mockResolvedValue(new BatchId(fakeBatchIdHash))
await invokeTestCli(['stamp', 'buy', '--amount', '600_000_000', '--depth', '20', '--yes'])
expect(createPostageBatch).toHaveBeenCalledWith('600000000', 20, expect.objectContaining({ immutableFlag: true }))
expect(getLastMessage()).toContain('Stamp ID:')
await System.sleepMillis(11_000)
expect(getLastMessage()).toContain(fakeBatchIdHash)
})

it('should buy stamp with immutable flag', async () => {
const execution = await invokeTestCli([
const createPostageBatch = jest
.spyOn(Bee.prototype, 'createPostageBatch')
.mockResolvedValue(new BatchId(fakeBatchIdHash))
await invokeTestCli([
'stamp',
'buy',
'--amount',
Expand All @@ -53,13 +98,10 @@ describeCommand(
'--wait-usable',
'--yes',
])
const command = execution.runnable as Buy

const id = command.postageBatchId
await invokeTestCli(['stamp', 'show', id.toHex(), '--verbose'])
const pattern = [['Capacity (immutable)']]
expect(consoleMessages).toMatchLinesInOrder(pattern)
await System.sleepMillis(11_000)
expect(createPostageBatch).toHaveBeenCalledWith('600000000', 20, expect.objectContaining({ immutableFlag: true }))
expect(getLastMessage()).toContain('Stamp ID:')
expect(getLastMessage()).toContain(fakeBatchIdHash)
})

it('should print custom message when there are no stamps', async () => {
Expand All @@ -74,7 +116,10 @@ describeCommand(
})

it('should wait until stamp is usable', async () => {
const execution = await invokeTestCli([
const createPostageBatch = jest
.spyOn(Bee.prototype, 'createPostageBatch')
.mockResolvedValue(new BatchId(fakeBatchIdHash))
await invokeTestCli([
'stamp',
'buy',
'--depth',
Expand All @@ -86,39 +131,43 @@ describeCommand(
'--label',
'Alice',
])
const command = execution.runnable as Buy
expect(command.yes).toBe(true)

const id = command.postageBatchId
await invokeTestCli(['stamp', 'show', id.toHex(), '--verbose'])
const pattern = [
['Stamp ID', id.toHex()],
['Label', 'Alice'],
['Usable', 'true'],
]
expect(consoleMessages).toMatchLinesInOrder(pattern)
expect(createPostageBatch).toHaveBeenCalledWith(
'555000000',
20,
expect.objectContaining({ label: 'Alice', waitForUsable: true }),
)
})

it('should accept estimate cost prompt', async () => {
jest.spyOn(inquirer, 'prompt').mockClear().mockResolvedValueOnce({ value: true })
jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ value: true })
const createPostageBatch = jest
.spyOn(Bee.prototype, 'createPostageBatch')
.mockResolvedValue(new BatchId(fakeBatchIdHash))
const execution = await invokeTestCli(['stamp', 'buy', '--depth', '20', '--amount', '1b'])
const command = execution.runnable as Buy
expect(command.yes).toBe(true)
expect(inquirer.prompt).toHaveBeenCalledTimes(1)
await System.sleepMillis(11_000)
expect(createPostageBatch).toHaveBeenCalledWith('1000000000', 20, expect.anything())
})

it('should reject estimate cost prompt', async () => {
jest.spyOn(inquirer, 'prompt').mockClear().mockResolvedValueOnce({ value: false })
jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ value: false })
const createPostageBatch = jest
.spyOn(Bee.prototype, 'createPostageBatch')
.mockResolvedValue(new BatchId(fakeBatchIdHash))
const execution = await invokeTestCli(['stamp', 'buy', '--depth', '20', '--amount', '1b'])
const command = execution.runnable as Buy
expect(command.yes).toBe(false)
expect(inquirer.prompt).toHaveBeenCalledTimes(1)
await System.sleepMillis(11_000)
expect(createPostageBatch).not.toHaveBeenCalled()
})

it('should be possible to buy with underscores and units', async () => {
const execution = await invokeTestCli([
const createPostageBatch = jest
.spyOn(Bee.prototype, 'createPostageBatch')
.mockResolvedValue(new BatchId(fakeBatchIdHash))
await invokeTestCli([
'stamp',
'buy',
'--amount',
Expand All @@ -129,10 +178,11 @@ describeCommand(
'100_000_000',
'--yes',
])
const command = execution.runnable as Buy
expect(command.yes).toBe(true)
expect(getLastMessage()).toContain('Stamp ID:')
await System.sleepMillis(11_000)
expect(createPostageBatch).toHaveBeenCalledWith(
'600000000',
17,
expect.objectContaining({ gasPrice: '100000000' }),
)
})

it.skip('should only be able to dilute stamp with greater depth', async () => {
Expand Down
8 changes: 4 additions & 4 deletions test/coverage/coverage-summary.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{"total": {"lines":{"total":2863,"covered":2186,"skipped":0,"pct":76.35},"statements":{"total":2884,"covered":2200,"skipped":0,"pct":76.28},"functions":{"total":343,"covered":271,"skipped":0,"pct":79},"branches":{"total":621,"covered":353,"skipped":0,"pct":56.84},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
{"total": {"lines":{"total":2863,"covered":2188,"skipped":0,"pct":76.42},"statements":{"total":2884,"covered":2202,"skipped":0,"pct":76.35},"functions":{"total":343,"covered":272,"skipped":0,"pct":79.3},"branches":{"total":621,"covered":355,"skipped":0,"pct":57.16},"branchesTrue":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/application.ts": {"lines":{"total":2,"covered":0,"skipped":0,"pct":0},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":2,"covered":0,"skipped":0,"pct":0},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/config.ts": {"lines":{"total":33,"covered":32,"skipped":0,"pct":96.96},"functions":{"total":1,"covered":0,"skipped":0,"pct":0},"statements":{"total":33,"covered":32,"skipped":0,"pct":96.96},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/curl.ts": {"lines":{"total":24,"covered":24,"skipped":0,"pct":100},"functions":{"total":7,"covered":7,"skipped":0,"pct":100},"statements":{"total":25,"covered":25,"skipped":0,"pct":100},"branches":{"total":13,"covered":12,"skipped":0,"pct":92.3}}
Expand Down Expand Up @@ -77,7 +77,7 @@
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stamp/dilute.ts": {"lines":{"total":27,"covered":12,"skipped":0,"pct":44.44},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":27,"covered":12,"skipped":0,"pct":44.44},"branches":{"total":5,"covered":0,"skipped":0,"pct":0}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stamp/extend.ts": {"lines":{"total":51,"covered":10,"skipped":0,"pct":19.6},"functions":{"total":3,"covered":1,"skipped":0,"pct":33.33},"statements":{"total":52,"covered":10,"skipped":0,"pct":19.23},"branches":{"total":13,"covered":0,"skipped":0,"pct":0}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stamp/index.ts": {"lines":{"total":11,"covered":11,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":11,"covered":11,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stamp/list.ts": {"lines":{"total":31,"covered":28,"skipped":0,"pct":90.32},"functions":{"total":6,"covered":4,"skipped":0,"pct":66.66},"statements":{"total":32,"covered":29,"skipped":0,"pct":90.62},"branches":{"total":11,"covered":9,"skipped":0,"pct":81.81}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stamp/list.ts": {"lines":{"total":31,"covered":29,"skipped":0,"pct":93.54},"functions":{"total":6,"covered":5,"skipped":0,"pct":83.33},"statements":{"total":32,"covered":30,"skipped":0,"pct":93.75},"branches":{"total":11,"covered":9,"skipped":0,"pct":81.81}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stamp/show.ts": {"lines":{"total":14,"covered":13,"skipped":0,"pct":92.85},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":14,"covered":13,"skipped":0,"pct":92.85},"branches":{"total":1,"covered":0,"skipped":0,"pct":0}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stamp/stamp-command.ts": {"lines":{"total":7,"covered":3,"skipped":0,"pct":42.85},"functions":{"total":1,"covered":0,"skipped":0,"pct":0},"statements":{"total":7,"covered":3,"skipped":0,"pct":42.85},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/command/stamp/topup.ts": {"lines":{"total":23,"covered":11,"skipped":0,"pct":47.82},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":23,"covered":11,"skipped":0,"pct":47.82},"branches":{"total":4,"covered":0,"skipped":0,"pct":0}}
Expand All @@ -96,7 +96,7 @@
,"/home/runner/work/swarm-cli/swarm-cli/src/service/identity/index.ts": {"lines":{"total":36,"covered":32,"skipped":0,"pct":88.88},"functions":{"total":7,"covered":7,"skipped":0,"pct":100},"statements":{"total":36,"covered":32,"skipped":0,"pct":88.88},"branches":{"total":9,"covered":6,"skipped":0,"pct":66.66}}
,"/home/runner/work/swarm-cli/swarm-cli/src/service/identity/types/identity.ts": {"lines":{"total":4,"covered":4,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":4,"covered":4,"skipped":0,"pct":100},"branches":{"total":2,"covered":2,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/service/identity/types/index.ts": {"lines":{"total":1,"covered":1,"skipped":0,"pct":100},"functions":{"total":2,"covered":1,"skipped":0,"pct":50},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/service/stamp/index.ts": {"lines":{"total":32,"covered":30,"skipped":0,"pct":93.75},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":32,"covered":30,"skipped":0,"pct":93.75},"branches":{"total":11,"covered":8,"skipped":0,"pct":72.72}}
,"/home/runner/work/swarm-cli/swarm-cli/src/service/stamp/index.ts": {"lines":{"total":32,"covered":30,"skipped":0,"pct":93.75},"functions":{"total":4,"covered":4,"skipped":0,"pct":100},"statements":{"total":32,"covered":30,"skipped":0,"pct":93.75},"branches":{"total":11,"covered":9,"skipped":0,"pct":81.81}}
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/bzz-address.ts": {"lines":{"total":30,"covered":24,"skipped":0,"pct":80},"functions":{"total":3,"covered":2,"skipped":0,"pct":66.66},"statements":{"total":30,"covered":24,"skipped":0,"pct":80},"branches":{"total":6,"covered":3,"skipped":0,"pct":50}}
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/chainsync.ts": {"lines":{"total":4,"covered":4,"skipped":0,"pct":100},"functions":{"total":1,"covered":1,"skipped":0,"pct":100},"statements":{"total":4,"covered":4,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/contracts.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
Expand All @@ -108,5 +108,5 @@
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/option.ts": {"lines":{"total":3,"covered":3,"skipped":0,"pct":100},"functions":{"total":0,"covered":0,"skipped":0,"pct":100},"statements":{"total":3,"covered":3,"skipped":0,"pct":100},"branches":{"total":0,"covered":0,"skipped":0,"pct":100}}
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/rpc.ts": {"lines":{"total":37,"covered":9,"skipped":0,"pct":24.32},"functions":{"total":6,"covered":0,"skipped":0,"pct":0},"statements":{"total":37,"covered":9,"skipped":0,"pct":24.32},"branches":{"total":5,"covered":0,"skipped":0,"pct":0}}
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/spinner.ts": {"lines":{"total":15,"covered":15,"skipped":0,"pct":100},"functions":{"total":2,"covered":2,"skipped":0,"pct":100},"statements":{"total":15,"covered":15,"skipped":0,"pct":100},"branches":{"total":5,"covered":3,"skipped":0,"pct":60}}
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/text.ts": {"lines":{"total":21,"covered":18,"skipped":0,"pct":85.71},"functions":{"total":9,"covered":7,"skipped":0,"pct":77.77},"statements":{"total":22,"covered":19,"skipped":0,"pct":86.36},"branches":{"total":5,"covered":3,"skipped":0,"pct":60}}
,"/home/runner/work/swarm-cli/swarm-cli/src/utils/text.ts": {"lines":{"total":21,"covered":19,"skipped":0,"pct":90.47},"functions":{"total":9,"covered":7,"skipped":0,"pct":77.77},"statements":{"total":22,"covered":20,"skipped":0,"pct":90.9},"branches":{"total":5,"covered":4,"skipped":0,"pct":80}}
}