raindrop_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package raindrop
  4. import (
  5. "encoding/json"
  6. "slices"
  7. "testing"
  8. )
  9. func TestNewClientTagParsing(t *testing.T) {
  10. tests := []struct {
  11. name string
  12. tags string
  13. want []string
  14. }{
  15. {name: "empty string produces no tags", tags: "", want: nil},
  16. {name: "single tag", tags: "news", want: []string{"news"}},
  17. {name: "multiple tags", tags: "news,tech", want: []string{"news", "tech"}},
  18. {name: "whitespace is trimmed", tags: " news , tech ", want: []string{"news", "tech"}},
  19. {name: "empty items are dropped", tags: "news,,tech,", want: []string{"news", "tech"}},
  20. {name: "only separators produce no tags", tags: ", ,", want: nil},
  21. }
  22. for _, tt := range tests {
  23. t.Run(tt.name, func(t *testing.T) {
  24. client := NewClient("token", "collection", tt.tags)
  25. if !slices.Equal(client.tags, tt.want) {
  26. t.Errorf("NewClient(%q) tags = %#v, want %#v", tt.tags, client.tags, tt.want)
  27. }
  28. })
  29. }
  30. }
  31. func TestPayloadOmitsEmptyTags(t *testing.T) {
  32. payload, err := json.Marshal(&raindrop{Link: "https://example.com", Title: "Example"})
  33. if err != nil {
  34. t.Fatalf("unable to marshal payload: %v", err)
  35. }
  36. var fields map[string]json.RawMessage
  37. if err := json.Unmarshal(payload, &fields); err != nil {
  38. t.Fatalf("unable to unmarshal payload: %v", err)
  39. }
  40. if _, found := fields["tags"]; found {
  41. t.Errorf("payload without tags should omit the tags field, got %s", payload)
  42. }
  43. }