|
| 1 | +package player |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// Regression for the issue surfaced 2026-04-28 alongside the issue #166 fix. |
| 11 | +// |
| 12 | +// Symptom in the wild: a Goyabu episode whose Blogger token was dead upstream |
| 13 | +// (video deleted/region-blocked) caused 6 doomed batchexecute calls (3 in the |
| 14 | +// scraper retry loop + 3 in the player-layer redundant resolve), wasting |
| 15 | +// ~12 seconds before the user saw a generic "no valid video URL found". |
| 16 | +// Google signals "this RPC produced no result" with HTTP 200 + a body that is |
| 17 | +// only the `)]}'` anti-hijacking prefix. |
| 18 | +// |
| 19 | +// parseBatchexecuteResponse must distinguish that empty-body case |
| 20 | +// (errBloggerVideoUnavailable, which callers fast-fail on) from a structured |
| 21 | +// response that we just failed to walk (a generic error, which is worth |
| 22 | +// retrying because Google's response shape has shifted before — see the |
| 23 | +// 2026-04-23 fix in the function's docstring). |
| 24 | +func TestParseBatchexecuteResponse_EmptyBodyReturnsUnavailableSentinel(t *testing.T) { |
| 25 | + t.Parallel() |
| 26 | + |
| 27 | + cases := []struct { |
| 28 | + name string |
| 29 | + body string |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "bare anti-hijacking prefix", |
| 33 | + body: `)]}'` + "\n", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "prefix with surrounding whitespace", |
| 37 | + body: "\n )]}'\n\n", |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "prefix only no newline", |
| 41 | + body: `)]}'`, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tc := range cases { |
| 46 | + tc := tc |
| 47 | + t.Run(tc.name, func(t *testing.T) { |
| 48 | + t.Parallel() |
| 49 | + |
| 50 | + _, err := parseBatchexecuteResponse([]byte(tc.body)) |
| 51 | + if err == nil { |
| 52 | + t.Fatalf("expected error for empty batchexecute body, got nil") |
| 53 | + } |
| 54 | + if !errors.Is(err, errBloggerVideoUnavailable) { |
| 55 | + t.Fatalf("expected errBloggerVideoUnavailable, got %v", err) |
| 56 | + } |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// A non-empty response with no recognizable streams is *not* the |
| 62 | +// "video unavailable" case — it's a parser-shape problem worth retrying. |
| 63 | +// This guards the sentinel from being over-broad. |
| 64 | +func TestParseBatchexecuteResponse_StructuredButUnparsedIsNotUnavailable(t *testing.T) { |
| 65 | + t.Parallel() |
| 66 | + |
| 67 | + // Valid wrb.fr envelope but the inner data has no streams array. |
| 68 | + body := `)]}' |
| 69 | +
|
| 70 | +41 |
| 71 | +[["wrb.fr","WcwnYd","[\"some-payload\",null,1]",null,null,null,"generic"]] |
| 72 | +` |
| 73 | + |
| 74 | + _, err := parseBatchexecuteResponse([]byte(body)) |
| 75 | + if err == nil { |
| 76 | + t.Fatalf("expected error, got nil") |
| 77 | + } |
| 78 | + if errors.Is(err, errBloggerVideoUnavailable) { |
| 79 | + t.Fatalf("structured-but-unparsed must NOT be classified as unavailable, got %v", err) |
| 80 | + } |
| 81 | + if !strings.Contains(err.Error(), "no video URL found") { |
| 82 | + t.Fatalf("expected generic parse error, got %v", err) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// Sanity: a real-shape response with a googlevideo.com URL still parses |
| 87 | +// successfully. This protects against the empty-body branch swallowing the |
| 88 | +// happy path. |
| 89 | +func TestParseBatchexecuteResponse_RegexFallbackPicksGoogleVideoURL(t *testing.T) { |
| 90 | + t.Parallel() |
| 91 | + |
| 92 | + body := `)]}' |
| 93 | +
|
| 94 | +128 |
| 95 | +[["wrb.fr","WcwnYd","[null,null,[[[\"https://rr1---sn-xxx.googlevideo.com/videoplayback?expire=123&mime=video%2Fmp4&itag=22\"]]]]",null,null,null,"generic"]] |
| 96 | +` |
| 97 | + |
| 98 | + got, err := parseBatchexecuteResponse([]byte(body)) |
| 99 | + if err != nil { |
| 100 | + t.Fatalf("expected success, got %v", err) |
| 101 | + } |
| 102 | + if !strings.Contains(got, "googlevideo.com") { |
| 103 | + t.Fatalf("expected googlevideo URL, got %q", got) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// TestParseBatchexecuteResponse_TrulyEmptyBody covers a zero-byte body — |
| 108 | +// the most degenerate case. Real Blogger responses always include the |
| 109 | +// `)]}'` prefix, but a connection that closes mid-stream can produce |
| 110 | +// this. Treat it as token-unavailable so the retry loop fast-fails just |
| 111 | +// as it does for the prefix-only case. |
| 112 | +func TestParseBatchexecuteResponse_TrulyEmptyBody(t *testing.T) { |
| 113 | + t.Parallel() |
| 114 | + |
| 115 | + _, err := parseBatchexecuteResponse(nil) |
| 116 | + if err == nil { |
| 117 | + t.Fatalf("expected error for nil body, got nil") |
| 118 | + } |
| 119 | + if !errors.Is(err, errBloggerVideoUnavailable) { |
| 120 | + t.Fatalf("nil body must map to errBloggerVideoUnavailable, got %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + _, err = parseBatchexecuteResponse([]byte("")) |
| 124 | + if err == nil { |
| 125 | + t.Fatalf("expected error for empty body, got nil") |
| 126 | + } |
| 127 | + if !errors.Is(err, errBloggerVideoUnavailable) { |
| 128 | + t.Fatalf("empty body must map to errBloggerVideoUnavailable, got %v", err) |
| 129 | + } |
| 130 | + |
| 131 | + _, err = parseBatchexecuteResponse([]byte(" \n\t\n")) |
| 132 | + if err == nil { |
| 133 | + t.Fatalf("expected error for whitespace-only body, got nil") |
| 134 | + } |
| 135 | + if !errors.Is(err, errBloggerVideoUnavailable) { |
| 136 | + t.Fatalf("whitespace-only body must map to errBloggerVideoUnavailable, got %v", err) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +// TestParseBatchexecuteResponse_SentinelSurvivesWrap proves the sentinel |
| 141 | +// remains identifiable through fmt.Errorf("…: %w", err). This is the |
| 142 | +// exact wrap used by extractActualVideoURL → GetVideoURLForEpisodeEnhanced |
| 143 | +// when surfacing the error to the caller, and it is what the retry-loop |
| 144 | +// fast-fail and the player-layer skip rely on. If anyone replaces %w |
| 145 | +// with %v in the wrap, this test fires. |
| 146 | +func TestParseBatchexecuteResponse_SentinelSurvivesWrap(t *testing.T) { |
| 147 | + t.Parallel() |
| 148 | + |
| 149 | + _, raw := parseBatchexecuteResponse([]byte(`)]}'` + "\n")) |
| 150 | + if raw == nil { |
| 151 | + t.Fatalf("expected raw error from empty-body parse, got nil") |
| 152 | + } |
| 153 | + |
| 154 | + wrapped := fmt.Errorf("video unavailable on this source: %w", |
| 155 | + fmt.Errorf("failed to extract video URL: %w", raw)) |
| 156 | + |
| 157 | + if !errors.Is(wrapped, errBloggerVideoUnavailable) { |
| 158 | + t.Fatalf("sentinel must survive double-wrap; got %v", wrapped) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +// TestParseBatchexecuteResponse_LiveLogReplay locks down the production |
| 163 | +// payload shape observed on 2026-04-28 16:12:30 for the working Black |
| 164 | +// Clover episode 8 — data[2] streams, googlevideo CDN URL, mp4 mime. |
| 165 | +// Built via the same buildBatchexecuteBody helper used by the rest of |
| 166 | +// the suite so the JSON shape is canonical. If Google later changes |
| 167 | +// the wire format and we silently regress, this snapshot fires. |
| 168 | +func TestParseBatchexecuteResponse_LiveLogReplay(t *testing.T) { |
| 169 | + t.Parallel() |
| 170 | + |
| 171 | + body := buildBatchexecuteBody(2, []string{ |
| 172 | + "https://rr5---sn-q4f7l.googlevideo.com/videoplayback?expire=1745958900&itag=18&mime=video%2Fmp4", |
| 173 | + "https://rr5---sn-q4f7l.googlevideo.com/videoplayback?expire=1745958900&itag=22&mime=video%2Fmp4", |
| 174 | + }) |
| 175 | + |
| 176 | + got, err := parseBatchexecuteResponse(body) |
| 177 | + if err != nil { |
| 178 | + t.Fatalf("expected success on the production-shape payload, got %v", err) |
| 179 | + } |
| 180 | + if !strings.Contains(got, "googlevideo.com") { |
| 181 | + t.Fatalf("URL must be a googlevideo CDN URL, got %q", got) |
| 182 | + } |
| 183 | + if !strings.Contains(got, "itag=22") { |
| 184 | + t.Fatalf("expected 720p (itag=22) preference, got %q", got) |
| 185 | + } |
| 186 | +} |
0 commit comments