diff --git a/client/identity.go b/client/identity.go index 3443669..258d2ad 100644 --- a/client/identity.go +++ b/client/identity.go @@ -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 diff --git a/client/identity_test.go b/client/identity_test.go index f01aa0d..ec1f39b 100644 --- a/client/identity_test.go +++ b/client/identity_test.go @@ -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") +} diff --git a/client/serverstorage_test.go b/client/serverstorage_test.go index 04f454a..c310549 100644 --- a/client/serverstorage_test.go +++ b/client/serverstorage_test.go @@ -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") }