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

@ -246,9 +246,10 @@ type RequestsJob struct {
func (id *Identity) GetRequestJobs() []*RequestsJob {
var list []*RequestsJob
srvs := map[string]*RequestsJob{}
// build a server map
// get all servers
servers, err := id.MessageServers.LoadAllServers()
if err != nil {
if err == nil {
// build a server map
for _, server := range servers {
var rj RequestsJob
rj.Server = server
@ -271,8 +272,10 @@ func (id *Identity) GetRequestJobs() []*RequestsJob {
// todo random reorder
// build list
for _, value := range srvs {
list = append(list, value)
for _, srv := range srvs {
if len(srv.LookupKeys) > 0 {
list = append(list, srv)
}
}
}
return list

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")
}

View File

@ -2,6 +2,7 @@ package client
import (
"log"
"os"
"testing"
"forge.redroom.link/yves/meowlib"
@ -42,7 +43,9 @@ func TestStoreServer(t *testing.T) {
if sout.Name != srv.Name {
log.Fatal("name not found")
}
// Clean up
// recursively remove the test.db folder
os.RemoveAll("test.db")
}
func TestLoadServersFromUids(t *testing.T) {
@ -67,6 +70,9 @@ func TestLoadServersFromUids(t *testing.T) {
if sout[0].Name != srv.Name {
log.Fatal("name not found")
}
// Clean up
// recursively remove the test.db folder
os.RemoveAll("test.db")
}
func TestLoadServerCardsFromUids(t *testing.T) {
@ -91,4 +97,7 @@ func TestLoadServerCardsFromUids(t *testing.T) {
if sout[0].Name != srv.Name {
log.Fatal("name not found")
}
// Clean up
// recursively remove the test.db folder
os.RemoveAll("test.db")
}