Files
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

116 lines
4.0 KiB
Go

package dashscoperank
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"academy/internal/models/api"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// documentedResponse is the response shape of DashScope's text-rerank API,
// copied from the reference so the decoder is pinned against the vendor's own
// document: https://help.aliyun.com/zh/model-studio/text-rerank-api
const documentedResponse = `{
"output": {
"results": [
{"document": {"text": "上海气候"}, "index": 1, "relevance_score": 0.7314},
{"document": {"text": "北京美食"}, "index": 0, "relevance_score": 0.0002}
]
},
"usage": {"total_tokens": 24},
"request_id": "4c5b1c4d-0e2f-9b1a-8a1e-1b2c3d4e5f60"
}`
const endpointURL = "https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank"
func newClient(t *testing.T, url string, settings api.RerankSettings) *Client {
t.Helper()
return New(Config{
Endpoint: api.Endpoint{BaseURL: url, Model: "gte-rerank-v2", Auth: api.BearerAuth("k")},
Settings: settings,
})
}
func TestRequestBodyMatchesTheDocumentedSchema(t *testing.T) {
c := newClient(t, endpointURL, api.RerankSettings{SendReturnDocs: true})
body, err := c.BuildRequestBody("上海天气", []string{"北京美食", "上海气候"})
require.NoError(t, err)
assert.Equal(t, map[string]any{
"model": "gte-rerank-v2",
"input": map[string]any{
"query": "上海天气",
"documents": []any{"北京美食", "上海气候"},
},
"parameters": map[string]any{
"return_documents": true,
"top_n": float64(2),
},
}, body)
}
// top_n is mandatory in this shape: zero would ask for nothing back, so the
// full document count is always sent regardless of vendor settings.
func TestTopNAlwaysCarriesTheDocumentCount(t *testing.T) {
c := newClient(t, endpointURL, api.RerankSettings{})
body, err := c.BuildRequestBody("q", []string{"a", "b", "c", "d"})
require.NoError(t, err)
assert.Equal(t, float64(4), body["parameters"].(map[string]any)["top_n"])
}
func TestDecodesTheDocumentedResponse(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(documentedResponse))
}))
defer server.Close()
c := newClient(t, server.URL, api.RerankSettings{})
got, err := c.Rerank(context.Background(), "上海天气", []string{"北京美食", "上海气候"})
require.NoError(t, err)
assert.Equal(t, []api.RerankResult{
{Index: 1, Score: 0.7314, Text: "上海气候"},
{Index: 0, Score: 0.0002, Text: "北京美食"},
}, got)
}
// DashScope answers some failures with a 200 and an error code in the body,
// which would otherwise decode as an empty result set.
func TestSurfacesAnInBodyError(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte(`{"code":"InvalidParameter","message":"documents is too long","request_id":"r"}`))
}))
defer server.Close()
c := newClient(t, server.URL, api.RerankSettings{})
_, err := c.Rerank(context.Background(), "q", []string{"d"})
require.Error(t, err)
assert.Contains(t, err.Error(), "documents is too long")
}
func TestPostsToTheBaseURLItself(t *testing.T) {
t.Setenv("SSRF_WHITELIST", "127.0.0.1")
var gotPath string
var gotBody map[string]any
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gotPath = r.URL.Path
_ = json.NewDecoder(r.Body).Decode(&gotBody)
_, _ = w.Write([]byte(`{"output":{"results":[]}}`))
}))
defer server.Close()
c := newClient(t, server.URL+"/api/v1/services/rerank/text-rerank/text-rerank", api.RerankSettings{})
_, err := c.Rerank(context.Background(), "q", []string{"d"})
require.NoError(t, err)
assert.Equal(t, "/api/v1/services/rerank/text-rerank/text-rerank", gotPath,
"the vendor's base URL already names the full endpoint")
}