| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/BurntSushi/toml" |
| 10 | ) |
| 11 | |
| 12 | func TestEffectiveWebSearchDefaultsOnlySupportedOfficialDeepSeekAPIs(t *testing.T) { |
| 13 | explicitTrue := true |
| 14 | explicitFalse := false |
| 15 | tests := []struct { |
| 16 | name string |
| 17 | entry ProviderEntry |
| 18 | want bool |
| 19 | }{ |
| 20 | { |
| 21 | name: "official responses omitted defaults on", |
| 22 | entry: ProviderEntry{Kind: "responses", BaseURL: "https://api.deepseek.com"}, |
| 23 | want: true, |
| 24 | }, |
| 25 | { |
| 26 | name: "official anthropic omitted defaults on", |
| 27 | entry: ProviderEntry{Kind: "anthropic", BaseURL: "https://api.deepseek.com/anthropic"}, |
| 28 | want: true, |
| 29 | }, |
| 30 | { |
| 31 | name: "official responses explicit off wins", |
| 32 | entry: ProviderEntry{Kind: "responses", BaseURL: "https://api.deepseek.com", WebSearch: &explicitFalse}, |
| 33 | want: false, |
| 34 | }, |
| 35 | { |
| 36 | name: "compatible third party omitted stays off", |
| 37 | entry: ProviderEntry{Kind: "responses", BaseURL: "https://gateway.example/v1"}, |
| 38 | want: false, |
| 39 | }, |
| 40 | { |
| 41 | name: "compatible third party explicit on wins", |
| 42 | entry: ProviderEntry{Kind: "anthropic", BaseURL: "https://gateway.example/anthropic", WebSearch: &explicitTrue}, |
| 43 | want: true, |
| 44 | }, |
| 45 | { |
| 46 | name: "deepseek subdomain is not treated as official", |
| 47 | entry: ProviderEntry{Kind: "responses", BaseURL: "https://relay.deepseek.com"}, |
| 48 | want: false, |
| 49 | }, |
| 50 | { |
| 51 | name: "responses rejects anthropic path", |
| 52 | entry: ProviderEntry{Kind: "responses", BaseURL: "https://api.deepseek.com/anthropic"}, |
| 53 | want: false, |
| 54 | }, |
| 55 | { |
| 56 | name: "anthropic requires documented path", |
| 57 | entry: ProviderEntry{Kind: "anthropic", BaseURL: "https://api.deepseek.com"}, |
| 58 | want: false, |
| 59 | }, |
| 60 | { |
| 61 | name: "official trailing slash accepted", |
| 62 | entry: ProviderEntry{Kind: "anthropic", BaseURL: "https://api.deepseek.com/anthropic/"}, |
| 63 | want: true, |
| 64 | }, |
| 65 | { |
| 66 | name: "insecure scheme is not official", |
| 67 | entry: ProviderEntry{Kind: "responses", BaseURL: "http://api.deepseek.com"}, |
| 68 | want: false, |
| 69 | }, |
| 70 | { |
| 71 | name: "official chat completions has no server tool wire", |
| 72 | entry: ProviderEntry{Kind: "openai", BaseURL: "https://api.deepseek.com", WebSearch: &explicitTrue}, |
| 73 | want: false, |
| 74 | }, |
| 75 | } |
| 76 | for _, tt := range tests { |
| 77 | t.Run(tt.name, func(t *testing.T) { |
| 78 | if got := EffectiveWebSearch(&tt.entry); got != tt.want { |
| 79 | t.Fatalf("EffectiveWebSearch(%+v) = %t, want %t", tt.entry, got, tt.want) |
| 80 | } |
| 81 | }) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestWebSearchTOMLRoundTripPreservesExplicitOff(t *testing.T) { |
| 86 | explicitFalse := false |
| 87 | cfg := &Config{Providers: []ProviderEntry{{ |
| 88 | Name: "deepseek-responses", |
| 89 | Kind: "responses", |
| 90 | BaseURL: "https://api.deepseek.com", |
| 91 | Model: "deepseek-v4-flash", |
| 92 | WebSearch: &explicitFalse, |
| 93 | }}} |
| 94 | rendered := RenderTOML(cfg) |
| 95 | if !strings.Contains(rendered, "web_search = false") { |
| 96 | t.Fatalf("rendered TOML lost explicit web_search=false:\n%s", rendered) |
| 97 | } |
| 98 | |
| 99 | var roundTrip Config |
| 100 | if _, err := toml.Decode(rendered, &roundTrip); err != nil { |
| 101 | t.Fatalf("decode rendered TOML: %v", err) |
| 102 | } |
| 103 | entry, ok := roundTrip.Provider("deepseek-responses") |
| 104 | if !ok { |
| 105 | t.Fatal("round-tripped provider missing") |
| 106 | } |
| 107 | if entry.WebSearch == nil || *entry.WebSearch || EffectiveWebSearch(entry) { |
| 108 | t.Fatalf("round-tripped web search = pointer:%v effective:%t, want explicit false", entry.WebSearch, EffectiveWebSearch(entry)) |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestLegacyOfficialWebSearchOmissionDefaultsOnAcrossLoaders(t *testing.T) { |
| 113 | const legacy = `config_version = 1 |
| 114 | default_model = "deepseek-responses/deepseek-v4-flash" |
| 115 | |
| 116 | [[providers]] |
| 117 | name = "deepseek-responses" |
| 118 | kind = "responses" |
| 119 | base_url = "https://api.deepseek.com" |
| 120 | model = "deepseek-v4-flash" |
| 121 | ` |
| 122 | home := t.TempDir() |
| 123 | workspace := t.TempDir() |
| 124 | t.Setenv("REASONIX_HOME", home) |
| 125 | path := filepath.Join(home, "config.toml") |
| 126 | if err := os.WriteFile(path, []byte(legacy), 0o600); err != nil { |
| 127 | t.Fatal(err) |
| 128 | } |
| 129 | |
| 130 | runtimeConfig, err := LoadForRootReadOnly(workspace) |
| 131 | if err != nil { |
| 132 | t.Fatalf("runtime load: %v", err) |
| 133 | } |
| 134 | editConfig := LoadForEditWithoutCredentials(path) |
| 135 | for name, cfg := range map[string]*Config{"runtime": runtimeConfig, "edit": editConfig} { |
| 136 | entry, ok := cfg.Provider("deepseek-responses") |
| 137 | if !ok { |
| 138 | t.Fatalf("%s legacy provider missing", name) |
| 139 | } |
| 140 | if entry.WebSearch != nil || !EffectiveWebSearch(entry) { |
| 141 | t.Fatalf("%s legacy omitted web search = pointer:%v effective:%t, want nil/true", name, entry.WebSearch, EffectiveWebSearch(entry)) |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 |