package store import ( "context" "reflect" "strings" "testing" ) func TestDecodeWarnings(t *testing.T) { old := decodeWarnings(`["Only 3 of 9 folders match."]`) if len(old) != 1 || old[0].Title != "Only 3 of 9 folders match." { t.Fatalf("plain-string warnings from older scans should decode as titles, got %+v", old) } cur := decodeWarnings(`[{"title":"t","items":[{"slave":"a","master":"b","count":2}],"total":5}]`) if len(cur) != 1 || cur[0].Total != 5 || cur[0].Items[0].Count != 2 { t.Fatalf("structured warnings should round-trip, got %+v", cur) } if ws := decodeWarnings(`[]`); ws == nil || len(ws) != 0 { t.Fatalf("empty list should stay an empty (non-nil) list, got %#v", ws) } } func TestMatchLetters(t *testing.T) { ctx := context.Background() st, err := Open(t.TempDir()) if err != nil { t.Fatal(err) } t.Cleanup(func() { st.DB.Close() }) p := Pair{Name: "p", MasterRoot: "/m", SlaveRoot: "/s", RetentionDays: 30} if err := st.SavePair(ctx, &p); err != nil { t.Fatal(err) } for _, rel := range []string{"beck/a.flac", "ABBA/a.flac", "Air/a.flac", "2Pac/a.flac", "Björk/a.flac", "abba/b.flac"} { m := Match{PairID: p.ID, RelSlave: rel, Status: StatusUnmatched, Kind: KindNone} if err := st.UpsertMatch(ctx, &m); err != nil { t.Fatal(err) } } ls, err := st.MatchLetters(ctx, p.ID, MatchFilter{}) if err != nil { t.Fatal(err) } want := []LetterStart{{"2", 0}, {"a", 1}, {"b", 4}} if !reflect.DeepEqual(ls, want) { t.Fatalf("letters = %+v, want %+v", ls, want) } // The offsets must agree with the listing order, whatever the case. ms, _, err := st.Matches(ctx, p.ID, MatchFilter{Limit: 10}) if err != nil { t.Fatal(err) } for _, l := range ls { if got := strings.ToLower(ms[l.Offset].RelSlave[:1]); got != l.Letter { t.Fatalf("offset %d starts with %q, want %q (listing %v)", l.Offset, got, l.Letter, ms) } } }