Files
zapret-autotune/cmd/zapret-autotune/managed_googlevideo_discovery_test.go
T
ZeusinaandGPT-6 Bot 3fc8563995 feat: discover and qualify Googlevideo CDNs autonomously
Retain strict three-host native qualification, discover fresh representative inventory automatically, and recheck deployed media before promotion. Document offline-only verification and outstanding field gates.

Co-authored-by: GPT-6 <gpt-6@openai.com>
2026-10-13 10:12:10 +03:00

192 lines
6.8 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
"time"
)
func samplePlayerBody(host string, expires time.Time) []byte {
u, _ := json.Marshal(fmt.Sprintf("https://%s/videoplayback?expire=%d&sig=private-secret", host, expires.Unix()))
return []byte(fmt.Sprintf(`{"playabilityStatus":{"status":"OK"},"streamingData":{"formats":[{"mimeType":"video/mp4","url":%s}]}}`, u))
}
func TestManagedGooglevideoDiscoveryRotatesFreshInventory(t *testing.T) {
ids := []string{"dQw4w9WgXcQ", "aqz-KE-bpKQ", "jNQXAC9IVRw"}
hosts := []string{"rr1---sn-alpha.googlevideo.com", "rr2---sn-beta.googlevideo.com", "rr5---sn-gamma.googlevideo.com"}
for _, scenario := range []string{"rotating", "unavailable", "expired", "same-root", "unsafe-origin", "invalid-host", "control", "unknown", "mixed-safety", "cancel"} {
t.Run(scenario, func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
calls := 0
collect := managedGooglevideoSamples(ids, func(ctx context.Context, id string) ([]byte, error) {
if _, ok := ctx.Deadline(); !ok {
t.Fatal("unbounded discovery")
}
if id != ids[calls%3] {
t.Fatal("not round robin")
}
calls++
host := hosts[(calls-1)%3]
expires := time.Now().Add(time.Hour)
if calls <= 3 {
switch scenario {
case "rotating":
host = hosts[0]
case "unavailable":
return nil, context.DeadlineExceeded
case "expired":
expires = time.Now().Add(5 * time.Minute)
case "unsafe-origin":
host += ":444"
case "invalid-host":
host = "not-a-ggc.googlevideo.com"
case "control":
host += "\n"
case "unknown":
return nil, errors.New("runner unavailable")
case "mixed-safety":
return nil, errors.Join(context.DeadlineExceeded, errors.New("cleanup failed"))
case "cancel":
cancel()
return nil, ctx.Err()
}
}
if scenario == "same-root" {
host = fmt.Sprintf("rr%d---sn-alpha-x.googlevideo.com", calls)
}
return samplePlayerBody(host, expires), nil
})
resources, err := collect(ctx)
if scenario == "unknown" || scenario == "mixed-safety" {
var fatal *managedRecoveryFatal
if !errors.As(err, &fatal) || calls != 1 || len(resources) != 0 {
t.Fatal(err, calls)
}
return
}
if scenario == "cancel" {
if !errors.Is(err, context.Canceled) || calls != 1 || len(resources) != 0 {
t.Fatal(err, calls)
}
return
}
if scenario == "same-root" {
var insufficient *InsufficientAdaptiveSamples
if !errors.As(err, &insufficient) || insufficient.Attempts != 9 || len(resources) != 0 || strings.Contains(err.Error(), "private-secret") {
t.Fatal(err, calls)
}
return
}
if err != nil || len(resources) != 3 || calls != 6 {
t.Fatal(err, calls, len(resources))
}
for i, r := range resources {
if r.host != hosts[i] || !strings.Contains(r.url, "private-secret") {
t.Fatal("changed signed inventory")
}
}
})
}
}
func TestManagedGooglevideoDiscoveryBoundsAndLateCancellation(t *testing.T) {
ids := []string{"dQw4w9WgXcQ", "aqz-KE-bpKQ", "jNQXAC9IVRw", "aaaaaaaaaaa", "bbbbbbbbbbb"}
calls := 0
resources, err := managedGooglevideoSamples(ids, func(context.Context, string) ([]byte, error) {
calls++
return []byte(`{"playabilityStatus":{"status":"OK"},"streamingData":{"serverAbrStreamingUrl":"private-secret"}}`), nil
})(context.Background())
var insufficient *InsufficientAdaptiveSamples
if !errors.As(err, &insufficient) || calls != 15 || insufficient.Unsupported != 15 || len(resources) != 0 {
t.Fatal(err, calls)
}
for _, invalid := range [][]string{nil, {"bad"}, append(ids, "ccccccccccc")} {
_, err := managedGooglevideoSamples(invalid, func(context.Context, string) ([]byte, error) { t.Fatal("invalid config queried"); return nil, nil })(context.Background())
var fatal *managedRecoveryFatal
if !errors.As(err, &fatal) {
t.Fatal(err)
}
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := []string{"rr1---sn-alpha.googlevideo.com", "rr2---sn-beta.googlevideo.com", "rr5---sn-gamma.googlevideo.com"}
calls = 0
resources, err = managedGooglevideoSamples(ids[:3], func(context.Context, string) ([]byte, error) {
host := hosts[calls]
calls++
if calls == 3 {
cancel()
}
return samplePlayerBody(host, time.Now().Add(time.Hour)), nil
})(ctx)
if !errors.Is(err, context.Canceled) || len(resources) != 0 || calls != 3 {
t.Fatal(err, calls)
}
}
func TestExperimentalPromotionRequiresRepresentativeOwnedProof(t *testing.T) {
for _, enough := range []bool{false, true} {
t.Run(fmt.Sprint(enough), func(t *testing.T) {
d, front, _ := recoveryFixture(t, true)
r, _, resources, _ := googlevideoRuntimeFixture(t)
d.service = true
d.frontHealth = d.health
thumb := unitAssetCandidate(t, d.db, "i.ytimg.com", "--lua-desync=fake", true)
avatar := unitAssetCandidate(t, d.db, "yt3.ggpht.com", "--lua-desync=fake", true)
tx, err := d.db.Begin()
if err != nil {
t.Fatal(err)
}
experiment, err := insertStrategyCandidate(tx, "googlevideo", []string{resources[0].host}, managedGooglevideoArgs, nil, QualificationCold)
if err != nil {
_ = tx.Rollback()
t.Fatal(err)
}
if err := tx.Commit(); err != nil {
t.Fatal(err)
}
old := []int64{front, thumb, avatar, experiment}
if err := d.transaction.Apply(d.db, old, GooglevideoScopeExperimental); err != nil {
t.Fatal(err)
}
d.resources = func(context.Context) (managedServiceInventory, error) {
return managedServiceInventory{media: resources[0], images: map[string]string{"i.ytimg.com": "https://i.ytimg.com/sample.jpg", "yt3.ggpht.com": "https://yt3.ggpht.com/sample.jpg"}, proxy: r.proxy, runner: adaptiveImageRunner}, nil
}
d.recheckExperimental = func(context.Context, storedCandidate, mediaResource, *managedTransaction) error { return nil }
d.mediaSamples = func(context.Context) ([]mediaResource, error) {
if enough {
return resources, nil
}
return resources[:1], nil
}
d.verifyGooglevideo = r.verifyGooglevideo
d.recheckGooglevideo = managedGooglevideoApplyRecheck(r, d.db)
id, err := recoverManagedExperimental(context.Background(), d)
if !enough {
if !errors.Is(err, errGooglevideoInsufficientSamples) || id != experiment {
t.Fatal(id, err)
}
assertActiveCandidates(t, d.transaction, d.db, old)
return
}
if err != nil || id == experiment {
t.Fatal(id, err)
}
assertActiveCandidates(t, d.transaction, d.db, []int64{front, thumb, avatar, id})
p, err := readGooglevideoProof(d.db, id)
if err != nil || p.Version != 1 || len(p.Hosts) != 3 {
t.Fatal(p, err)
}
c, err := readStrategyCandidate(d.db, experiment)
if err != nil || c.Qualified || c.Mode != QualificationCold {
t.Fatal("old row changed", c, err)
}
})
}
}