47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
|
|
package exa
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestSearchParsesHighlights(t *testing.T) {
|
||
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
if r.Header.Get("x-api-key") != "test-key" {
|
||
|
|
t.Fatalf("missing api key header")
|
||
|
|
}
|
||
|
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
||
|
|
"results": []map[string]any{
|
||
|
|
{
|
||
|
|
"title": "Threads post",
|
||
|
|
"url": "https://www.threads.net/@alice/post/abc123",
|
||
|
|
"highlights": []string{"這是一則測試貼文內容"},
|
||
|
|
"highlightScores": []float64{0.82},
|
||
|
|
},
|
||
|
|
},
|
||
|
|
})
|
||
|
|
}))
|
||
|
|
defer server.Close()
|
||
|
|
|
||
|
|
client := NewClient("test-key")
|
||
|
|
client.baseURL = server.URL
|
||
|
|
|
||
|
|
res, err := client.Search(context.Background(), SearchOptions{
|
||
|
|
Query: "Threads 貼文",
|
||
|
|
Limit: 5,
|
||
|
|
Mode: ModeThreadsDiscover,
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
t.Fatalf("search failed: %v", err)
|
||
|
|
}
|
||
|
|
if res.Status != "success" || len(res.Results) != 1 {
|
||
|
|
t.Fatalf("unexpected response: %+v", res)
|
||
|
|
}
|
||
|
|
if res.Results[0].Snippet != "這是一則測試貼文內容" {
|
||
|
|
t.Fatalf("unexpected snippet: %q", res.Results[0].Snippet)
|
||
|
|
}
|
||
|
|
}
|