|
| 1 | +// arc_test.go |
| 2 | + |
| 3 | +package broadcaster |
| 4 | + |
| 5 | +import ( |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/bitcoin-sv/go-sdk/transaction" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// MockArcFailureClient simulates a failed API response for Arc. |
| 16 | +type MockArcFailureClient struct{} |
| 17 | + |
| 18 | +// Do implements the HTTPClient interface for failure scenarios. |
| 19 | +func (m *MockArcFailureClient) Do(req *http.Request) (*http.Response, error) { |
| 20 | + return &http.Response{ |
| 21 | + StatusCode: 500, |
| 22 | + Body: io.NopCloser(strings.NewReader(`{"blockHash":"","blockHeight":0,"extraInfo":"","status":500,"timestamp":"2023-01-01T00:00:00Z","title":"Internal Server Error","txStatus":null,"instance":null,"txid":"","detail":""}`)), |
| 23 | + }, nil |
| 24 | +} |
| 25 | + |
| 26 | +// MockArcSuccessClient simulates a successful API response for Arc. |
| 27 | +type MockArcSuccessClient struct{} |
| 28 | + |
| 29 | +// Do implements the HTTPClient interface for success scenarios. |
| 30 | +func (m *MockArcSuccessClient) Do(req *http.Request) (*http.Response, error) { |
| 31 | + return &http.Response{ |
| 32 | + StatusCode: 200, |
| 33 | + Body: io.NopCloser(strings.NewReader(`{ |
| 34 | + "blockHash":"abc123", |
| 35 | + "blockHeight":100, |
| 36 | + "extraInfo":"extra", |
| 37 | + "status":200, |
| 38 | + "timestamp":"2023-01-01T00:00:00Z", |
| 39 | + "title":"Broadcast Success", |
| 40 | + "txStatus":"7", |
| 41 | + "instance":"instance1", |
| 42 | + "txid":"4d76b00f29e480e0a933cef9d9ffe303d6ab919e2cdb265dd2cea41089baa85a", |
| 43 | + "detail":"detail info" |
| 44 | + }`)), |
| 45 | + }, nil |
| 46 | +} |
| 47 | + |
| 48 | +// TestArcBroadcast tests the Broadcast method of Arc. |
| 49 | +func TestArcBroadcast(t *testing.T) { |
| 50 | + // Create a real transaction with a known TxID. |
| 51 | + txHex := "0100000001a9b0c5a2437042e5d0c6288fad6abc2ef8725adb6fef5f1bab21b2124cfb7cf6dc9300006a47304402204c3f88aadc90a3f29669bba5c4369a2eebc10439e857a14e169d19626243ffd802205443013b187a5c7f23e2d5dd82bc4ea9a79d138a3dc6cae6e6ef68874bd23a42412103fd290068ae945c23a06775de8422ceb6010aaebab40b78e01a0af3f1322fa861ffffffff010000000000000000b1006a0963657274696861736822314c6d763150594d70387339594a556e374d3948565473446b64626155386b514e4a4032356163343531383766613035616532626436346562323632386666336432666636646338313665383335376364616366343765663862396331656433663531403064383963343363343636303262643865313831376530393137313736343134353938373337623161663865363939343930646364653462343937656338643300000000" |
| 52 | + tx, err := transaction.NewTransactionFromHex(txHex) |
| 53 | + require.NoError(t, err, "Failed to create transaction from hex") |
| 54 | + |
| 55 | + // Initialize Arc with a failure client. |
| 56 | + a := &Arc{ |
| 57 | + ApiUrl: "https://arc.gorillapool.io", |
| 58 | + ApiKey: "test_api_key", |
| 59 | + Client: &MockArcFailureClient{}, |
| 60 | + } |
| 61 | + |
| 62 | + // Broadcast with failure client. |
| 63 | + success, failure := a.Broadcast(tx) |
| 64 | + require.Nil(t, success, "Expected no success when client fails") |
| 65 | + require.NotNil(t, failure, "Expected failure when client fails") |
| 66 | + require.Equal(t, "500", failure.Code, "Failure code mismatch") |
| 67 | + require.Equal(t, "Internal Server Error", failure.Description, "Failure description mismatch") |
| 68 | + |
| 69 | + // Initialize Arc with a success client. |
| 70 | + a.Client = &MockArcSuccessClient{} |
| 71 | + |
| 72 | + // Broadcast with success client. |
| 73 | + success, failure = a.Broadcast(tx) |
| 74 | + require.NotNil(t, success, "Expected success when client succeeds") |
| 75 | + require.Nil(t, failure, "Expected no failure when client succeeds") |
| 76 | + require.Equal(t, tx.TxID().String(), success.Txid, "Txid mismatch") |
| 77 | + require.Equal(t, "Broadcast Success", success.Message, "Message mismatch") |
| 78 | +} |
0 commit comments