| 1 | package provider |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestParseServerSearchHitsIgnoresEncryptedContent(t *testing.T) { |
| 9 | raw := json.RawMessage(`[{"type":"web_search_result","title":"Change Log","url":"https://api-docs.deepseek.com/updates/","encrypted_content":"xxx"},{"title":"No URL"},{"text":"body only"}]`) |
| 10 | got := ParseServerSearchHits(raw) |
| 11 | if len(got) != 2 || got[0].Title != "Change Log" || got[0].URL != "https://api-docs.deepseek.com/updates/" || got[1].Title != "No URL" || got[1].URL != "" { |
| 12 | t.Fatalf("hits = %#v", got) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | func TestParseServerSearchQuery(t *testing.T) { |
| 17 | if got := ParseServerSearchQuery(`{"query":"bitcoin price"}`); got != "bitcoin price" { |
| 18 | t.Fatalf("query = %q", got) |
| 19 | } |
| 20 | if got := ParseServerSearchQuery(`{`); got != "" { |
| 21 | t.Fatalf("malformed query = %q", got) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | func TestMergeServerSearch(t *testing.T) { |
| 26 | var dst []ServerSearchCall |
| 27 | dst = MergeServerSearch(dst, ServerSearchCall{ID: "s1", Query: "q"}) |
| 28 | dst = MergeServerSearch(dst, ServerSearchCall{ID: "s1", Results: []ServerSearchHit{{Title: "A", URL: "https://a.example"}}, Raw: json.RawMessage(`[{"title":"A"}]`)}) |
| 29 | if len(dst) != 1 || dst[0].Query != "q" || len(dst[0].Results) != 1 || string(dst[0].Raw) != `[{"title":"A"}]` { |
| 30 | t.Fatalf("merged = %#v", dst) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestFormatServerSearchFootnotes(t *testing.T) { |
| 35 | got := FormatServerSearchFootnotes([]ServerSearchHit{{Title: "Change Log", URL: "https://api-docs.deepseek.com/updates/"}, {Title: "No URL"}}) |
| 36 | want := "\n\n- **Change Log**\n <https://api-docs.deepseek.com/updates/>\n- **No URL**\n" |
| 37 | if got != want { |
| 38 | t.Fatalf("footnotes = %q, want %q", got, want) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | func TestParseServerSearchOutput(t *testing.T) { |
| 43 | got := ParseServerSearchOutput("Change Log\nhttps://api-docs.deepseek.com/updates/\nNo URL") |
| 44 | if len(got) != 2 || got[0].Title != "Change Log" || got[0].URL != "https://api-docs.deepseek.com/updates/" || got[1].Title != "No URL" { |
| 45 | t.Fatalf("parsed = %#v", got) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func TestFormatServerSearchOutput(t *testing.T) { |
| 50 | got := FormatServerSearchOutput([]ServerSearchHit{{Title: "A", URL: "https://a.example"}, {Title: "B"}}) |
| 51 | if got != "A\nhttps://a.example\nB" { |
| 52 | t.Fatalf("output = %q", got) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestFormatServerSearchArgs(t *testing.T) { |
| 57 | if got := FormatServerSearchArgs("bitcoin"); got != `{"query":"bitcoin"}` { |
| 58 | t.Fatalf("args = %q", got) |
| 59 | } |
| 60 | if got := FormatServerSearchArgs(" "); got != "" { |
| 61 | t.Fatalf("blank query args = %q", got) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestWalkServerSearchEstimateOmitsEncryptedRaw(t *testing.T) { |
| 66 | var got []string |
| 67 | WalkServerSearchEstimate(ServerSearchCall{ |
| 68 | ID: "s1", Query: "latest", |
| 69 | Results: []ServerSearchHit{{Title: "Change Log", URL: "https://api-docs.deepseek.com/updates/"}}, |
| 70 | Raw: json.RawMessage(`[{"encrypted_content":"xxx"}]`), |
| 71 | }, func(s string) { |
| 72 | if s != "" { |
| 73 | got = append(got, s) |
| 74 | } |
| 75 | }) |
| 76 | if len(got) != 4 || got[0] != "s1" || got[1] != "latest" || got[2] != "Change Log" || got[3] != "https://api-docs.deepseek.com/updates/" { |
| 77 | t.Fatalf("estimate fields = %#v", got) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestServerSearchFromResponsesItem(t *testing.T) { |
| 82 | raw := json.RawMessage(`{"id":"ws_1","type":"web_search_call","status":"completed","action":{"type":"search","query":"latest","sources":[{"title":"Change Log","url":"https://api-docs.deepseek.com/updates/"}]}}`) |
| 83 | got := ServerSearchFromResponsesItem(raw) |
| 84 | if got == nil || got.ID != "ws_1" || got.Query != "latest" || len(got.Results) != 1 || got.Results[0].Title != "Change Log" { |
| 85 | t.Fatalf("search = %#v", got) |
| 86 | } |
| 87 | if ServerSearchFromResponsesItem(json.RawMessage(`{"id":"x","type":"function_call"}`)) != nil { |
| 88 | t.Fatal("non-search item should be ignored") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestServerSearchFromResponsesVisitedPages(t *testing.T) { |
| 93 | for _, action := range []string{"open_page", "find_in_page"} { |
| 94 | raw := json.RawMessage(`{"id":"ws_page","type":"web_search_call","status":"completed","action":{"type":"` + action + `","url":"https://api-docs.deepseek.com/guides/thinking_mode"}}`) |
| 95 | got := ServerSearchFromResponsesItem(raw) |
| 96 | if got == nil || len(got.Results) != 1 || got.Results[0].URL != "https://api-docs.deepseek.com/guides/thinking_mode" || string(got.Raw) != string(raw) { |
| 97 | t.Fatalf("lost visited source or replay: %+v", got) |
| 98 | } |
| 99 | } |
| 100 | got := ServerSearchFromResponsesItem(json.RawMessage(`{"id":"ws_search","type":"web_search_call","status":"completed","action":{"type":"search","queries":["first query","second query"]}}`)) |
| 101 | if got == nil || got.Query != "first query\nsecond query" || len(got.Results) != 0 { |
| 102 | t.Fatalf("unexpected search: %+v", got) |
| 103 | } |
| 104 | got = ServerSearchFromResponsesItem(json.RawMessage(`{"id":"ws_failed","type":"web_search_call","status":"failed","action":{"type":"open_page","url":"https://example.com"}}`)) |
| 105 | if got == nil || len(got.Results) != 0 { |
| 106 | t.Fatalf("failed page counted as source: %+v", got) |
| 107 | } |
| 108 | } |
| 109 |