|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "io" |
| 7 | + "log/slog" |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/nextroad-dev/Auto-Router/internal/config" |
| 14 | + "github.com/nextroad-dev/Auto-Router/internal/models" |
| 15 | + "github.com/nextroad-dev/Auto-Router/internal/storage" |
| 16 | +) |
| 17 | + |
| 18 | +func newRegistrySyncTestDB(t *testing.T) *sql.DB { |
| 19 | + t.Helper() |
| 20 | + ctx := context.Background() |
| 21 | + db, err := storage.Open(ctx, storage.Options{Path: ":memory:", BusyTimeout: time.Second}) |
| 22 | + if err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + t.Cleanup(func() { _ = db.Close() }) |
| 26 | + if err := storage.Migrate(ctx, db); err != nil { |
| 27 | + t.Fatal(err) |
| 28 | + } |
| 29 | + return db |
| 30 | +} |
| 31 | + |
| 32 | +func registrySyncConfig(url string, include []string) config.Config { |
| 33 | + cfg := config.Defaults() |
| 34 | + cfg.Registry.Sync.URL = url |
| 35 | + cfg.Registry.Sync.Timeout = config.Duration(time.Second) |
| 36 | + cfg.Registry.Sync.Include = include |
| 37 | + return cfg |
| 38 | +} |
| 39 | + |
| 40 | +func TestRegistrySyncRejectsEmptyAllowlistWithoutNetworkRequest(t *testing.T) { |
| 41 | + requests := 0 |
| 42 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 43 | + requests++ |
| 44 | + w.WriteHeader(http.StatusOK) |
| 45 | + })) |
| 46 | + defer server.Close() |
| 47 | + |
| 48 | + db := newRegistrySyncTestDB(t) |
| 49 | + err := syncRegistry(context.Background(), registrySyncConfig(server.URL, nil), db, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 50 | + if err == nil { |
| 51 | + t.Fatal("empty allowlist unexpectedly synchronized") |
| 52 | + } |
| 53 | + if requests != 0 { |
| 54 | + t.Fatalf("empty allowlist made %d upstream requests", requests) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestRegistrySyncFailureLeavesExistingCatalogAndStateUntouched(t *testing.T) { |
| 59 | + ctx := context.Background() |
| 60 | + db := newRegistrySyncTestDB(t) |
| 61 | + if err := storage.CreateProvider(ctx, db, storage.NewProvider{Key: "existing-provider", Enabled: true}); err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + if err := storage.CreateModel(ctx, db, storage.NewModel{ID: "existing-model", Enabled: true}); err != nil { |
| 65 | + t.Fatal(err) |
| 66 | + } |
| 67 | + |
| 68 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 69 | + http.Error(w, "upstream unavailable", http.StatusBadGateway) |
| 70 | + })) |
| 71 | + defer server.Close() |
| 72 | + |
| 73 | + err := syncRegistry(context.Background(), registrySyncConfig(server.URL, []string{"openai/gpt-test"}), db, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 74 | + if err == nil { |
| 75 | + t.Fatal("upstream failure unexpectedly reported success") |
| 76 | + } |
| 77 | + if _, found, err := storage.GetProvider(ctx, db, "existing-provider"); err != nil || !found { |
| 78 | + t.Fatalf("failed sync changed existing provider: found=%t err=%v", found, err) |
| 79 | + } |
| 80 | + if _, found, err := storage.GetModel(ctx, db, "existing-model"); err != nil || !found { |
| 81 | + t.Fatalf("failed sync changed existing model: found=%t err=%v", found, err) |
| 82 | + } |
| 83 | + if _, found, err := storage.LoadSyncState(ctx, db, models.SourceModelsDev); err != nil || found { |
| 84 | + t.Fatalf("failed sync changed the success state: found=%t err=%v", found, err) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestRegistrySyncSuccessImportsAllowlistedRowsAndState(t *testing.T) { |
| 89 | + ctx := context.Background() |
| 90 | + db := newRegistrySyncTestDB(t) |
| 91 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 92 | + w.Header().Set("Content-Type", "application/json") |
| 93 | + _, _ = io.WriteString(w, `{"openai":{"id":"openai","name":"OpenAI","models":{"gpt-test":{"id":"gpt-test","name":"GPT Test","tool_call":true,"modalities":{"input":["text"],"output":["text"]},"limit":{"context":128000,"output":4096}}}}}`) |
| 94 | + })) |
| 95 | + defer server.Close() |
| 96 | + |
| 97 | + if err := syncRegistry(context.Background(), registrySyncConfig(server.URL, []string{"openai/gpt-test"}), db, slog.New(slog.NewTextHandler(io.Discard, nil))); err != nil { |
| 98 | + t.Fatalf("syncRegistry failed: %v", err) |
| 99 | + } |
| 100 | + catalog, err := storage.LoadCatalog(ctx, db) |
| 101 | + if err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + if len(catalog.Providers) != 1 || len(catalog.Models) != 1 || len(catalog.Pairs) != 1 { |
| 105 | + t.Fatalf("unexpected imported catalog: providers=%d models=%d pairs=%d", len(catalog.Providers), len(catalog.Models), len(catalog.Pairs)) |
| 106 | + } |
| 107 | + state, found, err := storage.LoadSyncState(ctx, db, models.SourceModelsDev) |
| 108 | + if err != nil || !found { |
| 109 | + t.Fatalf("sync state found=%t err=%v", found, err) |
| 110 | + } |
| 111 | + if state.ImportedPairs != 1 || state.URL != server.URL { |
| 112 | + t.Fatalf("unexpected sync state: %+v", state) |
| 113 | + } |
| 114 | +} |
0 commit comments