| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/config" |
| 8 | ) |
| 9 | |
| 10 | func TestProviderViewCarriesHiddenCatalogIdentity(t *testing.T) { |
| 11 | view := providerViewFromEntry(config.ProviderEntry{ |
| 12 | Name: "deepseek-anthropic", Kind: "anthropic", BaseURL: "https://api.deepseek.com/anthropic", Model: "deepseek-v4-flash", |
| 13 | }, false, true) |
| 14 | if view.PresetID != "deepseek-anthropic" || view.Catalog == nil { |
| 15 | t.Fatalf("catalog identity missing: %+v", view) |
| 16 | } |
| 17 | if view.Catalog.BrandID != "deepseek" || view.Catalog.Region != "global" || view.Catalog.Product != "api" { |
| 18 | t.Fatalf("catalog identity = %+v", view.Catalog) |
| 19 | } |
| 20 | if got := view.Catalog.Protocols["anthropic"].BaseURL; got != "https://api.deepseek.com/anthropic" { |
| 21 | t.Fatalf("Anthropic route = %q", got) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestSaveProviderRejectsProtocolEndpointMismatch(t *testing.T) { |
| 26 | cfg := config.Default() |
| 27 | cfg.Providers = []config.ProviderEntry{{ |
| 28 | Name: "deepseek-anthropic", PresetID: "deepseek-anthropic", Kind: "anthropic", BaseURL: "https://api.deepseek.com/anthropic", Model: "deepseek-v4-flash", |
| 29 | }} |
| 30 | view := providerViewFromEntry(cfg.Providers[0], false, true) |
| 31 | view.Kind = "openai" |
| 32 | view.RequestURL = "https://api.deepseek.com/anthropic/v1/chat/completions" |
| 33 | view.ChatURL = view.RequestURL |
| 34 | if err := saveProviderConfig(cfg, view); err == nil || !strings.Contains(err.Error(), "https://api.deepseek.com/v1/chat/completions") { |
| 35 | t.Fatalf("save mismatch error = %v", err) |
| 36 | } |
| 37 | if got := cfg.Providers[0].Kind; got != "anthropic" { |
| 38 | t.Fatalf("rejected save mutated provider kind to %q", got) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestSaveProviderRepairsExactCatalogProtocolMismatch(t *testing.T) { |
| 43 | cfg := config.Default() |
| 44 | cfg.DefaultModel = "deepseek-anthropic/deepseek-v4-flash" |
| 45 | cfg.Providers = []config.ProviderEntry{{ |
| 46 | Name: "deepseek-anthropic", DisplayName: "Deepseek2", PresetID: "deepseek-anthropic", |
| 47 | Kind: "anthropic", BaseURL: "https://api.deepseek.com/anthropic", |
| 48 | Model: "deepseek-v4-flash", Models: []string{"deepseek-v4-flash"}, |
| 49 | }} |
| 50 | view := providerViewFromEntry(cfg.Providers[0], false, true) |
| 51 | view.Kind = "responses" |
| 52 | view.BaseURL = "https://api.deepseek.com" |
| 53 | view.RequestURL = "https://api.deepseek.com/anthropic/v1/messages" |
| 54 | view.ChatURL = "https://stale.example/chat/completions" |
| 55 | if err := saveProviderConfig(cfg, view); err != nil { |
| 56 | t.Fatalf("save repaired provider: %v", err) |
| 57 | } |
| 58 | got := cfg.Providers[0] |
| 59 | if got.Name != "deepseek-anthropic" || got.DisplayName != "Deepseek2" || |
| 60 | got.Kind != "anthropic" || got.BaseURL != "https://api.deepseek.com/anthropic" || |
| 61 | got.RequestURL != "" || got.ChatURL != "" || |
| 62 | cfg.DefaultModel != "deepseek-anthropic/deepseek-v4-flash" { |
| 63 | t.Fatalf("saved provider = %+v default=%q", got, cfg.DefaultModel) |
| 64 | } |
| 65 | refreshed := providerViewFromEntry(got, false, true) |
| 66 | if refreshed.Kind != "anthropic" || refreshed.BaseURL != "https://api.deepseek.com/anthropic" { |
| 67 | t.Fatalf("refreshed provider view = %+v", refreshed) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestProtocolSwitchKeepsStableProviderAndModelReferences(t *testing.T) { |
| 72 | cfg := config.Default() |
| 73 | cfg.DefaultModel = "deepseek-anthropic/deepseek-v4-flash" |
| 74 | cfg.Providers = []config.ProviderEntry{{ |
| 75 | Name: "deepseek-anthropic", DisplayName: "Deepseek2", PresetID: "deepseek-anthropic", Kind: "anthropic", |
| 76 | BaseURL: "https://api.deepseek.com/anthropic", RequestURL: "https://api.deepseek.com/anthropic/v1/messages", |
| 77 | Model: "deepseek-v4-flash", Models: []string{"deepseek-v4-flash"}, |
| 78 | }} |
| 79 | view := providerViewFromEntry(cfg.Providers[0], false, true) |
| 80 | view.Kind = "openai" |
| 81 | view.BaseURL = "https://api.deepseek.com/v1" |
| 82 | view.RequestURL = "https://api.deepseek.com/v1/chat/completions" |
| 83 | view.ChatURL = view.RequestURL |
| 84 | if err := saveProviderConfig(cfg, view); err != nil { |
| 85 | t.Fatalf("save protocol switch: %v", err) |
| 86 | } |
| 87 | got := cfg.Providers[0] |
| 88 | if got.Name != "deepseek-anthropic" || got.PresetID != "deepseek-anthropic" || cfg.DefaultModel != "deepseek-anthropic/deepseek-v4-flash" { |
| 89 | t.Fatalf("stable references changed: provider=%+v default=%q", got, cfg.DefaultModel) |
| 90 | } |
| 91 | if got.DisplayName != "Deepseek2" || got.Kind != "openai" || got.RequestURL != "https://api.deepseek.com/v1/chat/completions" { |
| 92 | t.Fatalf("switched connection = %+v", got) |
| 93 | } |
| 94 | } |
| 95 |