mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-06-29 23:41:12 +08:00
## Summary
This PR re-enables the Go test steps in CI that were previously
commented out, and fixes all compilation errors that have accumulated in
`internal/entity/models/` since the `ListModels` return type was changed
from `[]string` to `[]ListModelResponse`.
## Changes
### CI (`.github/workflows/tests.yml`)
- Re-enable **Prepare test resources** step (clones resource repo with
WordNet data)
- Re-enable **Test Go packages** step (runs `go test ./internal/...`)
- Fix resource path race condition by using
`/tmp/resource-${GITHUB_RUN_ID}` instead of `/tmp/resource`
- Exclude `/cli` package from Go tests (contains `main` redeclarations)
### Test fixes (16 model provider test files)
All errors were caused by the upstream change from `[]string` to
`[]ListModelResponse` in the `ListModels` interface:
- Add `joinModelNames` test helper to extract `.Name` from
`[]ListModelResponse` slices
- `strings.Join(models, ",")` → `joinModelNames(models, ",")` (11 files)
- `ids[i] != "..."` → `ids[i].Name != "..."` (cometapi, mistral)
- `got[i] != want[i]` → `got[i].Name != want[i]` (bedrock)
- `[]string` return types → `[]ListModelResponse` (google)
### Pre-existing bugs in model_test.go
Bugs introduced by the upstream `entity/` → `entity/models/` directory
rename:
- Add missing `pm := GetProviderManager()` calls in 3 test functions
- Fix `InitProviderManager` signature (`_, err :=` → `err :=`)
- Fix `MaxTokens` `*int` dereference (6 comparisons)
- Fix `readProviderConfig` relative path (3 levels up instead of 2)
### model.go
- Add `findRepoRoot()` to make `conf/all_models.json` resolution work
from any CWD, fixing `TestSiliconFlowProviderConfigLoadsLatestProModels`
### Test validation
```bash
go build ./internal/... # ✅
go test ./internal/entity/models/... -count=1 # ✅ all pass
```
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
323 lines
9.7 KiB
Go
323 lines
9.7 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func newReplicateForTest(baseURL string) *ReplicateModel {
|
|
return NewReplicateModel(
|
|
map[string]string{"default": baseURL},
|
|
URLSuffix{Chat: "v1/predictions", Models: "v1/models"},
|
|
)
|
|
}
|
|
|
|
func TestReplicateName(t *testing.T) {
|
|
if got := newReplicateForTest("http://unused").Name(); got != "replicate" {
|
|
t.Errorf("Name()=%q", got)
|
|
}
|
|
}
|
|
|
|
func TestReplicateFactory(t *testing.T) {
|
|
driver, err := NewModelFactory().CreateModelDriver("Replicate", map[string]string{"default": "http://unused"}, URLSuffix{})
|
|
if err != nil {
|
|
t.Fatalf("CreateModelDriver: %v", err)
|
|
}
|
|
if _, ok := driver.(*ReplicateModel); !ok {
|
|
t.Fatalf("driver type=%T, want *ReplicateModel", driver)
|
|
}
|
|
}
|
|
|
|
func TestReplicatePromptFromMessages(t *testing.T) {
|
|
prompt, system := replicatePromptFromMessages([]Message{
|
|
{Role: "system", Content: "be terse"},
|
|
{Role: "user", Content: "hello"},
|
|
{Role: "assistant", Content: "hi"},
|
|
{Role: "user", Content: map[string]interface{}{"text": "again"}},
|
|
})
|
|
if system != "be terse" {
|
|
t.Errorf("system=%q", system)
|
|
}
|
|
want := "user: hello\nassistant: hi\nuser: {\"text\":\"again\"}"
|
|
if prompt != want {
|
|
t.Errorf("prompt=%q want %q", prompt, want)
|
|
}
|
|
}
|
|
|
|
func TestReplicatePredictionEndpoint(t *testing.T) {
|
|
m := newReplicateForTest("https://api.example.test")
|
|
|
|
endpoint, version, err := m.predictionEndpoint(&APIConfig{}, "meta/meta-llama-3-70b-instruct")
|
|
if err != nil {
|
|
t.Fatalf("official endpoint: %v", err)
|
|
}
|
|
if endpoint != "https://api.example.test/v1/models/meta/meta-llama-3-70b-instruct/predictions" {
|
|
t.Errorf("official endpoint=%q", endpoint)
|
|
}
|
|
if version != "" {
|
|
t.Errorf("official version=%q want empty", version)
|
|
}
|
|
|
|
endpoint, version, err = m.predictionEndpoint(&APIConfig{}, "replicate/hello-world:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa")
|
|
if err != nil {
|
|
t.Fatalf("version endpoint: %v", err)
|
|
}
|
|
if endpoint != "https://api.example.test/v1/predictions" {
|
|
t.Errorf("version endpoint=%q", endpoint)
|
|
}
|
|
if version != "replicate/hello-world:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa" {
|
|
t.Errorf("version=%q", version)
|
|
}
|
|
}
|
|
|
|
func TestReplicateOfficialChatHappyPath(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models/meta/meta-llama-3-70b-instruct/predictions" {
|
|
t.Errorf("path=%s", r.URL.Path)
|
|
}
|
|
if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
|
|
t.Errorf("Authorization=%q", got)
|
|
}
|
|
if got := r.Header.Get("Prefer"); got != "wait=60" {
|
|
t.Errorf("Prefer=%q", got)
|
|
}
|
|
raw, _ := io.ReadAll(r.Body)
|
|
var body map[string]interface{}
|
|
if err := json.Unmarshal(raw, &body); err != nil {
|
|
t.Errorf("body: %v", err)
|
|
return
|
|
}
|
|
if body["version"] != nil {
|
|
t.Errorf("official model requests must not send version=%v", body["version"])
|
|
}
|
|
if body["stream"] != false {
|
|
t.Errorf("stream=%v", body["stream"])
|
|
}
|
|
input := body["input"].(map[string]interface{})
|
|
if input["prompt"] != "hello" {
|
|
t.Errorf("prompt=%v", input["prompt"])
|
|
}
|
|
if input["system_prompt"] != "be helpful" {
|
|
t.Errorf("system_prompt=%v", input["system_prompt"])
|
|
}
|
|
if input["max_new_tokens"] != float64(128) {
|
|
t.Errorf("max_new_tokens=%v", input["max_new_tokens"])
|
|
}
|
|
// Stop is deliberately filtered out because Replicate model
|
|
// inputs are model-specific and upstream support is undefined.
|
|
if input["stop"] != nil {
|
|
t.Errorf("unexpected stop=%v", input["stop"])
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"status": "successful",
|
|
"output": []string{"hel", "lo"},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
apiKey := "test-key"
|
|
maxTokens := 128
|
|
stop := []string{"END"}
|
|
resp, err := newReplicateForTest(srv.URL).ChatWithMessages(
|
|
"meta/meta-llama-3-70b-instruct",
|
|
[]Message{{Role: "system", Content: "be helpful"}, {Role: "user", Content: "hello"}},
|
|
&APIConfig{ApiKey: &apiKey},
|
|
&ChatConfig{MaxTokens: &maxTokens, Stop: &stop},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("ChatWithMessages: %v", err)
|
|
}
|
|
if *resp.Answer != "hello" {
|
|
t.Errorf("Answer=%q", *resp.Answer)
|
|
}
|
|
if *resp.ReasonContent != "" {
|
|
t.Errorf("ReasonContent=%q", *resp.ReasonContent)
|
|
}
|
|
}
|
|
|
|
func TestReplicateCommunityChatUsesVersionEndpoint(t *testing.T) {
|
|
const version = "replicate/hello-world:5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa"
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/predictions" {
|
|
t.Errorf("path=%s", r.URL.Path)
|
|
}
|
|
raw, _ := io.ReadAll(r.Body)
|
|
var body map[string]interface{}
|
|
if err := json.Unmarshal(raw, &body); err != nil {
|
|
t.Errorf("body: %v", err)
|
|
return
|
|
}
|
|
if body["version"] != version {
|
|
t.Errorf("version=%v", body["version"])
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"status": "successful",
|
|
"output": "ok",
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
apiKey := "test-key"
|
|
resp, err := newReplicateForTest(srv.URL).ChatWithMessages(
|
|
version,
|
|
[]Message{{Role: "user", Content: "hello"}},
|
|
&APIConfig{ApiKey: &apiKey}, nil,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("ChatWithMessages: %v", err)
|
|
}
|
|
if *resp.Answer != "ok" {
|
|
t.Errorf("Answer=%q", *resp.Answer)
|
|
}
|
|
}
|
|
|
|
func TestReplicateChatPollsUntilSucceeded(t *testing.T) {
|
|
var getCount int
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
|
|
t.Errorf("Authorization=%q", got)
|
|
}
|
|
switch r.URL.Path {
|
|
case "/v1/models/meta/meta-llama-3-70b-instruct/predictions":
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"status": "processing",
|
|
"urls": map[string]string{
|
|
"get": "http://" + r.Host + "/v1/predictions/p1",
|
|
},
|
|
})
|
|
case "/v1/predictions/p1":
|
|
getCount++
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"status": "successful",
|
|
"output": "done",
|
|
})
|
|
default:
|
|
t.Errorf("unexpected path=%s", r.URL.Path)
|
|
}
|
|
}))
|
|
defer srv.Close()
|
|
|
|
apiKey := "test-key"
|
|
resp, err := newReplicateForTest(srv.URL).ChatWithMessages(
|
|
"meta/meta-llama-3-70b-instruct",
|
|
[]Message{{Role: "user", Content: "hello"}},
|
|
&APIConfig{ApiKey: &apiKey}, nil)
|
|
if err != nil {
|
|
t.Fatalf("ChatWithMessages: %v", err)
|
|
}
|
|
if getCount != 1 {
|
|
t.Errorf("getCount=%d", getCount)
|
|
}
|
|
if *resp.Answer != "done" {
|
|
t.Errorf("Answer=%q", *resp.Answer)
|
|
}
|
|
}
|
|
|
|
func TestReplicateStreamHappyPath(t *testing.T) {
|
|
var streamURL string
|
|
streamSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if got := r.Header.Get("Accept"); got != "text/event-stream" {
|
|
t.Errorf("Accept=%q", got)
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
_, _ = io.WriteString(w, "event: output\n")
|
|
_, _ = io.WriteString(w, "data: Hello\n\n")
|
|
_, _ = io.WriteString(w, "event: output\n")
|
|
_, _ = io.WriteString(w, "data: world\n\n")
|
|
_, _ = io.WriteString(w, "event: done\n")
|
|
_, _ = io.WriteString(w, "data: {}\n\n")
|
|
}))
|
|
defer streamSrv.Close()
|
|
streamURL = streamSrv.URL
|
|
|
|
apiSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models/meta/meta-llama-3-70b-instruct/predictions" {
|
|
t.Errorf("path=%s", r.URL.Path)
|
|
}
|
|
raw, _ := io.ReadAll(r.Body)
|
|
var body map[string]interface{}
|
|
if err := json.Unmarshal(raw, &body); err != nil {
|
|
t.Errorf("body: %v", err)
|
|
return
|
|
}
|
|
if body["stream"] != true {
|
|
t.Errorf("stream=%v", body["stream"])
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"status": "starting",
|
|
"urls": map[string]string{
|
|
"stream": streamURL,
|
|
},
|
|
})
|
|
}))
|
|
defer apiSrv.Close()
|
|
|
|
apiKey := "test-key"
|
|
var chunks []string
|
|
err := newReplicateForTest(apiSrv.URL).ChatStreamlyWithSender(
|
|
"meta/meta-llama-3-70b-instruct",
|
|
[]Message{{Role: "user", Content: "hello"}},
|
|
&APIConfig{ApiKey: &apiKey}, nil,
|
|
func(c *string, _ *string) error {
|
|
if c != nil {
|
|
chunks = append(chunks, *c)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("ChatStreamlyWithSender: %v", err)
|
|
}
|
|
if strings.Join(chunks, "") != "Hello world[DONE]" {
|
|
t.Errorf("chunks=%q", strings.Join(chunks, ""))
|
|
}
|
|
}
|
|
|
|
func TestReplicateListModelsAndCheckConnection(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/models" {
|
|
t.Errorf("path=%s", r.URL.Path)
|
|
}
|
|
if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
|
|
t.Errorf("Authorization=%q", got)
|
|
}
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
"results": []map[string]string{
|
|
{"owner": "meta", "name": "meta-llama-3-70b-instruct"},
|
|
{"owner": "replicate", "name": "hello-world"},
|
|
},
|
|
})
|
|
}))
|
|
defer srv.Close()
|
|
|
|
apiKey := "test-key"
|
|
model := newReplicateForTest(srv.URL)
|
|
models, err := model.ListModels(&APIConfig{ApiKey: &apiKey})
|
|
if err != nil {
|
|
t.Fatalf("ListModels: %v", err)
|
|
}
|
|
if joinModelNames(models, ",") != "meta/meta-llama-3-70b-instruct,replicate/hello-world" {
|
|
t.Errorf("models=%v", models)
|
|
}
|
|
if err := model.CheckConnection(&APIConfig{ApiKey: &apiKey}); err != nil {
|
|
t.Fatalf("CheckConnection: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestReplicateUnsupportedMethods(t *testing.T) {
|
|
m := newReplicateForTest("http://unused")
|
|
apiKey := "test-key"
|
|
// Rerank IS implemented; with empty documents it short-circuits (no error).
|
|
// Pass non-empty docs + nil modelName to trigger model-name validation.
|
|
if _, err := m.Rerank(nil, "", []string{"d"}, &APIConfig{ApiKey: &apiKey}, nil); err == nil || !strings.Contains(err.Error(), "model name is required") {
|
|
t.Errorf("Rerank error=%v", err)
|
|
}
|
|
// Balance IS a stub → "no such method"
|
|
if _, err := m.Balance(&APIConfig{ApiKey: &apiKey}); err == nil || !strings.Contains(err.Error(), "no such method") {
|
|
t.Errorf("Balance error=%v", err)
|
|
}
|
|
}
|