1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
package main

import (
	"strings"
	"testing"
	"time"
)

const fixtureList = `{"Filmliste":["29.09.2026, 14:19","29.09.2026, 12:19","3","MSearch [Vers.: 3.1.292]","abc"],"Filmliste":["Sender","Thema","Titel","Datum","Zeit","Dauer","Größe [MB]","Beschreibung","Url","Website","Url Untertitel","Url RTMP","Url Klein","Url RTMP Klein","Url HD","Url RTMP HD","DatumL","Url History","Geo","neu"],` +
	`"X":["3Sat","...von oben","Portugal von oben","16.10.2024","16:18:00","00:42:52","881","Desc","https://a.example/x/film_3360k_p36v17.mp4","https://www.3sat.de/p","","","20|film_808k_p11v17.mp4","","25|6660k_p37v17.mp4","","1729088280","","DE-AT-CH","false"],` +
	`"X":["","","Zweiter","","","01:00:01","","","https://a.example/y.mp4","","","","","","","","1729088281","","","false"],` +
	`"X":["ZDF","","Dritter","","","bogus","","","https://z.example/z.mp4","","","","","","","","","","","false"],` +
	`"X":["too","short"],` +
	`"X":["","Neues Thema","Vierter","","","","","","https://z.example/w.mp4","","https://sub.example/s.xml","","","","https://full.example/hd.mp4","","1","","","false"]}` + "\n"

func parseAll(t *testing.T, s string) ([]Entry, Stats, error) {
	t.Helper()
	var es []Entry
	st, err := Parse(strings.NewReader(s), nil, func(e Entry) error {
		es = append(es, e)
		return nil
	})
	return es, st, err
}

func TestParse(t *testing.T) {
	es, st, err := parseAll(t, fixtureList)
	if err != nil {
		t.Fatal(err)
	}
	if !st.Complete || st.Rows != 4 || st.RowErrors != 1 {
		t.Fatalf("stats = %+v", st)
	}
	if want := time.Date(2026, 9, 29, 12, 19, 0, 0, time.UTC); !st.Header.Created.Equal(want) {
		t.Errorf("header created = %v", st.Header.Created)
	}

	e := es[0]
	if e.URLSmall != "https://a.example/x/film_808k_p11v17.mp4" {
		t.Errorf("small = %q", e.URLSmall)
	}
	if e.URLHD != "https://a.example/x/film_6660k_p37v17.mp4" {
		t.Errorf("hd = %q", e.URLHD)
	}
	if e.Duration != 42*60+52 || e.SizeMB != 881 || e.Timestamp != 1729088280 {
		t.Errorf("numbers: %+v", e)
	}

	// carried sender and topic
	if es[1].Sender != "3Sat" || es[1].Topic != "...von oben" || es[1].Duration != 3601 {
		t.Errorf("row 2: %+v", es[1])
	}
	if es[1].URLSmall != "" || es[1].URLHD != "" {
		t.Errorf("empty small/hd not empty: %+v", es[1])
	}
	// new sender, carried topic
	if es[2].Sender != "ZDF" || es[2].Topic != "...von oben" || es[2].Duration != 0 {
		t.Errorf("row 3: %+v", es[2])
	}
	// carried sender, new topic, full (non-relative) HD URL
	if es[3].Sender != "ZDF" || es[3].Topic != "Neues Thema" || es[3].URLHD != "https://full.example/hd.mp4" {
		t.Errorf("row 4: %+v", es[3])
	}
}

func TestParseTruncated(t *testing.T) {
	for _, cut := range []int{10, len(fixtureList) / 2, len(fixtureList) - 3} {
		_, st, err := parseAll(t, fixtureList[:cut])
		if err == nil || st.Complete {
			t.Errorf("cut at %d: err=%v complete=%v", cut, err, st.Complete)
		}
	}
}

func TestParseEmptyAndGarbage(t *testing.T) {
	for _, s := range []string{"", "[]", `{"X":["a"]}`, fixtureList + "{}"} {
		_, st, err := parseAll(t, s)
		if err == nil || st.Complete {
			t.Errorf("%.20q: err=%v complete=%v", s, err, st.Complete)
		}
	}
}

func TestExpandURL(t *testing.T) {
	cases := []struct {
		base, v, want string
		ok            bool
	}{
		{"https://a/b.mp4", "", "", true},
		{"https://a/b.mp4", "10|c.mp4", "https://a/c.mp4", true},
		{"https://a/b.mp4", "https://x/y.mp4", "https://x/y.mp4", true},
		{"https://a/b.mp4", "99|c.mp4", "", false},
		{"https://a/b.mp4", "x|c.mp4", "", false},
	}
	for _, c := range cases {
		got, ok := expandURL(c.base, c.v)
		if got != c.want || ok != c.ok {
			t.Errorf("expandURL(%q,%q) = %q,%v", c.base, c.v, got, ok)
		}
	}
}