@@ -2,9 +2,17 @@ package data_providers
22
33import (
44 "context"
5+ "strconv"
56 "testing"
67
8+ "github.com/stretchr/testify/mock"
9+ "github.com/stretchr/testify/require"
710 "github.com/stretchr/testify/suite"
11+
12+ "github.com/onflow/flow-go/engine/access/rest/common/parser"
13+ "github.com/onflow/flow-go/engine/access/rest/websockets/models"
14+ statestreamsmock "github.com/onflow/flow-go/engine/access/state_stream/mock"
15+ "github.com/onflow/flow-go/model/flow"
816)
917
1018type BlockDigestsProviderSuite struct {
@@ -43,4 +51,79 @@ func (s *BlockDigestsProviderSuite) TestBlockDigestsDataProvider_InvalidArgument
4351 }
4452}
4553
46- // TODO: add tests for responses after the WebsocketController is ready
54+ // validBlockDigestsArgumentsTestCases defines test happy cases for block digests data providers.
55+ // Each test case specifies input arguments, and setup functions for the mock API used in the test.
56+ func (s * BlockDigestsProviderSuite ) validBlockDigestsArgumentsTestCases () []testType {
57+ return []testType {
58+ {
59+ name : "happy path with start_block_id argument" ,
60+ arguments : models.Arguments {
61+ "start_block_id" : s .rootBlock .ID ().String (),
62+ "block_status" : parser .Finalized ,
63+ },
64+ setupBackend : func (sub * statestreamsmock.Subscription ) {
65+ s .api .On (
66+ "SubscribeBlockDigestsFromStartBlockID" ,
67+ mock .Anything ,
68+ s .rootBlock .ID (),
69+ flow .BlockStatusFinalized ,
70+ ).Return (sub ).Once ()
71+ },
72+ },
73+ {
74+ name : "happy path with start_block_height argument" ,
75+ arguments : models.Arguments {
76+ "start_block_height" : strconv .FormatUint (s .rootBlock .Header .Height , 10 ),
77+ "block_status" : parser .Finalized ,
78+ },
79+ setupBackend : func (sub * statestreamsmock.Subscription ) {
80+ s .api .On (
81+ "SubscribeBlockDigestsFromStartHeight" ,
82+ mock .Anything ,
83+ s .rootBlock .Header .Height ,
84+ flow .BlockStatusFinalized ,
85+ ).Return (sub ).Once ()
86+ },
87+ },
88+ {
89+ name : "happy path without any start argument" ,
90+ arguments : models.Arguments {
91+ "block_status" : parser .Finalized ,
92+ },
93+ setupBackend : func (sub * statestreamsmock.Subscription ) {
94+ s .api .On (
95+ "SubscribeBlockDigestsFromLatest" ,
96+ mock .Anything ,
97+ flow .BlockStatusFinalized ,
98+ ).Return (sub ).Once ()
99+ },
100+ },
101+ }
102+ }
103+
104+ // TestBlockDigestsDataProvider_HappyPath tests the behavior of the block digests data provider
105+ // when it is configured correctly and operating under normal conditions. It
106+ // validates that block digests are correctly streamed to the channel and ensures
107+ // no unexpected errors occur.
108+ func (s * BlockDigestsProviderSuite ) TestBlockDigestsDataProvider_HappyPath () {
109+ s .testHappyPath (
110+ BlockDigestsTopic ,
111+ s .validBlockDigestsArgumentsTestCases (),
112+ func (dataChan chan interface {}, blocks []* flow.Block ) {
113+ for _ , block := range blocks {
114+ dataChan <- flow .NewBlockDigest (block .Header .ID (), block .Header .Height , block .Header .Timestamp )
115+ }
116+ },
117+ s .requireBlockDigests ,
118+ )
119+ }
120+
121+ // requireBlockHeaders ensures that the received block header information matches the expected data.
122+ func (s * BlocksProviderSuite ) requireBlockDigests (v interface {}, expectedBlock * flow.Block ) {
123+ actualResponse , ok := v .(* models.BlockDigestMessageResponse )
124+ require .True (s .T (), ok , "unexpected response type: %T" , v )
125+
126+ s .Require ().Equal (expectedBlock .Header .ID (), actualResponse .Block .ID ())
127+ s .Require ().Equal (expectedBlock .Header .Height , actualResponse .Block .Height )
128+ s .Require ().Equal (expectedBlock .Header .Timestamp , actualResponse .Block .Timestamp )
129+ }
0 commit comments