| 1 | package storage |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/FelineStateMachine/tinyrelay/internal/event" |
| 8 | ) |
| 9 | |
| 10 | func TestRetentionPreservesIdentityListsAndMail(t *testing.T) { |
| 11 | ctx := context.Background() |
| 12 | s := openTestStore(t) |
| 13 | for _, kind := range []int{1, 0, 3, 30023, 1059, 9735} { |
| 14 | e := sampleEvent(t, kind, 100, [][]string{{"d" , "test" }}) |
| 15 | if _, err := s.Save(ctx, e, SaveOptions{Now: 101}); err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | } |
| 19 | n, err := s.SweepRetention(ctx, RetentionOptions{Now: 200000, Rules: []RetentionRule{{Kind: -1, Days: 1}}}) |
| 20 | if err != nil || n != 1 { |
| 21 | t.Fatalf("sweep %d %v" , n, err) |
| 22 | } |
| 23 | rows, err := s.Query(ctx, event.Filter{}, QueryOptions{Now: 200000, Access: Access{All: true}}) |
| 24 | if err != nil || len(rows.Events) != 5 { |
| 25 | t.Fatalf("retained %d %v" , len(rows.Events), err) |
| 26 | } |
| 27 | n, err = s.SweepRetention(ctx, RetentionOptions{Now: 200000, Rules: []RetentionRule{{Kind: 1059, Days: 1}}}) |
| 28 | if err != nil || n != 1 { |
| 29 | t.Fatalf("explicit mail rule %d %v" , n, err) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestRetentionSpecificRuleOverridesCatchAllAndKeepsRelayRecords(t *testing.T) { |
| 34 | ctx := context.Background() |
| 35 | s := openTestStore(t) |
| 36 | e := sampleEvent(t, 1, 100, nil) |
| 37 | if _, err := s.Save(ctx, e, SaveOptions{Now: 101}); err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | opts := RetentionOptions{Now: 200000, Identity: e.PubKey, Rules: []RetentionRule{{Kind: -1, Days: 1}, {Kind: 1, Days: 1}}} |
| 41 | n, err := s.SweepRetention(ctx, opts) |
| 42 | if err != nil || n != 0 { |
| 43 | t.Fatalf("relay records %d %v" , n, err) |
| 44 | } |
| 45 | opts.Identity = "" |
| 46 | opts.Rules[1].Days = 7 |
| 47 | n, err = s.SweepRetention(ctx, opts) |
| 48 | if err != nil || n != 0 { |
| 49 | t.Fatalf("specific longer rule %d %v" , n, err) |
| 50 | } |
| 51 | } |