@@ -5,21 +5,15 @@ import (
5
5
"testing"
6
6
"time"
7
7
8
+ "github.com/lightninglabs/lightning-terminal/session"
8
9
"github.com/stretchr/testify/require"
9
10
)
10
11
11
- // TestActionStorage tests that the ActionsDB CRUD logic.
12
- func TestActionStorage ( t * testing. T ) {
13
- tmpDir := t . TempDir ( )
12
+ var (
13
+ sessionID1 = intToSessionID ( 1 )
14
+ sessionID2 = intToSessionID ( 2 )
14
15
15
- db , err := NewDB (tmpDir , "test.db" , nil )
16
- require .NoError (t , err )
17
- t .Cleanup (func () {
18
- _ = db .Close ()
19
- })
20
-
21
- sessionID1 := [4 ]byte {1 , 1 , 1 , 1 }
22
- action1 := & Action {
16
+ action1 = & Action {
23
17
SessionID : sessionID1 ,
24
18
ActorName : "Autopilot" ,
25
19
FeatureName : "auto-fees" ,
@@ -32,8 +26,7 @@ func TestActionStorage(t *testing.T) {
32
26
State : ActionStateDone ,
33
27
}
34
28
35
- sessionID2 := [4 ]byte {2 , 2 , 2 , 2 }
36
- action2 := & Action {
29
+ action2 = & Action {
37
30
SessionID : sessionID2 ,
38
31
ActorName : "Autopilot" ,
39
32
FeatureName : "rebalancer" ,
@@ -44,6 +37,17 @@ func TestActionStorage(t *testing.T) {
44
37
AttemptedAt : time .Unix (12300 , 0 ),
45
38
State : ActionStateInit ,
46
39
}
40
+ )
41
+
42
+ // TestActionStorage tests that the ActionsDB CRUD logic.
43
+ func TestActionStorage (t * testing.T ) {
44
+ tmpDir := t .TempDir ()
45
+
46
+ db , err := NewDB (tmpDir , "test.db" , nil )
47
+ require .NoError (t , err )
48
+ t .Cleanup (func () {
49
+ _ = db .Close ()
50
+ })
47
51
48
52
actionsStateFilterFn := func (state ActionState ) ListActionsFilterFn {
49
53
return func (a * Action , _ bool ) (bool , bool ) {
@@ -335,3 +339,91 @@ func TestListActions(t *testing.T) {
335
339
{sessionID2 , "6" },
336
340
})
337
341
}
342
+
343
+ // TestListGroupActions tests that the ListGroupActions correctly returns all
344
+ // actions in a particular session group.
345
+ func TestListGroupActions (t * testing.T ) {
346
+ group1 := intToSessionID (0 )
347
+
348
+ // Link session 1 and session 2 to group 1.
349
+ index := newMockSessionIDIndex ()
350
+ index .addPair (sessionID1 , group1 )
351
+ index .addPair (sessionID2 , group1 )
352
+
353
+ db , err := NewDB (t .TempDir (), "test.db" , index )
354
+ require .NoError (t , err )
355
+ t .Cleanup (func () {
356
+ _ = db .Close ()
357
+ })
358
+
359
+ // There should not be any actions in group 1 yet.
360
+ al , err := db .ListGroupActions (group1 , nil )
361
+ require .NoError (t , err )
362
+ require .Empty (t , al )
363
+
364
+ // Add an action under session 1.
365
+ _ , err = db .AddAction (sessionID1 , action1 )
366
+ require .NoError (t , err )
367
+
368
+ // There should now be one action in the group.
369
+ al , err = db .ListGroupActions (group1 , nil )
370
+ require .NoError (t , err )
371
+ require .Len (t , al , 1 )
372
+ require .Equal (t , sessionID1 , al [0 ].SessionID )
373
+
374
+ // Add an action under session 2.
375
+ _ , err = db .AddAction (sessionID2 , action2 )
376
+ require .NoError (t , err )
377
+
378
+ // There should now be actions in the group.
379
+ al , err = db .ListGroupActions (group1 , nil )
380
+ require .NoError (t , err )
381
+ require .Len (t , al , 2 )
382
+ require .Equal (t , sessionID1 , al [0 ].SessionID )
383
+ require .Equal (t , sessionID2 , al [1 ].SessionID )
384
+ }
385
+
386
+ type mockSessionIDIndex struct {
387
+ sessionToGroupID map [session.ID ]session.ID
388
+ groupToSessionIDs map [session.ID ][]session.ID
389
+ }
390
+
391
+ var _ session.IDToGroupIndex = (* mockSessionIDIndex )(nil )
392
+
393
+ func newMockSessionIDIndex () * mockSessionIDIndex {
394
+ return & mockSessionIDIndex {
395
+ sessionToGroupID : make (map [session.ID ]session.ID ),
396
+ groupToSessionIDs : make (map [session.ID ][]session.ID ),
397
+ }
398
+ }
399
+
400
+ func (m * mockSessionIDIndex ) addPair (sessionID , groupID session.ID ) {
401
+ m .sessionToGroupID [sessionID ] = groupID
402
+
403
+ m .groupToSessionIDs [groupID ] = append (
404
+ m .groupToSessionIDs [groupID ], sessionID ,
405
+ )
406
+ }
407
+
408
+ func (m * mockSessionIDIndex ) GetGroupID (sessionID session.ID ) (session.ID ,
409
+ error ) {
410
+
411
+ id , ok := m .sessionToGroupID [sessionID ]
412
+ if ! ok {
413
+ return session.ID {}, fmt .Errorf ("no group ID found for " +
414
+ "session ID" )
415
+ }
416
+
417
+ return id , nil
418
+ }
419
+
420
+ func (m * mockSessionIDIndex ) GetSessionIDs (groupID session.ID ) ([]session.ID ,
421
+ error ) {
422
+
423
+ ids , ok := m .groupToSessionIDs [groupID ]
424
+ if ! ok {
425
+ return nil , fmt .Errorf ("no session IDs found for group ID" )
426
+ }
427
+
428
+ return ids , nil
429
+ }
0 commit comments