Files
zapret-autotune/cmd/zapret-autotune/managed_googlevideo_recovery_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

240 lines
8.3 KiB
Go

package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"image"
"image/png"
"io"
"reflect"
"strconv"
"strings"
"testing"
"time"
)
func TestManagedAdaptiveConfig(t *testing.T) {
for _, tc := range []struct {
mode, service, ids string
valid bool
}{
{"", "", "", true}, {"adaptive", "1", `["dQw4w9WgXcQ","aqz-KE-bpKQ","jNQXAC9IVRw"]`, true},
{"adaptive", "0", "", false}, {"unknown", "1", "", false},
{"adaptive", "1", `[]`, false}, {"adaptive", "1", `["bad"]`, false},
{"adaptive", "1", `["dQw4w9WgXcQ","dQw4w9WgXcQ"]`, false},
{"adaptive", "1", `["dQw4w9WgXcQ","aqz-KE-bpKQ","jNQXAC9IVRw","aaaaaaaaaaa","bbbbbbbbbbb","ccccccccccc"]`, false},
} {
lookup := func(key string) (string, bool) {
v := map[string]string{"ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE": tc.mode, "ZAPRET_AUTOTUNE_YOUTUBE_SERVICE": tc.service, "ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS": tc.ids}[key]
return v, v != ""
}
cfg, err := parseHealthConfig(lookup)
if (err == nil) != tc.valid {
t.Fatalf("%+v: %v", tc, err)
}
mode, ids, _ := managedGooglevideoConfig(lookup, cfg.VideoID)
if tc.mode == "" && (mode != QualificationCold || !reflect.DeepEqual(ids, []string{cfg.VideoID})) {
t.Fatal(cfg)
}
}
}
func TestManagedAdaptiveRecoveryProductionPath(t *testing.T) {
for _, scenario := range []string{"qualified", "one-host", "unqualified-assets", "avatar-missing", "rollback", "apply-bad-body", "apply-mixed-guard", "apply-learns"} {
t.Run(scenario, func(t *testing.T) {
d, front, events := recoveryFixture(t, true)
r, _, resources, _ := googlevideoRuntimeFixture(t)
d.service, d.adaptive = true, true
d.frontHealth = d.health
assets := []int64{unitAssetCandidate(t, d.db, "i.ytimg.com", "--lua-desync=fake", scenario != "unqualified-assets"), unitAssetCandidate(t, d.db, "yt3.ggpht.com", "--lua-desync=fake", true)}
old := []int64{front}
if scenario != "unqualified-assets" {
old = append(old, assets...)
}
if err := d.transaction.Apply(d.db, old); err != nil {
t.Fatal(err)
}
d.resources = func(context.Context) (managedServiceInventory, error) {
return managedServiceInventory{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.mediaSamples = func(context.Context) ([]mediaResource, error) {
if scenario == "one-host" {
return resources[:1], nil
}
return resources, nil
}
d.verifyGooglevideo = r.verifyGooglevideo
d.recheckGooglevideo = managedGooglevideoApplyRecheck(r, d.db)
applyCalls := 0
if strings.HasPrefix(scenario, "apply-") {
deployed := r
if scenario == "apply-mixed-guard" {
deployed.guard = func() error { return errors.Join(context.Canceled, errors.New("guard failed")) }
}
deployed.curl = func(ctx context.Context, args []string, stdin io.Reader, out, diagnostic io.Writer) error {
applyCalls++
if scenario == "apply-learns" && applyCalls <= 2 {
return context.DeadlineExceeded
}
var body bytes.Buffer
err := r.curl(ctx, args, stdin, &body, diagnostic)
if err != nil {
return err
}
b := body.Bytes()
if scenario == "apply-bad-body" {
b = bytes.ReplaceAll(b, []byte("ftyp"), []byte("oops"))
}
_, err = out.Write(b)
return err
}
d.recheckGooglevideo = managedGooglevideoApplyRecheck(deployed, d.db)
}
if scenario == "avatar-missing" {
d.health = func(context.Context) (string, error) {
return "DEGRADED", &managedResourceFailure{"avatar", "yt3.ggpht.com", nil}
}
_, ids, err := managedGooglevideoConfig(func(key string) (string, bool) { return "1", key == "ZAPRET_AUTOTUNE_YOUTUBE_SERVICE" }, "dQw4w9WgXcQ")
if err != nil || len(ids) != 3 {
t.Fatal(ids, err)
}
calls := 0
d.mediaSamples = managedGooglevideoSamples(ids, func(_ context.Context, id string) ([]byte, error) {
if id != ids[calls%3] {
t.Fatal("wrong default video")
}
index := calls % 3
if calls < 3 {
index = 0
}
calls++
return samplePlayerBody(resources[index].host, time.Now().Add(time.Hour)), nil
})
}
if scenario == "rollback" {
d.recheckGooglevideo = func(context.Context, storedCandidate, []mediaResource, *managedTransaction) error {
return errCandidateNonpass
}
}
result, err := recoverManagedTarget(context.Background(), d)
if scenario == "apply-mixed-guard" {
var fatal *managedRecoveryFatal
if !errors.As(err, &fatal) {
t.Fatal("mixed guard downgraded", err)
}
assertActiveCandidates(t, d.transaction, d.db, old)
return
}
if err != nil {
t.Fatal(err)
}
if scenario == "one-host" || scenario == "rollback" || scenario == "apply-bad-body" {
if result.GGCError == nil {
t.Fatal("unqualified stage succeeded")
}
assertActiveCandidates(t, d.transaction, d.db, old)
b, _ := d.transaction.read("config")
if strings.Contains(string(b), "--hostlist-domains=googlevideo.com") {
t.Fatal("unproven suffix")
}
return
}
if result.GGCError != nil || result.GooglevideoID == 0 {
t.Fatal(result)
}
c, err := readStrategyCandidate(d.db, result.GooglevideoID)
if err != nil || !c.Qualified || c.Mode != QualificationAdaptive {
t.Fatal(c, err)
}
p, err := readGooglevideoProof(d.db, c.ID)
if err != nil || p.SourceKind != NativeCircularPostWarmup {
t.Fatal(p, err)
}
j, err := d.transaction.load()
if err != nil || j.Scope != GooglevideoScopeSuffix {
t.Fatal(j, err)
}
assertActiveCandidates(t, d.transaction, d.db, append(old, c.ID))
if scenario == "apply-learns" && applyCalls < 20 {
t.Fatal("deployed warmup or full TLS transfers skipped", applyCalls)
}
if scenario == "avatar-missing" {
status, err := d.health(context.Background())
if status != "DEGRADED" || err == nil {
t.Fatal("whole-service health concealed")
}
}
if strings.Join(*events, ",") != "stop,start,restart" {
t.Fatal("restore must precede apply", *events)
}
})
}
}
func adaptiveImageRunner(_ context.Context, args []string, stdin io.Reader, out, diagnostic io.Writer) error {
b, err := io.ReadAll(stdin)
if err != nil {
return err
}
raw, err := strconv.Unquote(strings.TrimSuffix(strings.TrimPrefix(string(b), "url = "), "\n"))
if err != nil {
return err
}
var body bytes.Buffer
if err := png.Encode(&body, image.NewRGBA(image.Rect(0, 0, 32, 32))); err != nil {
return err
}
fmt.Fprintf(out, "HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: %d\r\n\r\n", body.Len())
_, _ = out.Write(body.Bytes())
metrics, _ := json.Marshal(map[string]interface{}{"url_effective": raw, "ssl_verify_result": 0, "num_redirects": 0, "http_code": 200, "time_total": 0.1, "speed_download": 10000, "size_download": body.Len()})
fmt.Fprint(out, resourceTrailer, string(metrics))
tls := "TLSv1.2"
if strings.Contains(strings.Join(args, " "), "--tlsv1.3") {
tls = "TLSv1.3"
}
fmt.Fprintln(diagnostic, "SSL connection using", tls)
return nil
}
func TestInsufficientAdaptiveSamplesCooldown(t *testing.T) {
err := &InsufficientAdaptiveSamples{}
if errors.Is(err, errNoQualifiedStrategy) || !errors.Is(err, errGooglevideoInsufficientSamples) || managedRecoveryCooldown(err, time.Minute) != time.Minute {
t.Fatal(err)
}
}
func TestGenuineNoQualifiedCooldown(t *testing.T) {
if managedRecoveryCooldown(errNoQualifiedStrategy, time.Minute) != 2*time.Hour {
t.Fatal("lost exhausted-search pause")
}
}
func TestManagedYouTubeDefaultSampleInventory(t *testing.T) {
for _, primary := range []string{"dQw4w9WgXcQ", "aqz-KE-bpKQ"} {
lookup := func(key string) (string, bool) { return "1", key == "ZAPRET_AUTOTUNE_YOUTUBE_SERVICE" }
_, ids, err := managedGooglevideoConfig(lookup, primary)
want := []string{primary, "aqz-KE-bpKQ", "jNQXAC9IVRw"}
if primary == "aqz-KE-bpKQ" {
want = []string{primary, "jNQXAC9IVRw"}
}
if err != nil || !reflect.DeepEqual(ids, want) {
t.Fatal(ids, err)
}
}
_, ids, err := managedGooglevideoConfig(func(key string) (string, bool) {
switch key {
case "ZAPRET_AUTOTUNE_YOUTUBE_SERVICE":
return "1", true
case "ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS":
return `["aaaaaaaaaaa"]`, true
}
return "", false
}, "dQw4w9WgXcQ")
if err != nil || !reflect.DeepEqual(ids, []string{"aaaaaaaaaaa"}) {
t.Fatal(ids, err)
}
}