| 1 | package daemon |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/FelineStateMachine/tinyrelay/internal/event" |
| 12 | gitrelay "github.com/FelineStateMachine/tinyrelay/tinygit" |
| 13 | "github.com/FelineStateMachine/tinyrelay/internal/replication" |
| 14 | "github.com/FelineStateMachine/tinyrelay/internal/storage" |
| 15 | ) |
| 16 | |
| 17 | func TestGitTargetTransportRequiresFeatureAndPrivatePeer(t *testing.T) { |
| 18 | _, tenant := testTenant(t) |
| 19 | repo := gitrelay.Repository{Owner: tenant.Policy().Owner, Identifier: "test" } |
| 20 | if _, err := tenant.gitTargetTransport(context.Background(), repo, "wss://unconfigured.example" ); err == nil { |
| 21 | t.Fatal("disabled sync opened a transport" ) |
| 22 | } |
| 23 | p := tenant.Policy() |
| 24 | p.Features.Grasp, p.Features.Grasp02, p.Features.Grasp08 = true, true, true |
| 25 | p.Reads = "members" |
| 26 | if err := tenant.applyPolicy(context.Background(), p); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if _, err := tenant.gitTargetTransport(context.Background(), repo, "wss://unconfigured.example" ); err == nil { |
| 30 | t.Fatal("private repository could send filters to an unconfigured peer" ) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestLocalSyncItemsUsesBoundedInventoryWindow(t *testing.T) { |
| 35 | _, tenant := testTenant(t) |
| 36 | owner := tenant.Policy().Owner |
| 37 | now := time.Now().Unix() |
| 38 | for i := 0; i < 10001; i++ { |
| 39 | item := event.Event{ID: fmt.Sprintf("%064x" , i+1), PubKey: owner, CreatedAt: now - int64(i), Kind: 1, Tags: [][]string{}, Content: "sync window" } |
| 40 | if _, err := tenant.store.Save(context.Background(), item, storage.SaveOptions{Now: now, SearchMode: tenant.Policy().Features.Search}); err != nil { |
| 41 | t.Fatalf("save event %d: %v" , i, err) |
| 42 | } |
| 43 | } |
| 44 | items, err := tenant.localSyncItems(context.Background(), event.Filter{Authors: []string{owner}, Kinds: []int{1}}) |
| 45 | if !errors.Is(err, replication.ErrPullIncomplete) { |
| 46 | t.Fatalf("inventory error = %v, want ErrPullIncomplete" , err) |
| 47 | } |
| 48 | if len(items) != 10000 { |
| 49 | t.Fatalf("inventory size = %d, want bounded 10000" , len(items)) |
| 50 | } |
| 51 | if strings.TrimSpace(items[0].ID) == "" { |
| 52 | t.Fatal("bounded inventory returned an empty event ID" ) |
| 53 | } |
| 54 | } |