Skip to content

Commit ba5b186

Browse files
slapec93Gergely Békésigithub-actions[bot]
authored
test: use mock instead of real bee.js calls (#709)
* test: use mock instead of real bee.js calls * ci: update deposit command * test: update test coverage * test: add e2e test for stamp creation * test: update test coverage * test: fire up a bee node for e2e environment * test: update test coverage * test: move e2e case to stable environment * test: update test coverage * test: revert e2e jest config * test: update test coverage --------- Co-authored-by: Gergely Békési <gergely.bekesi@ethswarm.org> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 3a4bd36 commit ba5b186

4 files changed

Lines changed: 95 additions & 45 deletions

File tree

.github/workflows/tests-rc.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ jobs:
5858

5959
- name: Deposit to chequebook
6060
run: |
61-
swarm-cli cheque deposit 100000000000000000
62-
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:11633
63-
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:21633
64-
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:31633
65-
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:41633
61+
swarm-cli cheque deposit 10
62+
swarm-cli cheque deposit 10 --bee-api-url http://localhost:11633
63+
swarm-cli cheque deposit 10 --bee-api-url http://localhost:21633
64+
swarm-cli cheque deposit 10 --bee-api-url http://localhost:31633
65+
swarm-cli cheque deposit 10 --bee-api-url http://localhost:41633
6666
6767
- name: Print swarm-cli status
6868
continue-on-error: true

.github/workflows/tests.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ jobs:
4242

4343
- name: Deposit to chequebook
4444
run: |
45-
swarm-cli cheque deposit 100000000000000000
46-
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:11633
47-
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:21633
48-
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:31633
49-
swarm-cli cheque deposit 100000000000000000 --bee-api-url http://localhost:41633
45+
swarm-cli cheque deposit 10
46+
swarm-cli cheque deposit 10 --bee-api-url http://localhost:11633
47+
swarm-cli cheque deposit 10 --bee-api-url http://localhost:21633
48+
swarm-cli cheque deposit 10 --bee-api-url http://localhost:31633
49+
swarm-cli cheque deposit 10 --bee-api-url http://localhost:41633
5050
5151
- name: Print swarm-cli status
5252
continue-on-error: true

test/command/stamp.spec.ts

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { System, Types } from 'cafe-utility'
1+
import { Types } from 'cafe-utility'
22
import inquirer from 'inquirer'
33
import { Buy } from '../../src/command/stamp/buy'
44
import { toMatchLinesInOrder } from '../custom-matcher'
55
import { describeCommand, invokeTestCli } from '../utility'
6+
import { BatchId, Bee } from '@ethersphere/bee-js'
7+
import { randomBytes } from 'crypto'
68

79
expect.extend({
810
toMatchLinesInOrder,
@@ -11,6 +13,44 @@ expect.extend({
1113
describeCommand(
1214
'Test Stamp command',
1315
({ consoleMessages, getLastMessage, getNthLastMessage, hasMessageContaining }) => {
16+
const fakeBatchIdHash = randomBytes(32).toString('hex')
17+
18+
afterEach(() => {
19+
jest.clearAllMocks()
20+
})
21+
22+
describe('e2e', () => {
23+
it('should buy stamp with right capacity and properties', async () => {
24+
const execution = await invokeTestCli([
25+
'stamp',
26+
'buy',
27+
'--depth',
28+
'20',
29+
'--amount',
30+
'555m',
31+
'--label',
32+
'Alice',
33+
'--gas-price',
34+
'100_000_000',
35+
'--wait-usable',
36+
'--yes',
37+
])
38+
const command = execution.runnable as Buy
39+
expect(command.yes).toBe(true)
40+
41+
const id = command.postageBatchId
42+
await invokeTestCli(['stamp', 'show', id.toHex(), '--verbose'])
43+
const pattern = [
44+
['Type', 'Immutable'],
45+
['Stamp ID', id.toHex()],
46+
['Label', 'Alice'],
47+
['Depth', '20'],
48+
['Usable', 'true'],
49+
]
50+
expect(consoleMessages).toMatchLinesInOrder(pattern)
51+
})
52+
})
53+
1454
it('should list stamps', async () => {
1555
await invokeTestCli(['stamp', 'list'])
1656
const pattern = [['Stamp ID'], ['Usage'], ['Capacity'], ['TTL']]
@@ -26,23 +66,28 @@ describeCommand(
2666
it('should not allow buying stamp with amount 0', async () => {
2767
await invokeTestCli(['stamp', 'buy', '--amount', '0', '--depth', '20'])
2868
expect(getLastMessage()).toContain('[amount] must be at least 1')
29-
await System.sleepMillis(11_000)
3069
})
3170

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

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

4486
it('should buy stamp with immutable flag', async () => {
45-
const execution = await invokeTestCli([
87+
const createPostageBatch = jest
88+
.spyOn(Bee.prototype, 'createPostageBatch')
89+
.mockResolvedValue(new BatchId(fakeBatchIdHash))
90+
await invokeTestCli([
4691
'stamp',
4792
'buy',
4893
'--amount',
@@ -53,13 +98,10 @@ describeCommand(
5398
'--wait-usable',
5499
'--yes',
55100
])
56-
const command = execution.runnable as Buy
57101

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

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

76118
it('should wait until stamp is usable', async () => {
77-
const execution = await invokeTestCli([
119+
const createPostageBatch = jest
120+
.spyOn(Bee.prototype, 'createPostageBatch')
121+
.mockResolvedValue(new BatchId(fakeBatchIdHash))
122+
await invokeTestCli([
78123
'stamp',
79124
'buy',
80125
'--depth',
@@ -86,39 +131,43 @@ describeCommand(
86131
'--label',
87132
'Alice',
88133
])
89-
const command = execution.runnable as Buy
90-
expect(command.yes).toBe(true)
91134

92-
const id = command.postageBatchId
93-
await invokeTestCli(['stamp', 'show', id.toHex(), '--verbose'])
94-
const pattern = [
95-
['Stamp ID', id.toHex()],
96-
['Label', 'Alice'],
97-
['Usable', 'true'],
98-
]
99-
expect(consoleMessages).toMatchLinesInOrder(pattern)
135+
expect(createPostageBatch).toHaveBeenCalledWith(
136+
'555000000',
137+
20,
138+
expect.objectContaining({ label: 'Alice', waitForUsable: true }),
139+
)
100140
})
101141

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

111154
it('should reject estimate cost prompt', async () => {
112-
jest.spyOn(inquirer, 'prompt').mockClear().mockResolvedValueOnce({ value: false })
155+
jest.spyOn(inquirer, 'prompt').mockResolvedValueOnce({ value: false })
156+
const createPostageBatch = jest
157+
.spyOn(Bee.prototype, 'createPostageBatch')
158+
.mockResolvedValue(new BatchId(fakeBatchIdHash))
113159
const execution = await invokeTestCli(['stamp', 'buy', '--depth', '20', '--amount', '1b'])
114160
const command = execution.runnable as Buy
115161
expect(command.yes).toBe(false)
116162
expect(inquirer.prompt).toHaveBeenCalledTimes(1)
117-
await System.sleepMillis(11_000)
163+
expect(createPostageBatch).not.toHaveBeenCalled()
118164
})
119165

120166
it('should be possible to buy with underscores and units', async () => {
121-
const execution = await invokeTestCli([
167+
const createPostageBatch = jest
168+
.spyOn(Bee.prototype, 'createPostageBatch')
169+
.mockResolvedValue(new BatchId(fakeBatchIdHash))
170+
await invokeTestCli([
122171
'stamp',
123172
'buy',
124173
'--amount',
@@ -129,10 +178,11 @@ describeCommand(
129178
'100_000_000',
130179
'--yes',
131180
])
132-
const command = execution.runnable as Buy
133-
expect(command.yes).toBe(true)
134-
expect(getLastMessage()).toContain('Stamp ID:')
135-
await System.sleepMillis(11_000)
181+
expect(createPostageBatch).toHaveBeenCalledWith(
182+
'600000000',
183+
17,
184+
expect.objectContaining({ gasPrice: '100000000' }),
185+
)
136186
})
137187

138188
it.skip('should only be able to dilute stamp with greater depth', async () => {

test/coverage/coverage-summary.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{"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}}
1+
{"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}}
22
,"/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}}
33
,"/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}}
44
,"/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}}
@@ -77,7 +77,7 @@
7777
,"/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}}
7878
,"/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}}
7979
,"/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}}
80-
,"/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}}
80+
,"/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}}
8181
,"/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}}
8282
,"/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}}
8383
,"/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}}
@@ -96,7 +96,7 @@
9696
,"/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}}
9797
,"/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}}
9898
,"/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}}
99-
,"/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}}
99+
,"/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}}
100100
,"/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}}
101101
,"/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}}
102102
,"/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}}
@@ -108,5 +108,5 @@
108108
,"/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}}
109109
,"/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}}
110110
,"/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}}
111-
,"/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}}
111+
,"/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}}
112112
}

0 commit comments

Comments
 (0)