|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package ttx_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/hyperledger-labs/fabric-smart-client/pkg/utils/errors" |
| 13 | + "github.com/hyperledger-labs/fabric-smart-client/platform/view/view" |
| 14 | + "github.com/hyperledger-labs/fabric-token-sdk/token/services/ttx" |
| 15 | + mock2 "github.com/hyperledger-labs/fabric-token-sdk/token/services/ttx/dep/mock" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | +) |
| 18 | + |
| 19 | +// mockExternalWalletSigner is a mock implementation of ExternalWalletSigner for testing |
| 20 | +type mockExternalWalletSigner struct { |
| 21 | + signFunc func(party view.Identity, message []byte) ([]byte, error) |
| 22 | + doneFunc func() error |
| 23 | + doneCalled bool |
| 24 | + signCalled bool |
| 25 | +} |
| 26 | + |
| 27 | +func (m *mockExternalWalletSigner) Sign(party view.Identity, message []byte) ([]byte, error) { |
| 28 | + m.signCalled = true |
| 29 | + if m.signFunc != nil { |
| 30 | + return m.signFunc(party, message) |
| 31 | + } |
| 32 | + return []byte("mock_signature"), nil |
| 33 | +} |
| 34 | + |
| 35 | +func (m *mockExternalWalletSigner) Done() error { |
| 36 | + m.doneCalled = true |
| 37 | + if m.doneFunc != nil { |
| 38 | + return m.doneFunc() |
| 39 | + } |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +// TestCleanupExternalWallets_Success tests that CleanupExternalWallets calls Done() on all wallets |
| 44 | +func TestCleanupExternalWallets_Success(t *testing.T) { |
| 45 | + wallet := &mockExternalWalletSigner{} |
| 46 | + |
| 47 | + externalWallets := map[string]ttx.ExternalWalletSigner{ |
| 48 | + "wallet1": wallet, |
| 49 | + } |
| 50 | + |
| 51 | + // Create a minimal view and context |
| 52 | + view := &ttx.CollectEndorsementsView{} |
| 53 | + ctx := &mock2.Context{} |
| 54 | + ctx.ContextReturns(t.Context()) |
| 55 | + |
| 56 | + // Call the cleanup method |
| 57 | + view.CleanupExternalWallets(ctx, externalWallets) |
| 58 | + |
| 59 | + assert.True(t, wallet.doneCalled, "Done() should have been called on the wallet") |
| 60 | +} |
| 61 | + |
| 62 | +// TestCleanupExternalWallets_MultipleWallets tests that Done() is called on all wallets |
| 63 | +func TestCleanupExternalWallets_MultipleWallets(t *testing.T) { |
| 64 | + wallet1 := &mockExternalWalletSigner{} |
| 65 | + wallet2 := &mockExternalWalletSigner{} |
| 66 | + wallet3 := &mockExternalWalletSigner{} |
| 67 | + |
| 68 | + externalWallets := map[string]ttx.ExternalWalletSigner{ |
| 69 | + "wallet1": wallet1, |
| 70 | + "wallet2": wallet2, |
| 71 | + "wallet3": wallet3, |
| 72 | + } |
| 73 | + |
| 74 | + view := &ttx.CollectEndorsementsView{} |
| 75 | + ctx := &mock2.Context{} |
| 76 | + ctx.ContextReturns(t.Context()) |
| 77 | + |
| 78 | + view.CleanupExternalWallets(ctx, externalWallets) |
| 79 | + |
| 80 | + assert.True(t, wallet1.doneCalled, "Done() should have been called on wallet1") |
| 81 | + assert.True(t, wallet2.doneCalled, "Done() should have been called on wallet2") |
| 82 | + assert.True(t, wallet3.doneCalled, "Done() should have been called on wallet3") |
| 83 | +} |
| 84 | + |
| 85 | +// TestCleanupExternalWallets_DoneError tests that errors from Done() don't stop cleanup |
| 86 | +func TestCleanupExternalWallets_DoneError(t *testing.T) { |
| 87 | + wallet1 := &mockExternalWalletSigner{ |
| 88 | + doneFunc: func() error { |
| 89 | + return errors.New("wallet1 done failed") |
| 90 | + }, |
| 91 | + } |
| 92 | + wallet2 := &mockExternalWalletSigner{} |
| 93 | + wallet3 := &mockExternalWalletSigner{ |
| 94 | + doneFunc: func() error { |
| 95 | + return errors.New("wallet3 done failed") |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + externalWallets := map[string]ttx.ExternalWalletSigner{ |
| 100 | + "wallet1": wallet1, |
| 101 | + "wallet2": wallet2, |
| 102 | + "wallet3": wallet3, |
| 103 | + } |
| 104 | + |
| 105 | + view := &ttx.CollectEndorsementsView{} |
| 106 | + ctx := &mock2.Context{} |
| 107 | + ctx.ContextReturns(t.Context()) |
| 108 | + |
| 109 | + // Should not panic even if Done() returns errors |
| 110 | + view.CleanupExternalWallets(ctx, externalWallets) |
| 111 | + |
| 112 | + assert.True(t, wallet1.doneCalled, "Done() should have been called on wallet1 despite error") |
| 113 | + assert.True(t, wallet2.doneCalled, "Done() should have been called on wallet2") |
| 114 | + assert.True(t, wallet3.doneCalled, "Done() should have been called on wallet3 despite error") |
| 115 | +} |
| 116 | + |
| 117 | +// TestCleanupExternalWallets_EmptyMap tests cleanup with no wallets |
| 118 | +func TestCleanupExternalWallets_EmptyMap(t *testing.T) { |
| 119 | + externalWallets := map[string]ttx.ExternalWalletSigner{} |
| 120 | + |
| 121 | + view := &ttx.CollectEndorsementsView{} |
| 122 | + ctx := &mock2.Context{} |
| 123 | + ctx.ContextReturns(t.Context()) |
| 124 | + |
| 125 | + // Should not panic with empty map |
| 126 | + view.CleanupExternalWallets(ctx, externalWallets) |
| 127 | +} |
0 commit comments