discourse/backend/blockchain/service_test.go

221 lines
5.0 KiB
Go
Raw Normal View History

2025-03-25 03:52:30 -04:00
package blockchain
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSubmitPerspective(t *testing.T) {
// Set up test environment variables
os.Setenv("POLYGON_RPC", "https://rpc-amoy.polygon.technology")
os.Setenv("PRIVATE_KEY", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
os.Setenv("CONTRACT_ADDRESS", "0x1234567890123456789012345678901234567890")
// Create service
service, err := NewBlockchainService()
assert.NoError(t, err)
defer service.Close()
// Test cases
tests := []struct {
name string
hash string
wantErr bool
}{
{
name: "Valid hash",
hash: "0x1234567890123456789012345678901234567890123456789012345678901234",
wantErr: false,
},
{
name: "Empty hash",
hash: "",
wantErr: true,
},
{
name: "Invalid hash format",
hash: "invalid",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
txHash, err := service.SubmitPerspective(tt.hash)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.NotEmpty(t, txHash)
})
}
}
func TestProposeInsight(t *testing.T) {
// Set up test environment variables
os.Setenv("POLYGON_RPC", "https://rpc-amoy.polygon.technology")
os.Setenv("PRIVATE_KEY", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
os.Setenv("CONTRACT_ADDRESS", "0x1234567890123456789012345678901234567890")
// Create service
service, err := NewBlockchainService()
assert.NoError(t, err)
defer service.Close()
// Test cases
tests := []struct {
name string
insight string
wantErr bool
}{
{
name: "Valid insight",
insight: "This is a test insight",
wantErr: false,
},
{
name: "Empty insight",
insight: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
txHash, err := service.ProposeInsight(tt.insight)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.NotEmpty(t, txHash)
})
}
}
func TestVoteOnInsight(t *testing.T) {
// Set up test environment variables
os.Setenv("POLYGON_RPC", "https://rpc-amoy.polygon.technology")
os.Setenv("PRIVATE_KEY", "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")
os.Setenv("CONTRACT_ADDRESS", "0x1234567890123456789012345678901234567890")
// Create service
service, err := NewBlockchainService()
assert.NoError(t, err)
defer service.Close()
// Test cases
tests := []struct {
name string
insightID string
vote bool
wantErr bool
}{
{
name: "Valid vote - true",
insightID: "0x1234567890123456789012345678901234567890123456789012345678901234",
vote: true,
wantErr: false,
},
{
name: "Valid vote - false",
insightID: "0x1234567890123456789012345678901234567890123456789012345678901234",
vote: false,
wantErr: false,
},
{
name: "Empty insight ID",
insightID: "",
vote: true,
wantErr: true,
},
{
name: "Invalid insight ID format",
insightID: "invalid",
vote: true,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
txHash, err := service.VoteOnInsight(tt.insightID, tt.vote)
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.NotEmpty(t, txHash)
})
}
}
func TestNewBlockchainService(t *testing.T) {
tests := []struct {
name string
envVars map[string]string
wantErr bool
}{
{
name: "All required environment variables set",
envVars: map[string]string{
"POLYGON_RPC": "https://rpc-amoy.polygon.technology",
"PRIVATE_KEY": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
"CONTRACT_ADDRESS": "0x1234567890123456789012345678901234567890",
},
wantErr: false,
},
{
name: "Missing POLYGON_RPC",
envVars: map[string]string{
"PRIVATE_KEY": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
"CONTRACT_ADDRESS": "0x1234567890123456789012345678901234567890",
},
wantErr: true,
},
{
name: "Missing PRIVATE_KEY",
envVars: map[string]string{
"POLYGON_RPC": "https://rpc-amoy.polygon.technology",
"CONTRACT_ADDRESS": "0x1234567890123456789012345678901234567890",
},
wantErr: true,
},
{
name: "Missing CONTRACT_ADDRESS",
envVars: map[string]string{
"POLYGON_RPC": "https://rpc-amoy.polygon.technology",
"PRIVATE_KEY": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Clear environment variables
os.Unsetenv("POLYGON_RPC")
os.Unsetenv("PRIVATE_KEY")
os.Unsetenv("CONTRACT_ADDRESS")
// Set test environment variables
for key, value := range tt.envVars {
os.Setenv(key, value)
}
// Create service
service, err := NewBlockchainService()
if tt.wantErr {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.NotNil(t, service)
service.Close()
})
}
}