|
1 | 1 | package logpoller |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "testing" |
5 | 6 | "time" |
6 | 7 |
|
7 | 8 | "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + "github.com/xssnick/tonutils-go/address" |
| 11 | + "github.com/xssnick/tonutils-go/ton" |
| 12 | + |
| 13 | + "github.com/smartcontractkit/chainlink-common/pkg/logger" |
8 | 14 | ) |
9 | 15 |
|
| 16 | +// mockAPIClient is a minimal mock for ton.APIClientWrapped used in unit tests |
| 17 | +type mockAPIClient struct { |
| 18 | + ton.APIClientWrapped // embed to satisfy interface |
| 19 | + masterchainInfo *ton.BlockIDExt |
| 20 | + masterchainErr error |
| 21 | +} |
| 22 | + |
| 23 | +func (m *mockAPIClient) CurrentMasterchainInfo(_ context.Context) (*ton.BlockIDExt, error) { |
| 24 | + return m.masterchainInfo, m.masterchainErr |
| 25 | +} |
| 26 | + |
| 27 | +func TestGetMasterchainBlockRange_WorkchainValidation(t *testing.T) { |
| 28 | + t.Run("rejects non-masterchain workchain", func(t *testing.T) { |
| 29 | + mock := &mockAPIClient{ |
| 30 | + masterchainInfo: &ton.BlockIDExt{Workchain: 0, SeqNo: 100}, // workchain 0 is base chain, not masterchain |
| 31 | + } |
| 32 | + |
| 33 | + lp := &service{ |
| 34 | + lggr: logger.Sugared(logger.Nop()), |
| 35 | + clientProvider: func(_ context.Context) (ton.APIClientWrapped, error) { |
| 36 | + return mock, nil |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + _, err := lp.getMasterchainBlockRange(context.Background()) |
| 41 | + require.Error(t, err) |
| 42 | + }) |
| 43 | + |
| 44 | + t.Run("accepts masterchain workchain", func(t *testing.T) { |
| 45 | + mock := &mockAPIClient{ |
| 46 | + masterchainInfo: &ton.BlockIDExt{Workchain: address.MasterchainID, SeqNo: 100}, |
| 47 | + } |
| 48 | + |
| 49 | + lp := &service{ |
| 50 | + lggr: logger.Sugared(logger.Nop()), |
| 51 | + clientProvider: func(_ context.Context) (ton.APIClientWrapped, error) { |
| 52 | + return mock, nil |
| 53 | + }, |
| 54 | + lastProcessedBlock: 100, // same as SeqNo, so no new blocks |
| 55 | + } |
| 56 | + |
| 57 | + // should return nil (no new blocks) without error |
| 58 | + blockRange, err := lp.getMasterchainBlockRange(context.Background()) |
| 59 | + require.NoError(t, err) |
| 60 | + require.Nil(t, blockRange, "no new blocks when seqno matches lastProcessedBlock") |
| 61 | + }) |
| 62 | +} |
| 63 | + |
10 | 64 | func TestComputeLookbackWindow(t *testing.T) { |
11 | 65 | t.Run("Basic lookback calculation", func(t *testing.T) { |
12 | 66 | currentSeqNo := uint32(1000) |
|
0 commit comments