Skip to content

Commit 1b170d7

Browse files
committed
add tests
1 parent c471af4 commit 1b170d7

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

block/header_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package block
22

33
import (
44
"encoding/hex"
5+
"strings"
56
"testing"
67
)
78

@@ -110,3 +111,29 @@ func TestHeaderPrevBlockAndMerkleRoot(t *testing.T) {
110111
t.Errorf("PrevBlock = %s, want %s", prevBlockStr, expectedPrevBlock)
111112
}
112113
}
114+
115+
func TestHeaderString(t *testing.T) {
116+
genesisHex := "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c"
117+
118+
header, err := NewHeaderFromHex(genesisHex)
119+
if err != nil {
120+
t.Fatalf("NewHeaderFromHex() error = %v", err)
121+
}
122+
123+
str := header.String()
124+
if str == "" {
125+
t.Error("String() returned empty string")
126+
}
127+
128+
expectedHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
129+
if !strings.Contains(str, expectedHash) {
130+
t.Errorf("String() should contain hash %s, got %s", expectedHash, str)
131+
}
132+
}
133+
134+
func TestNewHeaderFromHexInvalid(t *testing.T) {
135+
_, err := NewHeaderFromHex("invalid hex")
136+
if err == nil {
137+
t.Error("NewHeaderFromHex() with invalid hex should return error")
138+
}
139+
}

transaction/beef_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,26 @@ func TestBeefClone(t *testing.T) {
267267
original, err := NewBeefFromBytes(beefBytes)
268268
require.NoError(t, err)
269269

270+
// Test cloning with nil fields by adding a BeefTx with minimal data
271+
nilFieldsTxID := chainhash.HashH([]byte("test-nil-fields"))
272+
original.Transactions[nilFieldsTxID] = &BeefTx{
273+
DataFormat: TxIDOnly,
274+
KnownTxID: nil,
275+
InputTxids: nil,
276+
Transaction: nil,
277+
}
278+
279+
// Test cloning with InputTxids populated
280+
inputTxidsTxID := chainhash.HashH([]byte("test-input-txids"))
281+
knownID := chainhash.HashH([]byte("known-id"))
282+
inputID1 := chainhash.HashH([]byte("input-1"))
283+
original.Transactions[inputTxidsTxID] = &BeefTx{
284+
DataFormat: TxIDOnly,
285+
KnownTxID: &knownID,
286+
InputTxids: []*chainhash.Hash{&inputID1, nil},
287+
Transaction: nil,
288+
}
289+
270290
// Clone the object
271291
clone := original.Clone()
272292

transaction/merklepath_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,25 @@ func TestMerklePathCombine(t *testing.T) {
296296
}
297297
})
298298
}
299+
300+
func TestMerklePathClone(t *testing.T) {
301+
t.Run("clones nil merkle path", func(t *testing.T) {
302+
var mp *MerklePath
303+
clone := mp.Clone()
304+
require.Nil(t, clone)
305+
})
306+
307+
t.Run("clones valid merkle path", func(t *testing.T) {
308+
original, err := NewMerklePathFromHex(BRC74Hex)
309+
require.NoError(t, err)
310+
311+
clone := original.Clone()
312+
require.NotNil(t, clone)
313+
require.Equal(t, original.BlockHeight, clone.BlockHeight)
314+
require.Equal(t, len(original.Path), len(clone.Path))
315+
316+
// Verify modifying clone doesn't affect original
317+
clone.BlockHeight = 999999
318+
require.NotEqual(t, original.BlockHeight, clone.BlockHeight)
319+
})
320+
}

0 commit comments

Comments
 (0)