Debug GetRequestJobs function and add unit tests
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
ycc
2024-02-12 20:02:02 +01:00
parent 69a07d77d5
commit 0466b1fe05
3 changed files with 67 additions and 5 deletions

View File

@ -3,6 +3,7 @@ package client
import (
"log"
"os"
"strconv"
"testing"
"forge.redroom.link/yves/meowlib"
@ -79,3 +80,52 @@ func TestHidePeer(t *testing.T) {
os.Remove("test.id")
}
}
// test GetRequestJobs
func TestGetRequestJobs(t *testing.T) {
// Create a mock Identity object
id := &Identity{
Peers: []Peer{
{
MyPullServers: []string{"server1", "server2"},
MyLookupKp: meowlib.NewKeyPair(),
},
{
MyPullServers: []string{"server3", "server4"},
MyLookupKp: meowlib.NewKeyPair(),
},
},
unlockedHiddenPeers: []Peer{
{
MyPullServers: []string{"server5", "server6"},
MyLookupKp: meowlib.NewKeyPair(),
},
},
}
id.MessageServers = ServerStorage{
DbFile: "test.db",
}
GetConfig().SetMemPass("test")
GetConfig().SetIdentity(id)
for i := 1; i < 10; i++ {
// initialize a Server with name "server+i"
srv := CreateServerFromUrl("server" + strconv.Itoa(i))
id.MessageServers.StoreServer(srv)
}
// Call GetRequestJobs
jobs := id.GetRequestJobs()
// Check that the returned list is as expected
assert.Equal(t, 6, len(jobs), "Expected 6 jobs")
// Check that each job has the correct server and lookup keys
for _, job := range jobs {
//fmt.Println(job.Server.GetUid(), job.LookupKeys)
assert.Contains(t, []string{"server1", "server2", "server3", "server4", "server5", "server6"}, job.Server.GetUid(), "Unexpected server UID")
assert.Len(t, job.LookupKeys, 1, "Expected 1 lookup key per job")
}
// Clean up
// recursively remove the test.db folder
os.RemoveAll("test.db")
}