package service import ( "testing" ) func TestParseFormatJSON(t *testing.T) { data := []byte(`{ "id": "vid", "formats": [ {"format_id": "18", "ext": "mp4", "resolution": "640x360", "fps": 30, "vcodec": "avc1", "acodec": "mp4a", "format_note": "360p"}, {"format_id": "137", "ext": "mp4", "width": 1920, "height": 1080, "fps": 60, "vcodec": "avc1", "acodec": "none", "filesize": 1048576, "format_note": "1080p"}, {"format_id": "233", "ext": "m4a", "resolution": "audio only", "vcodec": "none", "acodec": "mp4a", "audio_channels": 2, "format_note": "audio"} ] }`) formats, err := parseFormatJSON(data) if err != nil { t.Fatalf("parseFormatJSON: %v", err) } if len(formats) != 3 { t.Fatalf("expected 3 formats, got %d: %+v", len(formats), formats) } if formats[0].ID != "18" || formats[0].Ext != "mp4" || formats[0].Resolution != "640x360" { t.Errorf("format[0] = %+v", formats[0]) } if formats[0].FPS != "30" { t.Errorf("format[0] fps = %q, want 30", formats[0].FPS) } // Resolution is derived from width/height when no resolution string is present. if formats[1].Resolution != "1920x1080" { t.Errorf("format[1] resolution = %q, want 1920x1080", formats[1].Resolution) } if formats[1].FileSize != "1.0 MiB" { t.Errorf("format[1] filesize = %q, want 1.0 MiB", formats[1].FileSize) } // Audio-only format: codec falls back to acodec and channels are populated. if formats[2].Codec != "mp4a" { t.Errorf("format[2] codec = %q, want mp4a", formats[2].Codec) } if formats[2].Channels != "2" { t.Errorf("format[2] channels = %q, want 2", formats[2].Channels) } } func TestParseFormatJSONPlaylistFallback(t *testing.T) { // A playlist dump exposes formats under the first entry, not at the top level. data := []byte(`{"_type":"playlist","entries":[{"id":"a","formats":[{"format_id":"18","ext":"mp4"}]}]}`) formats, err := parseFormatJSON(data) if err != nil { t.Fatalf("parseFormatJSON: %v", err) } if len(formats) != 1 || formats[0].ID != "18" { t.Fatalf("expected 1 format from entry fallback, got %+v", formats) } }