RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
test(api): migrate and improve search API tests, make the Go API suite self-sufficient (#21086)
qinling021073c2b240798e
diff --git a/.github/workflows/sep-tests.yml b/.github/workflows/sep-tests.ymlindex d1643f8160..0f0a207c45 100644--- a/.github/workflows/sep-tests.yml+++ b/.github/workflows/sep-tests.yml@@ -49,7 +49,6 @@ jobs:contents: readruns-on: [ "self-hosted", "ragflow-test" ]outputs:- http_api_test_level: ${{ steps.test_level.outputs.http_api_test_level }}has_go_changes: ${{ steps.detect_changes.outputs.has_go_changes }}has_web_changes: ${{ steps.detect_changes.outputs.has_web_changes }}has_native_deepdoc_changes: ${{ steps.detect_changes.outputs.has_native_deepdoc_changes }}@@ -146,18 +145,6 @@ jobs:lefthook run web-checks --files-from-stdin --no-auto-install < "$changed_files"fi- - name: Set test level- id: test_level- run: |- set -euo pipefail- if [[ ${GITHUB_EVENT_NAME} == "schedule" ]]; then- export HTTP_API_TEST_LEVEL=p3- else- export HTTP_API_TEST_LEVEL=p2- fi- echo "HTTP_API_TEST_LEVEL=${HTTP_API_TEST_LEVEL}" >> ${GITHUB_ENV}- echo "http_api_test_level=${HTTP_API_TEST_LEVEL}" >> ${GITHUB_OUTPUT}-- name: Detect changed file typesid: detect_changesrun: |@@ -225,7 +212,6 @@ jobs:env:DOC_ENGINE: infinityRAGFLOW_IMAGE: infiniflow/ragflow:${{ github.run_id }}-infinity- HTTP_API_TEST_LEVEL: ${{ needs.ragflow_preflight.outputs.http_api_test_level }}HAS_GO: ${{ needs.ragflow_preflight.outputs.has_go_changes }}HAS_WEB: ${{ needs.ragflow_preflight.outputs.has_web_changes }}steps:@@ -687,30 +673,12 @@ jobs:sudo docker ps -a --filter "label=com.docker.compose.project=${COMPOSE_PROJECT_NAME}" -q | xargs -r sudo docker rm -fsudo docker compose -f "${COMPOSE_FILE}" -p ${COMPOSE_PROJECT_NAME} up -d- - name: Run search API tests against Infinity- run: |- set -euo pipefail- export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""- svc_ready=0- for i in $(seq 1 60); do- if sudo docker exec ${RAGFLOW_CONTAINER} curl -sf --connect-timeout 5 "${HOST_ADDRESS}/api/v1/system/ping" > /dev/null 2>&1; then- svc_ready=1- break- fi- echo "Waiting for service to be available... ($i/60)"- sleep 5- done- if [ "$svc_ready" -ne 1 ]; then- echo "Service did not become ready after 5 minutes. Docker logs:"- sudo docker logs ${RAGFLOW_CONTAINER}- exit 1- fi- source .venv/bin/activate- pytest -s --tb=short --level=${HTTP_API_TEST_LEVEL} --durations=100 test/testcases/restful_api/test_searches.py 2>&1 | tee infinity_search_api_test.log-- name: Run Go API tests against Infinity- # Search fixtures above configure the default models used by the Go suite.- # Go API tests have no priority filtering; run the full suite every time.+ # test/api/config.go prepares everything this suite needs: it retries the+ # session bootstrap for up to 5 minutes (so no readiness step is needed+ # between `docker compose up -d` and this one) and it registers the+ # providers, instances and tenant default models itself (so no fixture+ # step has to run first). No priority filtering: run it all.run: |set -euo pipefailexport http_proxy="" https_proxy="" no_proxy="" HTTP_PROXY="" HTTPS_PROXY="" NO_PROXY=""@@ -718,7 +686,7 @@ jobs:export GOMODCACHE="${RUNNER_WORKSPACE_PREFIX}/gomodcache"export GOCACHE="${RUNNER_WORKSPACE_PREFIX}/gobuild"fi- ./build.sh --test-integration-go -timeout 60m ./test/api 2>&1 | tee infinity_go_api_test.log+ ./build.sh --test-integration-go -timeout 60m ./test/api 2>&1 | tee "${ARTIFACTS_DIR}/infinity_go_api_test.log"- name: Upload coverage reports to Codecovuses: codecov/codecov-action@v5@@ -790,7 +758,6 @@ jobs:env:DOC_ENGINE: elasticsearchRAGFLOW_IMAGE: infiniflow/ragflow:${{ github.run_id }}-elasticsearch- HTTP_API_TEST_LEVEL: ${{ needs.ragflow_preflight.outputs.http_api_test_level }}HAS_GO: ${{ needs.ragflow_preflight.outputs.has_go_changes }}HAS_WEB: ${{ needs.ragflow_preflight.outputs.has_web_changes }}steps:@@ -1265,30 +1232,12 @@ jobs:sudo docker ps -a --filter "label=com.docker.compose.project=${COMPOSE_PROJECT_NAME}" -q | xargs -r sudo docker rm -fsudo docker compose -f "${COMPOSE_FILE}" -p ${COMPOSE_PROJECT_NAME} up -d- - name: Run search API tests against Elasticsearch- run: |- set -euo pipefail- export http_proxy=""; export https_proxy=""; export no_proxy=""; export HTTP_PROXY=""; export HTTPS_PROXY=""; export NO_PROXY=""- svc_ready=0- for i in $(seq 1 60); do- if sudo docker exec ${RAGFLOW_CONTAINER} curl -sf --connect-timeout 5 "${HOST_ADDRESS}/api/v1/system/ping" > /dev/null 2>&1; then- svc_ready=1- break- fi- echo "Waiting for service to be available... ($i/60)"- sleep 5- done- if [ "$svc_ready" -ne 1 ]; then- echo "Service did not become ready after 5 minutes. Docker logs:"- sudo docker logs ${RAGFLOW_CONTAINER}- exit 1- fi- source .venv/bin/activate- pytest -s --tb=short --level=${HTTP_API_TEST_LEVEL} --durations=100 test/testcases/restful_api/test_searches.py 2>&1 | tee es_search_api_test.log-- name: Run Go API tests against Elasticsearch- # Search fixtures above configure the default models used by the Go suite.- # Go API tests have no priority filtering; run the full suite every time.+ # test/api/config.go prepares everything this suite needs: it retries the+ # session bootstrap for up to 5 minutes (so no readiness step is needed+ # between `docker compose up -d` and this one) and it registers the+ # providers, instances and tenant default models itself (so no fixture+ # step has to run first). No priority filtering: run it all.run: |set -euo pipefailexport http_proxy="" https_proxy="" no_proxy="" HTTP_PROXY="" HTTPS_PROXY="" NO_PROXY=""@@ -1296,7 +1245,7 @@ jobs:export GOMODCACHE="${RUNNER_WORKSPACE_PREFIX}/gomodcache"export GOCACHE="${RUNNER_WORKSPACE_PREFIX}/gobuild"fi- ./build.sh --test-integration-go -timeout 60m ./test/api 2>&1 | tee es_go_api_test.log+ ./build.sh --test-integration-go -timeout 60m ./test/api 2>&1 | tee "${ARTIFACTS_DIR}/es_go_api_test.log"- name: Collect ragflow log Elasticsearchif: ${{ !cancelled() }}diff --git a/test/api/config.go b/test/api/config.goindex b4c72c9cb6..ec5a730eeb 100644--- a/test/api/config.go+++ b/test/api/config.go@@ -22,6 +22,7 @@ import ("bytes""context""encoding/json"+ "errors""fmt""io""net/http"@@ -223,7 +224,67 @@ func (c *ServerTestConfig) decodeJSONBody(body io.Reader) (map[string]interface{return payload, nil}+// A freshly started stack accepts TCP connections long before it can serve+// requests (admin-service bootstrap, database migrations, model init), so the+// session bootstrap retries "not ready yet" answers instead of failing the+// suite on the first attempt. This is what lets the workflow start the Go API+// tests right after `docker compose up -d` instead of running a separate+// readiness gate. Variables rather than constants so tests can shrink them.+var (+ bootstrapWait = 300 * time.Second+ bootstrapPoll = 2 * time.Second+)++// errServerStarting marks a reply that only means "not ready yet".+var errServerStarting = errors.New("server is not ready yet")++// bootstrapRetryable reports whether an attempt failed because the service is+// still starting rather than because the request itself was rejected.+func bootstrapRetryable(err error) bool {+ if err == nil {+ return false+ }+ if errors.Is(err, errServerStarting) || errors.Is(err, io.EOF) {+ return true+ }+ message := strings.ToLower(err.Error())+ for _, fragment := range []string{+ "admin server not connected",+ "connection refused",+ "connection reset",+ "no such host",+ "server closed idle connection",+ } {+ if strings.Contains(message, fragment) {+ return true+ }+ }+ return false+}++// Login bootstraps the test session: it registers this test user (a repeat+// registration is fine), signs in, and swaps the session token for the API key+// the REST calls authenticate with. Attempts that fail because the service is+// still starting are retried until the deadline.func (c *ServerTestConfig) Login() error {+ deadline := time.Now().Add(bootstrapWait)+ for {+ err := c.loginOnce()+ if err == nil {+ return nil+ }+ if !bootstrapRetryable(err) {+ return err+ }+ if time.Now().After(deadline) {+ return fmt.Errorf("service did not become ready within %s: %w", bootstrapWait, err)+ }+ fmt.Fprintf(os.Stderr, "Auth bootstrap: %v; retrying in %s\n", err, bootstrapPoll)+ time.Sleep(bootstrapPoll)+ }+}++func (c *ServerTestConfig) loginOnce() error {// Register (ignore already registered error)registerResp, err := c.PostJSON("/users", map[string]interface{}{"email": c.Email,@@ -233,8 +294,11 @@ func (c *ServerTestConfig) Login() error {if err != nil {return fmt.Errorf("register request failed: %w", err)}+ defer registerResp.Body.Close()+ if registerResp.StatusCode >= http.StatusInternalServerError {+ return fmt.Errorf("%w: register status=%d", errServerStarting, registerResp.StatusCode)+ }registerPayload, err := c.decodeJSONBody(registerResp.Body)- registerResp.Body.Close()if err != nil {return fmt.Errorf("failed to decode register response: %w", err)}@@ -255,10 +319,17 @@ func (c *ServerTestConfig) Login() error {if err != nil {return fmt.Errorf("login request failed: %w", err)}+ defer loginResp.Body.Close()+ if loginResp.StatusCode >= http.StatusInternalServerError {+ return fmt.Errorf("%w: login status=%d", errServerStarting, loginResp.StatusCode)+ }authToken := loginResp.Header.Get("Authorization")- loginResp.Body.Close()+ loginPayload, decodeErr := c.decodeJSONBody(loginResp.Body)if authToken == "" {- return fmt.Errorf("login response missing Authorization header")+ if decodeErr == nil {+ return fmt.Errorf("login failed: code=%v, message=%v", loginPayload["code"], loginPayload["message"])+ }+ return fmt.Errorf("login failed: status=%d without an Authorization header", loginResp.StatusCode)}c.AuthToken = authToken@@ -270,8 +341,11 @@ func (c *ServerTestConfig) Login() error {if err != nil {return fmt.Errorf("system token request failed: %w", err)}+ defer tokenResp.Body.Close()+ if tokenResp.StatusCode >= http.StatusInternalServerError {+ return fmt.Errorf("%w: system token status=%d", errServerStarting, tokenResp.StatusCode)+ }tokenPayload, err := c.decodeJSONBody(tokenResp.Body)- tokenResp.Body.Close()if err != nil {return fmt.Errorf("failed to decode token response: %w", err)}@@ -319,3 +393,239 @@ func (c *ServerTestConfig) Logout() error {c.AuthToken = ""return nil}++// ---- Session preparation: providers, instances, tenant default models ----+// The REST suite addresses models as "<model>@<instance>@<provider>" (see+// memoryLLMModel / memoryEmbeddingModel), so it needs the same providers,+// instances, and tenant defaults that the deleted pytest tree used to set up.+// A fresh deployment ships the Builtin embedding model only, and GitHub CI has+// no ZHIPU/SILICONFLOW credentials, so the suite registers the rest itself.+const (+ defaultModelInstance = "CI"+ defaultChatProvider = "ZHIPU-AI"+ defaultChatModel = "glm-4-flash"+ defaultEmbedProvider = "Builtin"+ defaultEmbedInstance = "Local"+ defaultEmbedModel = "BAAI/bge-small-en-v1.5"+ defaultRerankProvider = "SILICONFLOW"+ defaultRerankModel = "BAAI/bge-reranker-v2-m3"+)++// defaultModelSpec describes one provider the suite has to have available.+// bindOnly limits which catalog entries the provider instance binds: without+// it the server binds (and validates) the provider's entire catalog, including+// models the suite never uses.+type defaultModelSpec struct {+ provider string+ apiKey func(*ServerTestConfig) string+ bindOnly []apiObject+}++func defaultModelSpecs() []defaultModelSpec {+ return []defaultModelSpec{+ {+ provider: defaultChatProvider,+ apiKey: func(c *ServerTestConfig) string { return c.ZhipuAPIKey },+ },+ {+ provider: defaultRerankProvider,+ apiKey: func(c *ServerTestConfig) string { return c.SiliconFlowAPIKey },+ bindOnly: []apiObject{+ {"model_type": []string{"rerank"}, "model_name": defaultRerankModel, "max_tokens": 8192},+ {"model_type": []string{"embedding"}, "model_name": "BAAI/bge-m3", "max_tokens": 8192},+ {"model_type": []string{"embedding"}, "model_name": "BAAI/bge-large-en-v1.5", "max_tokens": 512},+ {"model_type": []string{"embedding"}, "model_name": "BAAI/bge-large-zh-v1.5", "max_tokens": 512},+ },+ },+ }+}++// providerCredential reports the credential registered for a provider, empty+// when the environment has none and the fixture therefore leaves the provider+// and its defaults alone.+func providerCredential(provider string) string {+ for _, spec := range defaultModelSpecs() {+ if spec.provider == provider {+ return spec.apiKey(TestConfig)+ }+ }+ return ""+}++// defaultModelSelections are the tenant defaults the suite relies on: chat for+// the session/agent completion paths, embedding for datasets, rerank for the+// retrieval paths.+func defaultModelSelections() []apiObject {+ return []apiObject{+ {"model_provider": defaultChatProvider, "model_instance": defaultModelInstance, "model_type": "chat", "model_name": defaultChatModel},+ {"model_provider": defaultEmbedProvider, "model_instance": defaultEmbedInstance, "model_type": "embedding", "model_name": defaultEmbedModel},+ {"model_provider": defaultRerankProvider, "model_instance": defaultModelInstance, "model_type": "rerank", "model_name": defaultRerankModel},+ }+}++// apiCallError is a non-2xx application envelope returned by the server.+type apiCallError struct {+ method string+ path string+ code float64+ message string+}++func (e *apiCallError) Error() string {+ return fmt.Sprintf("%s %s: code=%v message=%s", e.method, e.path, e.code, e.message)+}++// alreadyExists reports the "the thing you asked for is already there" family+// of errors, which are success for an idempotent setup step.+func alreadyExists(err error) bool {+ call, ok := asAPICallError(err)+ if !ok {+ return false+ }+ message := strings.ToLower(call.message)+ return strings.Contains(message, "duplicat") || strings.Contains(message, "already exist")+}++func asAPICallError(err error) (*apiCallError, bool) {+ for err != nil {+ if call, ok := err.(*apiCallError); ok {+ return call, true+ }+ unwrapper, ok := err.(interface{ Unwrap() error })+ if !ok {+ return nil, false+ }+ err = unwrapper.Unwrap()+ }+ return nil, false+}++// callAPI performs a request as the configured tenant and returns the decoded+// envelope, failing on any non-zero application code.+func callAPI(method, path string, body apiObject) (apiObject, error) {+ response, err := TestConfig.RequestJSON(method, path, body, nil, nil)+ if err != nil {+ return nil, err+ }+ defer response.Body.Close()+ raw, err := io.ReadAll(io.LimitReader(response.Body, 1<<20))+ if err != nil {+ return nil, err+ }+ var payload apiObject+ if err := json.Unmarshal(raw, &payload); err != nil {+ return nil, fmt.Errorf("%s %s: non-JSON response (status %d) %s", method, path, response.StatusCode, diagnosticBody(raw, TestConfig.APIKey, TestConfig.Password))+ }+ code, _ := payload["code"].(float64)+ if code != 0 {+ message, _ := payload["message"].(string)+ return payload, &apiCallError{method: method, path: path, code: code, message: message}+ }+ return payload, nil+}++// tenantProviders lists the provider and "<provider>/<instance>" pairs already+// added to the tenant, so the setup below only creates what is missing.+func tenantProviders() (map[string]bool, error) {+ payload, err := callAPI(http.MethodGet, "/models", nil)+ if err != nil {+ return nil, err+ }+ present := map[string]bool{}+ items, _ := payload["data"].([]any)+ for _, item := range items {+ model, ok := item.(map[string]any)+ if !ok {+ continue+ }+ provider, _ := model["provider_name"].(string)+ instance, _ := model["instance_name"].(string)+ if provider == "" {+ continue+ }+ present[strings.ToLower(provider)] = true+ if instance != "" {+ present[strings.ToLower(provider+"/"+instance)] = true+ }+ }+ return present, nil+}++// prepareDefaultModels mirrors the pytest session fixture+// (test/testcases/conftest.py::set_tenant_info) that used to run before this+// suite. It is best-effort by design: providers are registered without usable+// credentials in CI, so a failure here must not abort the run. Tests that+// genuinely need a model report their own error, which is how the Python+// fixture behaved too (it downgraded the same failures to warnings).+func prepareDefaultModels(logf func(format string, args ...any)) {+ present, err := tenantProviders()+ if err != nil {+ logf("default models: cannot list the tenant's models: %v", err)+ return+ }++ // Builtin (the TEI embedding model) needs no credential of its own.+ usable := map[string]bool{defaultEmbedProvider: true}+ created := make([]string, 0, 2)+ failures := 0++ for _, spec := range defaultModelSpecs() {+ // Without a credential for the provider the model can never serve a+ // request, so registering it and pointing the tenant's default at it+ // would only turn the suite's explicit "no default model" skips into+ // authentication failures. Leave that provider alone instead.+ if spec.apiKey(TestConfig) == "" {+ logf("default models: no %s credential in the environment; leaving %s defaults untouched", spec.provider, spec.provider)+ continue+ }+ usable[spec.provider] = true++ if !present[strings.ToLower(spec.provider)] {+ if _, err := callAPI(http.MethodPut, "/providers", apiObject{"provider_name": spec.provider}); err != nil && !alreadyExists(err) {+ logf("default models: register provider %s: %v", spec.provider, err)+ failures+++ continue+ }+ created = append(created, spec.provider)+ }+ if present[strings.ToLower(spec.provider+"/"+defaultModelInstance)] {+ continue+ }+ instance := apiObject{+ "instance_name": defaultModelInstance,+ "api_key": spec.apiKey(TestConfig),+ "region": "default",+ "base_url": "",+ }+ if len(spec.bindOnly) > 0 {+ instance["model_info"] = spec.bindOnly+ }+ if _, err := callAPI(http.MethodPost, "/providers/"+spec.provider+"/instances", instance); err != nil && !alreadyExists(err) {+ logf("default models: create instance %s/%s: %v", spec.provider, defaultModelInstance, err)+ failures+++ continue+ }+ created = append(created, spec.provider+"/"+defaultModelInstance)+ }++ selected, skipped := 0, 0+ for _, selection := range defaultModelSelections() {+ provider, _ := selection["model_provider"].(string)+ if !usable[provider] {+ skipped+++ continue+ }+ if _, err := callAPI(http.MethodPatch, "/models/default", selection); err != nil {+ logf("default models: set default %v model (%s@%s@%s): %v",+ selection["model_type"], selection["model_name"], selection["model_instance"], selection["model_provider"], err)+ failures+++ continue+ }+ selected+++ }++ // One summary line so a CI log proves the fixture ran and shows whether+ // the defaults are actually in place.+ logf("default models: %d/%d defaults selected (%d skipped for lack of credentials), %d failure(s), created %v",+ selected, len(defaultModelSelections()), skipped, failures, created)+}diff --git a/test/api/config_test.go b/test/api/config_test.gonew file mode 100644index 0000000000..000f447c8c--- /dev/null+++ b/test/api/config_test.go@@ -0,0 +1,166 @@+//go:build integration++//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package api++import (+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "sync/atomic"+ "testing"+ "time"+)++// TestServerTestConfigLoginRetriesWhileServerStarts covers the bootstrap retry+// that lets the Go API suite start right after `docker compose up -d` instead+// of waiting on a separate readiness gate. Without it, the very first+// connection is refused (or answered with a proxy 5xx) and TestMain would take+// the whole suite down before the server finished booting.+func TestServerTestConfigLoginRetriesWhileServerStarts(t *testing.T) {+ const notReadyReplies = 3+ var requests atomic.Int64+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ if requests.Add(1) <= notReadyReplies {+ // What a booting deployment answers: TCP accepted, work not done.+ w.WriteHeader(http.StatusBadGateway)+ return+ }+ w.Header().Set("Content-Type", "application/json")+ switch r.URL.Path {+ case "/api/v1/users":+ _, _ = io.WriteString(w, `{"code":0,"data":null}`)+ case "/api/v1/auth/login":+ w.Header().Set("Authorization", "Bearer session-token")+ _, _ = io.WriteString(w, `{"code":0,"data":null}`)+ case "/api/v1/system/tokens":+ _, _ = io.WriteString(w, `{"code":0,"data":{"token":"api-key"}}`)+ default:+ w.WriteHeader(http.StatusNotFound)+ }+ }))+ t.Cleanup(server.Close)++ previousWait, previousPoll := bootstrapWait, bootstrapPoll+ bootstrapWait, bootstrapPoll = 5*time.Second, 10*time.Millisecond+ t.Cleanup(func() { bootstrapWait, bootstrapPoll = previousWait, previousPoll })++ config := &ServerTestConfig{HostAddress: server.URL, APIVersion: "v1", Email: "[email protected]", Password: "password"}+ if err := config.Login(); err != nil {+ t.Fatalf("Login did not retry through the boot phase: %v", err)+ }+ if config.APIKey != "api-key" {+ t.Fatalf("APIKey = %q, want the token from /system/tokens", config.APIKey)+ }+ if got := requests.Load(); got <= notReadyReplies {+ t.Fatalf("requests = %d, want more than the %d not-ready replies", got, notReadyReplies)+ }+}++// TestServerTestConfigLoginFailsFastOnRejection makes sure the retry does not+// turn a real rejection (wrong credentials) into a five-minute wait.+func TestServerTestConfigLoginFailsFastOnRejection(t *testing.T) {+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ switch r.URL.Path {+ case "/api/v1/users":+ _, _ = io.WriteString(w, `{"code":0,"data":null}`)+ case "/api/v1/auth/login":+ _, _ = io.WriteString(w, `{"code":100,"message":"Email or password error"}`)+ default:+ w.WriteHeader(http.StatusNotFound)+ }+ }))+ t.Cleanup(server.Close)++ previousWait, previousPoll := bootstrapWait, bootstrapPoll+ bootstrapWait, bootstrapPoll = 5*time.Second, 10*time.Millisecond+ t.Cleanup(func() { bootstrapWait, bootstrapPoll = previousWait, previousPoll })++ config := &ServerTestConfig{HostAddress: server.URL, APIVersion: "v1", Email: "[email protected]", Password: "wrong"}+ started := time.Now()+ err := config.Login()+ if err == nil {+ t.Fatal("Login accepted a rejected sign-in")+ }+ if elapsed := time.Since(started); elapsed > time.Second {+ t.Fatalf("Login waited %s on a rejected sign-in, want a fast failure", elapsed)+ }+ if !strings.Contains(err.Error(), "Email or password error") {+ t.Fatalf("error %q does not surface the server message", err)+ }+}++// TestRESTDefaultModelsPrepared verifies that the suite's own fixture+// (prepareDefaultModels, invoked from TestMain) took effect. Without this check+// a silently failing preparation would only surface as unrelated failures in+// the model-backed tests. Entries whose provider has no credential in the+// environment are reported and skipped, because the fixture deliberately+// leaves those providers untouched (that is what keeps the model-backed tests+// on their "no default model" skip path instead of having them fail on an+// unreachable model).+//+// The embedding default is not asserted: it resolves through the TEI Builtin+// bypass, which the test runner cannot observe (COMPOSE_PROFILES/TEI_MODEL live+// in the server container's environment). The dataset and memory suites cover+// it wherever it is available.+func TestRESTDefaultModelsPrepared(t *testing.T) {+ data := object(t, restOK(t, restCall(t, TestConfig, http.MethodGet, "/models/default", nil, nil))["data"])+ models := array(t, data["models"])++ configured := map[string]apiObject{}+ for _, item := range models {+ model := object(t, item)+ if modelType, _ := model["model_type"].(string); modelType != "" {+ configured[modelType] = model+ }+ }++ credentials := map[string]string{}+ for _, provider := range []string{defaultChatProvider, defaultRerankProvider} {+ credentials[provider] = providerCredential(provider)+ }++ checked := 0+ for _, selection := range defaultModelSelections() {+ modelType, _ := selection["model_type"].(string)+ provider, _ := selection["model_provider"].(string)+ expected, ok := credentials[provider]+ if !ok {+ continue // Builtin embedding: covered by the dataset/memory suites.+ }+ if expected == "" {+ t.Logf("no %s credential in the environment; the default %s model is not asserted", provider, modelType)+ continue+ }+ model, exists := configured[modelType]+ if !exists {+ t.Fatalf("no default %s model configured although %s has a credential: %v", modelType, provider, models)+ }+ for _, key := range []string{"model_provider", "model_instance", "model_name"} {+ if model[key] != selection[key] {+ t.Fatalf("default %s %s = %v, want %v (configured defaults: %v)", modelType, key, model[key], selection[key], models)+ }+ }+ checked+++ }+ if checked == 0 {+ t.Skip("no model credentials configured; the suite's model-backed tests skip for the same reason")+ }+}diff --git a/test/api/main_test.go b/test/api/main_test.goindex fd445eac9c..d32c72ef0d 100644--- a/test/api/main_test.go+++ b/test/api/main_test.go@@ -25,22 +25,33 @@ import ()func TestMain(m *testing.M) {- if TestConfig.APIKey != "" {- os.Exit(m.Run())+ logf := func(format string, args ...any) { fmt.Fprintf(os.Stderr, format+"\n", args...) }++ // An externally supplied API key means the caller owns the tenant; only a+ // self-registered session is logged out again below.+ loggedIn := false+ if TestConfig.APIKey == "" {+ fmt.Fprintln(os.Stderr, "Login")+ if err := TestConfig.Login(); err != nil {+ fmt.Fprintf(os.Stderr, "Failed to login: %v\n", err)+ os.Exit(1)+ }+ loggedIn = true}- fmt.Fprintln(os.Stderr, "Login")- err := TestConfig.Login()- if err != nil {- fmt.Fprintf(os.Stderr, "Failed to login: %v\n", err)- os.Exit(1)- }+ // Register the providers, instances, and tenant defaults the suite needs.+ // This used to be the job of the pytest session fixture, which is why the+ // Go suite had to run after the Python tests in CI.+ prepareDefaultModels(logf)// Run the test casescode := m.Run()- err = TestConfig.Logout()- if err != nil {+ if !loggedIn {+ os.Exit(code)+ }++ if err := TestConfig.Logout(); err != nil {fmt.Fprintf(os.Stderr, "Failed to logout: %v\n", err)os.Exit(1)}diff --git a/test/api/rest_request_test.go b/test/api/rest_request_test.goindex 15242b852b..22dd1494b8 100644--- a/test/api/rest_request_test.go+++ b/test/api/rest_request_test.go@@ -227,6 +227,8 @@ func cleanupResource(t *testing.T, c *ServerTestConfig, family, id string) {restCode(t, restCall(t, c, http.MethodGet, "/agents/"+id, nil, nil), 200, 103)case "chats":restCode(t, restCall(t, c, http.MethodGet, "/chats/"+id, nil, nil), 200, 404)+ case "searches":+ restCode(t, restCall(t, c, http.MethodGet, "/searches/"+id, nil, nil), 200, 404)case "datasets":result := restCall(t, c, http.MethodGet, "/datasets", nil, apiObject{"id": id})if result.Status == http.StatusOK && result.JSON["code"] == float64(404) {diff --git a/test/api/search_api_test.go b/test/api/search_api_test.gonew file mode 100644index 0000000000..61c21166ae--- /dev/null+++ b/test/api/search_api_test.go@@ -0,0 +1,494 @@+//go:build integration++//+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.+//+// Licensed under the Apache License, Version 2.0 (the "License");+// you may not use this file except in compliance with the License.+// You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing, software+// distributed under the License is distributed on an "AS IS" BASIS,+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+// See the License for the specific language governing permissions and+// limitations under the License.+//++package api++import (+ "bytes"+ "encoding/json/v2"+ "net/http"+ "os"+ "strings"+ "testing"+)++// Search App REST contract tests.+//+// Every request goes through restCall, which logs the (redacted) method, URL,+// payload, HTTP status, application code, response body and duration; SSE+// responses are logged as an event count plus a preview of the first frames.+// Assertions therefore only cover what the log cannot prove: persistence,+// isolation, and the shape of the payload.++func createRESTSearch(t *testing.T, name string) string {+ t.Helper()+ response := restCall(t, TestConfig, http.MethodPost, "/searches", apiObject{"name": name, "description": "restful search"}, nil)+ if data, ok := response.JSON["data"].(map[string]any); ok {+ if id, ok := data["search_id"].(string); ok && id != "" {+ cleanupResource(t, TestConfig, "searches", id)+ }+ }+ envelope := restOK(t, response)+ id, ok := object(t, envelope["data"])["search_id"].(string)+ if !ok || id == "" {+ t.Fatalf("created search has no ID: %v", envelope)+ }+ return id+}++func getRESTSearch(t *testing.T, id string) apiObject {+ t.Helper()+ return object(t, restOK(t, restCall(t, TestConfig, http.MethodGet, "/searches/"+id, nil, nil))["data"])+}++func listRESTSearches(t *testing.T, query apiObject) ([]any, float64) {+ t.Helper()+ data := object(t, restOK(t, restCall(t, TestConfig, http.MethodGet, "/searches", nil, query))["data"])+ total, ok := data["total"].(float64)+ if !ok {+ t.Fatalf("list response has no numeric total: %v", data)+ }+ return array(t, data["search_apps"]), total+}++func searchAppIDs(t *testing.T, apps []any) []string {+ t.Helper()+ ids := make([]string, 0, len(apps))+ for _, app := range apps {+ id, ok := object(t, app)["id"].(string)+ if !ok || id == "" {+ t.Fatalf("list row has no id: %v", app)+ }+ ids = append(ids, id)+ }+ return ids+}++func updateRESTSearch(t *testing.T, id, name string, config apiObject) apiObject {+ t.Helper()+ body := apiObject{"name": name}+ if config != nil {+ body["search_config"] = config+ }+ return object(t, restOK(t, restCall(t, TestConfig, http.MethodPut, "/searches/"+id, body, nil))["data"])+}++func createRESTSearchDataset(t *testing.T) string {+ t.Helper()+ response := restCall(t, TestConfig, http.MethodPost, "/datasets", apiObject{+ "name": uniqueName("search_dataset"), "parse_type": 1, "parser_id": "general",+ }, nil)+ if data, ok := response.JSON["data"].(map[string]any); ok {+ if id, ok := data["id"].(string); ok && id != "" {+ cleanupResource(t, TestConfig, "datasets", id)+ }+ }+ id, ok := object(t, restOK(t, response)["data"])["id"].(string)+ if !ok || id == "" {+ t.Fatal("dataset has no id")+ }+ return id+}++func TestRESTSearchLifecycle(t *testing.T) {+ name := uniqueName("search_lifecycle")+ id := createRESTSearch(t, name)++ detail := getRESTSearch(t, id)+ for _, key := range []string{"id", "tenant_id", "name", "description", "created_by", "create_time", "update_time", "search_config"} {+ if _, ok := detail[key]; !ok {+ t.Fatalf("detail omitted %s: %v", key, detail)+ }+ }+ if detail["name"] != name || detail["description"] != "restful search" {+ t.Fatalf("detail did not persist the creation payload: %v", detail)+ }+ if _, ok := detail["search_config"].(map[string]any); !ok {+ t.Fatalf("search_config must be an object: %v", detail["search_config"])+ }++ apps, _ := listRESTSearches(t, nil)+ listed := false+ for _, app := range apps {+ row := object(t, app)+ if row["id"] != id {+ continue+ }+ listed = true+ for _, key := range []string{"name", "status", "nickname", "search_config"} {+ if _, ok := row[key]; !ok {+ t.Fatalf("list row omitted %s: %v", key, row)+ }+ }+ if row["name"] != name {+ t.Fatalf("list row name = %v, want %s", row["name"], name)+ }+ }+ if !listed {+ t.Fatalf("list omitted the created search %s: %v", id, apps)+ }++ // Identity fields are immutable: a PUT echoing them back must not take.+ updated := updateRESTSearch(t, id, name+"_renamed", apiObject{"top_k": 3})+ if updated["name"] != name+"_renamed" {+ t.Fatalf("update response name = %v, want %s_renamed", updated["name"], name)+ }+ after := getRESTSearch(t, id)+ if after["name"] != name+"_renamed" {+ t.Fatalf("update did not persist: %v", after)+ }+ if after["id"] != id || after["tenant_id"] != detail["tenant_id"] || after["created_by"] != detail["created_by"] {+ t.Fatalf("identity fields changed: before=%v after=%v", detail, after)+ }+ if config := object(t, after["search_config"]); config["top_k"] != float64(3) {+ t.Fatalf("search_config did not persist top_k: %v", config)+ }++ deleted := restOK(t, restCall(t, TestConfig, http.MethodDelete, "/searches/"+id, nil, nil))+ if deleted["data"] != true {+ t.Fatalf("delete returned %v, want true", deleted["data"])+ }+ restCode(t, restCall(t, TestConfig, http.MethodGet, "/searches/"+id, nil, nil), http.StatusOK, 404)+ // Deletion is a hard delete, so a repeat must be invisible rather than a no-op success.+ restCode(t, restCall(t, TestConfig, http.MethodDelete, "/searches/"+id, nil, nil), http.StatusOK, 404)+}++func TestRESTSearchNameRules(t *testing.T) {+ t.Run("create_rejects_empty_and_oversized", func(t *testing.T) {+ for _, tc := range []struct{ name, value, want string }{+ {"empty", "", "empty"},+ {"whitespace", " \t ", "empty"},+ {"oversized", strings.Repeat("s", 257), "large than"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ result := restCall(t, TestConfig, http.MethodPost, "/searches", apiObject{"name": tc.value}, nil)+ restCode(t, result, http.StatusOK, 102)+ if message, _ := result.JSON["message"].(string); !strings.Contains(message, tc.want) {+ t.Fatalf("message = %q, want %q", message, tc.want)+ }+ })+ }+ })++ name := uniqueName("search_dup")+ first := createRESTSearch(t, name)+ second := createRESTSearch(t, name)+ if got := getRESTSearch(t, first)["name"]; got != name {+ t.Fatalf("first search name = %v, want %s", got, name)+ }+ if got := getRESTSearch(t, second)["name"]; got != name+"(1)" {+ t.Fatalf("duplicate creation name = %v, want %s(1)", got, name)+ }++ t.Run("update_rejects_duplicate_name_atomically", func(t *testing.T) {+ result := restCall(t, TestConfig, http.MethodPut, "/searches/"+second,+ apiObject{"name": name, "search_config": apiObject{}}, nil)+ restCode(t, result, http.StatusOK, 102)+ if message, _ := result.JSON["message"].(string); !strings.Contains(message, "Duplicated search name") {+ t.Fatalf("message = %q, want a duplicate-name rejection", message)+ }+ if got := getRESTSearch(t, second)["name"]; got != name+"(1)" {+ t.Fatalf("rejected update mutated the search: %v", got)+ }+ })++ t.Run("case_only_rename_allowed", func(t *testing.T) {+ other := createRESTSearch(t, uniqueName("search_case"))+ restOK(t, restCall(t, TestConfig, http.MethodPut, "/searches/"+other,+ apiObject{"name": strings.ToUpper(uniqueName("search_case")), "search_config": apiObject{}}, nil))+ })+}++func TestRESTSearchListFiltering(t *testing.T) {+ prefix := uniqueName("search_filter")+ alpha := createRESTSearch(t, prefix+"_alpha")+ beta := createRESTSearch(t, prefix+"_beta")+ tenantID, _ := getRESTSearch(t, alpha)["tenant_id"].(string)+ if tenantID == "" {+ t.Fatal("detail has no tenant_id")+ }++ apps, total := listRESTSearches(t, apiObject{"keywords": prefix})+ if ids := searchAppIDs(t, apps); len(ids) != 2 || total != 2 {+ t.Fatalf("keyword filter = %v total=%v, want both apps", ids, total)+ }+ if apps, total = listRESTSearches(t, apiObject{"keywords": strings.ToUpper(prefix)}); len(apps) != 2 || total != 2 {+ t.Fatalf("keyword filter must be case-insensitive: %v total=%v", apps, total)+ }+ if apps, _ = listRESTSearches(t, apiObject{"keywords": uniqueName("missing_search")}); len(apps) != 0 {+ t.Fatalf("absent keyword matched %v", apps)+ }++ for _, tc := range []struct {+ page int+ desc bool+ want string+ total float64+ }{+ {1, false, alpha, 2},+ {2, false, beta, 2},+ {1, true, beta, 2},+ } {+ t.Run("pagination", func(t *testing.T) {+ apps, total := listRESTSearches(t, apiObject{+ "keywords": prefix, "orderby": "name", "desc": tc.desc, "page": tc.page, "page_size": 1,+ })+ if len(apps) != 1 || total != tc.total {+ t.Fatalf("page=%d desc=%v rows=%d total=%v, want 1 row of %v", tc.page, tc.desc, len(apps), total, tc.total)+ }+ if got := object(t, apps[0])["id"]; got != tc.want {+ t.Fatalf("page=%d desc=%v first row = %v, want %s", tc.page, tc.desc, got, tc.want)+ }+ })+ }++ if apps, _ = listRESTSearches(t, apiObject{"keywords": prefix, "owner_ids": tenantID}); len(apps) != 2 {+ t.Fatalf("owner_ids filter dropped the owner's own apps: %v", apps)+ }+ if apps, _ = listRESTSearches(t, apiObject{"keywords": prefix, "owner_ids": "00000000000000000000000000000000"}); len(apps) != 0 {+ t.Fatalf("owner_ids filter leaked apps to a foreign owner: %v", apps)+ }+}++func TestRESTSearchConfigContract(t *testing.T) {+ name := uniqueName("search_config")+ id := createRESTSearch(t, name)++ // A partial search_config merges into the stored one instead of replacing it.+ updateRESTSearch(t, id, name, apiObject{"top_k": 3})+ updateRESTSearch(t, id, name, apiObject{"similarity_threshold": 0.2})+ config := object(t, getRESTSearch(t, id)["search_config"])+ if config["top_k"] != float64(3) || config["similarity_threshold"] != float64(0.2) {+ t.Fatalf("search_config must merge across updates: %v", config)+ }++ // The public payload carries the keyword weight only; the internal vector+ // weight is persisted as its complement and hidden from responses.+ updateRESTSearch(t, id, name, apiObject{"keywords_similarity_weight": 0.3})+ config = object(t, getRESTSearch(t, id)["search_config"])+ if config["keywords_similarity_weight"] != float64(0.3) {+ t.Fatalf("keywords_similarity_weight = %v, want 0.3", config["keywords_similarity_weight"])+ }+ if _, leaked := config["vector_similarity_weight"]; leaked {+ t.Fatalf("response must not expose vector_similarity_weight: %v", config)+ }++ rejected := restCall(t, TestConfig, http.MethodPut, "/searches/"+id,+ apiObject{"name": name, "search_config": apiObject{"keywords_similarity_weight": 1.5}}, nil)+ restCode(t, rejected, http.StatusOK, 102)+ if message, _ := rejected.JSON["message"].(string); !strings.Contains(message, "must be between 0 and 1") {+ t.Fatalf("message = %q, want a range rejection", message)+ }+ config = object(t, getRESTSearch(t, id)["search_config"])+ if config["keywords_similarity_weight"] != float64(0.3) {+ t.Fatalf("rejected update persisted: %v", config)+ }+}++func TestRESTSearchCompletionRequestContract(t *testing.T) {+ name := uniqueName("completion_request")+ id := createRESTSearch(t, name)+ path := "/searches/" + id + "/completions"++ t.Run("question_required", func(t *testing.T) {+ for _, body := range []apiObject{{}, {"question": " "}, {"messages": []any{}}} {+ result := restCall(t, TestConfig, http.MethodPost, path, body, nil)+ restCode(t, result, http.StatusOK, 101)+ if message, _ := result.JSON["message"].(string); !strings.Contains(message, "question is required") {+ t.Fatalf("message = %q, want %q", message, "question is required")+ }+ }+ })++ // Each alias must reach the next gate (the kb_ids check), which proves the+ // question was resolved rather than rejected.+ t.Run("question_aliases", func(t *testing.T) {+ for _, tc := range []struct {+ name string+ body apiObject+ code int+ }{+ {"query", apiObject{"query": "what is coriander?"}, 102},+ {"openai_messages", apiObject{"messages": []any{apiObject{"role": "user", "content": "what is coriander?"}}}, 102},+ {"question_beats_bad_messages", apiObject{"question": "what is coriander?", "messages": []any{apiObject{"role": "assistant", "content": "hi"}}}, 102},+ {"trailing_assistant_message", apiObject{"messages": []any{apiObject{"role": "assistant", "content": "hi"}}}, 101},+ } {+ t.Run(tc.name, func(t *testing.T) {+ result := restCall(t, TestConfig, http.MethodPost, path, tc.body, nil)+ restCode(t, result, http.StatusOK, tc.code)+ if tc.code == 101 {+ if message, _ := result.JSON["message"].(string); !strings.Contains(message, "not from user") {+ t.Fatalf("message = %q, want a trailing-user rejection", message)+ }+ }+ })+ }+ })++ t.Run("kb_ids_gate", func(t *testing.T) {+ result := restCall(t, TestConfig, http.MethodPost, path, apiObject{"question": "what is coriander?"}, nil)+ restCode(t, result, http.StatusOK, 102)+ if message, _ := result.JSON["message"].(string); !strings.Contains(message, "`kb_ids` is required") {+ t.Fatalf("message = %q, want %q", message, "`kb_ids` is required")+ }+ // A dataset the caller cannot use must stay hidden behind 404.+ result = restCall(t, TestConfig, http.MethodPost, path,+ apiObject{"question": "what is coriander?", "kb_ids": []string{"nonexistent_dataset"}}, nil)+ restCode(t, result, http.StatusOK, 404)+ if result.JSON["message"] != "Resource not found" {+ t.Fatalf("message = %v, want hidden resource-not-found response", result.JSON["message"])+ }+ })++ t.Run("saved_config_supplies_kb_ids", func(t *testing.T) {+ updateRESTSearch(t, id, name, apiObject{"kb_ids": []string{"nonexistent_dataset"}})+ restCode(t, restCall(t, TestConfig, http.MethodPost, path, apiObject{"question": "what is coriander?"}, nil), http.StatusOK, 404)+ })++ t.Run("unknown_search", func(t *testing.T) {+ restCode(t, restCall(t, TestConfig, http.MethodPost, "/searches/"+uniqueName("missing")+"/completions",+ apiObject{"question": "what is coriander?"}, nil), http.StatusOK, 404)+ })+}++// TestRESTSearchCompletionStream locks the SSE contract of the completion+// endpoint: once the request is accepted the handler must stop emitting JSON+// envelopes and stream newline delimited data frames, each of them parseable,+// and it must terminate with either a final answer frame, the trailing+// `{"data": true}` frame, or an error frame that repeats its message in the+// answer. Retrieval and generation failures are tolerated because they depend+// on the configured chat model; the frame shape is not.+func TestRESTSearchCompletionStream(t *testing.T) {+ id := createRESTSearch(t, uniqueName("completion_stream"))+ datasetID := createRESTSearchDataset(t)++ result := restCall(t, TestConfig, http.MethodPost, "/searches/"+id+"/completions",+ apiObject{"question": "what is coriander?", "kb_ids": []string{datasetID}}, nil)+ if result.Status != http.StatusOK || !strings.Contains(result.Header.Get("Content-Type"), "text/event-stream") {+ t.Fatalf("accepted completion must stream SSE: status=%d content-type=%q body=%s",+ result.Status, result.Header.Get("Content-Type"), result.Body)+ }+ events, err := parseSSE(bytes.NewReader(result.Body))+ if err != nil {+ t.Fatalf("parse SSE: %v", err)+ }+ if len(events) == 0 {+ t.Fatal("stream carried no event")+ }++ var answer strings.Builder+ terminal, errored := false, false+ for i, event := range events {+ var payload apiObject+ if err := json.Unmarshal([]byte(event.Data), &payload); err != nil {+ t.Fatalf("SSE frame %d is not a JSON envelope: %v (%q)", i, err, event.Data)+ }+ code, ok := payload["code"].(float64)+ if !ok {+ t.Fatalf("SSE frame %d has no numeric code: %v", i, payload)+ }+ // The trailing frame is `data: {"code":0,"message":"","data":true}`.+ if done, isBool := payload["data"].(bool); isBool {+ if !done || code != 0 {+ t.Fatalf("terminal frame %d = %v, want code 0 with data true", i, payload)+ }+ terminal = true+ continue+ }+ data := object(t, payload["data"])+ if code != 0 {+ errored = true+ message, _ := payload["message"].(string)+ if message == "" {+ t.Fatalf("error frame %d has no message: %v", i, payload)+ }+ if data["answer"] != "**ERROR**: "+message {+ t.Fatalf("error frame %d answer = %v, want the message echoed with the **ERROR** prefix", i, data["answer"])+ }+ t.Logf("completion stream degraded to an application error: %s", message)+ continue+ }+ if value, ok := data["answer"].(string); ok {+ answer.WriteString(value)+ }+ if data["final"] == true {+ terminal = true+ }+ }+ if !terminal && !errored {+ t.Fatalf("stream ended without a terminal frame: %d events", len(events))+ }+ if !errored && answer.Len() == 0 {+ t.Fatal("successful stream produced no answer text")+ }+}++func TestRESTSearchAuthorization(t *testing.T) {+ id := createRESTSearch(t, uniqueName("auth"))+ anonymous := *TestConfig+ anonymous.APIKey = ""+ for _, tc := range []struct {+ method string+ path string+ body apiObject+ }{+ {http.MethodGet, "/searches", nil},+ {http.MethodPost, "/searches", apiObject{"name": uniqueName("denied")}},+ {http.MethodGet, "/searches/" + id, nil},+ {http.MethodPut, "/searches/" + id, apiObject{"name": "denied", "search_config": apiObject{}}},+ {http.MethodDelete, "/searches/" + id, nil},+ {http.MethodPost, "/searches/" + id + "/completions", apiObject{"question": "denied"}},+ } {+ t.Run(tc.method+"_"+tc.path, func(t *testing.T) {+ restCode(t, restCall(t, &anonymous, tc.method, tc.path, tc.body, nil), http.StatusUnauthorized, 401)+ })+ }+ if got := getRESTSearch(t, id)["name"]; got == "denied" {+ t.Fatal("unauthenticated update persisted")+ }+ t.Run("foreign_owner", func(t *testing.T) {+ token := os.Getenv("RAGFLOW_TEST_OTHER_API_KEY")+ if token == "" {+ t.Skip("set RAGFLOW_TEST_OTHER_API_KEY for an unrelated user to test ownership")+ }+ other := *TestConfig+ other.APIKey = token+ for _, method := range []string{http.MethodGet, http.MethodPut, http.MethodDelete} {+ restCode(t, restCall(t, &other, method, "/searches/"+id,+ apiObject{"name": "denied", "search_config": apiObject{}}, nil), http.StatusOK, 404)+ }+ restCode(t, restCall(t, &other, http.MethodPost, "/searches/"+id+"/completions",+ apiObject{"question": "denied"}, nil), http.StatusOK, 404)+ restOK(t, restCall(t, TestConfig, http.MethodGet, "/searches/"+id, nil, nil))+ })+}++func TestRESTSearchValidation(t *testing.T) {+ t.Run("update_invalid_search_id", func(t *testing.T) {+ result := restCall(t, TestConfig, http.MethodPut, "/searches/invalid_search_id",+ apiObject{"name": "invalid", "search_config": apiObject{}}, nil)+ restCode(t, result, http.StatusOK, 404)+ if result.JSON["message"] != "Resource not found" {+ t.Fatalf("message = %v, want hidden resource-not-found response", result.JSON["message"])+ }+ })+ t.Run("update_requires_name_and_search_config", func(t *testing.T) {+ result := restCall(t, TestConfig, http.MethodPut, "/searches/invalid_search_id", apiObject{}, nil)+ restCode(t, result, http.StatusBadRequest, 400)+ })+}diff --git a/test/testcases/restful_api/conftest.py b/test/testcases/restful_api/conftest.pydeleted file mode 100644index 62f0261281..0000000000--- a/test/testcases/restful_api/conftest.py+++ /dev/null@@ -1,268 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import pytest--from libs.auth import RAGFlowHttpApiAuth-from test.testcases.restful_api.helpers.client import RestClient-from utils.file_utils import create_txt_file-from utils import wait_for---GO_ONLY_SKIPS = {- "Go route is not implemented": {- # Dataset-level graph/raptor/mindmap indexing via POST/GET/DELETE- # /datasets/:id/index is a Python-era RunIndex/TraceIndex/DeleteIndex- # contract; the Go port schedules dataset compilation through the- # knowledge_compile scheduler instead and does not serve /index.- "test_dataset_index_endpoints",- "test_dataset_index_trace_and_delete_type_contract",- "test_dataset_index_run_with_document_creates_task",- "test_document_download_by_id_invalid_id_contract",- "test_llm_factories_live_auth_contract",- "test_llm_list_live_auth_contract",- "test_retrieval_compatibility_endpoint",- "test_retrieval_compatibility_requires_dataset_ids",- "test_retrieval_compatibility_requires_auth",- "test_retrieval_requires_auth_contract",- "test_system_oceanbase_status_auth_contract",- "test_task_routes_require_auth",- "test_patch_task_rejects_unsupported_action",- "test_cancel_missing_task_sets_cancel_contract",- },- "Go ingestion pipeline does not complete document parsing within the test timeout": {- # Chunk add requires an embedding model call; document update/delete- # touches ES/Infinity indices that are only created during parsing.- # Without a working Go ingestion pipeline, these indices never exist.- "test_chunk_add_keyword_question_and_tag_contract",- "test_chunk_add_repeated_and_deleted_document_contract",- "test_chunk_concurrent_add_contract",- "test_documents_update_patch_and_delete",- "test_documents_update_name_contract",- "test_documents_update_meta_fields_contract",- "test_documents_metadata_batch_update_contract",- "test_documents_delete_invalid_dataset_partial_duplicate_repeat_and_cross_dataset",- "test_chat_list_concurrent_and_dataset_delete_contract",- "test_chat_create_dataset_ids_contract",- "test_chat_create_llm_contract",- "test_chat_update_dataset_ids_contract",- "test_chat_update_avatar_contract",- "test_chat_update_llm_contract",- "test_chat_update_prompt_contract",- "test_dataset_search_endpoint",- "test_dataset_search_params_and_doc_ids_contract",- "test_dataset_search_rest_endpoint",- "test_multi_dataset_search_rest_endpoint",- "test_multi_dataset_search_with_metadata_filter",- "test_retrieval_page_and_page_size_contract",- "test_retrieval_highlight_keyword_and_invalid_params_contract",- "test_retrieval_vector_similarity_and_top_k_contract",- "test_retrieval_document_ids_and_metadata_condition_contract",- "test_retrieval_rerank_unknown_contract",- "test_retrieval_concurrent_contract",- "test_documents_parse_contract_matrix",- "test_documents_parse_invalid_dataset_partial_duplicate_and_repeated",- "test_documents_parse_chunks_and_scaled_bulk_contract",- "test_documents_stop_parse_contract_matrix",- "test_documents_stop_parse_invalid_dataset_partial_and_scaled_concurrency",- "test_documents_table_parser_chat_patterns",- "test_chunks_add_list_get_update_delete_cycle",- "test_chunk_delete_basic_contract",- "test_chunk_delete_partial_duplicate_repeat_and_invalid_target_contract",- "test_chunk_delete_web_legacy_basic_variants",- "test_chunk_delete_concurrent_and_bulk_contract",- "test_chunk_list_default_get_id_and_invalid_target_contract",- "test_chunk_list_keyword_and_invalid_param_contract",- "test_chunk_list_page_and_page_size_contract",- "test_chunk_list_concurrent_contract",- "test_chunk_update_requires_auth",- "test_chunk_update_content_and_available_contract",- "test_chunk_update_keywords_questions_and_tag_contract",- "test_chunk_update_invalid_target_and_param_contract",- "test_chunk_update_repeated_concurrent_and_deleted_document_contract",- "test_dataset_update_embedding_model_with_existing_chunks_contract",- "test_deleted_chunk_not_in_retrieval_contract",- "test_deleted_chunks_batch_not_in_retrieval_contract",- },-}---def pytest_collection_modifyitems(items):- for item in items:- test_name = item.name.split("[", 1)[0]- for reason, skipped_tests in GO_ONLY_SKIPS.items():- if test_name in skipped_tests:- item.add_marker(pytest.mark.skip(reason=reason))- break--[email protected](scope="session")-def RestApiAuth(token):- return RAGFlowHttpApiAuth(token)--[email protected](scope="session")-def rest_client(token):- return RestClient(token=token)--[email protected](scope="session")-def rest_client_noauth():- return RestClient(token=None)---def clear_datasets(rest_client):- def _cleanup():- res = rest_client.delete("/datasets", json={"ids": None, "delete_all": True}, timeout=120)- assert res.status_code == 200, res.text- payload = res.json()- assert payload["code"] in (0, 102), payload-- yield- _cleanup()---def clear_chats(rest_client):- def _cleanup():- res = rest_client.delete("/chats", json={"ids": None, "delete_all": True})- assert res.status_code == 200, res.text- payload = res.json()- assert payload["code"] in (0, 102), payload-- yield- _cleanup()---def create_dataset(rest_client, clear_datasets):- created_ids: list[str] = []-- def _create(name: str = "restful_dataset") -> str:- res = rest_client.post("/datasets", json={"name": name})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- dataset_id = payload["data"]["id"]- created_ids.append(dataset_id)- return dataset_id-- yield _create-- if created_ids:- res = rest_client.delete("/datasets", json={"ids": created_ids}, timeout=120)- assert res.status_code == 200- payload = res.json()- # Dataset may already be removed by test logic/cleanup.- assert payload["code"] in (0, 102), payload---def create_chat(rest_client, clear_chats):- created_ids: list[str] = []-- def _create(name: str = "restful_chat") -> str:- res = rest_client.post("/chats", json={"name": name, "dataset_ids": []})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- chat_id = payload["data"]["id"]- created_ids.append(chat_id)- return chat_id-- yield _create-- if created_ids:- res = rest_client.delete("/chats", json={"ids": created_ids})- assert res.status_code == 200, res.text- payload = res.json()- assert payload["code"] in (0, 102), payload---def create_document(rest_client, create_dataset, tmp_path):- created_docs: list[tuple[str, str]] = []-- def _create(name: str = "restful_doc.txt") -> tuple[str, str]:- dataset_id = create_dataset("dataset_for_doc")- fp = create_txt_file(tmp_path / name)- with fp.open("rb") as file_obj:- files = [("file", (fp.name, file_obj))]- res = rest_client.post(f"/datasets/{dataset_id}/documents", files=files)- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- document_id = payload["data"][0]["id"]- created_docs.append((dataset_id, document_id))- return dataset_id, document_id-- yield _create-- for dataset_id, document_id in created_docs:- res = rest_client.delete(f"/datasets/{dataset_id}/documents", json={"ids": [document_id]})- assert res.status_code == 200, res.text- payload = res.json()- assert payload["code"] in (0, 102), payload---@wait_for(60, 1, "Document parsing timeout in RESTful batch2 tests")-def _parsed(rest_client: RestClient, dataset_id: str, document_id: str):- res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"id": document_id})- if res.status_code != 200:- return False- payload = res.json()- if payload["code"] != 0:- return False- docs = payload["data"]["docs"]- if not docs:- return False- return docs[0].get("run") == "DONE"---def ensure_parsed_document(rest_client, create_document):- def _ensure() -> tuple[str, str]:- dataset_id, document_id = create_document()- res = rest_client.post(- f"/datasets/{dataset_id}/documents/parse",- json={"document_ids": [document_id]},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- _parsed(rest_client, dataset_id, document_id)- return dataset_id, document_id-- return _ensure--[email protected](tryfirst=True, hookwrapper=True)-def pytest_runtest_protocol(item, nextitem):- import time-- start = time.perf_counter()- yield- duration = time.perf_counter() - start- if duration >= 10:- color = "****" # 4 stars- elif duration >= 5:- color = "***" # 3 stars- elif duration >= 1:- color = "**" # 2 stars- else:- color = ""- print(f" {color} [{duration:.3f}s]")diff --git a/test/testcases/restful_api/helpers/__init__.py b/test/testcases/restful_api/helpers/__init__.pydeleted file mode 100644index 117dea3cf0..0000000000--- a/test/testcases/restful_api/helpers/__init__.py+++ /dev/null@@ -1 +0,0 @@-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.diff --git a/test/testcases/restful_api/helpers/assertions.py b/test/testcases/restful_api/helpers/assertions.pydeleted file mode 100644index bc703df5d0..0000000000--- a/test/testcases/restful_api/helpers/assertions.py+++ /dev/null@@ -1,23 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#---def assert_auth_error(payload: dict, scenario_name: str) -> None:- assert payload["code"] == 401, (scenario_name, payload)- if scenario_name == "missing token":- assert payload["message"] == "Missing Authorization header", (scenario_name, payload)- else:- assert payload["message"] in {"Invalid access token", "Invalid auth credentials"}, (scenario_name, payload)diff --git a/test/testcases/restful_api/helpers/client.py b/test/testcases/restful_api/helpers/client.pydeleted file mode 100644index f47aa9ba59..0000000000--- a/test/testcases/restful_api/helpers/client.py+++ /dev/null@@ -1,112 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--from dataclasses import dataclass-from typing import Any--import pytest-import requests-from test.testcases.configs import HOST_ADDRESS, VERSION---@dataclass-class RestClient:- token: str | None = None- timeout: int = 30-- @property- def api_root(self) -> str:- return f"{HOST_ADDRESS}/api/{VERSION}"-- def _headers(self, headers: dict[str, str] | None = None) -> dict[str, str]:- merged: dict[str, str] = {"Content-Type": "application/json"}- if headers:- merged.update(headers)- if self.token and "Authorization" not in merged:- merged["Authorization"] = f"Bearer {self.token}"- return merged-- def request(- self,- method: str,- path: str,- *,- headers: dict[str, str] | None = None,- params: dict[str, Any] | None = None,- json: dict[str, Any] | None = None,- data: Any = None,- files: Any = None,- **request_kwargs: Any,- ) -> requests.Response:- req_headers = self._headers(headers)- if files is not None:- # requests sets multipart boundary automatically.- req_headers.pop("Content-Type", None)-- # The Go API requires parse_type on dataset creation (it explicitly- # selects BuiltIn (1) or Pipeline (2) mode). Historically a missing- # parse_type silently defaulted to BuiltIn/general; inject that- # previously-implicit default so dataset-create POSTs stay green without- # touching every call site. Tests that intentionally omit parse_type to- # assert the error path must pass parse_type explicitly.- normalized_path = f"/{path.lstrip('/')}" if path else "/"- if method == "POST" and normalized_path == "/datasets" and isinstance(json, dict) and "parse_type" not in json:- # Restore the previously-implicit default (BuiltIn/general) without- # clobbering any id the caller already supplied.- json = dict(json)- json["parse_type"] = 1- if "parser_id" not in json and "pipeline_id" not in json:- json["parser_id"] = "general"-- timeout = request_kwargs.pop("timeout", self.timeout)- response = requests.request(- method=method,- url=f"{self.api_root}{normalized_path}",- headers=req_headers,- params=params,- json=json,- data=data,- files=files,- timeout=timeout,- **request_kwargs,- )- try:- payload = response.json()- except ValueError:- return response- if not isinstance(payload, dict):- return response- message = payload.get("message", "")- if payload.get("code") == 500 and "Unknown column 'meta_fields'" in message:- pytest.skip("Go deployment database schema is missing document.meta_fields")- if payload.get("code") == 500 and "http://localhost:6380/embed" in message and "connect: connection refused" in message:- pytest.skip("Go memory embedding service is unavailable on localhost:6380")- return response-- def get(self, path: str, **kwargs) -> requests.Response:- return self.request("GET", path, **kwargs)-- def post(self, path: str, **kwargs) -> requests.Response:- return self.request("POST", path, **kwargs)-- def delete(self, path: str, **kwargs) -> requests.Response:- return self.request("DELETE", path, **kwargs)-- def put(self, path: str, **kwargs) -> requests.Response:- return self.request("PUT", path, **kwargs)-- def patch(self, path: str, **kwargs) -> requests.Response:- return self.request("PATCH", path, **kwargs)diff --git a/test/testcases/restful_api/test_agents.py b/test/testcases/restful_api/test_agents.pydeleted file mode 100644index fe97a5ad91..0000000000--- a/test/testcases/restful_api/test_agents.py+++ /dev/null@@ -1,353 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import json--import pytest--MINIMAL_DSL = {- "components": {- "begin": {- "obj": {"component_name": "Begin", "params": {}},- "downstream": ["message"],- "upstream": [],- },- "message": {- "obj": {"component_name": "Message", "params": {"content": ["{sys.query}"]}},- "downstream": [],- "upstream": ["begin"],- },- },- "history": [],- "retrieval": [],- "path": [],- "globals": {- "sys.query": "",- "sys.user_id": "",- "sys.conversation_turns": 0,- "sys.files": [],- },- "variables": {},-}---def _sse_events(response_text: str) -> list[str]:- return [line[5:] for line in response_text.splitlines() if line.startswith("data:")]---def create_agent_resource(rest_client):- created_agent_ids: list[str] = []-- def _create(title: str = "restful_agent") -> str:- res = rest_client.post("/agents", json={"title": title, "dsl": MINIMAL_DSL})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- agent_id = payload["data"]["id"]- created_agent_ids.append(agent_id)- return agent_id-- yield _create-- cleanup_errors = []- for agent_id in created_agent_ids:- res = rest_client.delete(f"/agents/{agent_id}")- if res.status_code != 200:- cleanup_errors.append((agent_id, res.status_code, res.text))- continue- payload = res.json()- if payload["code"] not in (0, 103):- cleanup_errors.append((agent_id, res.status_code, payload))- assert not cleanup_errors, f"Agent cleanup failed: {cleanup_errors}"---def test_agents_crud_validation_contract(rest_client, create_agent_resource):- list_empty = rest_client.get("/agents", params={"title": "missing_restful_agent"})- assert list_empty.status_code == 200- list_empty_payload = list_empty.json()- assert list_empty_payload["code"] == 0, list_empty_payload- assert "canvas" in list_empty_payload["data"], list_empty_payload- assert "total" in list_empty_payload["data"], list_empty_payload-- paged_list = rest_client.get(- "/agents",- params={"title": "missing_restful_agent", "desc": "true", "page_size": 1},- )- assert paged_list.status_code == 200- paged_list_payload = paged_list.json()- assert paged_list_payload["code"] == 0, paged_list_payload-- missing_dsl = rest_client.post("/agents", json={"title": "missing_dsl_agent"})- assert missing_dsl.status_code == 200- missing_dsl_payload = missing_dsl.json()- assert missing_dsl_payload["code"] == 101, missing_dsl_payload- assert "no dsl data in request" in missing_dsl_payload["message"].lower(), missing_dsl_payload-- missing_title = rest_client.post("/agents", json={"dsl": MINIMAL_DSL})- assert missing_title.status_code == 200- missing_title_payload = missing_title.json()- assert missing_title_payload["code"] == 101, missing_title_payload- assert "no title in request" in missing_title_payload["message"].lower(), missing_title_payload-- agent_id = create_agent_resource("restful_agent_crud")-- duplicate = rest_client.post("/agents", json={"title": "restful_agent_crud", "dsl": MINIMAL_DSL})- assert duplicate.status_code == 200- duplicate_payload = duplicate.json()- assert duplicate_payload["code"] == 0, duplicate_payload- assert duplicate_payload["data"]["title"] == "restful_agent_crud(1)", duplicate_payload- duplicate_id = duplicate_payload["data"]["id"]- cleanup_duplicate = rest_client.delete(f"/agents/{duplicate_id}")- assert cleanup_duplicate.status_code == 200- assert cleanup_duplicate.json()["code"] == 0, cleanup_duplicate.json()-- invalid_update = rest_client.put("/agents/invalid-agent-id", json={"title": "updated", "dsl": MINIMAL_DSL})- assert invalid_update.status_code == 200- invalid_update_payload = invalid_update.json()- assert invalid_update_payload["code"] == 103, invalid_update_payload- assert "Make sure you have permission to access the agent." in invalid_update_payload["message"], invalid_update_payload-- get_res = rest_client.get(f"/agents/{agent_id}")- assert get_res.status_code == 200- get_payload = get_res.json()- assert get_payload["code"] == 0, get_payload- assert get_payload["data"]["id"] == agent_id, get_payload-- update_res = rest_client.put(f"/agents/{agent_id}", json={"title": "restful_agent_crud_updated", "dsl": MINIMAL_DSL})- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload-- list_after_update = rest_client.get("/agents", params={"title": "restful_agent_crud_updated"})- assert list_after_update.status_code == 200- list_after_update_payload = list_after_update.json()- assert list_after_update_payload["code"] == 0, list_after_update_payload- assert list_after_update_payload["data"]["total"] >= 1, list_after_update_payload-- invalid_delete = rest_client.delete("/agents/invalid-agent-id")- assert invalid_delete.status_code == 200- invalid_delete_payload = invalid_delete.json()- # code=103 = permission denied (Python: "Only the owner of the agent..."; Go: "Make sure you have permission...")- assert invalid_delete_payload["code"] == 103, invalid_delete_payload- msg = invalid_delete_payload["message"]- assert "Only the owner of the agent is authorized" in msg or "Make sure you have permission" in msg, invalid_delete_payload-- delete_res = rest_client.delete(f"/agents/{agent_id}")- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload- assert delete_payload["data"] is True, delete_payload---def test_agent_sessions_crud(rest_client, create_agent_resource):- agent_id = create_agent_resource("restful_agent_sessions")-- create_session = rest_client.post(f"/agents/{agent_id}/sessions", json={"name": "agent_session_1"})- assert create_session.status_code == 200- create_session_payload = create_session.json()- assert create_session_payload["code"] == 0, create_session_payload- session_id = create_session_payload["data"]["id"]-- list_sessions = rest_client.get(f"/agents/{agent_id}/sessions")- assert list_sessions.status_code == 200- list_sessions_payload = list_sessions.json()- assert list_sessions_payload["code"] == 0, list_sessions_payload- assert isinstance(list_sessions_payload["data"], list), list_sessions_payload- assert any(item["id"] == session_id for item in list_sessions_payload["data"]), list_sessions_payload-- get_session = rest_client.get(f"/agents/{agent_id}/sessions/{session_id}")- assert get_session.status_code == 200- get_session_payload = get_session.json()- assert get_session_payload["code"] == 0, get_session_payload- assert get_session_payload["data"]["id"] == session_id, get_session_payload-- delete_session = rest_client.delete(f"/agents/{agent_id}/sessions/{session_id}")- assert delete_session.status_code == 200- delete_session_payload = delete_session.json()- assert delete_session_payload["code"] == 0, delete_session_payload---def test_agent_chat_completion_validation(rest_client):- missing_agent_id = rest_client.post(- "/agents/chat/completions",- json={"query": "hello", "stream": False},- )- assert missing_agent_id.status_code == 200- missing_agent_id_payload = missing_agent_id.json()- assert missing_agent_id_payload["code"] == 101, missing_agent_id_payload- assert "`agent_id` is required." in missing_agent_id_payload["message"], missing_agent_id_payload---def test_agent_chat_completion_nonstream(rest_client, create_agent_resource):- agent_id = create_agent_resource("restful_agent_nonstream")- create_session = rest_client.post(f"/agents/{agent_id}/sessions", json={"name": "agent_completion_session"})- assert create_session.status_code == 200- create_session_payload = create_session.json()- assert create_session_payload["code"] == 0, create_session_payload- session_id = create_session_payload["data"]["id"]-- res = rest_client.post(- "/agents/chat/completions",- json={"agent_id": agent_id, "query": "hello", "stream": False, "session_id": session_id},- timeout=60,- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert isinstance(payload["data"], dict), payload- assert payload["data"].get("session_id") == session_id, payload- assert isinstance(payload["data"].get("data"), dict), payload- content = payload["data"]["data"].get("content", "")- assert content, payload- assert "hello" in content, payload---def test_agent_chat_completion_stream_structure_and_done(rest_client, create_agent_resource):- agent_id = create_agent_resource("restful_agent_stream")- create_session = rest_client.post(f"/agents/{agent_id}/sessions", json={"name": "agent_stream_session"})- assert create_session.status_code == 200- create_session_payload = create_session.json()- assert create_session_payload["code"] == 0, create_session_payload- session_id = create_session_payload["data"]["id"]-- res = rest_client.post(- "/agents/chat/completions",- json={- "agent_id": agent_id,- "query": "hello",- "stream": True,- "session_id": session_id,- "return_trace": True,- },- timeout=60,- )- assert res.status_code == 200- content_type = res.headers.get("Content-Type", "")- assert "text/event-stream" in content_type, content_type-- events = _sse_events(res.text)- assert events, res.text- assert events[-1].strip() == "[DONE]", events[-1]-- json_events = [json.loads(evt) for evt in events if evt.strip() != "[DONE]"]- assert json_events, events- assert any(isinstance(evt, dict) for evt in json_events), json_events---def test_agent_openai_compatible_mode(rest_client, create_agent_resource):- agent_id = create_agent_resource("restful_agent_openai_compat")-- missing_messages = rest_client.post(- "/agents/chat/completions",- json={"agent_id": agent_id, "openai-compatible": True, "model": "model", "messages": []},- )- assert missing_messages.status_code == 200- missing_messages_payload = missing_messages.json()- assert missing_messages_payload["code"] == 102, missing_messages_payload- assert "at least one message" in missing_messages_payload["message"], missing_messages_payload-- nonstream = rest_client.post(- "/agents/chat/completions",- json={- "agent_id": agent_id,- "openai-compatible": True,- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- },- timeout=60,- )- assert nonstream.status_code == 200- nonstream_payload = nonstream.json()- assert isinstance(nonstream_payload, dict), nonstream_payload- assert nonstream_payload.get("object") == "chat.completion", nonstream_payload- assert "choices" in nonstream_payload, nonstream_payload-- stream = rest_client.post(- "/agents/chat/completions",- json={- "agent_id": agent_id,- "openai-compatible": True,- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "stream": True,- },- timeout=60,- )- assert stream.status_code == 200- stream_content_type = stream.headers.get("Content-Type", "")- assert "text/event-stream" in stream_content_type, stream_content_type---def test_agent_support_routes_auth_and_contracts(rest_client, rest_client_noauth, create_agent_resource):- prompts_unauth = rest_client_noauth.get("/agents/prompts")- assert prompts_unauth.status_code == 401- assert prompts_unauth.json()["code"] == 401-- prompts = rest_client.get("/agents/prompts")- assert prompts.status_code == 200- prompts_payload = prompts.json()- assert prompts_payload["code"] == 0, prompts_payload- assert "task_analysis" in prompts_payload["data"], prompts_payload- assert "citation_guidelines" in prompts_payload["data"], prompts_payload-- templates = rest_client.get("/agents/templates")- assert templates.status_code == 200- templates_payload = templates.json()- assert templates_payload["code"] == 0, templates_payload- assert isinstance(templates_payload["data"], list), templates_payload-- agent_id = create_agent_resource("restful_agent_support")- versions = rest_client.get(f"/agents/{agent_id}/versions")- assert versions.status_code == 200- versions_payload = versions.json()- assert versions_payload["code"] == 0, versions_payload- assert isinstance(versions_payload["data"], list), versions_payload-- logs = rest_client.get(f"/agents/{agent_id}/logs/missing_message")- assert logs.status_code == 200- logs_payload = logs.json()- assert logs_payload["code"] == 0, logs_payload- assert isinstance(logs_payload["data"], dict), logs_payload---def test_agent_webhook_logs_empty_poll_contract(rest_client, create_agent_resource):- agent_id = create_agent_resource("restful_agent_webhook_logs")- res = rest_client.get(f"/agents/{agent_id}/webhook/logs", params={"since_ts": 0})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert payload["data"]["events"] == [], payload- assert payload["data"]["finished"] is False, payload- assert "next_since_ts" in payload["data"], payload---def test_agent_db_connection_validates_required_fields(rest_client):- res = rest_client.post("/agents/test_db_connection", json={"db_type": "mysql"})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- assert "required argument are missing" in payload["message"], payloaddiff --git a/test/testcases/restful_api/test_agents_go_mode.py b/test/testcases/restful_api/test_agents_go_mode.pydeleted file mode 100644index a72bcbf877..0000000000--- a/test/testcases/restful_api/test_agents_go_mode.py+++ /dev/null@@ -1,53 +0,0 @@-import uuid--import pytest--V2_DSL = {- "graph": {- "nodes": [- {"id": "begin", "type": "beginNode", "position": {"x": 50, "y": 200}, "data": {"label": "Begin", "name": "begin"}},- {"id": "answer:0", "type": "messageNode", "position": {"x": 400, "y": 200}, "data": {"label": "Answer", "name": "answer"}},- ],- "edges": [- {"id": "xy-edge__begin-answer:0", "source": "begin", "target": "answer:0", "sourceHandle": "end", "targetHandle": "start"},- ],- },- "components": {- "begin": {"obj": {"component_name": "Begin", "params": {}}, "downstream": ["answer:0"], "upstream": []},- "answer:0": {"obj": {"component_name": "Answer", "params": {}}, "downstream": [], "upstream": ["begin"]},- },- "retrieval": [],- "history": [],- "path": [],- "variables": [],- "globals": {"sys.query": ""},-}---def test_v2_dsl_round_trip_position_preserved(rest_client):- # Unique title per run so the test is idempotent.- title = f"go_v2_e2e_{uuid.uuid4().hex[:8]}"-- # 1. POST v2 DSL- r = rest_client.post("/agents", json={"title": title, "dsl": V2_DSL})- assert r.status_code == 200 and r.json()["code"] == 0, r.text- agent_id = r.json()["data"]["id"]-- # 2. GET — verify v2 shape (graph + components, NO _layout)- dsl = rest_client.get(f"/agents/{agent_id}").json()["data"]["dsl"]- assert "graph" in dsl, f"v2 must have 'graph', got keys={sorted(dsl.keys())}"- assert "_layout" not in dsl, f"v2 must NOT emit '_layout', got keys={sorted(dsl.keys())}"- assert "components" in dsl- assert len(dsl["graph"]["nodes"]) == 2- assert len(dsl["graph"]["edges"]) == 1-- # 3. PUT — move begin node to (777, 888)- new_dsl = {**V2_DSL, "graph": {**V2_DSL["graph"]}}- new_dsl["graph"]["nodes"] = [{**n, "position": {"x": 777, "y": 888}} if n["id"] == "begin" else n for n in V2_DSL["graph"]["nodes"]]- r = rest_client.put(f"/agents/{agent_id}", json={"title": title, "dsl": new_dsl})- assert r.status_code == 200 and r.json()["code"] == 0, r.text-- # 4. Re-GET — position preserved- pos = next(n["position"] for n in rest_client.get(f"/agents/{agent_id}").json()["data"]["dsl"]["graph"]["nodes"] if n["id"] == "begin")- assert pos == {"x": 777, "y": 888}, f"position lost: {pos}"diff --git a/test/testcases/restful_api/test_chats.py b/test/testcases/restful_api/test_chats.pydeleted file mode 100644index cf1130b9d4..0000000000--- a/test/testcases/restful_api/test_chats.py+++ /dev/null@@ -1,1477 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import asyncio-import importlib.util-import re-import sys-from concurrent.futures import ThreadPoolExecutor-from copy import deepcopy-from enum import Enum-from functools import wraps-from pathlib import Path-from types import ModuleType, SimpleNamespace--import pytest--from test.testcases.configs import CHAT_ASSISTANT_NAME_LIMIT, INVALID_API_TOKEN-from test.testcases.restful_api.helpers.assertions import assert_auth_error-from test.testcases.restful_api.helpers.client import RestClient-from test.testcases.utils import encode_avatar-from test.testcases.utils.file_utils import create_image_file--DEFAULT_CHAT_EMPTY_RESPONSE = "Sorry! No relevant content was found in the knowledge base!"-DEFAULT_CHAT_PROLOGUE = "Hi! I'm your assistant. What can I do for you?"-DEFAULT_CHAT_SYSTEM_PROMPT = (- "You are an intelligent assistant. Please summarize the content of the dataset to answer the question. "- "Please list the data in the dataset and answer in detail. When all dataset content is irrelevant to the "- 'question, your answer must include the sentence "The answer you are looking for is not found in the dataset!" '- "Answers need to consider chat history.\n"- " Here is the knowledge base:\n"- " {knowledge}\n"- " The above is the knowledge base."-)---def _get_nested(data, path):- current = data- for key in path:- current = current[key]- return current---def _chat_names(payload):- return [chat["name"] for chat in payload["data"]["chats"]]---def _reset_chat_batch(rest_client, prefix, count=5):- cleanup_res = rest_client.delete("/chats", json={"ids": None, "delete_all": True})- assert cleanup_res.status_code == 200, cleanup_res.text- cleanup_payload = cleanup_res.json()- assert cleanup_payload["code"] in (0, 102), cleanup_payload-- ids = []- for index in range(count):- res = rest_client.post("/chats", json={"name": f"{prefix}_{index}", "dataset_ids": []})- assert res.status_code == 200, (prefix, index, res.text)- payload = res.json()- assert payload["code"] == 0, (prefix, index, payload)- ids.append(payload["data"]["id"])- return ids---class TestChatsAuthorization:- def test_create_requires_auth(self, rest_client_noauth):- res = rest_client_noauth.post("/chats", json={"name": "chat_auth", "dataset_ids": []})- assert res.status_code == 401---def test_chat_crud_cycle(rest_client, clear_chats):- create_res = rest_client.post("/chats", json={"name": "restful_chat_crud", "dataset_ids": []})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- chat_id = create_payload["data"]["id"]-- # A second chat in the same tenant, so the exact id lookup below only- # passes when the filter is applied.- other_res = rest_client.post("/chats", json={"name": "restful_chat_crud_other", "dataset_ids": []})- assert other_res.status_code == 200- other_payload = other_res.json()- assert other_payload["code"] == 0, other_payload- assert other_payload["data"]["id"] != chat_id, other_payload-- list_res = rest_client.get("/chats", params={"id": chat_id})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert len(list_payload["data"]["chats"]) == 1, list_payload- assert list_payload["data"]["chats"][0]["id"] == chat_id, list_payload-- # Exact filters take precedence over keywords, so the id wins even when the keyword names the other chat.- precedence_res = rest_client.get("/chats", params={"id": chat_id, "keywords": "restful_chat_crud_other"})- assert precedence_res.status_code == 200- precedence_payload = precedence_res.json()- assert precedence_payload["code"] == 0, precedence_payload- assert len(precedence_payload["data"]["chats"]) == 1, precedence_payload- assert precedence_payload["data"]["chats"][0]["id"] == chat_id, precedence_payload-- get_res = rest_client.get(f"/chats/{chat_id}")- assert get_res.status_code == 200- get_payload = get_res.json()- assert get_payload["code"] == 0, get_payload- assert get_payload["data"]["id"] == chat_id, get_payload-- update_res = rest_client.put(f"/chats/{chat_id}", json={"name": "restful_chat_crud_updated", "dataset_ids": []})- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload- assert update_payload["data"]["name"] == "restful_chat_crud_updated", update_payload-- patch_res = rest_client.patch(f"/chats/{chat_id}", json={"name": "restful_chat_crud_patched"})- assert patch_res.status_code == 200- patch_payload = patch_res.json()- assert patch_payload["code"] == 0, patch_payload- assert patch_payload["data"]["name"] == "restful_chat_crud_patched", patch_payload-- delete_res = rest_client.delete("/chats", json={"ids": [chat_id]})- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload- assert delete_payload["data"]["success_count"] == 1, delete_payload-- list_after_delete = rest_client.get("/chats", params={"id": chat_id})- assert list_after_delete.status_code == 200- list_after_delete_payload = list_after_delete.json()- assert list_after_delete_payload["code"] == 0, list_after_delete_payload- assert list_after_delete_payload["data"]["chats"] == [], list_after_delete_payload--- "name, expected_fragment",- [- ("", "`name` is required"),- (" ", "`name` is required"),- ],-)-def test_chat_create_name_validation(rest_client, clear_chats, name, expected_fragment):- res = rest_client.post("/chats", json={"name": name, "dataset_ids": []})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert expected_fragment in payload["message"], payload---def test_chat_duplicate_name_validation(rest_client, clear_chats):- first = rest_client.post("/chats", json={"name": "duplicate_chat_name", "dataset_ids": []})- assert first.status_code == 200- first_payload = first.json()- assert first_payload["code"] == 0, first_payload-- second = rest_client.post("/chats", json={"name": "duplicate_chat_name", "dataset_ids": []})- assert second.status_code == 200- second_payload = second.json()- assert second_payload["code"] == 0, second_payload- assert second_payload["data"]["name"] == "duplicate_chat_name(1)", second_payload---def test_chat_list_pagination(rest_client, clear_chats):- for i in range(3):- res = rest_client.post("/chats", json={"name": f"chat_page_{i}", "dataset_ids": []})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload-- page_res = rest_client.get("/chats", params={"page": 1, "page_size": 2, "orderby": "create_time", "desc": "true"})- assert page_res.status_code == 200- page_payload = page_res.json()- assert page_payload["code"] == 0, page_payload- assert len(page_payload["data"]["chats"]) == 2, page_payload- assert page_payload["data"]["total"] >= 3, page_payload---def test_chat_delete_requires_auth():- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.delete("/chats", json={"ids": []})- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_chat_delete_basic_scenarios(rest_client, clear_chats):- existing_ids = _reset_chat_batch(rest_client, "delete_basic")- existing_res = rest_client.delete("/chats", json={"ids": existing_ids})- assert existing_res.status_code == 200- existing_payload = existing_res.json()- assert existing_payload["code"] == 0, existing_payload- assert existing_payload["data"]["success_count"] == len(existing_ids), existing_payload-- list_after_existing = rest_client.get("/chats").json()- assert list_after_existing["code"] == 0, list_after_existing- assert list_after_existing["data"]["chats"] == [], list_after_existing-- empty_res = rest_client.delete("/chats", json={"ids": []})- assert empty_res.status_code == 200- empty_payload = empty_res.json()- assert empty_payload["code"] == 0, empty_payload- assert empty_payload["message"] == "success", empty_payload-- delete_all_ids = _reset_chat_batch(rest_client, "delete_all")- delete_all_res = rest_client.delete("/chats", json={"ids": None, "delete_all": True})- assert delete_all_res.status_code == 200- delete_all_payload = delete_all_res.json()- assert delete_all_payload["code"] == 0, delete_all_payload- assert delete_all_payload["data"]["success_count"] == len(delete_all_ids), delete_all_payload-- list_after_delete_all = rest_client.get("/chats").json()- assert list_after_delete_all["code"] == 0, list_after_delete_all- assert list_after_delete_all["data"]["chats"] == [], list_after_delete_all---def test_chat_delete_error_and_repeat_contract(rest_client, clear_chats):- partial_cases = [- ("partial invalid id", lambda ids: {"ids": ids + ["invalid_id"]}),- ("partial invalid punctuation id", lambda ids: {"ids": ids + ["!@#$%^&*()"]}),- ]- for scenario_name, payload in partial_cases:- ids = _reset_chat_batch(rest_client, f"delete_partial_{scenario_name.replace(' ', '_')}")- res = rest_client.delete("/chats", json=payload(ids))- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == 0, (scenario_name, body)- assert len(body["data"]["errors"]) == 1, (scenario_name, body)- assert body["data"]["success_count"] == 5, (scenario_name, body)-- list_payload = rest_client.get("/chats").json()- assert list_payload["code"] == 0, (scenario_name, list_payload)- assert list_payload["data"]["chats"] == [], (scenario_name, list_payload)-- duplicate_ids = _reset_chat_batch(rest_client, "delete_duplicate_all")- duplicate_all_res = rest_client.delete("/chats", json={"ids": duplicate_ids + duplicate_ids})- assert duplicate_all_res.status_code == 200- duplicate_all_payload = duplicate_all_res.json()- assert duplicate_all_payload["code"] == 0, duplicate_all_payload- assert duplicate_all_payload["data"]["success_count"] == 5, duplicate_all_payload- assert len(duplicate_all_payload["data"]["errors"]) == 5, duplicate_all_payload- assert all(error.startswith("Duplicate chat ids: ") for error in duplicate_all_payload["data"]["errors"]), duplicate_all_payload-- duplicate_one_ids = _reset_chat_batch(rest_client, "delete_duplicate_one")- duplicate_one_res = rest_client.delete("/chats", json={"ids": [duplicate_one_ids[0], duplicate_one_ids[0]]})- assert duplicate_one_res.status_code == 200- duplicate_one_payload = duplicate_one_res.json()- assert duplicate_one_payload["code"] == 0, duplicate_one_payload- assert duplicate_one_payload["data"]["success_count"] == 1, duplicate_one_payload- assert duplicate_one_payload["data"]["errors"] == [f"Duplicate chat ids: {duplicate_one_ids[0]}"], duplicate_one_payload-- all_missing_res = rest_client.delete("/chats", json={"ids": ["missing-1", "missing-2"]})- assert all_missing_res.status_code == 200- all_missing_payload = all_missing_res.json()- assert all_missing_payload["code"] == 102, all_missing_payload- assert "Chat(missing-1) not found." in all_missing_payload["message"], all_missing_payload- assert "Chat(missing-2) not found." in all_missing_payload["message"], all_missing_payload-- repeated_ids = _reset_chat_batch(rest_client, "delete_repeated")- first_res = rest_client.delete("/chats", json={"ids": repeated_ids})- assert first_res.status_code == 200- first_payload = first_res.json()- assert first_payload["code"] == 0, first_payload- assert first_payload["data"]["success_count"] == 5, first_payload-- second_res = rest_client.delete("/chats", json={"ids": repeated_ids})- assert second_res.status_code == 200- second_payload = second_res.json()- assert second_payload["code"] == 102, second_payload- for chat_id in repeated_ids:- assert f"Chat({chat_id}) not found." in second_payload["message"], second_payload---def test_chat_delete_concurrent_and_bulk_contract(rest_client, clear_chats):- concurrent_ids = _reset_chat_batch(rest_client, "delete_concurrent", count=20)- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(executor.map(lambda chat_id: rest_client.delete("/chats", json={"ids": [chat_id]}).json(), concurrent_ids))- assert len(results) == 20, results- assert all(result["code"] == 0 for result in results), results- assert all(result["data"]["success_count"] == 1 for result in results), results-- list_after_concurrent = rest_client.get("/chats").json()- assert list_after_concurrent["code"] == 0, list_after_concurrent- assert list_after_concurrent["data"]["chats"] == [], list_after_concurrent-- bulk_ids = _reset_chat_batch(rest_client, "delete_bulk", count=100)- bulk_res = rest_client.delete("/chats", json={"ids": bulk_ids})- assert bulk_res.status_code == 200- bulk_payload = bulk_res.json()- assert bulk_payload["code"] == 0, bulk_payload- assert bulk_payload["data"]["success_count"] == len(bulk_ids), bulk_payload---def test_chat_list_requires_auth():- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.get("/chats")- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_chat_list_default_get_and_separate_lookup_contract(rest_client, clear_chats):- ids = _reset_chat_batch(rest_client, "list_default")-- default_res = rest_client.get("/chats")- assert default_res.status_code == 200- default_payload = default_res.json()- assert default_payload["code"] == 0, default_payload- assert len(default_payload["data"]["chats"]) == 5, default_payload- assert default_payload["data"]["total"] == 5, default_payload-- valid_get_res = rest_client.get(f"/chats/{ids[0]}")- assert valid_get_res.status_code == 200- valid_get_payload = valid_get_res.json()- assert valid_get_payload["code"] == 0, valid_get_payload- assert valid_get_payload["data"]["id"] == ids[0], valid_get_payload-- invalid_get_res = rest_client.get("/chats/unknown")- assert invalid_get_res.status_code == 200- invalid_get_payload = invalid_get_res.json()- assert invalid_get_payload["code"] == 404, invalid_get_payload- assert invalid_get_payload["message"] == "Resource not found", invalid_get_payload-- for chat_id, keywords, expected_count in ((ids[0], "list_default_0", 1), (ids[0], "list_default_1", 1), (ids[0], "unknown", 0)):- get_res = rest_client.get(f"/chats/{chat_id}")- list_res = rest_client.get("/chats", params={"keywords": keywords})- assert get_res.status_code == 200, (keywords, get_res.text)- assert list_res.status_code == 200, (keywords, list_res.text)- get_payload = get_res.json()- list_payload = list_res.json()- assert get_payload["code"] == 0, (keywords, get_payload)- assert list_payload["code"] == 0, (keywords, list_payload)- assert len(list_payload["data"]["chats"]) == expected_count, (keywords, list_payload)---def test_chat_list_keyword_and_invalid_param_contract(rest_client, clear_chats):- _reset_chat_batch(rest_client, "list_keyword")- cases = [- ("keywords none", {"keywords": None}, 5, None),- ("keywords empty", {"keywords": ""}, 5, None),- ("keywords exact", {"keywords": "list_keyword_1"}, 1, "list_keyword_1"),- ("keywords unknown", {"keywords": "unknown"}, 0, None),- ("invalid params ignored", {"a": "b"}, 5, None),- ]-- for scenario_name, params, expected_count, expected_name in cases:- res = rest_client.get("/chats", params=params)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == 0, (scenario_name, payload)- assert len(payload["data"]["chats"]) == expected_count, (scenario_name, payload)- if expected_name is not None:- assert payload["data"]["chats"][0]["name"] == expected_name, (scenario_name, payload)---def test_chat_list_page_and_page_size_contract(rest_client, clear_chats):- cases = [- ("page none", {"page": None, "page_size": 2}, 0, lambda total: total, ""),- ("page zero", {"page": 0, "page_size": 2}, 0, lambda total: total, ""),- ("page two", {"page": 2, "page_size": 2}, 0, lambda total: min(max(total - 2, 0), 2), ""),- ("page three", {"page": 3, "page_size": 2}, 0, lambda total: min(max(total - 4, 0), 2), ""),- ("page string", {"page": "3", "page_size": 2}, 0, lambda total: min(max(total - 4, 0), 2), ""),- ("page negative", {"page": -1, "page_size": 2}, 0, lambda total: total, ""),- ("page alpha", {"page": "a", "page_size": 2}, 0, lambda total: total, ""),- ("page_size none", {"page_size": None}, 0, lambda total: total, ""),- ("page_size zero", {"page_size": 0}, 0, lambda total: total, ""),- ("page_size one", {"page_size": 1}, 0, lambda total: total, ""),- ("page_size six", {"page_size": 6}, 0, lambda total: total, ""),- ("page_size string", {"page_size": "1"}, 0, lambda total: total, ""),- ("page_size negative", {"page_size": -1}, 0, lambda total: total, ""),- ("page_size alpha", {"page_size": "a"}, 0, lambda total: total, ""),- ]-- for scenario_name, params, expected_code, expected_count_fn, expected_message in cases:- _reset_chat_batch(rest_client, f"list_page_{scenario_name.replace(' ', '_')}")- baseline_payload = rest_client.get("/chats").json()- assert baseline_payload["code"] == 0, (scenario_name, baseline_payload)- baseline_total = baseline_payload["data"]["total"]-- res = rest_client.get("/chats", params=params)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (scenario_name, payload)- if expected_code == 0:- assert len(payload["data"]["chats"]) == expected_count_fn(baseline_total), (scenario_name, payload)- assert payload["data"]["total"] == baseline_total, (scenario_name, payload)- else:- assert expected_message in payload["message"], (scenario_name, payload)---def test_chat_list_sorting_contract(rest_client, clear_chats):- _reset_chat_batch(rest_client, "list_sort")- ascending_names = [f"list_sort_{i}" for i in range(5)]- descending_names = list(reversed(ascending_names))- cases = [- ("orderby none", {"orderby": None}, 0, descending_names, ""),- ("orderby create", {"orderby": "create_time"}, 0, descending_names, ""),- ("orderby update", {"orderby": "update_time"}, 0, descending_names, ""),- ("orderby name ascending", {"orderby": "name", "desc": "False"}, 0, ascending_names, ""),- ("orderby unknown", {"orderby": "unknown"}, 101, None, "invalid orderby field"),- ("desc none", {"desc": None}, 0, descending_names, ""),- ("desc true", {"desc": "true"}, 0, descending_names, ""),- ("desc True", {"desc": "True"}, 0, descending_names, ""),- ("desc bool true", {"desc": True}, 0, descending_names, ""),- ("desc false", {"desc": "false"}, 0, ascending_names, ""),- ("desc False", {"desc": "False"}, 0, ascending_names, ""),- ("desc bool false", {"desc": False}, 0, ascending_names, ""),- ("desc False update_time", {"desc": "False", "orderby": "update_time"}, 0, ascending_names, ""),- ("desc unknown", {"desc": "unknown"}, 0, descending_names, ""),- ]-- for scenario_name, params, expected_code, expected_names, expected_message in cases:- res = rest_client.get("/chats", params=params)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (scenario_name, payload)- if expected_code == 0:- assert _chat_names(payload) == expected_names, (scenario_name, payload)- else:- assert expected_message in payload["message"], (scenario_name, payload)---def test_chat_list_concurrent_and_dataset_delete_contract(rest_client, clear_chats, ensure_parsed_document):- _reset_chat_batch(rest_client, "list_concurrent")- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(executor.map(lambda _idx: rest_client.get("/chats").json(), range(10)))- assert len(results) == 10, results- assert all(result["code"] == 0 for result in results), results- assert all(len(result["data"]["chats"]) == 5 for result in results), results-- dataset_id, _ = ensure_parsed_document()- create_res = rest_client.post("/chats", json={"name": "list_after_dataset_delete", "dataset_ids": [dataset_id]})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload-- delete_dataset_res = rest_client.delete("/datasets", json={"ids": [dataset_id]})- assert delete_dataset_res.status_code == 200- delete_dataset_payload = delete_dataset_res.json()- assert delete_dataset_payload["code"] == 0, delete_dataset_payload-- list_res = rest_client.get("/chats", params={"keywords": "list_after_dataset_delete"})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert len(list_payload["data"]["chats"]) == 1, list_payload---class _DummyManager:- def route(self, *_args, **_kwargs):- def decorator(func):- return func-- return decorator---class _AwaitableValue:- def __init__(self, value):- self._value = value-- def __await__(self):- async def _co():- return self._value-- return _co().__await__()---class _DummyArgs(dict):- def get(self, key, default=None):- return super().get(key, default)-- def getlist(self, key):- value = self.get(key, [])- if value is None:- return []- if isinstance(value, list):- return value- return [value]---class _StubHeaders:- def __init__(self):- self._items = []-- def add_header(self, key, value):- self._items.append((key, value))-- def get(self, key, default=None):- for existing_key, value in reversed(self._items):- if existing_key == key:- return value- return default---class _StubResponse:- def __init__(self, body=None, mimetype=None, content_type=None):- self.body = body- self.mimetype = mimetype- self.content_type = content_type- self.headers = _StubHeaders()---class _DummyUploadFile:- def __init__(self, filename):- self.filename = filename- self.saved_path = None-- async def save(self, path):- self.saved_path = path---def _passthrough_login_required(func):- @wraps(func)- async def _wrapper(*args, **kwargs):- return await func(*args, **kwargs)-- return _wrapper---class _DummyKB:- def __init__(self, kid="kb-1", embd_id="embd@factory", chunk_num=1, name="Dataset A", status="1"):- self.id = kid- self.embd_id = embd_id- self.chunk_num = chunk_num- self.name = name- self.status = status---class _DummyDialogRecord:- def __init__(self, data=None):- self._data = data or {- "id": "chat-1",- "name": "chat-name",- "description": "desc",- "icon": "icon.png",- "kb_ids": ["kb-1"],- "llm_id": "glm-4",- "llm_setting": {"temperature": 0.1},- "prompt_config": {- "system": "Answer with {knowledge}",- "parameters": [{"key": "knowledge", "optional": False}],- "prologue": "hello",- "quote": True,- },- "similarity_threshold": 0.2,- "vector_similarity_weight": 0.3,- "top_n": 6,- "top_k": 1024,- "rerank_id": "",- "meta_data_filter": {},- "tenant_id": "tenant-1",- }-- def to_dict(self):- return deepcopy(self._data)---def _run(coro):- return asyncio.run(coro)---async def _collect_stream(body):- items = []- if hasattr(body, "__aiter__"):- async for item in body:- if isinstance(item, bytes):- item = item.decode("utf-8")- items.append(item)- else:- for item in body:- if isinstance(item, bytes):- item = item.decode("utf-8")- items.append(item)- return items---def _load_chat_routes_unit_module(monkeypatch):- repo_root = Path(__file__).resolve().parents[3]- module_name = "test_chat_restful_routes_unit_module"- module_path = repo_root / "api" / "apps" / "restful_apis" / "chat_api.py"-- quart_mod = ModuleType("quart")- quart_mod.request = SimpleNamespace(args=_DummyArgs())- quart_mod.Response = _StubResponse- monkeypatch.setitem(sys.modules, "quart", quart_mod)-- api_pkg = ModuleType("api")- api_pkg.__path__ = [str(repo_root / "api")]- monkeypatch.setitem(sys.modules, "api", api_pkg)-- apps_pkg = ModuleType("api.apps")- apps_pkg.__path__ = [str(repo_root / "api" / "apps")]- apps_pkg.current_user = SimpleNamespace(id="tenant-1")- apps_pkg.login_required = _passthrough_login_required- monkeypatch.setitem(sys.modules, "api.apps", apps_pkg)- api_pkg.apps = apps_pkg-- common_pkg = ModuleType("common")- common_pkg.__path__ = [str(repo_root / "common")]- monkeypatch.setitem(sys.modules, "common", common_pkg)-- common_constants_mod = ModuleType("common.constants")-- class _StubLLMType(str, Enum):- CHAT = "chat"- VISION = "vision"- RERANK = "rerank"- ASR = "asr"- TTS = "tts"-- class _StubRetCode(int, Enum):- SUCCESS = 0- DATA_ERROR = 102- OPERATING_ERROR = 103- AUTHENTICATION_ERROR = 109-- class _StubStatusEnum(str, Enum):- VALID = "1"- INVALID = "0"-- common_constants_mod.LLMType = _StubLLMType- common_constants_mod.RetCode = _StubRetCode- common_constants_mod.StatusEnum = _StubStatusEnum- from common.constants import MAXIMUM_PAGE_NUMBER as _MPN- from common.constants import MAXIMUM_TASK_PAGE_NUMBER as _MTPN-- common_constants_mod.MAXIMUM_PAGE_NUMBER = _MPN- common_constants_mod.MAXIMUM_TASK_PAGE_NUMBER = _MTPN- monkeypatch.setitem(sys.modules, "common.constants", common_constants_mod)-- misc_utils_mod = ModuleType("common.misc_utils")- misc_utils_mod.get_uuid = lambda: "generated-chat-id"-- async def _thread_pool_exec(func, *args, **kwargs):- return func(*args, **kwargs)-- misc_utils_mod.thread_pool_exec = _thread_pool_exec- monkeypatch.setitem(sys.modules, "common.misc_utils", misc_utils_mod)-- settings_mod = ModuleType("common.settings")- settings_mod.STORAGE_IMPL = type("_StorageImpl", (), {"rm": staticmethod(lambda *_args, **_kwargs: None)})()- monkeypatch.setitem(sys.modules, "common.settings", settings_mod)-- dialog_service_mod = ModuleType("api.db.services.dialog_service")-- class _StubDialogService:- model = SimpleNamespace(- _meta=SimpleNamespace(- fields={- "id": None,- "tenant_id": None,- "name": None,- "description": None,- "icon": None,- "kb_ids": None,- "llm_id": None,- "llm_setting": None,- "prompt_config": None,- "similarity_threshold": None,- "vector_similarity_weight": None,- "top_n": None,- "top_k": None,- "rerank_id": None,- "meta_data_filter": None,- "created_by": None,- "create_time": None,- "create_date": None,- "update_time": None,- "update_date": None,- "status": None,- }- )- )-- @staticmethod- def query(**_kwargs):- return []-- @staticmethod- def save(**_kwargs):- return True-- @staticmethod- def get_by_id(_chat_id):- return False, None-- @staticmethod- def update_by_id(_chat_id, _payload):- return True-- @staticmethod- def get_by_tenant_ids(*_args, **_kwargs):- return [], 0-- dialog_service_mod.DialogService = _StubDialogService- dialog_service_mod.async_ask = lambda *_args, **_kwargs: None- dialog_service_mod.async_chat = lambda *_args, **_kwargs: None- dialog_service_mod.gen_mindmap = lambda *_args, **_kwargs: None- dialog_service_mod.rag_agent = lambda *_args, **_kwargs: None- monkeypatch.setitem(sys.modules, "api.db.services.dialog_service", dialog_service_mod)-- conversation_service_mod = ModuleType("api.db.services.conversation_service")-- class _StubConversationService:- @staticmethod- def query(**_kwargs):- return []-- @staticmethod- def get_list(*_args, **_kwargs):- return []-- @staticmethod- def get_by_id(_session_id):- return False, None-- @staticmethod- def update_by_id(_session_id, _payload):- return True-- @staticmethod- def delete_by_id(_session_id):- return True-- @staticmethod- def save(**_kwargs):- return True-- conversation_service_mod.ConversationService = _StubConversationService- conversation_service_mod.structure_answer = lambda *_args, **_kwargs: {}- monkeypatch.setitem(sys.modules, "api.db.services.conversation_service", conversation_service_mod)-- kb_service_mod = ModuleType("api.db.services.knowledgebase_service")-- class _StubKnowledgebaseService:- @staticmethod- def accessible(**_kwargs):- return []-- @staticmethod- def query(**_kwargs):- return []-- @staticmethod- def get_by_id(_kb_id):- return False, None-- kb_service_mod.KnowledgebaseService = _StubKnowledgebaseService- kb_service_mod.validate_dataset_embedding_models = lambda _kbs: None- monkeypatch.setitem(sys.modules, "api.db.services.knowledgebase_service", kb_service_mod)-- llm_service_mod = ModuleType("api.db.services.llm_service")- llm_service_mod.LLMBundle = lambda *_args, **_kwargs: None- llm_service_mod.resolve_llm_setting = lambda *_args, **_kwargs: {}- monkeypatch.setitem(sys.modules, "api.db.services.llm_service", llm_service_mod)-- search_service_mod = ModuleType("api.db.services.search_service")- search_service_mod.SearchService = SimpleNamespace()- monkeypatch.setitem(sys.modules, "api.db.services.search_service", search_service_mod)-- tenant_model_service_mod = ModuleType("api.db.joint_services.tenant_model_service")- tenant_model_service_mod.get_model_config_from_provider_instance = lambda *_args, **_kwargs: {}-- def _get_model_config_by_id(_tenant_id, _model_type, model_ref):- if model_ref == "tenant-llm-id":- return {}- raise LookupError(f"unknown tenant model id: {model_ref}")-- tenant_model_service_mod.get_model_config_by_id = _get_model_config_by_id- tenant_model_service_mod.resolve_model_id = lambda _tenant_id, _model_type, model_name: model_name- tenant_model_service_mod.get_composite_model_name_by_id = lambda model_id: model_id- tenant_model_service_mod.resolve_model_config = lambda *_args, **_kwargs: {}- tenant_model_service_mod.get_tenant_default_model_by_type = lambda *_args, **_kwargs: {}- tenant_model_service_mod.get_api_key = lambda *_args, **_kwargs: SimpleNamespace(id=1)- tenant_model_service_mod.split_model_name = lambda model: (model.split("@")[0], "default", "factory")- monkeypatch.setitem(sys.modules, "api.db.joint_services.tenant_model_service", tenant_model_service_mod)-- user_service_mod = ModuleType("api.db.services.user_service")-- class _StubTenantService:- @staticmethod- def get_by_id(_tenant_id):- return True, SimpleNamespace(llm_id="glm-4", tenant_llm_id="tenant-llm-id")-- @staticmethod- def get_joined_tenants_by_user_id(_user_id):- return [{"tenant_id": "tenant-1"}, {"tenant_id": "team-tenant-2"}]-- class _StubUserTenantService:- @staticmethod- def query(**_kwargs):- return []-- user_service_mod.UserService = type("UserService", (), {})- user_service_mod.TenantService = _StubTenantService- user_service_mod.UserTenantService = _StubUserTenantService- monkeypatch.setitem(sys.modules, "api.db.services.user_service", user_service_mod)-- chunk_feedback_service_mod = ModuleType("api.db.services.chunk_feedback_service")- chunk_feedback_service_mod.ChunkFeedbackService = type(- "ChunkFeedbackService",- (),- {"apply_feedback": staticmethod(lambda **_kwargs: {"success_count": 0, "fail_count": 0, "chunk_ids": []})},- )- monkeypatch.setitem(sys.modules, "api.db.services.chunk_feedback_service", chunk_feedback_service_mod)-- api_utils_mod = ModuleType("api.utils.api_utils")-- def _check_duplicate_ids(ids, label):- counts = {}- for item in ids or []:- counts[item] = counts.get(item, 0) + 1- duplicate_messages = [f"Duplicate {label} ids: {item}" for item, count in counts.items() if count > 1]- return list(dict.fromkeys(ids or [])), duplicate_messages-- api_utils_mod.check_duplicate_ids = _check_duplicate_ids- api_utils_mod.get_data_error_result = lambda message="": {"code": 102, "data": None, "message": message}- api_utils_mod.get_json_result = lambda data=None, message="", code=0: {"code": code, "data": data, "message": message}- api_utils_mod.get_request_json = lambda: _AwaitableValue({})- api_utils_mod.server_error_response = lambda ex: {"code": 500, "data": None, "message": str(ex)}- api_utils_mod.validate_request = lambda *_args, **_kwargs: lambda func: func- monkeypatch.setitem(sys.modules, "api.utils.api_utils", api_utils_mod)-- rag_pkg = ModuleType("rag")- rag_pkg.__path__ = [str(repo_root / "rag")]- monkeypatch.setitem(sys.modules, "rag", rag_pkg)-- rag_prompts_pkg = ModuleType("rag.prompts")- rag_prompts_pkg.__path__ = [str(repo_root / "rag" / "prompts")]- monkeypatch.setitem(sys.modules, "rag.prompts", rag_prompts_pkg)-- rag_prompts_generator_mod = ModuleType("rag.prompts.generator")- rag_prompts_generator_mod.chunks_format = lambda reference: reference.get("chunks", []) if isinstance(reference, dict) else []- monkeypatch.setitem(sys.modules, "rag.prompts.generator", rag_prompts_generator_mod)-- rag_prompts_template_mod = ModuleType("rag.prompts.template")- rag_prompts_template_mod.load_prompt = lambda *_args, **_kwargs: ""- monkeypatch.setitem(sys.modules, "rag.prompts.template", rag_prompts_template_mod)-- spec = importlib.util.spec_from_file_location(module_name, module_path)- module = importlib.util.module_from_spec(spec)- module.manager = _DummyManager()- monkeypatch.setitem(sys.modules, module_name, module)- spec.loader.exec_module(module)- return module---def _set_route_unit_request_json(monkeypatch, module, payload):- monkeypatch.setattr(module, "get_request_json", lambda: _AwaitableValue(deepcopy(payload)))---def test_chat_create_dataset_ids_contract(rest_client, clear_chats, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- cases = [- ("empty dataset_ids", [], 0, "", []),- ("owned parsed dataset", [dataset_id], 0, "", [dataset_id]),- ("invalid dataset id", ["invalid_dataset_id"], 102, "You don't own the dataset invalid_dataset_id", None),- ("dataset_ids wrong type", "invalid_dataset_id", 102, "`dataset_ids` should be a list.", None),- ]-- for index, (scenario_name, dataset_ids, expected_code, expected_message, expected_dataset_ids) in enumerate(cases, start=1):- res = rest_client.post(- "/chats",- json={"name": f"restful_chat_dataset_ids_{index}", "dataset_ids": dataset_ids},- )- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (scenario_name, payload)- if expected_code == 0:- assert payload["data"]["dataset_ids"] == expected_dataset_ids, (scenario_name, payload)- else:- assert payload["message"] == expected_message, (scenario_name, payload)---def test_chat_create_avatar_contract(rest_client, clear_chats, tmp_path):- image_path = create_image_file(tmp_path / "restful_chat_avatar.png")- encoded_avatar = encode_avatar(image_path)-- res = rest_client.post(- "/chats",- json={"name": "restful_chat_avatar", "dataset_ids": [], "icon": encoded_avatar},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert payload["data"]["icon"] == encoded_avatar, payload---def test_chat_create_llm_contract(rest_client, clear_chats, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- cases = [- ("default llm", {}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {}),- ("explicit llm_id", {"llm_id": "glm-4"}, 102, "`llm_id` glm-4 doesn't exist", None, None),- ("unknown llm_id", {"llm_id": "unknown"}, 102, "`llm_id` unknown doesn't exist", None, None),- ("temperature zero", {"llm_setting": {"temperature": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": 0}),- ("temperature one", {"llm_setting": {"temperature": 1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": 1}),- ("temperature negative one", {"llm_setting": {"temperature": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": -1}),- ("temperature ten", {"llm_setting": {"temperature": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": 10}),- ("temperature string", {"llm_setting": {"temperature": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": "a"}),- ("top_p zero", {"llm_setting": {"top_p": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": 0}),- ("top_p one", {"llm_setting": {"top_p": 1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": 1}),- ("top_p negative one", {"llm_setting": {"top_p": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": -1}),- ("top_p ten", {"llm_setting": {"top_p": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": 10}),- ("top_p string", {"llm_setting": {"top_p": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": "a"}),- ("presence_penalty zero", {"llm_setting": {"presence_penalty": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": 0}),- ("presence_penalty one", {"llm_setting": {"presence_penalty": 1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": 1}),- ("presence_penalty negative one", {"llm_setting": {"presence_penalty": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": -1}),- ("presence_penalty ten", {"llm_setting": {"presence_penalty": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": 10}),- ("presence_penalty string", {"llm_setting": {"presence_penalty": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": "a"}),- ("frequency_penalty zero", {"llm_setting": {"frequency_penalty": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": 0}),- ("frequency_penalty one", {"llm_setting": {"frequency_penalty": 1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": 1}),- ("frequency_penalty negative one", {"llm_setting": {"frequency_penalty": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": -1}),- ("frequency_penalty ten", {"llm_setting": {"frequency_penalty": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": 10}),- ("frequency_penalty string", {"llm_setting": {"frequency_penalty": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": "a"}),- ("max_token zero", {"llm_setting": {"max_token": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": 0}),- ("max_token 1024", {"llm_setting": {"max_token": 1024}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": 1024}),- ("max_token negative one", {"llm_setting": {"max_token": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": -1}),- ("max_token ten", {"llm_setting": {"max_token": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": 10}),- ("max_token string", {"llm_setting": {"max_token": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": "a"}),- ("unknown llm setting key", {"llm_setting": {"unknown": "unknown"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"unknown": "unknown"}),- ]-- for index, (scenario_name, extra_payload, expected_code, expected_message, expected_llm_id, expected_llm_setting) in enumerate(cases, start=1):- payload = {- "name": f"restful_chat_llm_{index}",- "dataset_ids": [dataset_id],- }- payload.update(extra_payload)- res = rest_client.post("/chats", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code == 0:- actual_llm_id = body["data"]["llm_id"]- tenant_llm_id = body["data"].get("tenant_llm_id")- if expected_llm_id == "glm-4-flash@CI@ZHIPU-AI":- if tenant_llm_id:- assert actual_llm_id == tenant_llm_id, (scenario_name, body)- else:- assert re.fullmatch(r"[0-9a-f]{32}", actual_llm_id), (scenario_name, body)- else:- assert actual_llm_id == expected_llm_id, (scenario_name, body)- assert body["data"]["llm_setting"] == expected_llm_setting, (scenario_name, body)- else:- assert body["message"] == expected_message, (scenario_name, body)---def test_chat_create_prompt_contract(rest_client, clear_chats):- cases = [- (- "default prompt config",- {},- {- ("similarity_threshold",): 0.1,- ("vector_similarity_weight",): 0.3,- ("top_n",): 6,- ("rerank_id",): "",- ("prompt_config", "parameters"): [{"key": "knowledge", "optional": False}],- ("prompt_config", "empty_response"): DEFAULT_CHAT_EMPTY_RESPONSE,- ("prompt_config", "prologue"): DEFAULT_CHAT_PROLOGUE,- ("prompt_config", "quote"): True,- ("prompt_config", "system"): "",- },- ),- ("similarity_threshold zero", {"similarity_threshold": 0}, {("similarity_threshold",): 0}),- ("similarity_threshold one", {"similarity_threshold": 1}, {("similarity_threshold",): 1}),- ("similarity_threshold negative one", {"similarity_threshold": -1}, {("similarity_threshold",): -1.0}),- ("similarity_threshold ten", {"similarity_threshold": 10}, {("similarity_threshold",): 10.0}),- ("similarity_threshold string", {"similarity_threshold": "a"}, {("similarity_threshold",): 0.0}),- ("vector_similarity_weight one", {"vector_similarity_weight": 1}, {("vector_similarity_weight",): 1}),- ("vector_similarity_weight zero", {"vector_similarity_weight": 0}, {("vector_similarity_weight",): 0}),- ("vector_similarity_weight two", {"vector_similarity_weight": 2}, {("vector_similarity_weight",): 2.0}),- ("vector_similarity_weight negative nine", {"vector_similarity_weight": -9}, {("vector_similarity_weight",): -9.0}),- ("vector_similarity_weight string", {"vector_similarity_weight": "a"}, {("vector_similarity_weight",): 0.0}),- ("empty prompt parameters", {"prompt_config": {"parameters": []}}, {("prompt_config", "parameters"): []}),- ("top_n zero", {"top_n": 0}, {("top_n",): 0}),- ("top_n one", {"top_n": 1}, {("top_n",): 1}),- ("top_n negative one", {"top_n": -1}, {("top_n",): -1}),- ("top_n ten", {"top_n": 10}, {("top_n",): 10}),- ("top_n string", {"top_n": "a"}, {("top_n",): 0}),- ("empty_response plain text", {"prompt_config": {"empty_response": "Hello World"}}, {("prompt_config", "empty_response"): "Hello World"}),- ("empty_response empty string", {"prompt_config": {"empty_response": ""}}, {("prompt_config", "empty_response"): ""}),- ("empty_response punctuation", {"prompt_config": {"empty_response": "!@#$%^&*()"}}, {("prompt_config", "empty_response"): "!@#$%^&*()"}),- ("empty_response chinese text", {"prompt_config": {"empty_response": "中文测试"}}, {("prompt_config", "empty_response"): "中文测试"}),- ("empty_response integer", {"prompt_config": {"empty_response": 123}}, {("prompt_config", "empty_response"): 123}),- ("empty_response boolean", {"prompt_config": {"empty_response": True}}, {("prompt_config", "empty_response"): True}),- ("empty_response space", {"prompt_config": {"empty_response": " "}}, {("prompt_config", "empty_response"): " "}),- ("prologue plain text", {"prompt_config": {"prologue": "Hello World"}}, {("prompt_config", "prologue"): "Hello World"}),- ("prologue empty string", {"prompt_config": {"prologue": ""}}, {("prompt_config", "prologue"): ""}),- ("prologue punctuation", {"prompt_config": {"prologue": "!@#$%^&*()"}}, {("prompt_config", "prologue"): "!@#$%^&*()"}),- ("prologue chinese text", {"prompt_config": {"prologue": "中文测试"}}, {("prompt_config", "prologue"): "中文测试"}),- ("prologue integer", {"prompt_config": {"prologue": 123}}, {("prompt_config", "prologue"): 123}),- ("prologue boolean", {"prompt_config": {"prologue": True}}, {("prompt_config", "prologue"): True}),- ("prologue space", {"prompt_config": {"prologue": " "}}, {("prompt_config", "prologue"): " "}),- ("quote true", {"prompt_config": {"quote": True}}, {("prompt_config", "quote"): True}),- ("quote false", {"prompt_config": {"quote": False}}, {("prompt_config", "quote"): False}),- ("system prompt with knowledge prefix", {"prompt_config": {"system": "Hello World {knowledge}"}}, {("prompt_config", "system"): "Hello World {knowledge}"}),- ("system prompt only knowledge", {"prompt_config": {"system": "{knowledge}"}}, {("prompt_config", "system"): "{knowledge}"}),- ("system prompt punctuation", {"prompt_config": {"system": "!@#$%^&*() {knowledge}"}}, {("prompt_config", "system"): "!@#$%^&*() {knowledge}"}),- ("system prompt chinese text", {"prompt_config": {"system": "中文测试 {knowledge}"}}, {("prompt_config", "system"): "中文测试 {knowledge}"}),- ("system prompt plain text", {"prompt_config": {"system": "Hello World"}}, {("prompt_config", "system"): "Hello World"}),- (- "system prompt with explicit empty parameters",- {"prompt_config": {"system": "Hello World", "parameters": []}},- {("prompt_config", "system"): "Hello World", ("prompt_config", "parameters"): []},- ),- ("system prompt integer", {"prompt_config": {"system": 123}}, {("prompt_config", "system"): 123}),- ("system prompt boolean", {"prompt_config": {"system": True}}, {("prompt_config", "system"): True}),- ("unknown prompt_config key", {"prompt_config": {"unknown": "unknown"}}, {("prompt_config", "unknown"): "unknown"}),- ]-- for index, (scenario_name, extra_payload, expected_values) in enumerate(cases, start=1):- res = rest_client.post(- "/chats",- json={"name": f"restful_chat_prompt_{index}", "dataset_ids": [], **extra_payload},- )- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == 0, (scenario_name, payload)- for path, expected_value in expected_values.items():- assert _get_nested(payload["data"], path) == expected_value, (scenario_name, path, payload)---def test_chat_create_additional_guards_contract(rest_client, clear_chats):- cases = [- ("reject tenant_id override", {"tenant_id": "tenant-should-not-pass"}, "`tenant_id` must not be provided"),- ("reject unknown rerank_id", {"rerank_id": "unknown-rerank-model"}, "`rerank_id` unknown-rerank-model doesn't exist"),- ]-- for index, (scenario_name, extra_payload, expected_message) in enumerate(cases, start=1):- res = rest_client.post(- "/chats",- json={"name": f"restful_chat_guard_{index}", "dataset_ids": [], **extra_payload},- )- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == 102, (scenario_name, payload)- assert expected_message in payload["message"], (scenario_name, payload)---def test_chat_create_rejects_unparsed_document(rest_client, clear_chats, create_document):- dataset_id, _ = create_document()- res = rest_client.post(- "/chats",- json={"name": "restful_chat_unparsed_document", "dataset_ids": [dataset_id]},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert "doesn't own parsed file" in payload["message"], payload---def test_chat_update_name_contract(rest_client, clear_chats):- duplicate_res = rest_client.post("/chats", json={"name": "restful_chat_update_duplicate", "dataset_ids": []})- assert duplicate_res.status_code == 200- duplicate_payload = duplicate_res.json()- assert duplicate_payload["code"] == 0, duplicate_payload-- target_res = rest_client.post("/chats", json={"name": "restful_chat_update_name_target", "dataset_ids": []})- assert target_res.status_code == 200- target_payload = target_res.json()- assert target_payload["code"] == 0, target_payload- chat_id = target_payload["data"]["id"]-- cases = [- ("valid name", {"name": "valid_name"}, 0, "", "valid_name"),- (- "name too long",- {"name": "a" * (CHAT_ASSISTANT_NAME_LIMIT + 1)},- 102,- f"chat name length is {CHAT_ASSISTANT_NAME_LIMIT + 1} which is larger than {CHAT_ASSISTANT_NAME_LIMIT}",- None,- ),- ("name wrong type", {"name": 1}, 102, "chat name must be a string", None),- ("name empty", {"name": ""}, 102, "`name` cannot be empty", None),- ("duplicate lowercase", {"name": "restful_chat_update_duplicate"}, 102, "duplicated chat name", None),- ("duplicate uppercase", {"name": "RESTFUL_CHAT_UPDATE_DUPLICATE"}, 102, "duplicated chat name", None),- ]-- for scenario_name, patch_payload, expected_code, expected_message, expected_name in cases:- res = rest_client.patch(f"/chats/{chat_id}", json=patch_payload)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (scenario_name, payload)- if expected_code == 0:- get_res = rest_client.get(f"/chats/{chat_id}")- assert get_res.status_code == 200, (scenario_name, get_res.text)- get_payload = get_res.json()- assert get_payload["code"] == 0, (scenario_name, get_payload)- assert get_payload["data"]["name"] == expected_name, (scenario_name, get_payload)- else:- assert payload["message"] == expected_message, (scenario_name, payload)---def test_chat_update_dataset_ids_contract(rest_client, clear_chats, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- target_res = rest_client.post("/chats", json={"name": "restful_chat_update_dataset_target", "dataset_ids": []})- assert target_res.status_code == 200- target_payload = target_res.json()- assert target_payload["code"] == 0, target_payload- chat_id = target_payload["data"]["id"]-- cases = [- ("empty dataset_ids", [], 0, "", []),- ("owned parsed dataset", [dataset_id], 0, "", [dataset_id]),- ("invalid dataset id", ["invalid_dataset_id"], 102, "You don't own the dataset invalid_dataset_id", None),- ("dataset_ids wrong type", "invalid_dataset_id", 102, "`dataset_ids` should be a list.", None),- ]-- for scenario_name, dataset_ids, expected_code, expected_message, expected_dataset_ids in cases:- res = rest_client.put(- f"/chats/{chat_id}",- json={"name": "ragflow test", "dataset_ids": dataset_ids},- )- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (scenario_name, payload)- if expected_code == 0:- get_res = rest_client.get(f"/chats/{chat_id}")- assert get_res.status_code == 200, (scenario_name, get_res.text)- get_payload = get_res.json()- assert get_payload["code"] == 0, (scenario_name, get_payload)- assert get_payload["data"]["name"] == "ragflow test", (scenario_name, get_payload)- assert get_payload["data"]["dataset_ids"] == expected_dataset_ids, (scenario_name, get_payload)- else:- assert payload["message"] == expected_message, (scenario_name, payload)---def test_chat_update_avatar_contract(rest_client, clear_chats, ensure_parsed_document, tmp_path):- dataset_id, _ = ensure_parsed_document()- create_res = rest_client.post("/chats", json={"name": "restful_chat_update_avatar_target", "dataset_ids": []})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- chat_id = create_payload["data"]["id"]-- image_path = create_image_file(tmp_path / "restful_chat_update_avatar.png")- encoded_avatar = encode_avatar(image_path)-- res = rest_client.put(- f"/chats/{chat_id}",- json={"name": "avatar_test", "icon": encoded_avatar, "dataset_ids": [dataset_id]},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload-- get_res = rest_client.get(f"/chats/{chat_id}")- assert get_res.status_code == 200- get_payload = get_res.json()- assert get_payload["code"] == 0, get_payload- assert get_payload["data"]["name"] == "avatar_test", get_payload- assert get_payload["data"]["icon"] == encoded_avatar, get_payload- assert get_payload["data"]["dataset_ids"] == [dataset_id], get_payload---def test_chat_update_llm_contract(rest_client, clear_chats, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- cases = [- ("default llm", {}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {}),- ("explicit llm_id", {"llm_id": "glm-4"}, 102, "`llm_id` glm-4 doesn't exist", None, None),- ("unknown llm_id", {"llm_id": "unknown"}, 102, "`llm_id` unknown doesn't exist", None, None),- ("temperature zero", {"llm_setting": {"temperature": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": 0}),- ("temperature one", {"llm_setting": {"temperature": 1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": 1}),- ("temperature negative one", {"llm_setting": {"temperature": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": -1}),- ("temperature ten", {"llm_setting": {"temperature": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": 10}),- ("temperature string", {"llm_setting": {"temperature": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"temperature": "a"}),- ("top_p zero", {"llm_setting": {"top_p": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": 0}),- ("top_p one", {"llm_setting": {"top_p": 1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": 1}),- ("top_p negative one", {"llm_setting": {"top_p": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": -1}),- ("top_p ten", {"llm_setting": {"top_p": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": 10}),- ("top_p string", {"llm_setting": {"top_p": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"top_p": "a"}),- ("presence_penalty zero", {"llm_setting": {"presence_penalty": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": 0}),- ("presence_penalty one", {"llm_setting": {"presence_penalty": 1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": 1}),- ("presence_penalty negative one", {"llm_setting": {"presence_penalty": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": -1}),- ("presence_penalty ten", {"llm_setting": {"presence_penalty": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": 10}),- ("presence_penalty string", {"llm_setting": {"presence_penalty": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"presence_penalty": "a"}),- ("frequency_penalty zero", {"llm_setting": {"frequency_penalty": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": 0}),- ("frequency_penalty one", {"llm_setting": {"frequency_penalty": 1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": 1}),- ("frequency_penalty negative one", {"llm_setting": {"frequency_penalty": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": -1}),- ("frequency_penalty ten", {"llm_setting": {"frequency_penalty": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": 10}),- ("frequency_penalty string", {"llm_setting": {"frequency_penalty": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"frequency_penalty": "a"}),- ("max_token zero", {"llm_setting": {"max_token": 0}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": 0}),- ("max_token 1024", {"llm_setting": {"max_token": 1024}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": 1024}),- ("max_token negative one", {"llm_setting": {"max_token": -1}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": -1}),- ("max_token ten", {"llm_setting": {"max_token": 10}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": 10}),- ("max_token string", {"llm_setting": {"max_token": "a"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"max_token": "a"}),- ("unknown llm setting key", {"llm_setting": {"unknown": "unknown"}}, 0, "", "glm-4-flash@CI@ZHIPU-AI", {"unknown": "unknown"}),- ]-- for index, (scenario_name, extra_payload, expected_code, expected_message, expected_llm_id, expected_llm_setting) in enumerate(cases, start=1):- create_res = rest_client.post(- "/chats",- json={"name": f"restful_chat_update_llm_target_{index}", "dataset_ids": [dataset_id]},- )- assert create_res.status_code == 200, (scenario_name, create_res.text)- create_payload = create_res.json()- assert create_payload["code"] == 0, (scenario_name, create_payload)- chat_id = create_payload["data"]["id"]-- updated_name = f"llm_test_{index}"- payload = {"name": updated_name, "dataset_ids": [dataset_id]}- payload.update(extra_payload)- res = rest_client.put(f"/chats/{chat_id}", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code == 0:- get_res = rest_client.get(f"/chats/{chat_id}")- assert get_res.status_code == 200, (scenario_name, get_res.text)- get_payload = get_res.json()- assert get_payload["code"] == 0, (scenario_name, get_payload)- assert get_payload["data"]["name"] == updated_name, (scenario_name, get_payload)- actual_llm_id = get_payload["data"]["llm_id"]- tenant_llm_id = get_payload["data"].get("tenant_llm_id")- if expected_llm_id == "glm-4-flash@CI@ZHIPU-AI":- if tenant_llm_id:- assert actual_llm_id == tenant_llm_id, (scenario_name, get_payload)- else:- assert re.fullmatch(r"[0-9a-f]{32}", actual_llm_id), (scenario_name, get_payload)- else:- assert actual_llm_id == expected_llm_id, (scenario_name, get_payload)- assert get_payload["data"]["llm_setting"] == expected_llm_setting, (scenario_name, get_payload)- else:- assert body["message"] == expected_message, (scenario_name, body)---def test_chat_update_prompt_contract(rest_client, clear_chats, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- cases = [- (- "default prompt config",- {},- {- ("similarity_threshold",): 0.1,- ("vector_similarity_weight",): 0.3,- ("top_n",): 6,- ("prompt_config", "parameters"): [{"key": "knowledge", "optional": False}],- ("prompt_config", "empty_response"): DEFAULT_CHAT_EMPTY_RESPONSE,- ("prompt_config", "prologue"): DEFAULT_CHAT_PROLOGUE,- ("prompt_config", "quote"): True,- ("prompt_config", "system"): DEFAULT_CHAT_SYSTEM_PROMPT,- },- ),- ("similarity_threshold zero", {"similarity_threshold": 0}, {("similarity_threshold",): 0}),- ("similarity_threshold one", {"similarity_threshold": 1}, {("similarity_threshold",): 1}),- ("similarity_threshold negative one", {"similarity_threshold": -1}, {("similarity_threshold",): -1.0}),- ("similarity_threshold ten", {"similarity_threshold": 10}, {("similarity_threshold",): 10.0}),- ("similarity_threshold string", {"similarity_threshold": "a"}, {("similarity_threshold",): 0.0}),- ("vector_similarity_weight zero", {"vector_similarity_weight": 0}, {("vector_similarity_weight",): 0}),- ("vector_similarity_weight one", {"vector_similarity_weight": 1}, {("vector_similarity_weight",): 1}),- ("vector_similarity_weight negative one", {"vector_similarity_weight": -1}, {("vector_similarity_weight",): -1.0}),- ("vector_similarity_weight ten", {"vector_similarity_weight": 10}, {("vector_similarity_weight",): 10.0}),- ("vector_similarity_weight string", {"vector_similarity_weight": "a"}, {("vector_similarity_weight",): 0.0}),- ("empty prompt parameters", {"prompt_config": {"parameters": []}}, {("prompt_config", "parameters"): []}),- ("top_n zero", {"top_n": 0}, {("top_n",): 0}),- ("top_n one", {"top_n": 1}, {("top_n",): 1}),- ("top_n negative one", {"top_n": -1}, {("top_n",): -1}),- ("top_n ten", {"top_n": 10}, {("top_n",): 10}),- ("top_n string", {"top_n": "a"}, {("top_n",): 0}),- ("empty_response plain text", {"prompt_config": {"empty_response": "Hello World"}}, {("prompt_config", "empty_response"): "Hello World"}),- ("empty_response empty string", {"prompt_config": {"empty_response": ""}}, {("prompt_config", "empty_response"): ""}),- ("empty_response punctuation", {"prompt_config": {"empty_response": "!@#$%^&*()"}}, {("prompt_config", "empty_response"): "!@#$%^&*()"}),- ("empty_response chinese text", {"prompt_config": {"empty_response": "中文测试"}}, {("prompt_config", "empty_response"): "中文测试"}),- ("empty_response integer", {"prompt_config": {"empty_response": 123}}, {("prompt_config", "empty_response"): 123}),- ("empty_response boolean", {"prompt_config": {"empty_response": True}}, {("prompt_config", "empty_response"): True}),- ("empty_response space", {"prompt_config": {"empty_response": " "}}, {("prompt_config", "empty_response"): " "}),- ("prologue plain text", {"prompt_config": {"prologue": "Hello World"}}, {("prompt_config", "prologue"): "Hello World"}),- ("prologue empty string", {"prompt_config": {"prologue": ""}}, {("prompt_config", "prologue"): ""}),- ("prologue punctuation", {"prompt_config": {"prologue": "!@#$%^&*()"}}, {("prompt_config", "prologue"): "!@#$%^&*()"}),- ("prologue chinese text", {"prompt_config": {"prologue": "中文测试"}}, {("prompt_config", "prologue"): "中文测试"}),- ("prologue integer", {"prompt_config": {"prologue": 123}}, {("prompt_config", "prologue"): 123}),- ("prologue boolean", {"prompt_config": {"prologue": True}}, {("prompt_config", "prologue"): True}),- ("prologue space", {"prompt_config": {"prologue": " "}}, {("prompt_config", "prologue"): " "}),- ("quote true", {"prompt_config": {"quote": True}}, {("prompt_config", "quote"): True}),- ("quote false", {"prompt_config": {"quote": False}}, {("prompt_config", "quote"): False}),- ("system prompt with knowledge prefix", {"prompt_config": {"system": "Hello World {knowledge}"}}, {("prompt_config", "system"): "Hello World {knowledge}"}),- ("system prompt only knowledge", {"prompt_config": {"system": "{knowledge}"}}, {("prompt_config", "system"): "{knowledge}"}),- ("system prompt punctuation", {"prompt_config": {"system": "!@#$%^&*() {knowledge}"}}, {("prompt_config", "system"): "!@#$%^&*() {knowledge}"}),- ("system prompt chinese text", {"prompt_config": {"system": "中文测试 {knowledge}"}}, {("prompt_config", "system"): "中文测试 {knowledge}"}),- ("system prompt plain text", {"prompt_config": {"system": "Hello World"}}, {("prompt_config", "system"): "Hello World"}),- (- "system prompt with explicit empty parameters",- {"prompt_config": {"system": "Hello World", "parameters": []}},- {("prompt_config", "system"): "Hello World", ("prompt_config", "parameters"): []},- ),- ("system prompt integer", {"prompt_config": {"system": 123}}, {("prompt_config", "system"): 123}),- ("system prompt boolean", {"prompt_config": {"system": True}}, {("prompt_config", "system"): True}),- ("unknown prompt key", {"unknown": "unknown"}, {}),- ]-- for index, (scenario_name, extra_payload, expected_values) in enumerate(cases, start=1):- create_res = rest_client.post(- "/chats",- json={"name": f"restful_chat_update_prompt_target_{index}", "dataset_ids": [dataset_id]},- )- assert create_res.status_code == 200, (scenario_name, create_res.text)- create_payload = create_res.json()- assert create_payload["code"] == 0, (scenario_name, create_payload)- chat_id = create_payload["data"]["id"]-- updated_name = f"prompt_test_{index}"- res = rest_client.put(- f"/chats/{chat_id}",- json={"name": updated_name, "dataset_ids": [dataset_id], **extra_payload},- )- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == 0, (scenario_name, payload)-- get_res = rest_client.get(f"/chats/{chat_id}")- assert get_res.status_code == 200, (scenario_name, get_res.text)- get_payload = get_res.json()- assert get_payload["code"] == 0, (scenario_name, get_payload)- assert get_payload["data"]["name"] == updated_name, (scenario_name, get_payload)- assert get_payload["data"]["dataset_ids"] == [dataset_id], (scenario_name, get_payload)- for path, expected_value in expected_values.items():- assert _get_nested(get_payload["data"], path) == expected_value, (scenario_name, path, get_payload)---def test_chat_update_mapping_and_validation_branches_p2(rest_client, clear_chats):- duplicate_res = rest_client.post("/chats", json={"name": "restful_chat_update_mapping_duplicate", "dataset_ids": []})- assert duplicate_res.status_code == 200- duplicate_payload = duplicate_res.json()- assert duplicate_payload["code"] == 0, duplicate_payload-- target_res = rest_client.post("/chats", json={"name": "restful_chat_update_mapping_target", "dataset_ids": []})- assert target_res.status_code == 200- target_payload = target_res.json()- assert target_payload["code"] == 0, target_payload- chat_id = target_payload["data"]["id"]- original_parameters = target_payload["data"]["prompt_config"]["parameters"]-- duplicate_create_res = rest_client.post(- "/chats",- json={- "name": "restful_chat_update_mapping_duplicate_parameters",- "dataset_ids": [],- "prompt_config": {"parameters": [{"key": "knowledge"}, {"key": "knowledge"}]},- },- )- assert duplicate_create_res.status_code == 200- duplicate_create_payload = duplicate_create_res.json()- assert duplicate_create_payload["code"] == 102, duplicate_create_payload- assert duplicate_create_payload["message"] == "`parameters` contains duplicate key: knowledge", duplicate_create_payload-- duplicate_parameters_res = rest_client.put(- f"/chats/{chat_id}",- json={"prompt_config": {"parameters": [{"key": "knowledge"}, {"key": "knowledge"}]}},- )- assert duplicate_parameters_res.status_code == 200- duplicate_parameters_payload = duplicate_parameters_res.json()- assert duplicate_parameters_payload["code"] == 102, duplicate_parameters_payload- assert duplicate_parameters_payload["message"] == "`parameters` contains duplicate key: knowledge", duplicate_parameters_payload- get_after_put_res = rest_client.get(f"/chats/{chat_id}")- assert get_after_put_res.json()["data"]["prompt_config"]["parameters"] == original_parameters-- duplicate_parameters_patch_res = rest_client.patch(- f"/chats/{chat_id}",- json={"prompt_config": {"parameters": [{"key": "knowledge"}, {"key": "knowledge"}]}},- )- assert duplicate_parameters_patch_res.status_code == 200- duplicate_parameters_patch_payload = duplicate_parameters_patch_res.json()- assert duplicate_parameters_patch_payload["code"] == 102, duplicate_parameters_patch_payload- assert duplicate_parameters_patch_payload["message"] == "`parameters` contains duplicate key: knowledge", duplicate_parameters_patch_payload- get_after_patch_res = rest_client.get(f"/chats/{chat_id}")- assert get_after_patch_res.json()["data"]["prompt_config"]["parameters"] == original_parameters-- unauthorized = rest_client.patch("/chats/invalid-chat-id", json={"name": "anything"})- assert unauthorized.status_code == 200- unauthorized_payload = unauthorized.json()- assert unauthorized_payload["code"] == 404, unauthorized_payload- assert unauthorized_payload["message"] == "Resource not found", unauthorized_payload-- quote_res = rest_client.patch(f"/chats/{chat_id}", json={"prompt_config": {"quote": False}})- assert quote_res.status_code == 200- quote_payload = quote_res.json()- assert quote_payload["code"] == 0, quote_payload- assert quote_payload["data"]["prompt_config"]["quote"] is False, quote_payload-- invalid_llm_res = rest_client.patch(- f"/chats/{chat_id}",- json={"llm_id": "unknown-llm-model", "llm_setting": {"model_type": "chat"}},- )- assert invalid_llm_res.status_code == 200- invalid_llm_payload = invalid_llm_res.json()- assert invalid_llm_payload["code"] == 102, invalid_llm_payload- assert "`llm_id` unknown-llm-model doesn't exist" in invalid_llm_payload["message"], invalid_llm_payload-- invalid_rerank_res = rest_client.patch(f"/chats/{chat_id}", json={"rerank_id": "unknown-rerank-model"})- assert invalid_rerank_res.status_code == 200- invalid_rerank_payload = invalid_rerank_res.json()- assert invalid_rerank_payload["code"] == 102, invalid_rerank_payload- assert "`rerank_id` unknown-rerank-model doesn't exist" in invalid_rerank_payload["message"], invalid_rerank_payload-- empty_name_res = rest_client.patch(f"/chats/{chat_id}", json={"name": ""})- assert empty_name_res.status_code == 200- empty_name_payload = empty_name_res.json()- assert empty_name_payload["code"] == 102, empty_name_payload- assert empty_name_payload["message"] == "`name` cannot be empty", empty_name_payload-- duplicate_name_res = rest_client.patch(f"/chats/{chat_id}", json={"name": "restful_chat_update_mapping_duplicate"})- assert duplicate_name_res.status_code == 200- duplicate_name_payload = duplicate_name_res.json()- assert duplicate_name_payload["code"] == 102, duplicate_name_payload- assert duplicate_name_payload["message"] == "duplicated chat name", duplicate_name_payload-- prompt_without_placeholder_res = rest_client.patch(- f"/chats/{chat_id}",- json={"prompt_config": {"system": "No required placeholder", "parameters": [{"key": "knowledge", "optional": False}]}},- )- assert prompt_without_placeholder_res.status_code == 200- prompt_without_placeholder_payload = prompt_without_placeholder_res.json()- assert prompt_without_placeholder_payload["code"] == 0, prompt_without_placeholder_payload-- icon_res = rest_client.patch(f"/chats/{chat_id}", json={"icon": "raw-avatar-value"})- assert icon_res.status_code == 200- icon_payload = icon_res.json()- assert icon_payload["code"] == 0, icon_payload-- get_res = rest_client.get(f"/chats/{chat_id}")- assert get_res.status_code == 200- get_payload = get_res.json()- assert get_payload["code"] == 0, get_payload- assert get_payload["data"]["prompt_config"]["system"] == "No required placeholder", get_payload- assert get_payload["data"]["prompt_config"]["parameters"] == [{"key": "knowledge", "optional": False}], get_payload- assert get_payload["data"]["icon"] == "raw-avatar-value", get_payload---def test_chat_update_rejects_unparsed_document(rest_client, clear_chats, create_document):- dataset_id, _ = create_document()- create_res = rest_client.post("/chats", json={"name": "restful_chat_update_unparsed_target", "dataset_ids": []})- assert create_res.status_code == 200, create_res.text- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- chat_id = create_payload["data"]["id"]-- res = rest_client.patch(f"/chats/{chat_id}", json={"dataset_ids": [dataset_id]})- assert res.status_code == 200, res.text- payload = res.json()- assert payload["code"] == 102, payload- assert "doesn't own parsed file" in payload["message"], payloaddiff --git a/test/testcases/restful_api/test_chunks.py b/test/testcases/restful_api/test_chunks.pydeleted file mode 100644index 0b9ba1301d..0000000000--- a/test/testcases/restful_api/test_chunks.py+++ /dev/null@@ -1,831 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--from concurrent.futures import ThreadPoolExecutor-import os-import pytest-from test.testcases.configs import INVALID_API_TOKEN, INVALID_ID_32-from test.testcases.restful_api.helpers.assertions import assert_auth_error-from test.testcases.restful_api.helpers.client import RestClient-from test.testcases.utils import wait_for---def _is_infinity_doc_engine(rest_client: RestClient) -> bool:- env_engine = (os.getenv("DOC_ENGINE") or "").strip().lower()- if env_engine:- return env_engine == "infinity"- try:- res = rest_client.get("/system/status")- if res.status_code != 200:- return False- payload = res.json()- if payload.get("code") != 0:- return False- engine = str(payload.get("data", {}).get("doc_engine", {}).get("type", "")).strip().lower()- return engine == "infinity"- except Exception:- return False---def _assert_created_chunk_id(payload):- chunk_id = payload["data"]["chunk"].get("id")- assert chunk_id, payload- assert isinstance(chunk_id, str), payload- assert chunk_id.strip(), payload- return chunk_id---@wait_for(10, 1, "Chunk indexing timeout in RESTful batch 09 tests")-def _chunk_count(rest_client, base_path, expected_total):- res = rest_client.get(base_path)- if res.status_code != 200:- return False- payload = res.json()- if payload["code"] != 0:- return False- return payload["data"]["total"] == expected_total and len(payload["data"]["chunks"]) == min(expected_total, 30)---def _reset_chunk_batch(rest_client, base_path, count=4):- cleanup_res = rest_client.delete(base_path, json={"chunk_ids": None, "delete_all": True})- assert cleanup_res.status_code == 200, cleanup_res.text- cleanup_payload = cleanup_res.json()- assert cleanup_payload["code"] == 0, cleanup_payload-- baseline_res = rest_client.post(base_path, json={"content": "ragflow test upload"})- assert baseline_res.status_code == 200, baseline_res.text- baseline_payload = baseline_res.json()- assert baseline_payload["code"] == 0, baseline_payload- baseline_id = _assert_created_chunk_id(baseline_payload)-- chunk_ids = []- for index in range(count):- res = rest_client.post(base_path, json={"content": f"chunk test {index}"})- assert res.status_code == 200, (index, res.text)- payload = res.json()- assert payload["code"] == 0, (index, payload)- chunk_ids.append(_assert_created_chunk_id(payload))-- _chunk_count(rest_client, base_path, count + 1)- return baseline_id, chunk_ids---def test_chunks_add_list_get_update_delete_cycle(rest_client, create_document):- dataset_id, document_id = create_document("chunk_cycle.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"-- add_res = rest_client.post(- base_path,- json={"content": "batch2 chunk content", "important_keywords": ["batch2"], "questions": ["what is batch2?"]},- )- assert add_res.status_code == 200- add_payload = add_res.json()- assert add_payload["code"] == 0, add_payload- chunk_id = _assert_created_chunk_id(add_payload)-- list_res = rest_client.get(base_path, params={"id": chunk_id})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert list_payload["data"]["total"] == 1, list_payload- assert list_payload["data"]["chunks"][0]["id"] == chunk_id, list_payload-- get_res = rest_client.get(f"{base_path}/{chunk_id}")- assert get_res.status_code == 200- get_payload = get_res.json()- assert get_payload["code"] == 0, get_payload- assert get_payload["data"]["id"] == chunk_id, get_payload-- update_res = rest_client.patch(- f"{base_path}/{chunk_id}",- json={"content": "batch2 chunk content updated"},- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload-- get_updated_res = rest_client.get(f"{base_path}/{chunk_id}")- assert get_updated_res.status_code == 200- get_updated_payload = get_updated_res.json()- assert get_updated_payload["code"] == 0, get_updated_payload- assert get_updated_payload["data"]["content_with_weight"] == "batch2 chunk content updated", get_updated_payload-- delete_candidate_res = rest_client.post(base_path, json={"content": "batch2 chunk content to delete"})- assert delete_candidate_res.status_code == 200- delete_candidate_payload = delete_candidate_res.json()- assert delete_candidate_payload["code"] == 0, delete_candidate_payload- delete_candidate_id = _assert_created_chunk_id(delete_candidate_payload)-- delete_res = rest_client.delete(base_path, json={"chunk_ids": [delete_candidate_id]})- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload-- deleted_list_res = rest_client.get(base_path, params={"id": delete_candidate_id})- assert deleted_list_res.status_code == 200- deleted_list_payload = deleted_list_res.json()- assert deleted_list_payload["code"] != 0, deleted_list_payload-- deleted_get_res = rest_client.get(f"{base_path}/{delete_candidate_id}")- assert deleted_get_res.status_code == 200- deleted_get_payload = deleted_get_res.json()- assert deleted_get_payload["code"] != 0, deleted_get_payload---def test_chunk_add_requires_auth(create_document):- dataset_id, document_id = create_document("chunk_add_auth.txt")- path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.post(path, json={"content": "chunk test"})- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_chunk_delete_requires_auth(create_document):- dataset_id, document_id = create_document("chunk_delete_auth.txt")- path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.delete(path, json={"chunk_ids": []})- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_chunk_list_requires_auth(create_document):- dataset_id, document_id = create_document("chunk_list_auth.txt")- path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.get(path)- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_chunks_add_requires_content(rest_client, create_document):- dataset_id, document_id = create_document("chunk_requires_content.txt")- res = rest_client.post(- f"/datasets/{dataset_id}/documents/{document_id}/chunks",- json={"content": " "},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert payload["message"] == "`content` is required", payload---def test_chunk_add_keyword_question_and_tag_contract(rest_client, create_document):- add_cases = [- (- "important keywords",- [- ({"content": "chunk test", "important_keywords": ["a", "b", "c"]}, 0, ""),- ({"content": "chunk test", "important_keywords": [""]}, 0, ""),- ({"content": "chunk test", "important_keywords": [1]}, 102, "`important_keywords` must be a list of strings"),- ({"content": "chunk test", "important_keywords": ["a", "a"]}, 0, ""),- ({"content": "chunk test", "important_keywords": "abc"}, 102, "`important_keywords` is required to be a list"),- ({"content": "chunk test", "important_keywords": 123}, 102, "`important_keywords` is required to be a list"),- ],- ),- (- "questions",- [- ({"content": "chunk test", "questions": ["a", "b", "c"]}, 0, ""),- ({"content": "chunk test", "questions": [""]}, 0, ""),- ({"content": "chunk test", "questions": [1]}, 102, "`questions` must be a list of strings"),- ({"content": "chunk test", "questions": ["a", "a"]}, 0, ""),- ({"content": "chunk test", "questions": "abc"}, 102, "`questions` is required to be a list"),- ({"content": "chunk test", "questions": 123}, 102, "`questions` is required to be a list"),- ],- ),- (- "tag_kwd",- [- ({"content": "chunk test", "tag_kwd": ["tag1", "tag2"]}, 0, ""),- ({"content": "chunk test", "tag_kwd": [""]}, 0, ""),- ({"content": "chunk test", "tag_kwd": [1]}, 102, "`tag_kwd` must be a list of strings"),- ({"content": "chunk test", "tag_kwd": ["tag", "tag"]}, 0, ""),- ({"content": "chunk test", "tag_kwd": "abc"}, 102, "`tag_kwd` is required to be a list"),- ({"content": "chunk test", "tag_kwd": 123}, 102, "`tag_kwd` is required to be a list"),- ],- ),- ]-- for group_index, (group_name, cases) in enumerate(add_cases):- dataset_id, document_id = create_document(f"chunk_add_contracts_{group_index}.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- for scenario_index, (payload, expected_code, expected_message) in enumerate(cases):- scenario_name = f"{group_name}-{scenario_index}"- before_payload = rest_client.get(base_path).json()- assert before_payload["code"] == 0, (scenario_name, before_payload)- before_total = before_payload["data"]["doc"]["chunk_count"]-- res = rest_client.post(base_path, json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code == 0:- chunk = body["data"]["chunk"]- assert chunk["dataset_id"] == dataset_id, (scenario_name, body)- assert chunk["document_id"] == document_id, (scenario_name, body)- assert chunk["content"] == payload["content"], (scenario_name, body)- if "important_keywords" in payload:- assert chunk["important_keywords"] == payload["important_keywords"], (scenario_name, body)- if "questions" in payload:- assert chunk["questions"] == [str(q).strip() for q in payload["questions"] if str(q).strip()], (scenario_name, body)- if "tag_kwd" in payload:- assert chunk["tag_kwd"] == payload["tag_kwd"], (scenario_name, body)- after_payload = rest_client.get(base_path).json()- assert after_payload["code"] == 0, (scenario_name, after_payload)- assert after_payload["data"]["doc"]["chunk_count"] == before_total + 1, (scenario_name, after_payload)- else:- assert body["message"] == expected_message, (scenario_name, body)---def test_chunk_add_invalid_dataset_and_document_contract(rest_client, create_document):- dataset_id, document_id = create_document("chunk_invalid_targets.txt")-- invalid_dataset_res = rest_client.post(- f"/datasets/{INVALID_ID_32}/documents/{document_id}/chunks",- json={"content": "chunk test"},- )- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 404, invalid_dataset_payload- assert invalid_dataset_payload["message"] == "Resource not found", invalid_dataset_payload-- invalid_document_res = rest_client.post(- f"/datasets/{dataset_id}/documents/{INVALID_ID_32}/chunks",- json={"content": "chunk test"},- )- assert invalid_document_res.status_code == 200- invalid_document_payload = invalid_document_res.json()- assert invalid_document_payload["code"] == 102, invalid_document_payload- assert invalid_document_payload["message"] == f"you don't own the document {INVALID_ID_32}", invalid_document_payload---def test_chunk_add_repeated_and_deleted_document_contract(rest_client, create_document):- dataset_id, document_id = create_document("chunk_repeat_deleted.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"-- first_payload = rest_client.get(base_path).json()- assert first_payload["code"] == 0, first_payload- initial_count = first_payload["data"]["doc"]["chunk_count"]-- first_add_res = rest_client.post(base_path, json={"content": "chunk test"})- second_add_res = rest_client.post(base_path, json={"content": "chunk test"})- first_add_payload = first_add_res.json()- second_add_payload = second_add_res.json()- assert first_add_payload["code"] == 0, first_add_payload- assert second_add_payload["code"] == 0, second_add_payload- assert first_add_payload["data"]["chunk"]["id"] == second_add_payload["data"]["chunk"]["id"], (first_add_payload, second_add_payload)-- repeated_list_payload = rest_client.get(base_path).json()- assert repeated_list_payload["code"] == 0, repeated_list_payload- assert repeated_list_payload["data"]["doc"]["chunk_count"] == initial_count + 2, repeated_list_payload- assert repeated_list_payload["data"]["total"] == 1, repeated_list_payload-- delete_document_res = rest_client.delete(f"/datasets/{dataset_id}/documents", json={"ids": [document_id]})- assert delete_document_res.status_code == 200- delete_document_payload = delete_document_res.json()- assert delete_document_payload["code"] == 0, delete_document_payload-- add_after_delete_res = rest_client.post(base_path, json={"content": "chunk test"})- assert add_after_delete_res.status_code == 200- add_after_delete_payload = add_after_delete_res.json()- assert add_after_delete_payload["code"] == 102, add_after_delete_payload- assert add_after_delete_payload["message"] == f"you don't own the document {document_id}", add_after_delete_payload--[email protected]("count", [20])-def test_chunk_concurrent_add_contract(rest_client, create_document, count):- dataset_id, document_id = create_document("chunk_concurrent_add.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- baseline_payload = rest_client.get(base_path).json()- assert baseline_payload["code"] == 0, baseline_payload- initial_count = baseline_payload["data"]["doc"]["chunk_count"]-- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(- executor.map(- lambda index: rest_client.post(base_path, json={"content": f"chunk test {index}"}).json(),- range(count),- )- )- assert len(results) == count, results- assert all(result["code"] == 0 for result in results), results-- final_payload = rest_client.get(base_path).json()- assert final_payload["code"] == 0, final_payload- assert final_payload["data"]["doc"]["chunk_count"] == initial_count + count, final_payload---def test_chunks_list_empty_document(rest_client, create_document):- dataset_id, document_id = create_document("chunk_list_empty.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- list_res = rest_client.get(base_path)- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert "chunks" in list_payload["data"], list_payload- assert "doc" in list_payload["data"], list_payload---def test_chunk_delete_basic_contract(rest_client, create_document):- dataset_id, document_id = create_document("chunk_delete_basic.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"-- cases = [- ("none payload", None, 0, "", 5),- ("invalid only", {"chunk_ids": ["invalid_id"]}, 102, "rm_chunk deleted chunks 0, expect 1", 5),- ("delete first", lambda ids: {"chunk_ids": ids[:1]}, 0, "", 4),- ("delete generated", lambda ids: {"chunk_ids": ids}, 0, "", 1),- ("empty ids", {"chunk_ids": []}, 0, "", 5),- ]-- for scenario_name, payload, expected_code, expected_message, remaining in cases:- _reset_chunk_batch(rest_client, base_path)- request_body = payload- generated_ids = rest_client.get(base_path).json()["data"]["chunks"][1:]- generated_ids = [chunk["id"] for chunk in generated_ids]- if callable(payload):- request_body = payload(generated_ids)- res = rest_client.delete(base_path, json=request_body)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_message:- assert body.get("message", "") == expected_message, (scenario_name, body)-- list_payload = rest_client.get(base_path).json()- assert list_payload["code"] == 0, (scenario_name, list_payload)- assert len(list_payload["data"]["chunks"]) == remaining, (scenario_name, list_payload)- assert list_payload["data"]["total"] == remaining, (scenario_name, list_payload)---def test_chunk_delete_partial_duplicate_repeat_and_invalid_target_contract(rest_client, create_document):- dataset_id, document_id = create_document("chunk_delete_detail.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"-- for scenario_name, payload_builder in (- ("invalid first", lambda ids: {"chunk_ids": ["invalid_id"] + ids}),- ("invalid middle", lambda ids: {"chunk_ids": ids[:1] + ["invalid_id"] + ids[1:]}),- ("invalid last", lambda ids: {"chunk_ids": ids + ["invalid_id"]}),- ):- _, generated_ids = _reset_chunk_batch(rest_client, base_path)- res = rest_client.delete(base_path, json=payload_builder(generated_ids))- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == 102, (scenario_name, body)- assert body["message"] == "rm_chunk deleted chunks 4, expect 5", (scenario_name, body)-- list_payload = rest_client.get(base_path).json()- assert list_payload["code"] == 0, (scenario_name, list_payload)- assert list_payload["data"]["total"] == 1, (scenario_name, list_payload)-- _, generated_ids = _reset_chunk_batch(rest_client, base_path)- duplicate_res = rest_client.delete(base_path, json={"chunk_ids": generated_ids * 2})- assert duplicate_res.status_code == 200- duplicate_payload = duplicate_res.json()- assert duplicate_payload["code"] == 0, duplicate_payload- assert duplicate_payload["data"]["success_count"] == 4, duplicate_payload- assert len(duplicate_payload["data"]["errors"]) == 4, duplicate_payload- assert all(error.startswith("Duplicate chunk ids: ") for error in duplicate_payload["data"]["errors"]), duplicate_payload- duplicate_list_payload = rest_client.get(base_path).json()- assert duplicate_list_payload["code"] == 0, duplicate_list_payload- assert duplicate_list_payload["data"]["total"] == 1, duplicate_list_payload-- _, generated_ids = _reset_chunk_batch(rest_client, base_path)- first_delete_res = rest_client.delete(base_path, json={"chunk_ids": generated_ids})- assert first_delete_res.status_code == 200- assert first_delete_res.json()["code"] == 0- second_delete_res = rest_client.delete(base_path, json={"chunk_ids": generated_ids})- assert second_delete_res.status_code == 200- second_delete_payload = second_delete_res.json()- assert second_delete_payload["code"] == 102, second_delete_payload- assert second_delete_payload["message"] == "rm_chunk deleted chunks 0, expect 4", second_delete_payload-- invalid_dataset_res = rest_client.delete(- f"/datasets/{INVALID_ID_32}/documents/{document_id}/chunks",- json={"chunk_ids": ["chunk-id"]},- )- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 102, invalid_dataset_payload- assert invalid_dataset_payload["message"] == f"You don't own the dataset {INVALID_ID_32}.", invalid_dataset_payload-- invalid_document_res = rest_client.delete(- f"/datasets/{dataset_id}/documents/{INVALID_ID_32}/chunks",- json={"chunk_ids": ["chunk-id"]},- )- assert invalid_document_res.status_code == 200- invalid_document_payload = invalid_document_res.json()- assert invalid_document_payload["code"] == 102, invalid_document_payload- assert invalid_document_payload["message"] == f"you don't own the document {INVALID_ID_32}", invalid_document_payload---def test_chunk_delete_web_legacy_basic_variants(rest_client, create_document):- dataset_id, document_id = create_document("chunk_delete_web_legacy_again.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- cases = [- ("web invalid id", {"chunk_ids": ["invalid_id"]}, 102, 5),- ("web delete first", lambda ids: {"chunk_ids": ids[:1]}, 0, 4),- ("web delete generated", lambda ids: {"chunk_ids": ids}, 0, 1),- ("web empty ids", {"chunk_ids": []}, 0, 5),- ]- for scenario_name, payload, expected_code, remaining in cases:- _, generated_ids = _reset_chunk_batch(rest_client, base_path)- request_body = payload(generated_ids) if callable(payload) else payload- res = rest_client.delete(base_path, json=request_body)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- list_payload = rest_client.get(base_path).json()- assert list_payload["code"] == 0, (scenario_name, list_payload)- assert list_payload["data"]["total"] == remaining, (scenario_name, list_payload)---def test_chunk_delete_concurrent_and_bulk_contract(rest_client, create_document):- dataset_id, document_id = create_document("chunk_delete_bulk_contract.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"-- rest_client.delete(base_path, json={"chunk_ids": None, "delete_all": True})- for index in range(12):- payload = rest_client.post(base_path, json={"content": f"chunk test {index}"}).json()- assert payload["code"] == 0, payload- ids_payload = rest_client.get(base_path).json()- assert ids_payload["code"] == 0, ids_payload- chunk_ids = [chunk["id"] for chunk in ids_payload["data"]["chunks"]]-- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(executor.map(lambda chunk_id: rest_client.delete(base_path, json={"chunk_ids": [chunk_id]}).json(), chunk_ids))- assert len(results) == len(chunk_ids), results- assert all(result["code"] == 0 for result in results), results-- final_payload = rest_client.get(base_path).json()- assert final_payload["code"] == 0, final_payload- assert final_payload["data"]["total"] == 0, final_payload-- rest_client.delete(base_path, json={"chunk_ids": None, "delete_all": True})- for index in range(40):- payload = rest_client.post(base_path, json={"content": f"bulk chunk {index}"}).json()- assert payload["code"] == 0, payload- bulk_ids_payload = rest_client.get(base_path, params={"page_size": 100}).json()- assert bulk_ids_payload["code"] == 0, bulk_ids_payload- bulk_ids = [chunk["id"] for chunk in bulk_ids_payload["data"]["chunks"]]- bulk_res = rest_client.delete(base_path, json={"chunk_ids": bulk_ids})- assert bulk_res.status_code == 200- bulk_payload = bulk_res.json()- assert bulk_payload["code"] == 0, bulk_payload---def test_chunk_list_default_get_id_and_invalid_target_contract(rest_client, create_document):- dataset_id, document_id = create_document("chunk_list_core.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- baseline_id, generated_ids = _reset_chunk_batch(rest_client, base_path)-- default_res = rest_client.get(base_path)- assert default_res.status_code == 200- default_payload = default_res.json()- assert default_payload["code"] == 0, default_payload- assert default_payload["data"]["total"] == 5, default_payload- assert len(default_payload["data"]["chunks"]) == 5, default_payload-- get_res = rest_client.get(f"{base_path}/{generated_ids[0]}")- assert get_res.status_code == 200- get_payload = get_res.json()- assert get_payload["code"] == 0, get_payload- assert get_payload["data"]["id"] == generated_ids[0], get_payload- assert get_payload["data"]["doc_id"] == document_id, get_payload-- invalid_get_res = rest_client.get(f"{base_path}/unknown")- assert invalid_get_res.status_code == 200- invalid_get_payload = invalid_get_res.json()- assert invalid_get_payload["code"] == 102, invalid_get_payload- assert invalid_get_payload["message"] == "Chunk not found!", invalid_get_payload-- id_cases = [- ("id none", {"id": None}, 0, 5, None),- ("id empty", {"id": ""}, 0, 5, None),- ("id valid", {"id": generated_ids[0]}, 0, 1, generated_ids[0]),- ("id invalid", {"id": "unknown"}, 102, None, None),- ]- for scenario_name, params, expected_code, expected_total, expected_id in id_cases:- res = rest_client.get(base_path, params=params)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (scenario_name, payload)- if expected_code == 0:- assert payload["data"]["total"] == expected_total, (scenario_name, payload)- if expected_id is not None:- assert payload["data"]["chunks"][0]["id"] == expected_id, (scenario_name, payload)- else:- assert payload["message"] == f"Chunk not found: {dataset_id}/unknown", (scenario_name, payload)-- invalid_dataset_res = rest_client.get(f"/datasets/{INVALID_ID_32}/documents/{document_id}/chunks")- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 102, invalid_dataset_payload- assert invalid_dataset_payload["message"] == f"You don't own the dataset {INVALID_ID_32}.", invalid_dataset_payload-- invalid_document_res = rest_client.get(f"/datasets/{dataset_id}/documents/{INVALID_ID_32}/chunks")- assert invalid_document_res.status_code == 200- invalid_document_payload = invalid_document_res.json()- assert invalid_document_payload["code"] == 102, invalid_document_payload- assert invalid_document_payload["message"] == f"you don't own the document {INVALID_ID_32}", invalid_document_payload---def test_chunk_list_keyword_and_invalid_param_contract(rest_client, create_document):- if _is_infinity_doc_engine(rest_client):- pytest.skip("infinity")- dataset_id, document_id = create_document("chunk_list_keywords.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- _reset_chunk_batch(rest_client, base_path)-- cases = [- ("keywords none", {"keywords": None}, 5),- ("keywords empty", {"keywords": ""}, 5),- ("keywords exact one", {"keywords": "1"}, 1),- ("keywords chunk", {"keywords": "chunk"}, 4),- ("keywords ragflow", {"keywords": "ragflow"}, 1),- ("keywords unknown", {"keywords": "unknown"}, 0),- ("invalid params ignored", {"a": "b"}, 5),- ]-- for scenario_name, params, expected_total in cases:- res = rest_client.get(base_path, params=params)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == 0, (scenario_name, payload)- assert payload["data"]["total"] == expected_total, (scenario_name, payload)- assert len(payload["data"]["chunks"]) == expected_total, (scenario_name, payload)---def test_chunk_list_page_and_page_size_contract(rest_client, create_document):- if _is_infinity_doc_engine(rest_client):- pytest.skip("infinity")- dataset_id, document_id = create_document("chunk_list_paging.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- _reset_chunk_batch(rest_client, base_path)-- cases = [- ("page none", {"page": None, "page_size": 2}, 0, 2, ""),- ("page zero", {"page": 0, "page_size": 2}, 0, 2, ""),- ("page two", {"page": 2, "page_size": 2}, 0, 2, ""),- ("page three", {"page": 3, "page_size": 2}, 0, 1, ""),- ("page string", {"page": "3", "page_size": 2}, 0, 1, ""),- ("page negative", {"page": -1, "page_size": 2}, 0, 2, ""),- ("page alpha", {"page": "a", "page_size": 2}, 0, 2, ""),- ("page_size none", {"page_size": None}, 0, 5, ""),- ("page_size zero", {"page_size": 0}, 0, 5, ""),- ("page_size one", {"page_size": 1}, 0, 1, ""),- ("page_size six", {"page_size": 6}, 0, 5, ""),- ("page_size string", {"page_size": "1"}, 0, 1, ""),- ("page_size negative", {"page_size": -1}, 0, 5, ""),- ("page_size alpha", {"page_size": "a"}, 0, 5, ""),- ]-- for scenario_name, params, expected_code, expected_total, expected_message in cases:- res = rest_client.get(base_path, params=params)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (scenario_name, payload)- if expected_code == 0:- assert payload["data"]["total"] == 5, (scenario_name, payload)- assert len(payload["data"]["chunks"]) == expected_total, (scenario_name, payload)- else:- assert expected_message in payload["message"], (scenario_name, payload)---def test_chunk_list_concurrent_contract(rest_client, create_document):- dataset_id, document_id = create_document("chunk_list_concurrent.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- _reset_chunk_batch(rest_client, base_path)-- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(executor.map(lambda _: rest_client.get(base_path).json(), range(20)))- assert len(results) == 20, results- assert all(result["code"] == 0 for result in results), results- assert all(result["data"]["total"] == 5 for result in results), results---def _create_chunk_for_update(rest_client, create_document, file_name):- dataset_id, document_id = create_document(file_name)- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- add_res = rest_client.post(base_path, json={"content": "chunk update test"})- assert add_res.status_code == 200, add_res.text- add_payload = add_res.json()- assert add_payload["code"] == 0, add_payload- chunk_id = add_payload["data"]["chunk"]["id"]- return dataset_id, document_id, chunk_id, base_path---def test_chunk_update_requires_auth(rest_client, create_document):- _, _, chunk_id, base_path = _create_chunk_for_update(rest_client, create_document, "chunk_update_auth.txt")- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.patch(f"{base_path}/{chunk_id}", json={"content": "updated"})- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_chunk_update_content_and_available_contract(rest_client, create_document):- content_cases = [- ("content none", {"content": None}, 0, ""),- ("content empty", {"content": ""}, 102, "`content` is required"),- ("content text", {"content": "update chunk"}, 0, ""),- ("content spaces", {"content": " "}, 102, "`content` is required"),- ("content punctuation", {"content": "\n!?。;!?\"'"}, 0, ""),- ]- for scenario_name, payload, expected_code, expected_message in content_cases:- _, _, chunk_id, base_path = _create_chunk_for_update(rest_client, create_document, f"{scenario_name}.txt")- res = rest_client.patch(f"{base_path}/{chunk_id}", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code != 0:- assert body["message"] == expected_message, (scenario_name, body)-- available_cases = [- ("available true", {"available": True}, 0, ""),- ("available true str", {"available": "True"}, 100, "invalid literal for int()"),- ("available one", {"available": 1}, 0, ""),- ("available false", {"available": False}, 0, ""),- ("available false str", {"available": "False"}, 100, "invalid literal for int()"),- ("available zero", {"available": 0}, 0, ""),- ]- for scenario_name, payload, expected_code, expected_message in available_cases:- _, _, chunk_id, base_path = _create_chunk_for_update(rest_client, create_document, f"{scenario_name}.txt")- res = rest_client.patch(f"{base_path}/{chunk_id}", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code != 0:- assert expected_message in body["message"], (scenario_name, body)---def test_chunk_update_keywords_questions_and_tag_contract(rest_client, create_document):- _, _, chunk_id, base_path = _create_chunk_for_update(rest_client, create_document, "chunk_update_fields.txt")- cases = [- ("important keywords", {"important_keywords": ["a", "b", "c"]}, 0, ""),- ("important keywords empty", {"important_keywords": [""]}, 0, ""),- ("important keywords int", {"important_keywords": [1]}, 100, "TypeError"),- ("important keywords dup", {"important_keywords": ["a", "a"]}, 0, ""),- ("important keywords str", {"important_keywords": "abc"}, 102, "`important_keywords` should be a list"),- ("important keywords number", {"important_keywords": 123}, 102, "`important_keywords` should be a list"),- ("questions", {"questions": ["a", "b", "c"]}, 0, ""),- ("questions empty", {"questions": [""]}, 0, ""),- ("questions int", {"questions": [1]}, 100, "TypeError"),- ("questions dup", {"questions": ["a", "a"]}, 0, ""),- ("questions str", {"questions": "abc"}, 102, "`questions` should be a list"),- ("questions number", {"questions": 123}, 102, "`questions` should be a list"),- ("tag kwd", {"tag_kwd": ["tag1", "tag2"]}, 0, ""),- ("tag kwd empty", {"tag_kwd": [""]}, 0, ""),- ("tag kwd int in list", {"tag_kwd": [1]}, 102, "`tag_kwd` must be a list of strings"),- ("tag kwd dup", {"tag_kwd": ["tag", "tag"]}, 0, ""),- ("tag kwd str", {"tag_kwd": "tag"}, 102, "`tag_kwd` should be a list"),- ("tag kwd number", {"tag_kwd": 123}, 102, "`tag_kwd` should be a list"),- ]- for scenario_name, payload, expected_code, expected_message in cases:- res = rest_client.patch(f"{base_path}/{chunk_id}", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code != 0:- assert expected_message in body["message"], (scenario_name, body)---def test_chunk_update_invalid_target_and_param_contract(rest_client, create_document):- dataset_id, document_id, chunk_id, base_path = _create_chunk_for_update(rest_client, create_document, "chunk_update_invalid_targets.txt")-- invalid_dataset_res = rest_client.patch(- f"/datasets/{INVALID_ID_32}/documents/{document_id}/chunks/{chunk_id}",- json={"content": "updated"},- )- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 102, invalid_dataset_payload- assert invalid_dataset_payload["message"] in {- f"You don't own the dataset {INVALID_ID_32}.",- f"Can't find this chunk {chunk_id}",- }, invalid_dataset_payload-- invalid_document_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{INVALID_ID_32}/chunks/{chunk_id}",- json={"content": "updated"},- )- assert invalid_document_res.status_code == 200- invalid_document_payload = invalid_document_res.json()- assert invalid_document_payload["code"] == 102, invalid_document_payload- assert invalid_document_payload["message"] == f"you don't own the document {INVALID_ID_32}", invalid_document_payload-- invalid_chunk_res = rest_client.patch(- f"{base_path}/{INVALID_ID_32}",- json={"content": "updated"},- )- assert invalid_chunk_res.status_code == 200- invalid_chunk_payload = invalid_chunk_res.json()- assert invalid_chunk_payload["code"] == 102, invalid_chunk_payload- assert invalid_chunk_payload["message"] == f"Can't find this chunk {INVALID_ID_32}", invalid_chunk_payload-- for scenario_name, payload in (- ("unknown key", {"unknown_key": "unknown_value"}),- ("empty payload", {}),- ):- res = rest_client.patch(f"{base_path}/{chunk_id}", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == 0, (scenario_name, body)---def test_chunk_update_repeated_concurrent_and_deleted_document_contract(rest_client, create_document):- dataset_id, document_id, chunk_id, base_path = _create_chunk_for_update(rest_client, create_document, "chunk_update_repeated_concurrent_deleted.txt")-- first_res = rest_client.patch(f"{base_path}/{chunk_id}", json={"content": "chunk test 1"})- assert first_res.status_code == 200- assert first_res.json()["code"] == 0-- second_res = rest_client.patch(f"{base_path}/{chunk_id}", json={"content": "chunk test 2"})- assert second_res.status_code == 200- assert second_res.json()["code"] == 0-- get_after_repeat = rest_client.get(f"{base_path}/{chunk_id}")- assert get_after_repeat.status_code == 200- get_after_repeat_payload = get_after_repeat.json()- assert get_after_repeat_payload["code"] == 0, get_after_repeat_payload- assert get_after_repeat_payload["data"]["content_with_weight"] == "chunk test 2", get_after_repeat_payload-- chunk_ids = [chunk_id]- for index in range(3):- add_res = rest_client.post(base_path, json={"content": f"concurrent update {index}"})- assert add_res.status_code == 200, add_res.text- add_payload = add_res.json()- assert add_payload["code"] == 0, add_payload- chunk_ids.append(add_payload["data"]["chunk"]["id"])-- with ThreadPoolExecutor(max_workers=5) as executor:- futures = []- for index in range(20):- target_id = chunk_ids[index % len(chunk_ids)]- futures.append(- executor.submit(- lambda cid, i: rest_client.patch(- f"{base_path}/{cid}",- json={"content": f"update chunk test {i}"},- ).json(),- target_id,- index,- )- )- results = [future.result() for future in futures]- assert len(results) == 20, results- assert all(item["code"] == 0 for item in results), results-- delete_document_res = rest_client.delete(f"/datasets/{dataset_id}/documents", json={"ids": [document_id]})- assert delete_document_res.status_code == 200- assert delete_document_res.json()["code"] == 0-- update_after_delete = rest_client.patch(f"{base_path}/{chunk_id}", json={"content": "after delete"})- assert update_after_delete.status_code == 200- update_after_delete_payload = update_after_delete.json()- assert update_after_delete_payload["code"] == 102, update_after_delete_payload- assert update_after_delete_payload["message"] in {- f"you don't own the document {document_id}",- f"Can't find this chunk {chunk_id}",- }, update_after_delete_payloaddiff --git a/test/testcases/restful_api/test_datasets.py b/test/testcases/restful_api/test_datasets.pydeleted file mode 100644index 2fa8ae42ce..0000000000--- a/test/testcases/restful_api/test_datasets.py+++ /dev/null@@ -1,2121 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import os-import uuid-from concurrent.futures import ThreadPoolExecutor, as_completed--import pytest-from configs import DATASET_NAME_LIMIT--from test.testcases.configs import INVALID_API_TOKEN-from test.testcases.restful_api.helpers.assertions import assert_auth_error-from test.testcases.restful_api.helpers.client import RestClient-from test.testcases.utils import encode_avatar-from test.testcases.utils.file_utils import create_image_file, create_txt_file--ARGUMENT_ERROR_CODE = 102-PARSER_ID_FIELD = "parser_id"---def _parser_id_fields(chunk_method):- """Build the parser_id field; the Go API requires parse_type alongside it."""- return {PARSER_ID_FIELD: chunk_method, "parse_type": 1}---def _expected_chunk_method(chunk_method):- """Return the API-visible parser ID."""- if chunk_method == "naive":- return "general"- return chunk_method---def _assert_go_pipeline_parser_config(parser_config, chunker_prefix="GeneralChunker:"):- assert parser_config- assert "File" in parser_config, parser_config- assert any(key.startswith("Parser:") for key in parser_config), parser_config- assert any(key.startswith(chunker_prefix) for key in parser_config), parser_config- assert all(isinstance(params, dict) for params in parser_config.values()), parser_config- assert "chunk_token_num" not in parser_config, parser_config- assert "raptor" not in parser_config, parser_config- assert "graphrag" not in parser_config, parser_config---def _is_infinity_doc_engine(rest_client: RestClient) -> bool:- env_engine = (os.getenv("DOC_ENGINE") or "").strip().lower()- if env_engine:- return env_engine == "infinity"- try:- res = rest_client.get("/system/status")- if res.status_code != 200:- return False- payload = res.json()- if payload.get("code") != 0:- return False- engine = str(payload.get("data", {}).get("doc_engine", {}).get("type", "")).strip().lower()- return engine == "infinity"- except Exception:- return False---class TestDatasetsAuthorization:- def test_create_requires_auth(self, rest_client_noauth):- res = rest_client_noauth.post("/datasets", json={"name": "auth_test"})- assert res.status_code == 401- payload = res.json()- assert payload["code"] == 401, payload---def test_dataset_crud_cycle(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "restful_dataset_crud"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- get_res = rest_client.get(f"/datasets/{dataset_id}")- assert get_res.status_code == 200- get_payload = get_res.json()- assert get_payload["code"] == 0, get_payload- assert get_payload["data"]["id"] == dataset_id, get_payload-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"name": "restful_dataset_crud_updated"},- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload- assert update_payload["data"]["name"] == "restful_dataset_crud_updated", update_payload-- list_res = rest_client.get("/datasets", params={"id": dataset_id})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert len(list_payload["data"]) == 1, list_payload- assert list_payload["data"][0]["id"] == dataset_id, list_payload- assert list_payload.get("total_datasets", 0) >= 1, list_payload-- delete_res = rest_client.delete("/datasets", json={"ids": [dataset_id]})- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload-- list_after_delete = rest_client.get("/datasets")- assert list_after_delete.status_code == 200- list_after_delete_payload = list_after_delete.json()- assert list_after_delete_payload["code"] == 0, list_after_delete_payload- assert all(dataset["id"] != dataset_id for dataset in list_after_delete_payload["data"]), list_after_delete_payload---def test_dataset_update_name_and_case_insensitive_contract(rest_client, clear_datasets):- first_res = rest_client.post("/datasets", json={"name": "dataset_update_name_source"})- assert first_res.status_code == 200- first_payload = first_res.json()- assert first_payload["code"] == 0, first_payload- first_dataset_id = first_payload["data"]["id"]-- second_res = rest_client.post("/datasets", json={"name": "dataset_update_name_target"})- assert second_res.status_code == 200- second_payload = second_res.json()- assert second_payload["code"] == 0, second_payload-- rename_res = rest_client.put(- f"/datasets/{first_dataset_id}",- json={"name": "dataset_update_name_renamed"},- )- assert rename_res.status_code == 200- rename_payload = rename_res.json()- assert rename_payload["code"] == 0, rename_payload- assert rename_payload["data"]["name"] == "dataset_update_name_renamed", rename_payload-- list_res = rest_client.get("/datasets", params={"id": first_dataset_id})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert list_payload["data"][0]["name"] == "dataset_update_name_renamed", list_payload-- duplicate_case_res = rest_client.put(- f"/datasets/{first_dataset_id}",- json={"name": second_payload["data"]["name"].upper()},- )- assert duplicate_case_res.status_code == 200- duplicate_case_payload = duplicate_case_res.json()- assert duplicate_case_payload["code"] == 102, duplicate_case_payload- assert "already exists" in duplicate_case_payload["message"], duplicate_case_payload---def test_dataset_update_language_connectors_avatar_and_description_contract(rest_client, clear_datasets, tmp_path):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_lang_connectors"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- image_path = create_image_file(tmp_path / "dataset_update_avatar.png")- encoded_avatar = encode_avatar(image_path)- avatar_value = f"data:image/png;base64,{encoded_avatar}"-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={- "name": "dataset_update_lang_connectors",- "description": "",- **_parser_id_fields("naive"),- "language": "English",- "connectors": [],- "avatar": avatar_value,- },- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload- assert update_payload["data"]["language"] == "English", update_payload- assert update_payload["data"]["connectors"] == [], update_payload- assert update_payload["data"]["avatar"] == avatar_value, update_payload-- description_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"description": "description"},- )- assert description_res.status_code == 200- description_payload = description_res.json()- assert description_payload["code"] == 0, description_payload- assert description_payload["data"]["description"] == "description", description_payload--- "chunk_method",- [- "naive",- "book",- "email",- "laws",- "manual",- "one",- "paper",- "picture",- "presentation",- "qa",- "table",- ],- ids=["naive", "book", "email", "laws", "manual", "one", "paper", "picture", "presentation", "qa", "table"],-)-def test_dataset_update_chunk_method_contract(rest_client, clear_datasets, chunk_method):- create_res = rest_client.post("/datasets", json={"name": f"dataset_update_chunk_{chunk_method}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json=_parser_id_fields(chunk_method),- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload- assert update_payload["data"][PARSER_ID_FIELD] == _expected_chunk_method(chunk_method), update_payload--- "name, update_payload",- [- ("parser_config_empty", {**_parser_id_fields("qa"), "parser_config": {}}),- ("parser_config_none", {**_parser_id_fields("qa"), "parser_config": None}),- ("parser_config_unset", _parser_id_fields("qa")),- ],- ids=["parser_config_empty", "parser_config_none", "parser_config_unset"],-)-def test_dataset_update_parser_config_with_chunk_method_change_contract(rest_client, clear_datasets, name, update_payload):- create_res = rest_client.post("/datasets", json={"name": f"dataset_update_{name}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- update_res = rest_client.put(f"/datasets/{dataset_id}", json=update_payload)- assert update_res.status_code == 200- update_body = update_res.json()- assert update_body["code"] == 0, update_body-- list_res = rest_client.get("/datasets", params={"id": dataset_id})- assert list_res.status_code == 200- list_body = list_res.json()- assert list_body["code"] == 0, list_body- actual_parser_config = list_body["data"][0]["parser_config"]- assert isinstance(actual_parser_config, dict) and actual_parser_config, list_body- assert "raptor" not in actual_parser_config, list_body- assert "graphrag" not in actual_parser_config, list_body--- "embedding_model, unauthorized_is_xfail",- [- ("BAAI/bge-small-en-v1.5@Builtin", False),- ("embedding-3@ZHIPU-AI", True),- ],- ids=["builtin_baai", "tenant_zhipu"],-)-def test_dataset_update_embedding_model_contract(rest_client, clear_datasets, embedding_model, unauthorized_is_xfail):- create_res = rest_client.post("/datasets", json={"name": f"dataset_update_embedding_{embedding_model.split('@')[0].replace('/', '_')}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"embedding_model": embedding_model},- )- assert update_res.status_code == 200- update_payload = update_res.json()- if unauthorized_is_xfail and update_payload["code"] == 102:- pytest.xfail(f"Environment has no authorized tenant model for {embedding_model}: {update_payload}")- assert update_payload["code"] == 0, update_payload- assert update_payload["data"]["embedding_model"] == embedding_model, update_payload--- "name, embedding_model, expected_fragment",- [- ("empty", "", "embedding model identifier must follow <model_name>@<provider> format"),- ("space", " ", "embedding model identifier must follow <model_name>@<provider> format"),- ("missing_at", "BAAI/bge-small-en-v1.5Builtin", "embedding model identifier must follow <model_name>@<provider> format"),- ("missing_model_name", "@Builtin", "both model_name and provider must be non-empty strings"),- ("missing_provider", "BAAI/bge-small-en-v1.5@", "both model_name and provider must be non-empty strings"),- ("whitespace_only_model_name", " @Builtin", "both model_name and provider must be non-empty strings"),- ("whitespace_only_provider", "BAAI/bge-small-en-v1.5@ ", "both model_name and provider must be non-empty strings"),- ],- ids=["empty", "space", "missing_at", "empty_model_name", "empty_provider", "whitespace_only_model_name", "whitespace_only_provider"],-)-def test_dataset_update_embedding_model_format_contract(rest_client, clear_datasets, name, embedding_model, expected_fragment):- create_res = rest_client.post("/datasets", json={"name": f"dataset_update_embedding_format_{name}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"embedding_model": embedding_model},- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == ARGUMENT_ERROR_CODE, update_payload- assert expected_fragment in update_payload["message"], update_payload---def test_dataset_update_embedding_model_with_existing_chunks_contract(rest_client, create_document):- dataset_id, document_id = create_document("dataset_update_embedding_with_chunks.txt")- chunk_res = rest_client.post(- f"/datasets/{dataset_id}/documents/{document_id}/chunks",- json={"content": "dataset update embedding with chunks"},- )- assert chunk_res.status_code == 200- chunk_payload = chunk_res.json()- assert chunk_payload["code"] == 0, chunk_payload-- dataset_res = rest_client.get(f"/datasets/{dataset_id}")- assert dataset_res.status_code == 200- dataset_payload = dataset_res.json()- assert dataset_payload["code"] == 0, dataset_payload- current_embedding = dataset_payload["data"]["embedding_model"]-- candidates = ["embedding-3@CI@ZHIPU-AI", "BAAI/bge-small-en-v1.5@Local@Builtin"]- last_payload = None- for candidate in candidates:- if candidate == current_embedding:- continue- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"embedding_model": candidate},- )- assert update_res.status_code == 200- update_payload = update_res.json()- last_payload = update_payload- if update_payload["code"] == 0:- assert update_payload["data"]["embedding_model"] == candidate, update_payload- return- if update_payload["code"] == 102 and "Unauthorized model" in update_payload.get("message", ""):- continue- assert False, update_payload-- pytest.xfail(f"No authorized alternative embedding model available for update: {last_payload}")--- "permission",- ["me", "team"],- ids=["me", "team"],-)-def test_dataset_update_permission_contract(rest_client, clear_datasets, permission):- create_res = rest_client.post("/datasets", json={"name": f"dataset_update_permission_{permission}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"permission": permission},- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload- assert update_payload["data"]["permission"] == permission.lower().strip(), update_payload--[email protected]("pagerank", [0, 50, 100], ids=["min", "mid", "max"])-def test_dataset_update_pagerank_contract(rest_client, clear_datasets, pagerank):- if _is_infinity_doc_engine(rest_client):- pytest.skip("#8208")- create_res = rest_client.post("/datasets", json={"name": f"dataset_update_pagerank_{pagerank}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"pagerank": pagerank},- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload-- list_res = rest_client.get("/datasets", params={"id": dataset_id})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert list_payload["data"][0]["pagerank"] == pagerank, list_payload---def test_dataset_update_pagerank_set_to_zero_contract(rest_client, clear_datasets):- if _is_infinity_doc_engine(rest_client):- pytest.skip("#8208")- create_res = rest_client.post("/datasets", json={"name": "dataset_update_pagerank_set_to_zero"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- fifty_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"pagerank": 50},- )- assert fifty_res.status_code == 200- fifty_payload = fifty_res.json()- assert fifty_payload["code"] == 0, fifty_payload-- zero_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"pagerank": 0},- )- assert zero_res.status_code == 200- zero_payload = zero_res.json()- assert zero_payload["code"] == 0, zero_payload-- list_res = rest_client.get("/datasets", params={"id": dataset_id})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert list_payload["data"][0]["pagerank"] == 0, list_payload---def test_dataset_update_pagerank_infinity_contract(rest_client, clear_datasets):- if not _is_infinity_doc_engine(rest_client):- pytest.skip("#8208")- create_res = rest_client.post("/datasets", json={"name": "dataset_update_pagerank_infinity"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"pagerank": 50},- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 102, update_payload- assert update_payload["message"] == "'pagerank' can only be set when doc_engine is elasticsearch", update_payload---def test_dataset_update_concurrent_contract(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_concurrent_base"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- count = 100- with ThreadPoolExecutor(max_workers=5) as executor:- futures = [executor.submit(rest_client.put, f"/datasets/{dataset_id}", json={"name": f"dataset_update_{i}"}) for i in range(count)]- responses = list(as_completed(futures))- assert len(responses) == count, responses- for index, future in enumerate(futures):- res = future.result()- assert res.status_code == 200, (index, res.text)- payload = res.json()- assert payload["code"] == 0, (index, payload)---def test_dataset_update_requires_auth_contract(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_auth_contract"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.put(f"/datasets/{dataset_id}", json={"name": "dataset_update_auth_invalid"})- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_dataset_update_content_type_and_payload_contract(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_payload_contract"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- bad_content_type = "text/xml"- bad_content_type_res = rest_client.put(- f"/datasets/{dataset_id}",- data='{"name": "bad_content_type"}',- headers={"Content-Type": bad_content_type},- )- assert bad_content_type_res.status_code == 200- bad_content_type_payload = bad_content_type_res.json()- assert bad_content_type_payload["code"] == 101, bad_content_type_payload- assert f"Unsupported content type: Expected application/json, got {bad_content_type}" in bad_content_type_payload["message"], bad_content_type_payload-- malformed_json_res = rest_client.put(f"/datasets/{dataset_id}", data="a")- assert malformed_json_res.status_code == 200- malformed_json_payload = malformed_json_res.json()- assert malformed_json_payload["code"] == 101, malformed_json_payload- assert "Malformed JSON syntax: Missing commas/brackets or invalid encoding" in malformed_json_payload["message"], malformed_json_payload-- invalid_payload_type_res = rest_client.put(f"/datasets/{dataset_id}", data='"a"')- assert invalid_payload_type_res.status_code == 200- invalid_payload_type_payload = invalid_payload_type_res.json()- assert invalid_payload_type_payload["code"] == 101, invalid_payload_type_payload- assert "Invalid request payload: expected object, got str" in invalid_payload_type_payload["message"], invalid_payload_type_payload-- empty_payload_res = rest_client.put(f"/datasets/{dataset_id}", json={})- assert empty_payload_res.status_code == 200- empty_payload = empty_payload_res.json()- assert empty_payload["code"] == 102, empty_payload- assert empty_payload["message"] == "no properties were modified", empty_payload-- unset_payload_res = rest_client.put(f"/datasets/{dataset_id}")- assert unset_payload_res.status_code == 200- unset_payload = unset_payload_res.json()- assert unset_payload["code"] == 101, unset_payload- assert "Malformed JSON syntax: Missing commas/brackets or invalid encoding" in unset_payload["message"], unset_payload---def test_dataset_update_identifier_validation_contract(rest_client):- payload = {"name": "dataset_update_identifier_validation"}-- not_uuid_res = rest_client.put("/datasets/not_uuid", json=payload)- assert not_uuid_res.status_code == 200- not_uuid_payload = not_uuid_res.json()- assert not_uuid_payload["code"] == 101, not_uuid_payload- assert "Invalid UUID format" in not_uuid_payload["message"], not_uuid_payload-- not_uuid1_res = rest_client.put(f"/datasets/{uuid.uuid4().hex}", json=payload)- assert not_uuid1_res.status_code == 200- not_uuid1_payload = not_uuid1_res.json()- assert not_uuid1_payload["code"] == 404, not_uuid1_payload- assert not_uuid1_payload["message"] == "Resource not found", not_uuid1_payload-- wrong_uuid_res = rest_client.put("/datasets/d94a8dc02c9711f0930f7fbc369eab6d", json=payload)- assert wrong_uuid_res.status_code == 200- wrong_uuid_payload = wrong_uuid_res.json()- assert wrong_uuid_payload["code"] == 404, wrong_uuid_payload- assert wrong_uuid_payload["message"] == "Resource not found", wrong_uuid_payload---def test_dataset_update_avatar_invalid_contract(rest_client, clear_datasets, tmp_path):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_avatar_invalid_contract"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- exceed_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"avatar": "a" * 65536},- )- assert exceed_res.status_code == 200- exceed_payload = exceed_res.json()- assert exceed_payload["code"] == ARGUMENT_ERROR_CODE, exceed_payload- assert "String should have at most 65535 characters" in exceed_payload["message"], exceed_payload-- image_path = create_image_file(tmp_path / "dataset_update_avatar_invalid.png")- encoded_avatar = encode_avatar(image_path)- invalid_prefix_cases = [- ("", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),- ("data:image/png;base64", "missing MIME prefix. Expected format: data:<mime>;base64,<data>"),- ("invalid_mine_prefix:image/png;base64,", "invalid MIME prefix format. Must start with 'data:'"),- ("data:unsupported_mine_type;base64,", "Unsupported MIME type. Allowed: ['image/jpeg', 'image/png']"),- ]- for prefix, expected_message in invalid_prefix_cases:- res = rest_client.put(- f"/datasets/{dataset_id}",- json={"avatar": f"{prefix}{encoded_avatar}"},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == ARGUMENT_ERROR_CODE, payload- if expected_message.startswith("Unsupported MIME type"):- expected_message = "unsupported MIME type. Allowed: [image/jpeg image/png]"- assert expected_message in payload["message"], payload---def test_dataset_update_description_validation_contract(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_description_contract"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- exceeds_limit_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"description": "a" * 65536},- )- assert exceeds_limit_res.status_code == 200- exceeds_limit_payload = exceeds_limit_res.json()- assert exceeds_limit_payload["code"] == ARGUMENT_ERROR_CODE, exceeds_limit_payload- assert "String should have at most 65535 characters" in exceeds_limit_payload["message"], exceeds_limit_payload---def test_dataset_update_name_invalid_and_duplicate_contract(rest_client, clear_datasets):- first_res = rest_client.post("/datasets", json={"name": "dataset_update_name_invalid_first"})- assert first_res.status_code == 200- first_payload = first_res.json()- assert first_payload["code"] == 0, first_payload- first_dataset_id = first_payload["data"]["id"]-- second_res = rest_client.post("/datasets", json={"name": "dataset_update_name_invalid_second"})- assert second_res.status_code == 200- second_payload = second_res.json()- assert second_payload["code"] == 0, second_payload-- invalid_cases = [- ("", "String should have at least 1 character"),- (" ", "String should have at least 1 character"),- ("a" * (DATASET_NAME_LIMIT + 1), f"String should have at most {DATASET_NAME_LIMIT} characters"),- (0, "Input should be a valid string"),- ]- for name, expected_message in invalid_cases:- res = rest_client.put(f"/datasets/{first_dataset_id}", json={"name": name})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == ARGUMENT_ERROR_CODE, payload- if not isinstance(name, str):- assert "cannot unmarshal" in payload["message"] and ".name" in payload["message"], payload- else:- assert expected_message in payload["message"], payload-- duplicated_res = rest_client.put(- f"/datasets/{first_dataset_id}",- json={"name": second_payload["data"]["name"]},- )- assert duplicated_res.status_code == 200- duplicated_payload = duplicated_res.json()- assert duplicated_payload["code"] == 102, duplicated_payload- assert f"Dataset name '{second_payload['data']['name']}' already exists" == duplicated_payload["message"], duplicated_payload---def test_dataset_update_embedding_model_invalid_contract(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_embedding_invalid_contract"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- invalid_cases = [- ("unknown@ZHIPU-AI", "Model unknown@ZHIPU-AI not found for model embedding"),- ("embedding-3@unknown", "Provider unknown not found for model embedding-3@unknown."),- ("text-embedding-v3@Tongyi-Qianwen", "Provider Tongyi-Qianwen not found for model text-embedding-v3@Tongyi-Qianwen."),- ("text-embedding-3-small@OpenAI", "Provider OpenAI not found for model text-embedding-3-small@OpenAI."),- ]- for embedding_model, _ in invalid_cases:- res = rest_client.put(- f"/datasets/{dataset_id}",- json={"embedding_model": embedding_model},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert "lookup failed: record not found" in payload["message"], payload---def test_dataset_update_permission_invalid_contract(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_permission_invalid_contract"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- invalid_permissions = ["", "unknown", [], "ME", "TEAM", " ME "]- for permission in invalid_permissions:- res = rest_client.put(f"/datasets/{dataset_id}", json={"permission": permission})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == ARGUMENT_ERROR_CODE, payload- if not isinstance(permission, str):- assert "cannot unmarshal" in payload["message"] and ".permission" in payload["message"], payload- else:- assert "Input should be 'me' or 'team'" in payload["message"], payload---def test_dataset_update_chunk_method_invalid_contract(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_chunk_method_invalid_contract"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- for chunk_method in ("", "unknown", []):- res = rest_client.put(f"/datasets/{dataset_id}", json=_parser_id_fields(chunk_method))- assert res.status_code == 200- payload = res.json()- assert payload["code"] == ARGUMENT_ERROR_CODE, payload- if not isinstance(chunk_method, str):- assert "cannot unmarshal" in payload["message"] and f".{PARSER_ID_FIELD}" in payload["message"], payload- elif chunk_method == "":- assert payload["message"] == "parser_id is required when parse_type is BuiltIn", payload- else:- assert payload["message"].startswith("input should be 'general', 'qa'") and payload["message"].endswith("or 'email'"), payload---def test_dataset_update_parser_config_defaults_contract(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_parser_defaults_contract"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- empty_res = rest_client.put(f"/datasets/{dataset_id}", json={"parser_config": {}})- assert empty_res.status_code == 200- empty_payload = empty_res.json()- assert empty_payload["code"] == 0, empty_payload-- none_res = rest_client.put(f"/datasets/{dataset_id}", json={"parser_config": None})- assert none_res.status_code == 200- none_payload = none_res.json()- assert none_payload["code"] == 0, none_payload-- list_res = rest_client.get("/datasets", params={"id": dataset_id})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- parser_config = list_payload["data"][0]["parser_config"]- assert isinstance(parser_config, dict) and parser_config, list_payload- assert "raptor" not in parser_config, list_payload- assert "graphrag" not in parser_config, list_payload--- "children_delimiter",- ["|", ";", "\n"],- ids=["delimiter_pipe", "delimiter_semicolon", "delimiter_newline"],-)-def test_dataset_update_parser_config_component_scoped_roundtrip(rest_client, clear_datasets, children_delimiter):- # The intended contract is component-scoped: every parser_config key must be- # keyed by a component id (e.g. "GeneralChunker:<id>"). This test verifies a- # dataset-level setting reaches the runtime chunker node and round-trips.- create_res = rest_client.post("/datasets", json={"name": "dataset_update_component_scoped"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- # Discover the real chunker component id from the created config.- list_res = rest_client.get("/datasets", params={"id": dataset_id})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- parser_config = list_payload["data"][0]["parser_config"]- chunker_id = next(- (k for k in parser_config if k.split(":", 1)[0].endswith("Chunker")),- None,- )- assert chunker_id is not None, parser_config-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={- "parser_config": {- chunker_id: {- "parent_child": {- "use_parent_child": True,- "children_delimiter": children_delimiter,- }- }- }- },- )- assert update_res.status_code == 200- assert update_res.json()["code"] == 0, update_res.json()-- list_res2 = rest_client.get("/datasets", params={"id": dataset_id})- assert list_res2.status_code == 200- final_config = list_res2.json()["data"][0]["parser_config"]- chunker = final_config[chunker_id]- assert chunker["parent_child"]["use_parent_child"] is True, final_config- assert chunker["children_delimiters"] == [children_delimiter], final_config---def test_dataset_update_parser_config_accepts_bare_file_node(rest_client, clear_datasets):- # The DSL emits a single input node keyed by the bare id "File" (no- # component colon). The Go backend tolerates this legacy shape (it is- # dropped server-side) rather than rejecting it, so existing frontends that- # send it must not receive a 101 error. See PR #20445.- create_res = rest_client.post("/datasets", json={"name": "dataset_update_bare_file"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- update_res = rest_client.put(- f"/datasets/{dataset_id}",- json={"parser_config": {"File": {"layout_recognize": "DeepDOC"}}},- )- assert update_res.status_code == 200- assert update_res.json()["code"] == 0, update_res.json()---def test_dataset_update_field_unset_and_unsupported_contract(rest_client, clear_datasets):- create_res = rest_client.post("/datasets", json={"name": "dataset_update_field_unset_contract"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_id = create_payload["data"]["id"]-- list_res = rest_client.get("/datasets", params={"id": dataset_id})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- original_data = list_payload["data"][0]-- name_update_res = rest_client.put(f"/datasets/{dataset_id}", json={"name": "dataset_update_field_unset_renamed"})- assert name_update_res.status_code == 200- name_update_payload = name_update_res.json()- assert name_update_payload["code"] == 0, name_update_payload-- after_list_res = rest_client.get("/datasets", params={"id": dataset_id})- assert after_list_res.status_code == 200- after_list_payload = after_list_res.json()- assert after_list_payload["code"] == 0, after_list_payload- assert after_list_payload["data"][0]["avatar"] == original_data["avatar"], after_list_payload- assert after_list_payload["data"][0]["description"] == original_data["description"], after_list_payload- assert after_list_payload["data"][0]["embedding_model"] == original_data["embedding_model"], after_list_payload- assert after_list_payload["data"][0]["permission"] == original_data["permission"], after_list_payload- assert after_list_payload["data"][0][PARSER_ID_FIELD] == original_data[PARSER_ID_FIELD], after_list_payload- assert after_list_payload["data"][0]["pagerank"] == original_data["pagerank"], after_list_payload- assert after_list_payload["data"][0]["parser_config"] == original_data["parser_config"], after_list_payload-- unsupported_field_payloads = [- {"id": "id"},- {"tenant_id": "e57c1966f99211efb41e9e45646e0111"},- {"created_by": "created_by"},- {"create_date": "Tue, 11 Mar 2025 13:37:23 GMT"},- {"create_time": 1741671443322},- {"update_date": "Tue, 11 Mar 2025 13:37:23 GMT"},- {"update_time": 1741671443339},- {"document_count": 1},- {"chunk_count": 1},- {"token_num": 1},- {"status": "1"},- {"unknown_field": "unknown_field"},- ]- for payload_data in unsupported_field_payloads:- res = rest_client.put(f"/datasets/{dataset_id}", json=payload_data)- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- assert "Extra inputs are not permitted" in payload["message"], payload--- "name, expected_fragment",- [- ("", "String should have at least 1 character"),- (" ", "String should have at least 1 character"),- ("a" * (DATASET_NAME_LIMIT + 1), f"String should have at most {DATASET_NAME_LIMIT} characters"),- ],- ids=["empty", "spaces", "too_long"],-)-def test_dataset_create_name_validation(rest_client, clear_datasets, name, expected_fragment):- res = rest_client.post("/datasets", json={"name": name})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == ARGUMENT_ERROR_CODE, payload- if not name:- expected_fragment = "failed on the 'required' tag"- elif not name.strip():- expected_fragment = "dataset name can't be empty"- else:- expected_fragment = f"Dataset name length is {len(name)} which is large than {DATASET_NAME_LIMIT}"- assert expected_fragment in payload["message"], payload---def test_dataset_create_name_and_case_insensitive_contract(rest_client, clear_datasets):- name = "CaseInsensitive"-- first_res = rest_client.post("/datasets", json={"name": name.upper()})- assert first_res.status_code == 200- first_payload = first_res.json()- assert first_payload["code"] == 0, first_payload- assert first_payload["data"]["name"] == name.upper(), first_payload-- second_res = rest_client.post("/datasets", json={"name": name.lower()})- assert second_res.status_code == 200- second_payload = second_res.json()- assert second_payload["code"] == 0, second_payload- assert second_payload["data"]["name"] == f"{name.lower()}(1)", second_payload--- "name, chunk_method",- [- ("naive", "naive"),- ("book", "book"),- ("email", "email"),- ("laws", "laws"),- ("manual", "manual"),- ("one", "one"),- ("paper", "paper"),- ("picture", "picture"),- ("presentation", "presentation"),- ("qa", "qa"),- ("table", "table"),- ],- ids=["naive", "book", "email", "laws", "manual", "one", "paper", "picture", "presentation", "qa", "table"],-)-def test_dataset_create_chunk_method_contract(rest_client, clear_datasets, name, chunk_method):- res = rest_client.post("/datasets", json={"name": name, **_parser_id_fields(chunk_method)})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert payload["data"][PARSER_ID_FIELD] == _expected_chunk_method(chunk_method), payload--- "name, permission",- [- ("me", "me"),- ("team", "team"),- ],- ids=["me", "team"],-)-def test_dataset_create_permission_contract(rest_client, clear_datasets, name, permission):- res = rest_client.post("/datasets", json={"name": name, "permission": permission})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert payload["data"]["permission"] == permission, payload--- "name, embedding_model, expected_code, expected_embedding_model, expected_message, unauthorized_is_xfail",- [- ("builtin_baai", "BAAI/bge-small-en-v1.5@Local@Builtin", 0, "BAAI/bge-small-en-v1.5@Local@Builtin", None, False),- ("tenant_zhipu", "embedding-3@CI@ZHIPU-AI", 0, "embedding-3@CI@ZHIPU-AI", None, True),- ("embedding_model_unset", "__UNSET__", 0, "BAAI/bge-small-en-v1.5@Local@Builtin", None, False),- ("embedding_model_none", None, 0, "BAAI/bge-small-en-v1.5@Local@Builtin", None, False),- ("unknown_llm_name", "unknown@ZHIPU-AI", 102, None, "Model unknown@ZHIPU-AI not found for model embedding", False),- ("unknown_llm_factory", "embedding-3@unknown", 102, None, "Provider unknown not found for model embedding-3@unknown.", False),- (- "tenant_no_auth_default_tenant_llm",- "text-embedding-v3@Tongyi-Qianwen",- 102,- None,- "Provider Tongyi-Qianwen not found for model text-embedding-v3@Tongyi-Qianwen.",- False,- ),- ("tenant_no_auth", "text-embedding-3-small@OpenAI", 102, None, "Provider OpenAI not found for model text-embedding-3-small@OpenAI.", False),- ],- ids=[- "builtin_baai",- "tenant_zhipu",- "embedding_model_unset",- "embedding_model_none",- "unknown_llm_name",- "unknown_llm_factory",- "tenant_no_auth_default_tenant_llm",- "tenant_no_auth",- ],-)-def test_dataset_create_embedding_model_contract(rest_client, clear_datasets, name, embedding_model, expected_code, expected_embedding_model, expected_message, unauthorized_is_xfail):- req = {"name": name}- if embedding_model != "__UNSET__":- req["embedding_model"] = embedding_model- res = rest_client.post("/datasets", json=req)- assert res.status_code == 200- payload = res.json()- if unauthorized_is_xfail and payload["code"] == 102:- pytest.xfail(f"Environment has no authorized tenant model for {embedding_model}: {payload}")- assert payload["code"] == expected_code, payload- if expected_embedding_model is not None:- if name in {"embedding_model_unset", "embedding_model_none"}:- assert payload["data"]["embedding_model"].startswith("BAAI/bge-small-en-v1.5"), payload- else:- assert payload["data"]["embedding_model"] == expected_embedding_model, payload- if expected_message is not None:- assert "lookup failed: record not found" in payload["message"], payload--- "name, embedding_model, expected_fragment",- [- ("empty", "", "embedding model identifier must follow <model_name>@<provider> format"),- ("space", " ", "embedding model identifier must follow <model_name>@<provider> format"),- ("missing_at", "BAAI/bge-small-en-v1.5Builtin", "embedding model identifier must follow <model_name>@<provider> format"),- ("missing_model_name", "@Builtin", "both model_name and provider must be non-empty strings"),- ("missing_provider", "BAAI/bge-small-en-v1.5@", "both model_name and provider must be non-empty strings"),- ("whitespace_only_model_name", " @Builtin", "both model_name and provider must be non-empty strings"),- ("whitespace_only_provider", "BAAI/bge-small-env1.5@ ", "both model_name and provider must be non-empty strings"),- ],- ids=["empty", "space", "missing_at", "empty_model_name", "empty_provider", "whitespace_only_model_name", "whitespace_only_provider"],-)-def test_dataset_create_embedding_model_format_contract(rest_client, clear_datasets, name, embedding_model, expected_fragment):- res = rest_client.post("/datasets", json={"name": name, "embedding_model": embedding_model})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == ARGUMENT_ERROR_CODE, payload- assert expected_fragment in payload["message"], payload---def test_dataset_create_1k_contract(rest_client, clear_datasets):- for i in range(1_000):- res = rest_client.post("/datasets", json={"name": f"dataset_{i}"})- assert res.status_code == 200, (i, res.text)- payload = res.json()- assert payload["code"] == 0, (i, payload)---def test_dataset_create_concurrent_contract(rest_client, clear_datasets):- count = 100- with ThreadPoolExecutor(max_workers=5) as executor:- futures = [executor.submit(rest_client.post, "/datasets", json={"name": f"dataset_{i}"}) for i in range(count)]- responses = list(as_completed(futures))- assert len(responses) == count, responses- for index, future in enumerate(futures):- res = future.result()- assert res.status_code == 200, (index, res.text)- payload = res.json()- assert payload["code"] == 0, (index, payload)---def test_dataset_create_name_invalid_and_duplicate_contract(rest_client, clear_datasets):- invalid_cases = [- ("", "String should have at least 1 character"),- (" ", "String should have at least 1 character"),- ("a" * (DATASET_NAME_LIMIT + 1), f"String should have at most {DATASET_NAME_LIMIT} characters"),- (0, "Input should be a valid string"),- (None, "Input should be a valid string"),- ]- for name, expected_message in invalid_cases:- res = rest_client.post("/datasets", json={"name": name})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- assert expected_message in payload["message"], payload-- create_res = rest_client.post("/datasets", json={"name": "duplicated_name"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload-- duplicate_res = rest_client.post("/datasets", json={"name": "duplicated_name"})- assert duplicate_res.status_code == 200- duplicate_payload = duplicate_res.json()- assert duplicate_payload["code"] == 0, duplicate_payload- assert duplicate_payload["data"]["name"] == "duplicated_name(1)", duplicate_payload---def test_dataset_create_content_type_and_payload_bad_contract(rest_client):- bad_content_type = "text/xml"- bad_content_type_res = rest_client.post(- "/datasets",- data='{"name": "bad_content_type"}',- headers={"Content-Type": bad_content_type},- )- assert bad_content_type_res.status_code == 200- bad_content_type_payload = bad_content_type_res.json()- assert bad_content_type_payload["code"] == 101, bad_content_type_payload- assert f"Unsupported content type: Expected application/json, got {bad_content_type}" in bad_content_type_payload["message"], bad_content_type_payload-- malformed_json_res = rest_client.post("/datasets", data="a")- assert malformed_json_res.status_code == 200- malformed_json_payload = malformed_json_res.json()- assert malformed_json_payload["code"] == 101, malformed_json_payload- assert "Malformed JSON syntax: Missing commas/brackets or invalid encoding" in malformed_json_payload["message"], malformed_json_payload-- invalid_payload_type_res = rest_client.post("/datasets", data='"a"')- assert invalid_payload_type_res.status_code == 200- invalid_payload_type_payload = invalid_payload_type_res.json()- assert invalid_payload_type_payload["code"] == 101, invalid_payload_type_payload- assert "Invalid request payload: expected object, got str" in invalid_payload_type_payload["message"], invalid_payload_type_payload---def test_dataset_create_permission_and_chunk_method_contract(rest_client, clear_datasets):- permission_invalid_cases = [- ("empty", ""),- ("unknown", "unknown"),- ("type_error", []),- ("me_upercase", "ME"),- ("team_upercase", "TEAM"),- ("whitespace", " ME "),- ]- for name, permission in permission_invalid_cases:- res = rest_client.post("/datasets", json={"name": name, "permission": permission})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == ARGUMENT_ERROR_CODE, payload- if not isinstance(permission, str):- assert "cannot unmarshal" in payload["message"] and ".permission" in payload["message"], payload- else:- assert "Input should be 'me' or 'team'" in payload["message"], payload-- permission_unset_res = rest_client.post("/datasets", json={"name": "permission_unset"})- assert permission_unset_res.status_code == 200- permission_unset_payload = permission_unset_res.json()- assert permission_unset_payload["code"] == 0, permission_unset_payload- assert permission_unset_payload["data"]["permission"] == "me", permission_unset_payload-- chunk_method_invalid_cases = [- ("chunk_empty", ""),- ("chunk_unknown", "unknown"),- ("chunk_type_error", []),- ]- for name, chunk_method in chunk_method_invalid_cases:- res = rest_client.post("/datasets", json={"name": name, **_parser_id_fields(chunk_method)})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == ARGUMENT_ERROR_CODE, payload- if not isinstance(chunk_method, str):- assert "cannot unmarshal" in payload["message"] and f".{PARSER_ID_FIELD}" in payload["message"], payload- elif chunk_method == "":- assert payload["message"] == "parser_id is required when parse_type is BuiltIn", payload- else:- assert payload["message"].startswith("input should be 'general', 'qa'") and payload["message"].endswith("or 'email'"), payload-- chunk_method_none_res = rest_client.post("/datasets", json={"name": "chunk_method_none", **_parser_id_fields(None)})- assert chunk_method_none_res.status_code == 200- chunk_method_none_payload = chunk_method_none_res.json()- assert chunk_method_none_payload["code"] == ARGUMENT_ERROR_CODE, chunk_method_none_payload- assert chunk_method_none_payload["message"] == "parser_id is required when parse_type is BuiltIn", chunk_method_none_payload-- chunk_method_unset_res = rest_client.post("/datasets", json={"name": "chunk_method_unset"})- assert chunk_method_unset_res.status_code == 200- chunk_method_unset_payload = chunk_method_unset_res.json()- assert chunk_method_unset_payload["code"] == 0, chunk_method_unset_payload- assert chunk_method_unset_payload["data"][PARSER_ID_FIELD] == _expected_chunk_method("naive"), chunk_method_unset_payload---def test_dataset_create_parser_config_defaults_and_extra_fields_contract(rest_client, clear_datasets):- empty_res = rest_client.post("/datasets", json={"name": "parser_config_empty", "parser_config": {}})- assert empty_res.status_code == 200- empty_payload = empty_res.json()- assert empty_payload["code"] == 0, empty_payload-- unset_res = rest_client.post("/datasets", json={"name": "parser_config_unset"})- assert unset_res.status_code == 200- unset_payload = unset_res.json()- assert unset_payload["code"] == 0, unset_payload-- none_res = rest_client.post("/datasets", json={"name": "parser_config_none", "parser_config": None})- assert none_res.status_code == 200- none_payload = none_res.json()- assert none_payload["code"] == 0, none_payload-- empty_parser_config = empty_payload["data"]["parser_config"]- unset_parser_config = unset_payload["data"]["parser_config"]- none_parser_config = none_payload["data"]["parser_config"]- assert empty_parser_config == unset_parser_config == none_parser_config- _assert_go_pipeline_parser_config(empty_parser_config)-- unsupported_field_payloads = [- {"name": "id", "id": "id"},- {"name": "tenant_id", "tenant_id": "e57c1966f99211efb41e9e45646e0111"},- {"name": "created_by", "created_by": "created_by"},- {"name": "create_date", "create_date": "Tue, 11 Mar 2025 13:37:23 GMT"},- {"name": "create_time", "create_time": 1741671443322},- {"name": "update_date", "update_date": "Tue, 11 Mar 2025 13:37:23 GMT"},- {"name": "update_time", "update_time": 1741671443339},- {"name": "document_count", "document_count": 1},- {"name": "chunk_count", "chunk_count": 1},- {"name": "token_num", "token_num": 1},- {"name": "status", "status": "1"},- {"name": "pagerank", "pagerank": 50},- {"name": "unknown_field", "unknown_field": "unknown_field"},- ]- for payload_data in unsupported_field_payloads:- res = rest_client.post("/datasets", json=payload_data)- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- assert "Extra inputs are not permitted" in payload["message"], payload---def test_dataset_list_ordering_and_pagination(rest_client, clear_datasets):- for i in range(3):- res = rest_client.post("/datasets", json={"name": f"dataset_page_{i}"})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload-- list_res = rest_client.get(- "/datasets",- params={"page": 1, "page_size": 2, "orderby": "create_time", "desc": "true"},- )- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert len(list_payload["data"]) == 2, list_payload- assert list_payload.get("total_datasets", 0) >= 3, list_payload---def test_dataset_delete_contract_matrix(rest_client, clear_datasets):- ids = []- for i in range(3):- create_res = rest_client.post("/datasets", json={"name": f"dataset_delete_matrix_{i}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- ids.append(create_payload["data"]["id"])-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- auth_res = client.delete("/datasets", json={"ids": [ids[0]]})- assert auth_res.status_code == 401, (scenario_name, auth_res.text)- auth_payload = auth_res.json()- assert_auth_error(auth_payload, scenario_name)-- bad_content_type = "text/xml"- bad_content_type_res = rest_client.delete(- "/datasets",- data='{"ids": []}',- headers={"Content-Type": bad_content_type},- )- assert bad_content_type_res.status_code == 200- bad_content_type_payload = bad_content_type_res.json()- assert bad_content_type_payload["code"] == 101, bad_content_type_payload- assert f"Unsupported content type: Expected application/json, got {bad_content_type}" in bad_content_type_payload["message"], bad_content_type_payload-- malformed_json_res = rest_client.delete("/datasets", data="a")- assert malformed_json_res.status_code == 200- malformed_json_payload = malformed_json_res.json()- assert malformed_json_payload["code"] == 101, malformed_json_payload- assert "Malformed JSON syntax: Missing commas/brackets or invalid encoding" in malformed_json_payload["message"], malformed_json_payload-- invalid_payload_type_res = rest_client.delete("/datasets", data='"a"')- assert invalid_payload_type_res.status_code == 200- invalid_payload_type_payload = invalid_payload_type_res.json()- assert invalid_payload_type_payload["code"] == 101, invalid_payload_type_payload- assert "Invalid request payload: expected object, got str" in invalid_payload_type_payload["message"], invalid_payload_type_payload-- unset_payload_res = rest_client.delete("/datasets")- assert unset_payload_res.status_code == 200- unset_payload = unset_payload_res.json()- assert unset_payload["code"] == 101, unset_payload- assert "Malformed JSON syntax: Missing commas/brackets or invalid encoding" in unset_payload["message"], unset_payload-- single_delete_res = rest_client.delete("/datasets", json={"ids": [ids[0]]})- assert single_delete_res.status_code == 200- single_delete_payload = single_delete_res.json()- assert single_delete_payload["code"] == 0, single_delete_payload-- list_after_single = rest_client.get("/datasets")- assert list_after_single.status_code == 200- list_after_single_payload = list_after_single.json()- assert list_after_single_payload["code"] == 0, list_after_single_payload- assert len(list_after_single_payload["data"]) == 2, list_after_single_payload-- ids_empty_res = rest_client.delete("/datasets", json={"ids": []})- assert ids_empty_res.status_code == 200- ids_empty_payload = ids_empty_res.json()- assert ids_empty_payload["code"] == 0, ids_empty_payload-- ids_none_res = rest_client.delete("/datasets", json={"ids": None})- assert ids_none_res.status_code == 200- ids_none_payload = ids_none_res.json()- assert ids_none_payload["code"] == 0, ids_none_payload-- id_not_uuid_res = rest_client.delete("/datasets", json={"ids": ["not_uuid"]})- assert id_not_uuid_res.status_code == 200- id_not_uuid_payload = id_not_uuid_res.json()- assert id_not_uuid_payload["code"] == 101, id_not_uuid_payload- assert "Invalid UUID format" in id_not_uuid_payload["message"], id_not_uuid_payload-- id_not_uuid1_res = rest_client.delete("/datasets", json={"ids": [uuid.uuid4().hex]})- assert id_not_uuid1_res.status_code == 200- id_not_uuid1_payload = id_not_uuid1_res.json()- assert id_not_uuid1_payload["code"] == 403, id_not_uuid1_payload- assert id_not_uuid1_payload["message"] == "Permission denied", id_not_uuid1_payload-- id_wrong_uuid_res = rest_client.delete("/datasets", json={"ids": ["d94a8dc02c9711f0930f7fbc369eab6d"]})- assert id_wrong_uuid_res.status_code == 200- id_wrong_uuid_payload = id_wrong_uuid_res.json()- assert id_wrong_uuid_payload["code"] == 403, id_wrong_uuid_payload- assert id_wrong_uuid_payload["message"] == "Permission denied", id_wrong_uuid_payload-- list_res = rest_client.get("/datasets")- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- remaining_ids = [dataset["id"] for dataset in list_payload["data"]]-- for invalid_ids in (- ["d94a8dc02c9711f0930f7fbc369eab6d"] + remaining_ids,- remaining_ids[:1] + ["d94a8dc02c9711f0930f7fbc369eab6d"] + remaining_ids[1:],- remaining_ids + ["d94a8dc02c9711f0930f7fbc369eab6d"],- ):- partial_invalid_res = rest_client.delete("/datasets", json={"ids": invalid_ids})- assert partial_invalid_res.status_code == 200- partial_invalid_payload = partial_invalid_res.json()- assert partial_invalid_payload["code"] == 403, partial_invalid_payload- assert partial_invalid_payload["message"] == "Permission denied", partial_invalid_payload-- duplicate_ids_res = rest_client.delete("/datasets", json={"ids": remaining_ids + remaining_ids})- assert duplicate_ids_res.status_code == 200- duplicate_ids_payload = duplicate_ids_res.json()- assert duplicate_ids_payload["code"] == 101, duplicate_ids_payload- assert "Duplicate ids:" in duplicate_ids_payload["message"], duplicate_ids_payload-- repeated_delete_payload = {"ids": remaining_ids}- first_delete_res = rest_client.delete("/datasets", json=repeated_delete_payload)- assert first_delete_res.status_code == 200- first_delete_payload = first_delete_res.json()- assert first_delete_payload["code"] == 0, first_delete_payload-- second_delete_res = rest_client.delete("/datasets", json=repeated_delete_payload)- assert second_delete_res.status_code == 200- second_delete_payload = second_delete_res.json()- assert second_delete_payload["code"] == 403, second_delete_payload- assert second_delete_payload["message"] == "Permission denied", second_delete_payload-- unsupported_field_res = rest_client.delete("/datasets", json={"unknown_field": "unknown_field"})- assert unsupported_field_res.status_code == 200- unsupported_field_payload = unsupported_field_res.json()- assert unsupported_field_payload["code"] == 101, unsupported_field_payload- assert "Extra inputs are not permitted" in unsupported_field_payload["message"], unsupported_field_payload---def test_dataset_delete_bulk_and_concurrent_contract(rest_client, clear_datasets):- bulk_ids = []- for i in range(1000):- create_res = rest_client.post("/datasets", json={"name": f"dataset_delete_bulk_{i}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- bulk_ids.append(create_payload["data"]["id"])-- bulk_delete_res = rest_client.delete("/datasets", json={"ids": bulk_ids})- assert bulk_delete_res.status_code == 200- bulk_delete_payload = bulk_delete_res.json()- assert bulk_delete_payload["code"] == 0, bulk_delete_payload-- list_after_bulk_delete = rest_client.get("/datasets")- assert list_after_bulk_delete.status_code == 200- list_after_bulk_delete_payload = list_after_bulk_delete.json()- assert list_after_bulk_delete_payload["code"] == 0, list_after_bulk_delete_payload- assert len(list_after_bulk_delete_payload["data"]) == 0, list_after_bulk_delete_payload-- concurrent_ids = []- for i in range(100):- create_res = rest_client.post("/datasets", json={"name": f"dataset_delete_concurrent_{i}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- concurrent_ids.append(create_payload["data"]["id"])-- with ThreadPoolExecutor(max_workers=5) as executor:- futures = [executor.submit(rest_client.delete, "/datasets", json={"ids": [dataset_id]}) for dataset_id in concurrent_ids]-- responses = list(as_completed(futures))- assert len(responses) == len(concurrent_ids), responses- for future in futures:- res = future.result()- assert res.status_code == 200, res.text- payload = res.json()- assert payload["code"] == 0, payload---def test_dataset_list_requires_auth_contract(rest_client, clear_datasets):- rest_client.post("/datasets", json={"name": "dataset_list_auth_contract"})-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.get("/datasets")- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_dataset_list_query_contract_matrix(rest_client, clear_datasets):- dataset_ids = []- for i in range(5):- create_res = rest_client.post("/datasets", json={"name": f"dataset_{i}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- dataset_ids.append(create_payload["data"]["id"])-- params_unset_res = rest_client.get("/datasets")- assert params_unset_res.status_code == 200- params_unset_payload = params_unset_res.json()- assert params_unset_payload["code"] == 0, params_unset_payload- assert len(params_unset_payload["data"]) == 5, params_unset_payload-- params_empty_res = rest_client.get("/datasets", params={})- assert params_empty_res.status_code == 200- params_empty_payload = params_empty_res.json()- assert params_empty_payload["code"] == 0, params_empty_payload- assert len(params_empty_payload["data"]) == 5, params_empty_payload-- for params, expected_size in (- ({"page": 2, "page_size": 2}, 2),- ({"page": 3, "page_size": 2}, 1),- ({"page": 4, "page_size": 2}, 0),- ({"page": "2", "page_size": 2}, 2),- ({"page": 1, "page_size": 10}, 5),- ):- page_res = rest_client.get("/datasets", params=params)- assert page_res.status_code == 200- page_payload = page_res.json()- assert page_payload["code"] == 0, page_payload- assert len(page_payload["data"]) == expected_size, page_payload-- for params, expected_fragment in (- ({"page": 0}, "Input should be greater than or equal to 1"),- ({"page": "a"}, "Input should be a valid integer, unable to parse string as an integer"),- ):- page_invalid_res = rest_client.get("/datasets", params=params)- assert page_invalid_res.status_code == 200- page_invalid_payload = page_invalid_res.json()- assert page_invalid_payload["code"] == 101, page_invalid_payload- assert expected_fragment in page_invalid_payload["message"], page_invalid_payload-- page_none_res = rest_client.get("/datasets", params={"page": None})- assert page_none_res.status_code == 200- page_none_payload = page_none_res.json()- assert page_none_payload["code"] == 0, page_none_payload- assert len(page_none_payload["data"]) == 5, page_none_payload-- for params, expected_size in (- ({"page_size": 1}, 1),- ({"page_size": 3}, 3),- ({"page_size": 5}, 5),- ({"page_size": 6}, 5),- ({"page_size": "1"}, 1),- ):- page_size_res = rest_client.get("/datasets", params=params)- assert page_size_res.status_code == 200- page_size_payload = page_size_res.json()- assert page_size_payload["code"] == 0, page_size_payload- assert len(page_size_payload["data"]) == expected_size, page_size_payload-- for params, expected_fragment in (- ({"page_size": 0}, "Input should be greater than or equal to 1"),- ({"page_size": "a"}, "Input should be a valid integer, unable to parse string as an integer"),- ):- page_size_invalid_res = rest_client.get("/datasets", params=params)- assert page_size_invalid_res.status_code == 200- page_size_invalid_payload = page_size_invalid_res.json()- assert page_size_invalid_payload["code"] == 101, page_size_invalid_payload- assert expected_fragment in page_size_invalid_payload["message"], page_size_invalid_payload-- page_size_none_res = rest_client.get("/datasets", params={"page_size": None})- assert page_size_none_res.status_code == 200- page_size_none_payload = page_size_none_res.json()- assert page_size_none_payload["code"] == 0, page_size_none_payload- assert len(page_size_none_payload["data"]) == 5, page_size_none_payload-- for params in ({"orderby": "create_time"}, {"orderby": "update_time"}):- orderby_res = rest_client.get("/datasets", params=params)- assert orderby_res.status_code == 200- orderby_payload = orderby_res.json()- assert orderby_payload["code"] == 0, orderby_payload-- for params in (- {"orderby": ""},- {"orderby": "unknown"},- {"orderby": "CREATE_TIME"},- {"orderby": "UPDATE_TIME"},- {"orderby": " create_time "},- ):- orderby_invalid_res = rest_client.get("/datasets", params=params)- assert orderby_invalid_res.status_code == 200- orderby_invalid_payload = orderby_invalid_res.json()- assert orderby_invalid_payload["code"] == 101, orderby_invalid_payload- assert "Input should be 'create_time' or 'update_time'" in orderby_invalid_payload["message"], orderby_invalid_payload-- orderby_none_res = rest_client.get("/datasets", params={"orderby": None})- assert orderby_none_res.status_code == 200- orderby_none_payload = orderby_none_res.json()- assert orderby_none_payload["code"] == 0, orderby_none_payload-- for params in (- {"desc": True},- {"desc": False},- {"desc": "true"},- {"desc": "false"},- {"desc": 1},- {"desc": 0},- {"desc": "yes"},- {"desc": "no"},- {"desc": "y"},- {"desc": "n"},- ):- desc_res = rest_client.get("/datasets", params=params)- assert desc_res.status_code == 200- desc_payload = desc_res.json()- assert desc_payload["code"] == 0, desc_payload-- for params in ({"desc": 3.14}, {"desc": "unknown"}):- desc_invalid_res = rest_client.get("/datasets", params=params)- assert desc_invalid_res.status_code == 200- desc_invalid_payload = desc_invalid_res.json()- assert desc_invalid_payload["code"] == 101, desc_invalid_payload- assert "Input should be a valid boolean, unable to interpret input" in desc_invalid_payload["message"], desc_invalid_payload-- desc_none_res = rest_client.get("/datasets", params={"desc": None})- assert desc_none_res.status_code == 200- desc_none_payload = desc_none_res.json()- assert desc_none_payload["code"] == 0, desc_none_payload-- name_res = rest_client.get("/datasets", params={"name": "dataset_1"})- assert name_res.status_code == 200- name_payload = name_res.json()- assert name_payload["code"] == 0, name_payload- assert len(name_payload["data"]) == 1, name_payload- assert name_payload["data"][0]["name"] == "dataset_1", name_payload-- name_wrong_res = rest_client.get("/datasets", params={"name": "wrong name"})- assert name_wrong_res.status_code == 200- name_wrong_payload = name_wrong_res.json()- assert name_wrong_payload["code"] == 0, name_wrong_payload- assert name_wrong_payload["data"] == [], name_wrong_payload-- name_empty_res = rest_client.get("/datasets", params={"name": ""})- assert name_empty_res.status_code == 200- name_empty_payload = name_empty_res.json()- assert name_empty_payload["code"] == 0, name_empty_payload- assert len(name_empty_payload["data"]) == 5, name_empty_payload-- name_none_res = rest_client.get("/datasets", params={"name": None})- assert name_none_res.status_code == 200- name_none_payload = name_none_res.json()- assert name_none_payload["code"] == 0, name_none_payload- assert len(name_none_payload["data"]) == 5, name_none_payload-- id_res = rest_client.get("/datasets", params={"id": dataset_ids[0]})- assert id_res.status_code == 200- id_payload = id_res.json()- assert id_payload["code"] == 0, id_payload- assert len(id_payload["data"]) == 1, id_payload- assert id_payload["data"][0]["id"] == dataset_ids[0], id_payload-- id_not_uuid_res = rest_client.get("/datasets", params={"id": "not_uuid"})- assert id_not_uuid_res.status_code == 200- id_not_uuid_payload = id_not_uuid_res.json()- assert id_not_uuid_payload["code"] == 101, id_not_uuid_payload- assert "Invalid UUID format" in id_not_uuid_payload["message"], id_not_uuid_payload-- id_not_uuid1_res = rest_client.get("/datasets", params={"id": uuid.uuid4().hex})- assert id_not_uuid1_res.status_code == 200- id_not_uuid1_payload = id_not_uuid1_res.json()- assert id_not_uuid1_payload["code"] == 404, id_not_uuid1_payload- assert id_not_uuid1_payload["message"] == "Resource not found", id_not_uuid1_payload-- id_wrong_uuid_res = rest_client.get("/datasets", params={"id": "d94a8dc02c9711f0930f7fbc369eab6d"})- assert id_wrong_uuid_res.status_code == 200- id_wrong_uuid_payload = id_wrong_uuid_res.json()- assert id_wrong_uuid_payload["code"] == 404, id_wrong_uuid_payload- assert id_wrong_uuid_payload["message"] == "Resource not found", id_wrong_uuid_payload-- id_empty_res = rest_client.get("/datasets", params={"id": ""})- assert id_empty_res.status_code == 200- id_empty_payload = id_empty_res.json()- assert id_empty_payload["code"] == 101, id_empty_payload- assert "Invalid UUID format" in id_empty_payload["message"], id_empty_payload-- id_none_res = rest_client.get("/datasets", params={"id": None})- assert id_none_res.status_code == 200- id_none_payload = id_none_res.json()- assert id_none_payload["code"] == 0, id_none_payload- assert len(id_none_payload["data"]) == 5, id_none_payload-- name_id_match_res = rest_client.get("/datasets", params={"id": dataset_ids[0], "name": "dataset_0"})- assert name_id_match_res.status_code == 200- name_id_match_payload = name_id_match_res.json()- assert name_id_match_payload["code"] == 0, name_id_match_payload- assert len(name_id_match_payload["data"]) == 1, name_id_match_payload-- name_id_mismatch_res = rest_client.get("/datasets", params={"id": dataset_ids[0], "name": "dataset_1"})- assert name_id_mismatch_res.status_code == 200- name_id_mismatch_payload = name_id_mismatch_res.json()- assert name_id_mismatch_payload["code"] == 0, name_id_mismatch_payload- assert len(name_id_mismatch_payload["data"]) == 0, name_id_mismatch_payload-- name_id_wrong_res = rest_client.get("/datasets", params={"id": dataset_ids[0], "name": "wrong_name"})- assert name_id_wrong_res.status_code == 200- name_id_wrong_payload = name_id_wrong_res.json()- assert name_id_wrong_payload["code"] == 0, name_id_wrong_payload- assert name_id_wrong_payload["data"] == [], name_id_wrong_payload-- missing_id_name_res = rest_client.get("/datasets", params={"id": uuid.uuid1().hex, "name": "dataset_0"})- assert missing_id_name_res.status_code == 200- missing_id_name_payload = missing_id_name_res.json()- assert missing_id_name_payload["code"] == 404, missing_id_name_payload- assert missing_id_name_payload["message"] == "Resource not found", missing_id_name_payload-- unsupported_field_res = rest_client.get("/datasets", params={"unknown_field": "unknown_field"})- assert unsupported_field_res.status_code == 200- unsupported_field_payload = unsupported_field_res.json()- assert unsupported_field_payload["code"] == 101, unsupported_field_payload- assert "Extra inputs are not permitted" in unsupported_field_payload["message"], unsupported_field_payload---def test_dataset_list_concurrent_contract(rest_client, clear_datasets):- for i in range(5):- create_res = rest_client.post("/datasets", json={"name": f"dataset_list_concurrent_{i}"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload-- with ThreadPoolExecutor(max_workers=5) as executor:- futures = [executor.submit(rest_client.get, "/datasets") for _ in range(100)]- responses = list(as_completed(futures))- assert len(responses) == 100, responses- for future in futures:- res = future.result()- assert res.status_code == 200, res.text- payload = res.json()- assert payload["code"] == 0, payload---def test_dataset_get_contract(rest_client, create_dataset):- dataset_id = create_dataset("dataset_get_success")-- success_res = rest_client.get(f"/datasets/{dataset_id}")- assert success_res.status_code == 200- success_payload = success_res.json()- assert success_payload["code"] == 0, success_payload- assert success_payload["data"]["id"] == dataset_id, success_payload-- invalid_id_res = rest_client.get("/datasets/invalid_dataset_id")- assert invalid_id_res.status_code == 200- invalid_id_payload = invalid_id_res.json()- assert invalid_id_payload["code"] != 0, invalid_id_payload-- unauthorized_res = RestClient(token=INVALID_API_TOKEN).get(f"/datasets/{dataset_id}")- assert unauthorized_res.status_code == 401- unauthorized_payload = unauthorized_res.json()- assert unauthorized_payload["code"] == 401, unauthorized_payload-- nonexistent_res = rest_client.get(f"/datasets/{'0' * 32}")- assert nonexistent_res.status_code == 200- nonexistent_payload = nonexistent_res.json()- assert nonexistent_payload["code"] != 0, nonexistent_payload---def test_dataset_metadata_config_get_and_update_contract(rest_client, create_dataset):- dataset_id = create_dataset("dataset_metadata_config_contract")-- success_res = rest_client.get(f"/datasets/{dataset_id}/metadata/config")- assert success_res.status_code == 200- success_payload = success_res.json()- assert success_payload["code"] == 0, success_payload- expected_empty = {"enabled": False, "metadata": [], "built_in_metadata": []}- assert success_payload["data"] == expected_empty, success_payload-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- get_res = client.get(f"/datasets/{dataset_id}/metadata/config")- assert get_res.status_code == 401, (scenario_name, get_res.text)- get_payload = get_res.json()- assert_auth_error(get_payload, scenario_name)-- put_res = client.put(- f"/datasets/{dataset_id}/metadata/config",- json={"metadata": [], "built_in_metadata": []},- )- assert put_res.status_code == 401, (scenario_name, put_res.text)- put_payload = put_res.json()- assert_auth_error(put_payload, scenario_name)-- invalid_dataset_res = rest_client.get("/datasets/invalid_dataset_id/metadata/config")- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 404, invalid_dataset_payload- assert invalid_dataset_payload["message"] == "Resource not found", invalid_dataset_payload-- update_payload = {- "metadata": [- {"key": "author", "type": "string", "description": "Author name"},- {"key": "tags", "type": "list", "description": "Tag list", "enum": ["foo", "bar"]},- ],- "built_in_metadata": [- {"key": "size", "type": "number", "description": "File size"},- ],- }- normalized_update_payload = {- "metadata": [- {"key": "author", "type": "string", "description": "Author name", "enum": None},- {"key": "tags", "type": "list", "description": "Tag list", "enum": ["foo", "bar"]},- ],- "built_in_metadata": [- {"key": "size", "type": "number", "description": "File size", "enum": None},- ],- }- # Go keeps the dataset-level metadata.enabled flag: it is not derived- # from whether fields are present, so a fresh dataset (flag disabled)- # stays disabled until enabled explicitly.- normalized_update_payload["enabled"] = False- update_res = rest_client.put(f"/datasets/{dataset_id}/metadata/config", json=update_payload)- assert update_res.status_code == 200- update_body = update_res.json()- assert update_body["code"] == 0, update_body- assert update_body["data"] == normalized_update_payload, update_body-- refetch_res = rest_client.get(f"/datasets/{dataset_id}/metadata/config")- assert refetch_res.status_code == 200- refetch_payload = refetch_res.json()- assert refetch_payload["code"] == 0, refetch_payload- assert refetch_payload["data"] == normalized_update_payload, refetch_payload-- missing_payload_res = rest_client.put(f"/datasets/{dataset_id}/metadata/config", json={})- assert missing_payload_res.status_code == 200- missing_payload = missing_payload_res.json()- assert missing_payload["code"] == 0, missing_payload- assert missing_payload["data"] == expected_empty, missing_payload-- invalid_update_dataset_res = rest_client.put(- "/datasets/invalid_dataset_id/metadata/config",- json={"metadata": [], "built_in_metadata": []},- )- assert invalid_update_dataset_res.status_code == 200- invalid_update_dataset_payload = invalid_update_dataset_res.json()- assert invalid_update_dataset_payload["code"] == 404, invalid_update_dataset_payload- assert invalid_update_dataset_payload["message"] == "Resource not found", invalid_update_dataset_payload---def test_dataset_metadata_summary_contract(rest_client, create_dataset, tmp_path):- dataset_id = create_dataset("dataset_metadata_summary")- document_ids = []- for i in range(3):- fp = create_txt_file(tmp_path / f"metadata_summary_{i}.txt")- with fp.open("rb") as file_obj:- upload_res = rest_client.post(- f"/datasets/{dataset_id}/documents",- files=[("file", (fp.name, file_obj))],- )- assert upload_res.status_code == 200- upload_payload = upload_res.json()- assert upload_payload["code"] == 0, upload_payload- document_ids.append(upload_payload["data"][0]["id"])-- payloads = [- {"tags": ["foo", "bar"], "author": "alice"},- {"tags": ["foo"], "author": "bob"},- {"tags": ["bar", "baz"], "author": ""},- ]- for document_id, meta_fields in zip(document_ids, payloads):- update_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{document_id}",- json={"meta_fields": meta_fields},- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload-- success_res = rest_client.get(f"/datasets/{dataset_id}/metadata/summary")- assert success_res.status_code == 200- success_payload = success_res.json()- assert success_payload["code"] == 0, success_payload- assert "summary" in success_payload["data"], success_payload-- summary = success_payload["data"]["summary"]- counts = {}- for key, field_data in summary.items():- counts[key] = {str(k): v for k, v in field_data["values"]}- assert counts["tags"]["foo"] == 2, counts- assert counts["tags"]["bar"] == 2, counts- assert counts["tags"]["baz"] == 1, counts- assert counts["author"]["alice"] == 1, counts- assert counts["author"]["bob"] == 1, counts- assert "None" not in counts["author"], counts-- invalid_dataset_id = f"invalid_{dataset_id}"- invalid_dataset_res = rest_client.get(f"/datasets/{invalid_dataset_id}/metadata/summary")- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 102, invalid_dataset_payload- assert invalid_dataset_payload["message"] == f"You don't own the dataset {invalid_dataset_id}. ", invalid_dataset_payload-- nonexistent_res = rest_client.get(f"/datasets/{'0' * 32}/metadata/summary")- assert nonexistent_res.status_code == 200- nonexistent_payload = nonexistent_res.json()- assert nonexistent_payload["code"] == 102, nonexistent_payload---def test_dataset_search_endpoint(rest_client, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- res = rest_client.post(- f"/datasets/{dataset_id}/search",- json={"question": "test TXT file", "page": 1, "page_size": 10},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert "chunks" in payload["data"], payload--- "payload",- [- {"question": "test TXT file", "page": 1, "page_size": 2},- {"question": "test TXT file", "similarity_threshold": 0.5},- {"question": "test TXT file", "vector_similarity_weight": 0.7},- {"question": "test TXT file", "top_k": 10},- ],- ids=["page_size", "similarity_threshold", "vector_similarity_weight", "top_k"],-)-def test_dataset_search_params_and_document_ids_contract(rest_client, ensure_parsed_document, payload):- dataset_id, document_id = ensure_parsed_document()- search_payload = dict(payload)- search_payload["document_ids"] = [document_id]- res = rest_client.post(f"/datasets/{dataset_id}/search", json=search_payload)- assert res.status_code == 200- body = res.json()- assert body["code"] == 0, body- assert "chunks" in body["data"], body---def test_dataset_search_requires_question(rest_client, create_dataset):- dataset_id = create_dataset("dataset_search_missing_question")- res = rest_client.post(f"/datasets/{dataset_id}/search", json={})- assert res.status_code == 200- payload = res.json()- assert payload["code"] in [101, 102], payload- assert "question" in payload["message"], payload---def test_dataset_tags_and_aggregation(rest_client, create_dataset):- dataset_id = create_dataset("dataset_tags")- second_dataset_id = create_dataset("dataset_tags_second")-- # The aggregation route is asserted before the per-dataset tag routes,- # which the Go API does not serve.- aggregate_res = rest_client.get(- "/datasets/tags/aggregation",- params={"dataset_ids": f"{dataset_id},{second_dataset_id}"},- )- assert aggregate_res.status_code == 200- aggregate_payload = aggregate_res.json()- assert aggregate_payload["code"] in (0, 102), aggregate_payload-- empty_aggregate_res = rest_client.get("/datasets/tags/aggregation")- assert empty_aggregate_res.status_code == 200- empty_aggregate_payload = empty_aggregate_res.json()- assert empty_aggregate_payload["code"] != 0, empty_aggregate_payload-- # Go keeps no tag dataset, so the per-dataset tag routes are not served- # at all; tagging is driven by the dataset's tag source file and only- # the aggregation route survives. The 404s below replace the old 200- # responses.- assert rest_client.get(f"/datasets/{dataset_id}/tags").status_code == 404- assert rest_client.get("/datasets/invalid_id/tags").status_code == 404---def test_dataset_tags_delete_and_rename_validation(rest_client, create_dataset):- dataset_id = create_dataset("dataset_tag_mutation")-- # With no tag dataset there is nothing to rename or delete, so both the- # validation cases and the invalid-dataset cases are unrouted instead of- # argument errors.- for url in (f"/datasets/{dataset_id}/tags", "/datasets/invalid_id/tags"):- assert rest_client.delete(url, json={"tags": ["tag1"]}).status_code == 404- assert rest_client.put(url, json={"from_tag": "old", "to_tag": "new"}).status_code == 404---def test_dataset_flattened_metadata(rest_client, create_dataset):- first_dataset_id = create_dataset("flattened_meta_1")- second_dataset_id = create_dataset("flattened_meta_2")-- flattened_res = rest_client.get(- "/datasets/metadata/flattened",- params={"dataset_ids": f"{first_dataset_id},{second_dataset_id}"},- )- assert flattened_res.status_code == 200- flattened_payload = flattened_res.json()- assert flattened_payload["code"] == 0, flattened_payload-- empty_ids_res = rest_client.get("/datasets/metadata/flattened")- assert empty_ids_res.status_code == 200- empty_ids_payload = empty_ids_res.json()- assert empty_ids_payload["code"] != 0, empty_ids_payload-- invalid_dataset_res = rest_client.get(- "/datasets/metadata/flattened",- params={"dataset_ids": "invalid_id"},- )- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] != 0, invalid_dataset_payload---def test_dataset_ingestion_summary_and_logs(rest_client, create_dataset):- dataset_id = create_dataset("dataset_ingestions")-- summary_res = rest_client.get(f"/datasets/{dataset_id}/ingestions/summary")- assert summary_res.status_code == 200- summary_payload = summary_res.json()- assert summary_payload["code"] == 0, summary_payload- assert "doc_num" in summary_payload["data"], summary_payload- assert "chunk_num" in summary_payload["data"], summary_payload- assert "token_num" in summary_payload["data"], summary_payload- assert "status" in summary_payload["data"], summary_payload-- logs_res = rest_client.get(- f"/datasets/{dataset_id}/ingestions",- params={"page": 1, "page_size": 10},- )- assert logs_res.status_code == 200- logs_payload = logs_res.json()- assert logs_payload["code"] == 0, logs_payload- assert "total" in logs_payload["data"], logs_payload- assert "logs" in logs_payload["data"], logs_payload-- abnormal_date_filter_res = rest_client.get(- f"/datasets/{dataset_id}/ingestions",- params={- "desc": "false",- "create_date_from": "2025-02-01",- "create_date_to": "2025-01-01",- },- )- assert abnormal_date_filter_res.status_code == 200- abnormal_date_filter_payload = abnormal_date_filter_res.json()- assert abnormal_date_filter_payload["code"] == 0, abnormal_date_filter_payload- assert abnormal_date_filter_payload["data"]["logs"] == [], abnormal_date_filter_payload-- not_found_log_res = rest_client.get(f"/datasets/{dataset_id}/ingestions/nonexistent_log")- assert not_found_log_res.status_code == 200- not_found_log_payload = not_found_log_res.json()- assert not_found_log_payload["code"] != 0, not_found_log_payload---def test_dataset_ingestion_invalid_dataset(rest_client):- summary_res = rest_client.get("/datasets/invalid_id/ingestions/summary")- assert summary_res.status_code == 200- summary_payload = summary_res.json()- assert summary_payload["code"] != 0, summary_payload-- logs_res = rest_client.get("/datasets/invalid_id/ingestions")- assert logs_res.status_code == 200- logs_payload = logs_res.json()- assert logs_payload["code"] != 0, logs_payload-- log_res = rest_client.get("/datasets/invalid_id/ingestions/some_log_id")- assert log_res.status_code == 200- log_payload = log_res.json()- assert log_payload["code"] != 0, log_payload---def test_dataset_index_endpoints(rest_client, create_dataset):- dataset_id = create_dataset("dataset_index_endpoints")-- run_invalid_type = rest_client.post(- f"/datasets/{dataset_id}/index",- params={"type": "invalid_type"},- )- assert run_invalid_type.status_code == 200- run_invalid_type_payload = run_invalid_type.json()- assert run_invalid_type_payload["code"] != 0, run_invalid_type_payload-- run_no_docs = rest_client.post(- f"/datasets/{dataset_id}/index",- params={"type": "graph"},- )- assert run_no_docs.status_code == 200- run_no_docs_payload = run_no_docs.json()- assert run_no_docs_payload["code"] == 102, run_no_docs_payload-- run_no_docs_raptor = rest_client.post(- f"/datasets/{dataset_id}/index",- params={"type": "raptor"},- )- assert run_no_docs_raptor.status_code == 200- run_no_docs_raptor_payload = run_no_docs_raptor.json()- assert run_no_docs_raptor_payload["code"] == 102, run_no_docs_raptor_payload-- trace_no_task = rest_client.get(- f"/datasets/{dataset_id}/index",- params={"type": "graph"},- )- assert trace_no_task.status_code == 200- trace_no_task_payload = trace_no_task.json()- assert trace_no_task_payload["code"] == 0, trace_no_task_payload- assert trace_no_task_payload["data"] == {}, trace_no_task_payload-- trace_invalid_type = rest_client.get(- f"/datasets/{dataset_id}/index",- params={"type": "invalid_type"},- )- assert trace_invalid_type.status_code == 200- trace_invalid_type_payload = trace_invalid_type.json()- assert trace_invalid_type_payload["code"] != 0, trace_invalid_type_payload-- delete_graph = rest_client.delete(f"/datasets/{dataset_id}/graph")- assert delete_graph.status_code == 200- delete_graph_payload = delete_graph.json()- assert delete_graph_payload["code"] == 0, delete_graph_payload-- delete_invalid_type = rest_client.delete(f"/datasets/{dataset_id}/invalid_type")- assert delete_invalid_type.status_code == 200- delete_invalid_type_payload = delete_invalid_type.json()- assert delete_invalid_type_payload["code"] != 0, delete_invalid_type_payload---def test_dataset_graph_endpoint_contract(rest_client, create_dataset):- dataset_id = create_dataset("dataset_graph_contract")-- get_graph_res = rest_client.get(f"/datasets/{dataset_id}/graph")- assert get_graph_res.status_code == 200- get_graph_payload = get_graph_res.json()- assert get_graph_payload["code"] == 0, get_graph_payload- assert "graph" in get_graph_payload["data"], get_graph_payload- assert "mind_map" in get_graph_payload["data"], get_graph_payload- assert isinstance(get_graph_payload["data"]["graph"], dict), get_graph_payload- assert isinstance(get_graph_payload["data"]["mind_map"], dict), get_graph_payload--[email protected]("index_type", ["graph", "raptor", "mindmap"])-def test_dataset_index_trace_and_delete_type_contract(rest_client, create_document, index_type):- dataset_id, _ = create_document(f"dataset_index_trace_{index_type}.txt")-- run_index_res = rest_client.post(- f"/datasets/{dataset_id}/index",- params={"type": index_type},- )- assert run_index_res.status_code == 200- run_index_payload = run_index_res.json()- assert run_index_payload["code"] == 0, run_index_payload- assert run_index_payload["data"].get("task_id"), run_index_payload-- trace_index_res = rest_client.get(- f"/datasets/{dataset_id}/index",- params={"type": index_type},- )- assert trace_index_res.status_code == 200- trace_index_payload = trace_index_res.json()- assert trace_index_payload["code"] == 0, trace_index_payload- assert isinstance(trace_index_payload["data"], dict), trace_index_payload-- delete_index_res = rest_client.delete(f"/datasets/{dataset_id}/{index_type}")- assert delete_index_res.status_code == 200- delete_index_payload = delete_index_res.json()- assert delete_index_payload["code"] == 0, delete_index_payload--[email protected]("index_type", ["graph", "raptor", "mindmap"])-def test_dataset_index_run_with_document_creates_task(rest_client, create_document, index_type):- dataset_id, _ = create_document("dataset_index_graph_source.txt")- run_graph = rest_client.post(- f"/datasets/{dataset_id}/index",- params={"type": index_type},- )- assert run_graph.status_code == 200- run_graph_payload = run_graph.json()- assert run_graph_payload["code"] == 0, run_graph_payload- assert run_graph_payload["data"].get("task_id"), run_graph_payload---def test_dataset_embedding_check_missing_embd_id(rest_client, create_dataset):- dataset_id = create_dataset("dataset_embedding_check_missing_embd_id")-- missing_embd_id_res = rest_client.post(f"/datasets/{dataset_id}/embedding/check", json={})- assert missing_embd_id_res.status_code == 200- missing_embd_id_payload = missing_embd_id_res.json()- assert missing_embd_id_payload["code"] != 0, missing_embd_id_payloaddiff --git a/test/testcases/restful_api/test_document_raw_routes.py b/test/testcases/restful_api/test_document_raw_routes.pydeleted file mode 100644index 454251c04f..0000000000--- a/test/testcases/restful_api/test_document_raw_routes.py+++ /dev/null@@ -1,83 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import pytest--from test.testcases.configs import INVALID_API_TOKEN-from test.testcases.restful_api.helpers.assertions import assert_auth_error-from test.testcases.restful_api.helpers.client import RestClient---def test_document_image_invalid_id_contract(rest_client):- res = rest_client.get("/documents/images/not-a-valid-image-id")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert payload["message"] == "Image not found.", payload--- "path",- [- "/documents/images/not-a-valid-image-id",- "/documents/doc-1/images/imagetemps-page-1.png",- "/documents/doc-1/thumbnail",- ],-)-def test_private_document_images_require_auth(rest_client_noauth, path):- res = rest_client_noauth.get(path)- assert res.status_code == 200, res.text- payload = res.json()- assert payload["code"] == 102, payload- assert payload["message"] == "Authorization is not valid!", payload---def test_document_download_by_id_requires_auth(create_document):- _dataset_id, document_id = create_document("document_raw_download_auth.txt")- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.get(f"/documents/{document_id}")- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_document_download_by_id_invalid_id_contract(rest_client):- res = rest_client.get("/documents/invalid_document_id")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert payload["message"] == "document not found", payload---def test_document_artifact_requires_auth(rest_client_noauth):- res = rest_client_noauth.get("/documents/artifact/not-an-artifact.txt")- assert res.status_code == 401- payload = res.json()- assert payload["code"] == 401, payload---def test_document_artifact_rejects_unsafe_filename(rest_client):- res = rest_client.get("/documents/artifact/not-an-artifact.exe")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert payload["message"] == "invalid file type", payloaddiff --git a/test/testcases/restful_api/test_documents.py b/test/testcases/restful_api/test_documents.pydeleted file mode 100644index 1737e3dc83..0000000000--- a/test/testcases/restful_api/test_documents.py+++ /dev/null@@ -1,1654 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import string-import uuid-from concurrent.futures import ThreadPoolExecutor, as_completed-from contextlib import ExitStack-from pathlib import Path--import pytest-import requests-from openpyxl import Workbook-from requests_toolbelt import MultipartEncoder-from utils import wait_for-from utils.file_utils import create_txt_file--from test.testcases.configs import DOCUMENT_NAME_LIMIT, HOST_ADDRESS, INVALID_API_TOKEN, INVALID_ID_32, VERSION-from test.testcases.restful_api.helpers.assertions import assert_auth_error-from test.testcases.restful_api.helpers.client import RestClient-from test.testcases.utils import compare_by_hash-from test.testcases.utils.file_utils import (- create_docx_file,- create_eml_file,- create_excel_file,- create_html_file,- create_image_file,- create_json_file,- create_md_file,- create_pdf_file,- create_ppt_file,-)---def test_documents_upload_and_list(rest_client, create_dataset, tmp_path):- dataset_id = create_dataset("dataset_upload_list")- fp = create_txt_file(tmp_path / "upload_and_list.txt")- with fp.open("rb") as file_obj:- res = rest_client.post(- f"/datasets/{dataset_id}/documents",- files=[("file", (fp.name, file_obj))],- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert payload["data"][0]["dataset_id"] == dataset_id, payload-- list_res = rest_client.get(f"/datasets/{dataset_id}/documents")- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert list_payload["data"]["total"] >= 1, list_payload- assert any(doc["name"] == fp.name for doc in list_payload["data"]["docs"]), list_payload---def _upload_files(rest_client, dataset_id, file_paths, timeout=None):- with ExitStack() as stack:- files = [("file", (fp.name, stack.enter_context(fp.open("rb")))) for fp in file_paths]- kwargs = {"files": files}- if timeout is not None:- kwargs["timeout"] = timeout- return rest_client.post(f"/datasets/{dataset_id}/documents", **kwargs)---def _seed_documents(rest_client, create_dataset, tmp_path, count=5, timeout=None):- dataset_id = create_dataset("dataset_list_contract")- file_paths = [create_txt_file(tmp_path / f"ragflow_test_upload_{i}.txt") for i in range(count)]- res = _upload_files(rest_client, dataset_id, file_paths, timeout=timeout)- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert len(payload["data"]) == count, payload- return dataset_id, payload["data"]---def _seed_documents_for_update(rest_client, create_dataset, tmp_path):- dataset_id = create_dataset("dataset_update_contract")- file_paths = [- create_txt_file(tmp_path / "ragflow_test_upload_0.txt"),- create_txt_file(tmp_path / "ragflow_test_upload_1.txt"),- ]- res = _upload_files(rest_client, dataset_id, file_paths)- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- return dataset_id, payload["data"]---def _assert_docs_sorted(docs, key, reverse):- values = [doc.get(key) for doc in docs]- assert values == sorted(values, reverse=reverse)---@wait_for(200, 1, "Document parsing timeout in RESTful document tests")-def _wait_document_runs(rest_client, dataset_id, document_ids, expected_run="DONE"):- res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"page_size": max(100, len(document_ids))})- if res.status_code != 200:- return False- payload = res.json()- if payload["code"] != 0:- return False- docs = {doc["id"]: doc for doc in payload["data"]["docs"]}- for doc_id in document_ids:- doc = docs.get(doc_id)- if not doc or doc.get("run") != expected_run:- return False- return True---def _download_document_to_file(rest_client, dataset_id, document_id, save_path):- res = rest_client.get(f"/datasets/{dataset_id}/documents/{document_id}", timeout=60)- if res.status_code == 200:- disposition = res.headers.get("Content-Disposition", "")- if disposition and "attachment" in disposition.lower():- save_path.write_bytes(res.content)- return res---def _create_table_excel(path, rows):- wb = Workbook()- ws = wb.active- for ridx, row in enumerate(rows, start=1):- for cidx, value in enumerate(row, start=1):- ws.cell(row=ridx, column=cidx, value=value)- wb.save(path)- return path---def test_documents_list_requires_auth(create_dataset):- dataset_id = create_dataset("dataset_list_auth")- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.get(f"/datasets/{dataset_id}/documents")- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_documents_upload_requires_auth(create_dataset, tmp_path):- dataset_id = create_dataset("dataset_upload_auth")- fp = create_txt_file(tmp_path / "upload_auth.txt")- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- with fp.open("rb") as file_obj:- res = client.post(- f"/datasets/{dataset_id}/documents",- files=[("file", (fp.name, file_obj))],- )- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_documents_list_default_concurrent_and_filters_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path)- first_id = uploaded_docs[0]["id"]- first_name = uploaded_docs[0]["name"]-- default_res = rest_client.get(f"/datasets/{dataset_id}/documents")- assert default_res.status_code == 200- default_payload = default_res.json()- assert default_payload["code"] == 0, default_payload- assert default_payload["data"]["total"] == 5, default_payload- assert len(default_payload["data"]["docs"]) == 5, default_payload-- with ThreadPoolExecutor(max_workers=5) as executor:- futures = [executor.submit(rest_client.get, f"/datasets/{dataset_id}/documents") for _ in range(30)]- responses = list(as_completed(futures))- assert len(responses) == 30, responses- assert all(f.result().json()["code"] == 0 for f in futures)-- for params, expected_code, expected_docs, expected_total in (- ({"create_time_from": "9999999999000"}, 0, 0, 5),- ({"create_time_to": "1"}, 0, 0, 5),- ({"create_time_from": "0", "create_time_to": "9999999999000"}, 0, 5, 5),- ({"keywords": None}, 0, 5, 5),- ({"keywords": ""}, 0, 5, 5),- ({"keywords": "0"}, 0, 1, 1),- ({"keywords": "ragflow_test_upload"}, 0, 5, 5),- ({"keywords": "unknown"}, 0, 0, 0),- ({"name": None}, 0, 5, 5),- ({"name": ""}, 0, 5, 5),- ({"name": first_name}, 0, 1, 1),- ({"id": None}, 0, 5, 5),- ({"id": ""}, 0, 5, 5),- ({"id": first_id}, 0, 1, 1),- ({"id": first_id, "name": first_name}, 0, 1, 1),- ({"id": first_id, "name": "ragflow_test_upload_1.txt"}, 0, 0, 0),- ({"run": ["UNSTART"]}, 0, 5, 5),- ):- res = rest_client.get(f"/datasets/{dataset_id}/documents", params=params)- assert res.status_code == 200, (params, res.text)- payload = res.json()- assert payload["code"] == expected_code, (params, payload)- assert payload["data"]["total"] == expected_total, (params, payload)- assert len(payload["data"]["docs"]) == expected_docs, (params, payload)---def test_documents_list_error_and_sorting_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path)- first_id = uploaded_docs[0]["id"]-- error_cases = [- (- "invalid dataset empty",- "/datasets//documents",- None,- 102,- "lacks permission for dataset 'documents'",- ),- (- "invalid dataset id",- "/datasets/invalid_dataset_id/documents",- None,- 102,- "You don't own the dataset invalid_dataset_id.",- ),- (- "invalid params ignored",- f"/datasets/{dataset_id}/documents",- {"a": "b"},- 0,- "",- ),- (- "metadata json invalid",- f"/datasets/{dataset_id}/documents",- {"metadata_condition": "{bad json"},- 102,- "metadata_condition must be valid JSON",- ),- (- "metadata json non-object",- f"/datasets/{dataset_id}/documents",- {"metadata_condition": "[1]"},- 102,- "metadata_condition must be an object",- ),- (- "name unknown",- f"/datasets/{dataset_id}/documents",- {"name": "unknown.txt"},- 102,- "you don't own the document unknown.txt",- ),- (- "id unknown",- f"/datasets/{dataset_id}/documents",- {"id": "unknown.txt"},- 102,- "you don't own the document unknown.txt",- ),- (- "name+id unknown name",- f"/datasets/{dataset_id}/documents",- {"id": first_id, "name": "unknown"},- 102,- "you don't own the document unknown",- ),- (- "name+id unknown id",- f"/datasets/{dataset_id}/documents",- {"id": "id", "name": "ragflow_test_upload_0.txt"},- 102,- "you don't own the document id",- ),- (- "run invalid",- f"/datasets/{dataset_id}/documents",- {"run": ["INVALID_STATUS"]},- 102,- "Invalid filter run status conditions: INVALID_STATUS",- ),- (- "orderby invalid",- f"/datasets/{dataset_id}/documents",- {"orderby": "unknown"},- 101,- "invalid orderby field",- ),- (- "page invalid number",- f"/datasets/{dataset_id}/documents",- {"page": -1, "page_size": 2},- 0,- "",- ),- (- "page invalid type",- f"/datasets/{dataset_id}/documents",- {"page": "a", "page_size": 2},- 0,- "",- ),- (- "page_size invalid number",- f"/datasets/{dataset_id}/documents",- {"page_size": -1},- 0,- "",- ),- (- "page_size invalid type",- f"/datasets/{dataset_id}/documents",- {"page_size": "a"},- 0,- "",- ),- ]- for case_name, path, params, expected_code, expected_message in error_cases:- res = rest_client.get(path, params=params)- assert res.status_code == 200, (case_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (case_name, payload)- assert expected_message in payload["message"], (case_name, payload)-- for params, expected_total in (- ({"page": None, "page_size": 2}, 2),- ({"page": 1, "page_size": 2}, 2),- ({"page": 2, "page_size": 2}, 2),- ({"page": 3, "page_size": 2}, 1),- ({"page": "3", "page_size": 2}, 1),- ({"page_size": None}, 5),- ({"page_size": 1}, 1),- ({"page_size": 6}, 5),- ({"page_size": "1"}, 1),- ):- res = rest_client.get(f"/datasets/{dataset_id}/documents", params=params)- assert res.status_code == 200, (params, res.text)- payload = res.json()- assert payload["code"] == 0, (params, payload)- assert len(payload["data"]["docs"]) == expected_total, (params, payload)- assert payload["data"]["total"] == 5, (params, payload)-- for params, expected_key, expected_desc in (- ({"orderby": None}, "create_time", True),- ({"orderby": "create_time"}, "create_time", True),- ({"orderby": "update_time"}, "update_time", True),- ({"orderby": "name", "desc": "False"}, "name", False),- ({"desc": None}, "create_time", True),- ({"desc": "true"}, "create_time", True),- ({"desc": "True"}, "create_time", True),- ({"desc": True}, "create_time", True),- ({"desc": "false"}, "create_time", False),- ({"desc": "False"}, "create_time", False),- ({"desc": False}, "create_time", False),- ({"desc": "False", "orderby": "update_time"}, "update_time", False),- ({"desc": "unknown"}, "create_time", True),- ):- res = rest_client.get(f"/datasets/{dataset_id}/documents", params=params)- assert res.status_code == 200, (params, res.text)- payload = res.json()- assert payload["code"] == 0, (params, payload)- _assert_docs_sorted(payload["data"]["docs"], expected_key, expected_desc)---def test_documents_upload_missing_file(rest_client, create_dataset):- dataset_id = create_dataset("dataset_upload_missing")- res = rest_client.post(f"/datasets/{dataset_id}/documents")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- assert payload["message"] == "No file part!", payload---def test_documents_upload_contract_matrix(rest_client, create_dataset, tmp_path):- dataset_id = create_dataset("dataset_upload_contract")-- valid_res = _upload_files(rest_client, dataset_id, [create_txt_file(tmp_path / "ragflow_test.txt")])- assert valid_res.status_code == 200- valid_payload = valid_res.json()- assert valid_payload["code"] == 0, valid_payload- assert valid_payload["data"][0]["dataset_id"] == dataset_id, valid_payload- assert valid_payload["data"][0]["name"] == "ragflow_test.txt", valid_payload-- for ext in ("docx", "xlsx", "pptx", "jpg", "pdf", "txt", "md", "json", "eml", "html"):- fp = create_txt_file(tmp_path / f"ragflow_test_file_type.{ext}")- res = _upload_files(rest_client, dataset_id, [fp])- assert res.status_code == 200, (ext, res.text)- payload = res.json()- assert payload["code"] == 0, (ext, payload)- assert payload["data"][0]["name"] == fp.name, (ext, payload)-- empty_fp = tmp_path / "empty.txt"- empty_fp.touch()- empty_res = _upload_files(rest_client, dataset_id, [empty_fp])- assert empty_res.status_code == 200- empty_payload = empty_res.json()- assert empty_payload["code"] == 0, empty_payload- assert empty_payload["data"][0]["size"] == 0, empty_payload-- duplicate_fp = create_txt_file(tmp_path / "duplicate.txt")- duplicate_res = _upload_files(rest_client, dataset_id, [duplicate_fp, duplicate_fp])- assert duplicate_res.status_code == 200- duplicate_payload = duplicate_res.json()- assert duplicate_payload["code"] == 0, duplicate_payload- assert [x["name"] for x in duplicate_payload["data"]] == ["duplicate.txt", "duplicate(1).txt"], duplicate_payload-- for index in range(3):- repeat_res = _upload_files(rest_client, dataset_id, [duplicate_fp])- assert repeat_res.status_code == 200- repeat_payload = repeat_res.json()- assert repeat_payload["code"] == 0, (index, repeat_payload)- expected_name = f"duplicate({index + 2}).txt"- assert repeat_payload["data"][0]["name"] == expected_name, (index, repeat_payload)-- max_name_fp = create_txt_file(tmp_path / f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt")- max_name_res = _upload_files(rest_client, dataset_id, [max_name_fp])- assert max_name_res.status_code == 200- max_name_payload = max_name_res.json()- assert max_name_payload["code"] == 0, max_name_payload- assert max_name_payload["data"][0]["name"] == max_name_fp.name, max_name_payload-- illegal_chars = '<>:"/\\|?*'- safe_filename = string.punctuation.translate(str.maketrans({char: "_" for char in illegal_chars}))- special_fp = tmp_path / f"{safe_filename}.txt"- special_fp.write_text("Sample text content")- special_res = _upload_files(rest_client, dataset_id, [special_fp])- assert special_res.status_code == 200- special_payload = special_res.json()- assert special_payload["code"] == 0, special_payload- assert special_payload["data"][0]["name"] == special_fp.name, special_payload-- multi_paths = [create_txt_file(tmp_path / f"ragflow_test_multi_{i}.txt") for i in range(20)]- multi_res = _upload_files(rest_client, dataset_id, multi_paths)- assert multi_res.status_code == 200- multi_payload = multi_res.json()- assert multi_payload["code"] == 0, multi_payload- assert len(multi_payload["data"]) == 20, multi_payload-- with ThreadPoolExecutor(max_workers=5) as executor:- futures = [executor.submit(_upload_files, rest_client, dataset_id, [create_txt_file(tmp_path / f"parallel_upload_{i}.txt")]) for i in range(20)]- responses = list(as_completed(futures))- assert len(responses) == 20, responses- assert all(f.result().json()["code"] == 0 for f in futures)---def test_documents_upload_error_contract(rest_client, create_dataset, tmp_path):- invalid_dataset_res = _upload_files(- rest_client,- "invalid_dataset_id",- [create_txt_file(tmp_path / "invalid_dataset.txt")],- )- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 102, invalid_dataset_payload- assert invalid_dataset_payload["message"] == "Can't find the dataset with ID invalid_dataset_id!", invalid_dataset_payload-- for file_type in ("exe", "unknown"):- bad_file = tmp_path / f"ragflow_test.{file_type}"- bad_file.touch()- res = _upload_files(rest_client, create_dataset(f"dataset_upload_unsupported_{file_type}"), [bad_file])- assert res.status_code == 200, (file_type, res.text)- payload = res.json()- assert payload["code"] == 500, (file_type, payload)- assert payload["message"] == f"ragflow_test.{file_type}: This type of file has not been supported yet!", (file_type, payload)-- dataset_id = create_dataset("dataset_upload_missing_empty")- missing_res = rest_client.post(f"/datasets/{dataset_id}/documents")- assert missing_res.status_code == 200- missing_payload = missing_res.json()- assert missing_payload["code"] == 101, missing_payload- assert missing_payload["message"] == "No file part!", missing_payload-- fp = create_txt_file(tmp_path / "filename_empty.txt")- with fp.open("rb") as file_obj:- m = MultipartEncoder(fields=(("file", ("", file_obj)),))- filename_empty_res = requests.post(- url=f"{HOST_ADDRESS}/api/{VERSION}/datasets/{dataset_id}/documents",- headers={"Content-Type": m.content_type, "Authorization": f"Bearer {rest_client.token}"},- data=m,- timeout=30,- )- assert filename_empty_res.status_code == 200- filename_empty_payload = filename_empty_res.json()- assert filename_empty_payload["code"] == 101, filename_empty_payload- expected_message = "No file part!"- assert filename_empty_payload["message"] == expected_message, filename_empty_payload---def test_documents_update_patch_and_delete(rest_client, create_document):- dataset_id, document_id = create_document("update_target.txt")-- patch_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{document_id}",- json={"name": "updated_target.txt"},- )- assert patch_res.status_code == 200- patch_payload = patch_res.json()- assert patch_payload["code"] == 0, patch_payload- assert patch_payload["data"]["name"] == "updated_target.txt", patch_payload-- delete_res = rest_client.delete(- f"/datasets/{dataset_id}/documents",- json={"ids": [document_id]},- )- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload- assert delete_payload["data"]["deleted"] == 1, delete_payload-- list_res = rest_client.get(f"/datasets/{dataset_id}/documents")- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert all(doc["id"] != document_id for doc in list_payload["data"]["docs"]), list_payload---def test_documents_update_requires_auth(create_document):- dataset_id, document_id = create_document("update_auth_target.txt")- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.patch(- f"/datasets/{dataset_id}/documents/{document_id}",- json={"name": "updated_auth_target.txt"},- )- assert res.status_code == 401, (scenario_name, res.text)- body = res.json()- assert_auth_error(body, scenario_name)---def test_documents_update_name_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents_for_update(rest_client, create_dataset, tmp_path)- first_document_id = uploaded_docs[0]["id"]-- long_name = f"{'a' * (DOCUMENT_NAME_LIMIT - 4)}.txt"- name_cases = [- ("new_name.txt", 0, ""),- (long_name, 0, ""),- (0, 102, "Field: <name> - Message: <Input should be a valid string> - Value: <0>"),- (None, 102, "Field: <name> - Message: <Input should be a valid string> - Value: <None>"),- ("", 101, "The extension of file can't be changed"),- ("ragflow_test_upload_0", 101, "The extension of file can't be changed"),- ("ragflow_test_upload_1.txt", 102, "Duplicated document name in the same dataset."),- ("RAGFLOW_TEST_UPLOAD_1.TXT", 0, ""),- ]- for name, expected_code, expected_message in name_cases:- res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{first_document_id}",- json={"name": name},- )- assert res.status_code == 200, (name, res.text)- body = res.json()- assert body["code"] == expected_code, (name, body)- if expected_code == 0:- assert body["data"]["name"] == name, (name, body)- list_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"id": first_document_id})- assert list_res.status_code == 200, (name, list_res.text)- list_body = list_res.json()- assert list_body["code"] == 0, (name, list_body)- assert list_body["data"]["docs"][0]["name"] == name, (name, list_body)- else:- assert body["message"] == expected_message, (name, body)---def test_documents_update_invalid_dataset_and_document_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents_for_update(rest_client, create_dataset, tmp_path)- first_document_id = uploaded_docs[0]["id"]-- invalid_dataset_res = rest_client.patch(- f"/datasets/{INVALID_ID_32}/documents/{first_document_id}",- json={"name": "new_name.txt"},- )- assert invalid_dataset_res.status_code == 200- invalid_dataset_body = invalid_dataset_res.json()- assert invalid_dataset_body["code"] == 404, invalid_dataset_body- assert invalid_dataset_body["message"] == "Resource not found", invalid_dataset_body-- invalid_document_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{INVALID_ID_32}",- json={"name": "new_name.txt"},- )- assert invalid_document_res.status_code == 200- invalid_document_body = invalid_document_res.json()- assert invalid_document_body["code"] == 102, invalid_document_body- assert invalid_document_body["message"] == "the dataset doesn't own the document", invalid_document_body---def test_documents_update_chunk_method_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents_for_update(rest_client, create_dataset, tmp_path)- first_document_id = uploaded_docs[0]["id"]-- chunk_method_cases = [- ("naive", 0, ""),- ("manual", 0, ""),- ("qa", 0, ""),- ("table", 0, ""),- ("paper", 0, ""),- ("book", 0, ""),- ("laws", 0, ""),- ("presentation", 0, ""),- ("picture", 0, ""),- ("one", 0, ""),- ("knowledge_graph", 0, ""),- ("email", 0, ""),- ("tag", 0, ""),- ("", 102, "`chunk_method` (empty string) is not valid"),- (- "other_chunk_method",- 102,- "Field: <chunk_method> - Message: <`chunk_method` other_chunk_method doesn't exist> - Value: <other_chunk_method>",- ),- ]- for chunk_method, expected_code, expected_message in chunk_method_cases:- res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{first_document_id}",- json={"chunk_method": chunk_method},- )- assert res.status_code == 200, (chunk_method, res.text)- body = res.json()- assert body["code"] == expected_code, (chunk_method, body)- if expected_code == 0:- list_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"id": first_document_id})- assert list_res.status_code == 200, (chunk_method, list_res.text)- list_body = list_res.json()- assert list_body["code"] == 0, (chunk_method, list_body)- assert list_body["data"]["docs"][0]["chunk_method"] == chunk_method, (chunk_method, list_body)- else:- assert body["message"] == expected_message, (chunk_method, body)---def test_documents_update_meta_fields_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents_for_update(rest_client, create_dataset, tmp_path)- first_document_id = uploaded_docs[0]["id"]-- meta_fields_cases = [- ({"test": "test"}, 0, ""),- ({"author": "alice", "year": 2024}, 0, ""),- ({"tags": ["tag1", "tag2"]}, 0, ""),- ({"count": 42, "price": 19.99}, 0, ""),- ("test", 102, "Field: <meta_fields> - Message: <Input should be a valid dictionary> - Value: <test>"),- ([], 102, "Field: <meta_fields> - Message: <Input should be a valid dictionary> - Value: <[]>"),- ({"tags": [{"x": {"a": "b"}}]}, 102, "Field: <meta_fields> - Message: <The type is not supported in list: [{'x': {'a': 'b'}}]> - Value: <{'tags': [{'x': {'a': 'b'}}]}>"),- ({"tags": [{"x": 1}]}, 102, "Field: <meta_fields> - Message: <The type is not supported in list: [{'x': 1}]> - Value: <{'tags': [{'x': 1}]}>"),- ({"obj": {"x": 1}}, 102, "Field: <meta_fields> - Message: <The type is not supported: {'x': 1}> - Value: <{'obj': {'x': 1}}>"),- ({"tags": [2, 1]}, 0, ""),- ]- for meta_fields, expected_code, expected_message in meta_fields_cases:- res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{first_document_id}",- json={"meta_fields": meta_fields},- )- assert res.status_code == 200, (meta_fields, res.text)- body = res.json()- assert body["code"] == expected_code, (meta_fields, body)- if expected_code == 0:- list_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"id": first_document_id})- assert list_res.status_code == 200, (meta_fields, list_res.text)- list_body = list_res.json()- assert list_body["code"] == 0, (meta_fields, list_body)- assert list_body["data"]["docs"][0]["meta_fields"] == meta_fields, (meta_fields, list_body)- else:- assert expected_message in body["message"] or body["message"] == expected_message, (meta_fields, body)-- invalid_meta_doc_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/invalid_doc_id_12345678901234567890",- json={"meta_fields": {"author": "alice"}},- )- assert invalid_meta_doc_res.status_code == 200- invalid_meta_doc_body = invalid_meta_doc_res.json()- assert invalid_meta_doc_body["code"] == 102, invalid_meta_doc_body- assert "the dataset doesn't own the document" in invalid_meta_doc_body["message"], invalid_meta_doc_body---def test_documents_update_invalid_field_and_guard_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents_for_update(rest_client, create_dataset, tmp_path)- first_document_id = uploaded_docs[0]["id"]-- strict_guard_cases = [- ({"chunk_count": 1}, 102, "can't change `chunk_count`"),- ({"token_count": 1}, 102, "can't change `token_count`"),- ({"chunk_count": 100}, 102, "can't change `chunk_count`"),- ({"token_count": 100}, 102, "can't change `token_count`"),- ({"progress": 2.0}, 102, "Field: <progress> - Message: <Input should be less than or equal to 1> - Value: <2.0>"),- ({"progress": 1.0}, 102, "can't change `progress`"),- ({"meta_fields": []}, 102, "Field: <meta_fields> - Message: <Input should be a valid dictionary> - Value: <[]>"),- ]- for payload, expected_code, expected_message in strict_guard_cases:- res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{first_document_id}",- json=payload,- )- assert res.status_code == 200, (payload, res.text)- body = res.json()- assert body["code"] == expected_code, (payload, body)- assert expected_message in body["message"] or body["message"] == expected_message, (payload, body)-- legacy_invalid_field_cases = [- {"create_date": "Fri, 14 Mar 2025 16:53:42 GMT"},- {"create_time": 1},- {"created_by": "ragflow_test"},- {"dataset_id": "ragflow_test"},- {"id": "ragflow_test"},- {"location": "ragflow_test.txt"},- {"process_begin_at": 1},- {"process_duration": 1.0},- {"progress_msg": "ragflow_test"},- {"run": "ragflow_test"},- {"size": 1},- {"source_type": "ragflow_test"},- {"thumbnail": "ragflow_test"},- {"type": "ragflow_test"},- {"update_date": "Fri, 14 Mar 2025 16:33:17 GMT"},- {"update_time": 1},- ]- for payload in legacy_invalid_field_cases:- res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{first_document_id}",- json=payload,- )- assert res.status_code == 200, (payload, res.text)- body = res.json()- assert body["code"] in (0, 102), (payload, body)- if body["code"] == 102:- assert "invalid" in body["message"].lower(), (payload, body)- else:- assert "data" in body, (payload, body)---def test_documents_update_parser_config_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents_for_update(rest_client, create_dataset, tmp_path)- first_document_id = uploaded_docs[0]["id"]- default_parser_config_for_test = {- "layout_recognize": "DeepDOC",- "chunk_token_num": 512,- "delimiter": "\n",- "auto_keywords": 0,- "auto_questions": 0,- "html4excel": False,- "topn_tags": 3,- "raptor": {- "use_raptor": True,- "prompt": "Summarize the paragraphs below without inventing facts or changing numbers.\nOutput exactly two parts in the same language as the source:\n1. First line: a concise title only.\n2. Following lines: a concise summary of the content.\nDo not output labels, Markdown headings, bullet points, or any other commentary.\n\nParagraphs:\n{cluster_content}",- "max_token": 512,- "clustering_threshold": 0.3,- "clustering_ratio": 0.5,- "max_cluster": 64,- "random_seed": 0,- },- "graphrag": {- "use_graphrag": True,- "entity_types": ["organization", "person", "geo", "event", "category"],- "method": "light",- "batch_chunk_token_size": 4096,- },- }-- parser_cases = [- ({}, 0, ""),- (default_parser_config_for_test, 0, ""),- ({"chunk_token_num": -1}, 102, "Field: <parser_config.chunk_token_num> - Message: <Input should be greater than or equal to 1> - Value: <-1>"),- ({"chunk_token_num": 0}, 102, "Field: <parser_config.chunk_token_num> - Message: <Input should be greater than or equal to 1> - Value: <0>"),- ({"chunk_token_num": 100000000}, 102, "Field: <parser_config.chunk_token_num> - Message: <Input should be less than or equal to 2048> - Value: <100000000>"),- ({"chunk_token_num": 3.14}, 102, "Field: <parser_config.chunk_token_num> - Message: <Input should be a valid integer> - Value: <3.14>"),- ({"chunk_token_num": "1024"}, 102, "Field: <parser_config.chunk_token_num> - Message: <Input should be a valid integer> - Value: <1024>"),- ({"layout_recognize": "DeepDOC"}, 0, ""),- ({"layout_recognize": "Naive"}, 0, ""),- ({"html4excel": True}, 0, ""),- ({"html4excel": False}, 0, ""),- ({"html4excel": 1}, 102, "Field: <parser_config.html4excel> - Message: <Input should be a valid boolean> - Value: <1>"),- ({"delimiter": ""}, 102, "Field: <parser_config.delimiter> - Message: <String should have at least 1 character> - Value: <>"),- ({"delimiter": "`##`"}, 0, ""),- ({"delimiter": 1}, 102, "Field: <parser_config.delimiter> - Message: <Input should be a valid string> - Value: <1>"),- ({"task_page_size": -1}, 102, "Field: <parser_config.task_page_size> - Message: <Input should be greater than or equal to 1> - Value: <-1>"),- ({"task_page_size": 0}, 102, "Field: <parser_config.task_page_size> - Message: <Input should be greater than or equal to 1> - Value: <0>"),- ({"task_page_size": 100000000}, 0, ""),- ({"task_page_size": 3.14}, 102, "Field: <parser_config.task_page_size> - Message: <Input should be a valid integer> - Value: <3.14>"),- ({"task_page_size": "1024"}, 102, "Field: <parser_config.task_page_size> - Message: <Input should be a valid integer> - Value: <1024>"),- ({"raptor": {"use_raptor": {"a": "b"}}}, 102, "Field: <parser_config.raptor.use_raptor> - Message: <Input should be a valid boolean> - Value: <{'a': 'b'}>"),- ({"raptor": {"use_raptor": False}}, 0, ""),- ({"invalid_key": "invalid_value"}, 0, ""),- ({"auto_keywords": -1}, 102, "Field: <parser_config.auto_keywords> - Message: <Input should be greater than or equal to 0> - Value: <-1>"),- ({"auto_keywords": 32}, 0, ""),- ({"auto_keywords": "1024"}, 102, "Field: <parser_config.auto_keywords> - Message: <Input should be a valid integer> - Value: <1024>"),- ({"auto_keywords": 3.14}, 102, "Field: <parser_config.auto_keywords> - Message: <Input should be a valid integer> - Value: <3.14>"),- ({"auto_questions": -1}, 102, "Field: <parser_config.auto_questions> - Message: <Input should be greater than or equal to 0> - Value: <-1>"),- ({"auto_questions": 10}, 0, ""),- ({"auto_questions": 3.14}, 102, "Field: <parser_config.auto_questions> - Message: <Input should be a valid integer> - Value: <3.14>"),- ({"auto_questions": "1024"}, 102, "Field: <parser_config.auto_questions> - Message: <Input should be a valid integer> - Value: <1024>"),- ({"topn_tags": -1}, 102, "Field: <parser_config.topn_tags> - Message: <Input should be greater than or equal to 1> - Value: <-1>"),- ({"topn_tags": 10}, 0, ""),- ({"topn_tags": 3.14}, 102, "Field: <parser_config.topn_tags> - Message: <Input should be a valid integer> - Value: <3.14>"),- ({"topn_tags": "1024"}, 102, "Field: <parser_config.topn_tags> - Message: <Input should be a valid integer> - Value: <1024>"),- ]- for parser_config, expected_code, expected_message in parser_cases:- res = rest_client.patch(- f"/datasets/{dataset_id}/documents/{first_document_id}",- json={"chunk_method": "naive", "parser_config": parser_config},- )- assert res.status_code == 200, (parser_config, res.text)- body = res.json()- assert body["code"] == expected_code, (parser_config, body)- if expected_code == 0:- list_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"id": first_document_id})- assert list_res.status_code == 200, (parser_config, list_res.text)- list_body = list_res.json()- assert list_body["code"] == 0, (parser_config, list_body)- doc_parser_config = list_body["data"]["docs"][0]["parser_config"]- assert isinstance(doc_parser_config, dict) and doc_parser_config, (parser_config, list_body)- assert "raptor" not in doc_parser_config, (parser_config, list_body)- assert "graphrag" not in doc_parser_config, (parser_config, list_body)- else:- assert body["message"] in expected_message, (parser_config, body)---def test_documents_parse_and_stop(rest_client, create_document):- dataset_id, document_id = create_document("parse_target.txt")-- parse_res = rest_client.post(- f"/datasets/{dataset_id}/documents/parse",- json={"document_ids": [document_id]},- )- assert parse_res.status_code == 200- parse_payload = parse_res.json()- assert parse_payload["code"] == 0, parse_payload-- stop_res = rest_client.post(- f"/datasets/{dataset_id}/documents/stop",- json={"document_ids": [document_id]},- )- assert stop_res.status_code == 200- stop_payload = stop_res.json()- # Depending on timing this can be immediate stop success or "already completed".- assert stop_payload["code"] in (0, 102), stop_payload- if stop_payload["code"] == 102:- assert "already completed" in stop_payload["message"], stop_payload---def test_documents_metadata_batch_update_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=5)- document_ids = [doc["id"] for doc in uploaded_docs]-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.patch(- f"/datasets/{dataset_id}/documents/metadatas",- json={"selector": {"document_ids": document_ids[:1]}, "updates": [], "deletes": []},- )- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)-- invalid_dataset_res = rest_client.patch(- "/datasets/invalid_dataset_id/documents/metadatas",- json={"selector": {"document_ids": []}, "updates": [], "deletes": []},- )- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 102, invalid_dataset_payload- assert invalid_dataset_payload["message"] == "You don't own the dataset invalid_dataset_id.", invalid_dataset_payload-- validation_cases = [- ("selector not object", {"selector": [1], "updates": [], "deletes": []}, 102, "selector must be an object."),- ("updates not list", {"selector": {}, "updates": {"key": "value"}, "deletes": []}, 102, "updates and deletes must be lists."),- ("metadata condition not object", {"selector": {"metadata_condition": [1]}, "updates": [], "deletes": []}, 102, "metadata_condition must be an object."),- ("document ids not list", {"selector": {"document_ids": "doc-1"}, "updates": [], "deletes": []}, 102, "document_ids must be a list."),- ("update missing key", {"selector": {}, "updates": [{"key": ""}], "deletes": []}, 102, "Each update requires key and value."),- ("delete missing key", {"selector": {}, "updates": [], "deletes": [{"x": "y"}]}, 102, "Each delete requires key."),- (- "document ids wrong dataset",- {"selector": {"document_ids": ["doc-does-not-exist-1", "doc-does-not-exist-2"]}, "updates": [{"key": "author", "value": "test"}], "deletes": []},- 102,- f"These documents do not belong to dataset {dataset_id}: ",- ),- ]- for scenario_name, payload, expected_code, expected_message in validation_cases:- res = rest_client.patch(f"/datasets/{dataset_id}/documents/metadatas", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if scenario_name == "document ids wrong dataset":- assert body["message"].startswith(expected_message), (scenario_name, body)- invalid_ids = set(body["message"][len(expected_message) :].split(", "))- assert invalid_ids == {"doc-does-not-exist-1", "doc-does-not-exist-2"}, (scenario_name, body)- else:- assert body["message"] == expected_message, (scenario_name, body)-- update_by_ids_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/metadatas",- json={- "selector": {"document_ids": document_ids},- "updates": [{"key": "author", "value": "test_author"}, {"key": "status", "value": "processed"}],- "deletes": [],- },- )- assert update_by_ids_res.status_code == 200- update_by_ids_payload = update_by_ids_res.json()- assert update_by_ids_payload["code"] == 0, update_by_ids_payload- assert update_by_ids_payload["data"] == {"updated": 5, "matched_docs": 5}, update_by_ids_payload-- filtered_update_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/metadatas",- json={- "selector": {- "document_ids": document_ids,- "metadata_condition": {"conditions": [{"comparison_operator": "is", "name": "status", "value": "processed"}]},- },- "updates": [{"key": "author", "value": "filtered_author"}],- "deletes": [],- },- )- assert filtered_update_res.status_code == 200- filtered_update_payload = filtered_update_res.json()- assert filtered_update_payload["code"] == 0, filtered_update_payload- assert filtered_update_payload["data"] == {"updated": 5, "matched_docs": 5}, filtered_update_payload-- delete_metadata_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/metadatas",- json={- "selector": {"document_ids": document_ids},- "updates": [],- "deletes": [{"key": "author"}],- },- )- assert delete_metadata_res.status_code == 200- delete_metadata_payload = delete_metadata_res.json()- assert delete_metadata_payload["code"] == 0, delete_metadata_payload- assert delete_metadata_payload["data"] == {"updated": 5, "matched_docs": 5}, delete_metadata_payload-- combined_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/metadatas",- json={- "selector": {"document_ids": document_ids},- "updates": [{"key": "author", "value": "new_author"}],- "deletes": [{"key": "status"}],- },- )- assert combined_res.status_code == 200- combined_payload = combined_res.json()- assert combined_payload["code"] == 0, combined_payload- assert combined_payload["data"] == {"updated": 5, "matched_docs": 5}, combined_payload-- empty_ids_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/metadatas",- json={"selector": {"document_ids": []}, "updates": [{"key": "author", "value": "test"}], "deletes": []},- )- assert empty_ids_res.status_code == 200- empty_ids_payload = empty_ids_res.json()- assert empty_ids_payload["code"] == 0, empty_ids_payload- assert empty_ids_payload["data"] == {"updated": 0, "matched_docs": 0}, empty_ids_payload-- no_match_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/metadatas",- json={- "selector": {- "document_ids": document_ids,- "metadata_condition": {"conditions": [{"comparison_operator": "is", "name": "nonexistent_key", "value": "nonexistent_value"}]},- },- "updates": [{"key": "author", "value": "test"}],- "deletes": [],- },- )- assert no_match_res.status_code == 200- no_match_payload = no_match_res.json()- assert no_match_payload["code"] == 0, no_match_payload- assert no_match_payload["data"] == {"updated": 0, "matched_docs": 0}, no_match_payload---def test_document_metadata_config_contract(rest_client, create_document):- dataset_id, document_id = create_document("document_metadata_config_contract.txt")-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.put(- f"/datasets/{dataset_id}/documents/{document_id}/metadata/config",- json={"metadata": {"author": "alice"}},- )- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)-- missing_payload_res = rest_client.put(f"/datasets/{dataset_id}/documents/{document_id}/metadata/config", json={})- assert missing_payload_res.status_code == 200- missing_payload = missing_payload_res.json()- assert missing_payload["code"] == 101, missing_payload- assert missing_payload["message"] == "metadata is required", missing_payload-- invalid_dataset_res = rest_client.put(- f"/datasets/{INVALID_ID_32}/documents/{document_id}/metadata/config",- json={"metadata": {"author": "alice"}},- )- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 404, invalid_dataset_payload- assert invalid_dataset_payload["message"] == "Resource not found", invalid_dataset_payload-- invalid_document_res = rest_client.put(- f"/datasets/{dataset_id}/documents/{INVALID_ID_32}/metadata/config",- json={"metadata": {"author": "alice"}},- )- assert invalid_document_res.status_code == 200- invalid_document_payload = invalid_document_res.json()- assert invalid_document_payload["code"] == 102, invalid_document_payload- assert invalid_document_payload["message"] == f"document {INVALID_ID_32} not found in dataset {dataset_id}", invalid_document_payload-- update_payload = {"metadata": {"author": "alice", "tags": ["one", "two"]}}- update_res = rest_client.put(- f"/datasets/{dataset_id}/documents/{document_id}/metadata/config",- json=update_payload,- )- assert update_res.status_code == 200- update_body = update_res.json()- assert update_body["code"] == 0, update_body- parser_config = update_body["data"]["parser_config"]- # Go scopes metadata onto the Extractor node rather than a flat key.- scoped = parser_config.get("Extractor:AutoExtractDefault", {})- assert scoped.get("metadata") == update_payload["metadata"], update_body---def test_documents_metadata_update_path(rest_client, create_document):- dataset_id, document_id = create_document("metadata_target.txt")-- res = rest_client.patch(- f"/datasets/{dataset_id}/documents/metadatas",- json={- "selector": {"document_ids": [document_id]},- "updates": [{"key": "author", "value": "qa"}],- "deletes": [],- },- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert payload["data"]["matched_docs"] == 1, payload- assert payload["data"]["updated"] >= 1, payload---def test_documents_delete_contract_matrix(rest_client, create_dataset, tmp_path):- scenarios = [- ("empty object", lambda ids: {}, 102, "should either provide doc ids or set delete_all(true)", 3),- ("empty ids", lambda ids: {"ids": []}, 102, "should either provide doc ids or set delete_all(true)", 3),- ("invalid id only", lambda ids: {"ids": ["invalid_id"]}, 102, "These documents do not belong to dataset", 3),- ("not json object", lambda ids: "not json", 101, "Invalid request payload: expected object, got str", 3),- ("delete one", lambda ids: {"ids": ids[:1]}, 0, "", 2),- ("delete all by ids", lambda ids: {"ids": ids}, 0, "", 0),- ("delete_all flag", lambda ids: {"delete_all": True}, 0, "", 0),- ]- for scenario_name, payload_builder, expected_code, expected_message, expected_total in scenarios:- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=3)- document_ids = [doc["id"] for doc in uploaded_docs]- payload = payload_builder(document_ids)-- res = rest_client.delete(f"/datasets/{dataset_id}/documents", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code != 0:- assert expected_message in body["message"], (scenario_name, body)- else:- assert body["data"]["deleted"] in (len(document_ids), len(document_ids[:1])), (scenario_name, body)-- list_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"page_size": 10})- assert list_res.status_code == 200, (scenario_name, list_res.text)- list_payload = list_res.json()- assert list_payload["code"] == 0, (scenario_name, list_payload)- assert list_payload["data"]["total"] == expected_total, (scenario_name, list_payload)---def test_documents_delete_requires_auth(rest_client, create_dataset, tmp_path):- dataset_id = create_dataset("dataset_delete_auth")- file_path = create_txt_file(tmp_path / "delete_auth_target.txt")- with file_path.open("rb") as file_obj:- upload_res = rest_client.post(f"/datasets/{dataset_id}/documents", files=[("file", (file_path.name, file_obj))])- assert upload_res.status_code == 200- upload_payload = upload_res.json()- assert upload_payload["code"] == 0, upload_payload- document_id = upload_payload["data"][0]["id"]-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.delete(f"/datasets/{dataset_id}/documents", json={"ids": [document_id]})- assert res.status_code == 401, (scenario_name, res.text)- body = res.json()- assert_auth_error(body, scenario_name)---def test_documents_delete_invalid_dataset_partial_duplicate_repeat_and_cross_dataset(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=3)- document_ids = [doc["id"] for doc in uploaded_docs]- other_dataset_id, other_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=1)- other_document_id = other_docs[0]["id"]-- invalid_dataset_res = rest_client.delete(- "/datasets/invalid_dataset_id/documents",- json={"ids": document_ids[:1]},- )- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 102, invalid_dataset_payload- assert "You don't own the dataset invalid_dataset_id." in invalid_dataset_payload["message"], invalid_dataset_payload-- partial_invalid_payloads = [- {"ids": ["invalid_id"] + document_ids},- {"ids": document_ids[:1] + ["invalid_id"] + document_ids[1:]},- {"ids": document_ids + ["invalid_id"]},- ]- for payload in partial_invalid_payloads:- res = rest_client.delete(f"/datasets/{dataset_id}/documents", json=payload)- assert res.status_code == 200, (payload, res.text)- body = res.json()- assert body["code"] == 102, (payload, body)- assert "These documents do not belong to dataset" in body["message"], (payload, body)-- cross_dataset_res = rest_client.delete(f"/datasets/{dataset_id}/documents", json={"ids": [other_document_id]})- assert cross_dataset_res.status_code == 200- cross_dataset_payload = cross_dataset_res.json()- assert cross_dataset_payload["code"] == 102, cross_dataset_payload- assert f"These documents do not belong to dataset {dataset_id}" in cross_dataset_payload["message"], cross_dataset_payload-- duplicate_res = rest_client.delete(f"/datasets/{dataset_id}/documents", json={"ids": document_ids + document_ids})- assert duplicate_res.status_code == 200- duplicate_payload = duplicate_res.json()- assert duplicate_payload["code"] == 101, duplicate_payload- assert "Field: <ids> - Message: <Duplicate ids:" in duplicate_payload["message"], duplicate_payload-- delete_once_res = rest_client.delete(f"/datasets/{dataset_id}/documents", json={"ids": document_ids}, timeout=120)- assert delete_once_res.status_code == 200- delete_once_payload = delete_once_res.json()- assert delete_once_payload["code"] == 0, delete_once_payload-- delete_twice_res = rest_client.delete(f"/datasets/{dataset_id}/documents", json={"ids": document_ids}, timeout=120)- assert delete_twice_res.status_code == 200- delete_twice_payload = delete_twice_res.json()- assert delete_twice_payload["code"] == 102, delete_twice_payload- assert "or Document not found" in delete_twice_payload["message"], delete_twice_payload-- other_list_res = rest_client.get(f"/datasets/{other_dataset_id}/documents")- assert other_list_res.status_code == 200- other_list_payload = other_list_res.json()- assert other_list_payload["code"] == 0, other_list_payload- assert other_list_payload["data"]["total"] == 1, other_list_payload---def test_documents_delete_concurrent_and_bulk_contract(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=60, timeout=120)- document_ids = [doc["id"] for doc in uploaded_docs]-- with ThreadPoolExecutor(max_workers=8) as executor:- futures = [- executor.submit(- rest_client.delete,- f"/datasets/{dataset_id}/documents",- json={"ids": [doc_id]},- )- for doc_id in document_ids- ]- _responses = list(as_completed(futures))- assert len(_responses) == len(document_ids), _responses- for future in futures:- response = future.result()- assert response.status_code == 200, response.text- payload = response.json()- assert payload["code"] == 0, payload-- list_after_concurrent = rest_client.get(f"/datasets/{dataset_id}/documents")- assert list_after_concurrent.status_code == 200- list_after_payload = list_after_concurrent.json()- assert list_after_payload["code"] == 0, list_after_payload- assert list_after_payload["data"]["total"] == 0, list_after_payload-- bulk_dataset_id, bulk_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=120, timeout=120)- bulk_ids = [doc["id"] for doc in bulk_docs]- bulk_delete_res = rest_client.delete(- f"/datasets/{bulk_dataset_id}/documents",- json={"ids": bulk_ids},- timeout=120,- )- assert bulk_delete_res.status_code == 200- bulk_delete_payload = bulk_delete_res.json()- assert bulk_delete_payload["code"] == 0, bulk_delete_payload- assert bulk_delete_payload["data"]["deleted"] == 120, bulk_delete_payload-- bulk_list_res = rest_client.get(f"/datasets/{bulk_dataset_id}/documents")- assert bulk_list_res.status_code == 200- bulk_list_payload = bulk_list_res.json()- assert bulk_list_payload["code"] == 0, bulk_list_payload- assert bulk_list_payload["data"]["total"] == 0, bulk_list_payload---def test_documents_parse_requires_auth(create_document):- dataset_id, document_id = create_document("parse_auth_target.txt")- payload = {"document_ids": [document_id]}- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.post(f"/datasets/{dataset_id}/documents/parse", json=payload)- assert res.status_code == 401, (scenario_name, res.text)- body = res.json()- assert_auth_error(body, scenario_name)---def test_documents_parse_contract_matrix(rest_client, create_dataset, tmp_path):- scenarios = [- ("empty ids", lambda ids: {"document_ids": []}, 102, "`document_ids` is required"),- ("invalid id", lambda ids: {"document_ids": ["invalid_id"]}, 102, "Documents not found: ['invalid_id']"),- ("special invalid id", lambda ids: {"document_ids": ["\\n!?。;!?\"'"]}, 102, "Documents not found:"),- ("not json object", lambda ids: "not json", 100, "object has no attribute"),- ("parse one", lambda ids: {"document_ids": ids[:1]}, 0, ""),- ("parse all", lambda ids: {"document_ids": ids}, 0, ""),- ]- for scenario_name, payload_builder, expected_code, expected_message in scenarios:- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=3)- doc_ids = [doc["id"] for doc in uploaded_docs]- payload = payload_builder(doc_ids)-- res = rest_client.post(f"/datasets/{dataset_id}/documents/parse", json=payload, timeout=60)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code != 0:- assert expected_message in body["message"], (scenario_name, body)- else:- target_ids = payload["document_ids"]- _wait_document_runs(rest_client, dataset_id, target_ids, expected_run="DONE")- detail_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"page_size": 10})- detail_payload = detail_res.json()- docs = {doc["id"]: doc for doc in detail_payload["data"]["docs"]}- for doc_id in target_ids:- doc = docs[doc_id]- assert doc["run"] == "DONE", (scenario_name, doc)- assert doc["process_begin_at"], (scenario_name, doc)- assert doc["process_duration"] >= 0, (scenario_name, doc)- assert doc["progress"] >= 0, (scenario_name, doc)- assert "Task done" in doc["progress_msg"], (scenario_name, doc)---def test_documents_parse_invalid_dataset_partial_duplicate_and_repeated(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=3)- doc_ids = [doc["id"] for doc in uploaded_docs]-- for bad_dataset in ("", "invalid_dataset_id"):- path = f"/datasets/{bad_dataset}/documents/parse" if bad_dataset else "/datasets//documents/parse"- res = rest_client.post(path, json={"document_ids": doc_ids})- assert res.status_code == 200, (bad_dataset, res.text)- body = res.json()- if bad_dataset == "":- assert body["code"] == 100, (bad_dataset, body)- assert "Method Not Allowed" in body["message"], (bad_dataset, body)- else:- assert body["code"] == 102, (bad_dataset, body)- assert "You don't own the dataset" in body["message"], (bad_dataset, body)-- for payload in (- {"document_ids": ["invalid_id"] + doc_ids},- {"document_ids": doc_ids[:1] + ["invalid_id"] + doc_ids[1:]},- {"document_ids": doc_ids + ["invalid_id"]},- ):- res = rest_client.post(f"/datasets/{dataset_id}/documents/parse", json=payload, timeout=60)- assert res.status_code == 200, (payload, res.text)- body = res.json()- assert body["code"] == 102, (payload, body)- assert body["message"] == "Documents not found: ['invalid_id']", (payload, body)-- duplicate_res = rest_client.post(- f"/datasets/{dataset_id}/documents/parse",- json={"document_ids": doc_ids + doc_ids},- timeout=60,- )- assert duplicate_res.status_code == 200- duplicate_payload = duplicate_res.json()- assert duplicate_payload["code"] == 0, duplicate_payload- assert duplicate_payload["data"]["success_count"] == len(doc_ids), duplicate_payload- assert any("Duplicate document ids:" in err for err in duplicate_payload["data"].get("errors", [])), duplicate_payload- _wait_document_runs(rest_client, dataset_id, doc_ids, expected_run="DONE")-- repeated_res = rest_client.post(f"/datasets/{dataset_id}/documents/parse", json={"document_ids": doc_ids}, timeout=60)- assert repeated_res.status_code == 200- repeated_payload = repeated_res.json()- assert repeated_payload["code"] == 0, repeated_payload- _wait_document_runs(rest_client, dataset_id, doc_ids, expected_run="DONE")---def test_documents_parse_chunks_and_scaled_bulk_contract(rest_client, create_dataset, tmp_path):- single_dataset_id, single_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=1)- single_doc_id = single_docs[0]["id"]- parse_single_res = rest_client.post(- f"/datasets/{single_dataset_id}/documents/parse",- json={"document_ids": [single_doc_id]},- timeout=60,- )- assert parse_single_res.status_code == 200- parse_single_payload = parse_single_res.json()- assert parse_single_payload["code"] == 0, parse_single_payload- _wait_document_runs(rest_client, single_dataset_id, [single_doc_id], expected_run="DONE")-- chunk_res = rest_client.get(f"/datasets/{single_dataset_id}/documents/{single_doc_id}/chunks")- assert chunk_res.status_code == 200, chunk_res.text- chunk_payload = chunk_res.json()- assert chunk_payload["code"] == 0, chunk_payload- assert chunk_payload["data"]["doc"]["chunk_count"] > 0, chunk_payload- assert len(chunk_payload["data"]["chunks"]) > 0, chunk_payload-- parse_bulk_dataset, parse_bulk_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=20)- parse_bulk_ids = [doc["id"] for doc in parse_bulk_docs]- parse_bulk_res = rest_client.post(- f"/datasets/{parse_bulk_dataset}/documents/parse",- json={"document_ids": parse_bulk_ids},- timeout=60,- )- assert parse_bulk_res.status_code == 200- parse_bulk_payload = parse_bulk_res.json()- assert parse_bulk_payload["code"] == 0, parse_bulk_payload- _wait_document_runs(rest_client, parse_bulk_dataset, parse_bulk_ids, expected_run="DONE")-- concurrent_dataset, concurrent_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=20)- concurrent_ids = [doc["id"] for doc in concurrent_docs]- with ThreadPoolExecutor(max_workers=8) as executor:- futures = [- executor.submit(- rest_client.post,- f"/datasets/{concurrent_dataset}/documents/parse",- json={"document_ids": [doc_id]},- timeout=60,- )- for doc_id in concurrent_ids- ]- _responses = list(as_completed(futures))- assert len(_responses) == len(concurrent_ids), _responses- for future in futures:- response = future.result()- assert response.status_code == 200, response.text- payload = response.json()- assert payload["code"] == 0, payload- _wait_document_runs(rest_client, concurrent_dataset, concurrent_ids, expected_run="DONE")---def test_documents_stop_parse_requires_auth(rest_client, create_document):- dataset_id, document_id = create_document("stop_parse_auth_target.txt")- parse_res = rest_client.post(- f"/datasets/{dataset_id}/documents/parse",- json={"document_ids": [document_id]},- )- assert parse_res.status_code == 200- assert parse_res.json()["code"] == 0, parse_res.json()- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.post(f"/datasets/{dataset_id}/documents/stop", json={"document_ids": [document_id]})- assert res.status_code == 401, (scenario_name, res.text)- body = res.json()- assert_auth_error(body, scenario_name)---def test_documents_stop_parse_contract_matrix(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=6)- doc_ids = [doc["id"] for doc in uploaded_docs]-- parse_res = rest_client.post(f"/datasets/{dataset_id}/documents/parse", json={"document_ids": doc_ids}, timeout=60)- assert parse_res.status_code == 200- parse_payload = parse_res.json()- assert parse_payload["code"] == 0, parse_payload-- invalid_payloads = [- ("empty ids", {"document_ids": []}, 102, "`document_ids` is required"),- ("invalid id", {"document_ids": ["invalid_id"]}, 102, "Documents not found: ['invalid_id']"),- ("special invalid id", {"document_ids": ["\\n!?。;!?\"'"]}, 102, "Documents not found:"),- ("not json object", "not json", 100, "object has no attribute"),- ]- for case_name, payload, expected_code, expected_message in invalid_payloads:- res = rest_client.post(f"/datasets/{dataset_id}/documents/stop", json=payload, timeout=60)- assert res.status_code == 200, (case_name, res.text)- body = res.json()- assert body["code"] == expected_code, (case_name, body)- assert expected_message in body["message"], (case_name, body)-- stop_subset_res = rest_client.post(f"/datasets/{dataset_id}/documents/stop", json={"document_ids": doc_ids[:3]}, timeout=60)- assert stop_subset_res.status_code == 200- stop_subset_payload = stop_subset_res.json()- assert stop_subset_payload["code"] == 0, stop_subset_payload- assert stop_subset_payload["data"]["success_count"] >= 0, stop_subset_payload-- duplicate_stop_res = rest_client.post(- f"/datasets/{dataset_id}/documents/stop",- json={"document_ids": doc_ids[:3] + doc_ids[:3]},- timeout=60,- )- assert duplicate_stop_res.status_code == 200- duplicate_stop_payload = duplicate_stop_res.json()- assert duplicate_stop_payload["code"] == 0, duplicate_stop_payload- assert any("Duplicate document ids:" in err for err in duplicate_stop_payload["data"].get("errors", [])), duplicate_stop_payload-- repeated_stop_res = rest_client.post(f"/datasets/{dataset_id}/documents/stop", json={"document_ids": doc_ids[:3]}, timeout=60)- assert repeated_stop_res.status_code == 200- repeated_stop_payload = repeated_stop_res.json()- assert repeated_stop_payload["code"] in (0, 102), repeated_stop_payload- if repeated_stop_payload["code"] == 102:- assert "Can't stop parsing document that has not started or already completed" in repeated_stop_payload["message"], repeated_stop_payload---def test_documents_stop_parse_invalid_dataset_partial_and_scaled_concurrency(rest_client, create_dataset, tmp_path):- dataset_id, uploaded_docs = _seed_documents(rest_client, create_dataset, tmp_path, count=25)- doc_ids = [doc["id"] for doc in uploaded_docs]-- parse_res = rest_client.post(f"/datasets/{dataset_id}/documents/parse", json={"document_ids": doc_ids}, timeout=60)- assert parse_res.status_code == 200- assert parse_res.json()["code"] == 0, parse_res.json()-- for bad_dataset in ("", "invalid_dataset_id"):- path = f"/datasets/{bad_dataset}/documents/stop" if bad_dataset else "/datasets//documents/stop"- res = rest_client.post(path, json={"document_ids": doc_ids[:1]})- assert res.status_code == 200, (bad_dataset, res.text)- body = res.json()- if bad_dataset == "":- assert body["code"] == 100, (bad_dataset, body)- assert "Method Not Allowed" in body["message"], (bad_dataset, body)- else:- assert body["code"] == 102, (bad_dataset, body)- assert "You don't own the dataset" in body["message"], (bad_dataset, body)-- for payload in (- {"document_ids": ["invalid_id"] + doc_ids[:3]},- {"document_ids": doc_ids[:1] + ["invalid_id"] + doc_ids[1:3]},- {"document_ids": doc_ids[:3] + ["invalid_id"]},- ):- res = rest_client.post(f"/datasets/{dataset_id}/documents/stop", json=payload, timeout=60)- assert res.status_code == 200, (payload, res.text)- body = res.json()- assert body["code"] == 102, (payload, body)- assert body["message"] == "Documents not found: ['invalid_id']", (payload, body)-- with ThreadPoolExecutor(max_workers=8) as executor:- futures = [- executor.submit(- rest_client.post,- f"/datasets/{dataset_id}/documents/stop",- json={"document_ids": [doc_id]},- timeout=60,- )- for doc_id in doc_ids- ]- responses = [future.result() for future in futures]- assert len(responses) == len(doc_ids), responses- assert all(res.status_code == 200 for res in responses)- assert all(res.json()["code"] == 0 for res in responses)-- stop_all_res = rest_client.post(f"/datasets/{dataset_id}/documents/stop", json={"document_ids": doc_ids}, timeout=60)- assert stop_all_res.status_code == 200- stop_all_payload = stop_all_res.json()- assert stop_all_payload["code"] == 0, stop_all_payload- assert stop_all_payload["data"]["success_count"] >= 0, stop_all_payload---def test_documents_download_requires_auth_and_invalid_id_contract(rest_client, create_document, tmp_path):- dataset_id, document_id = create_document("download_target.txt")-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.get(f"/datasets/{dataset_id}/documents/{document_id}")- assert res.status_code == 401, (scenario_name, res.text)- body = res.json()- assert body["code"] == 401, (scenario_name, body)-- invalid_doc_path = tmp_path / "invalid_doc_download.txt"- invalid_doc_res = _download_document_to_file(rest_client, dataset_id, "invalid_document_id", invalid_doc_path)- assert invalid_doc_res.status_code == 200- invalid_doc_payload = invalid_doc_res.json()- assert invalid_doc_payload["code"] == 404, invalid_doc_payload- assert invalid_doc_payload["message"] == "Resource not found", invalid_doc_payload-- invalid_dataset_path = tmp_path / "invalid_dataset_download.txt"- invalid_dataset_res = _download_document_to_file(rest_client, "invalid_dataset_id", document_id, invalid_dataset_path)- assert invalid_dataset_res.status_code == 200- invalid_dataset_payload = invalid_dataset_res.json()- assert invalid_dataset_payload["code"] == 404, invalid_dataset_payload- assert invalid_dataset_payload["message"] == "Resource not found", invalid_dataset_payload---def test_documents_download_filetype_repeat_and_concurrent_contract(rest_client, create_dataset, tmp_path):- dataset_id = create_dataset("dataset_download_contract")-- creators = {- "docx": create_docx_file,- "xlsx": create_excel_file,- "pptx": create_ppt_file,- "png": create_image_file,- "pdf": create_pdf_file,- "txt": create_txt_file,- "md": create_md_file,- "json": create_json_file,- "eml": create_eml_file,- "html": create_html_file,- }-- uploaded = []- for ext, creator in creators.items():- source_path = Path(creator(tmp_path / f"download_type_{ext}.{ext}"))- with source_path.open("rb") as file_obj:- upload_res = rest_client.post(- f"/datasets/{dataset_id}/documents",- files=[("file", (source_path.name, file_obj))],- )- assert upload_res.status_code == 200, (ext, upload_res.text)- upload_payload = upload_res.json()- assert upload_payload["code"] == 0, (ext, upload_payload)- uploaded.append((source_path, upload_payload["data"][0]["id"]))-- for source_path, document_id in uploaded:- target_path = tmp_path / f"download_once_{source_path.name}"- download_res = _download_document_to_file(rest_client, dataset_id, document_id, target_path)- assert download_res.status_code == 200, (source_path.name, download_res.text)- assert compare_by_hash(source_path, target_path), source_path.name-- first_source, first_document_id = uploaded[0]- for index in range(5):- repeated_path = tmp_path / f"download_repeat_{index}_{first_source.name}"- repeated_res = _download_document_to_file(rest_client, dataset_id, first_document_id, repeated_path)- assert repeated_res.status_code == 200, (index, repeated_res.text)- assert compare_by_hash(first_source, repeated_path), index-- with ThreadPoolExecutor(max_workers=8) as executor:- futures = [- executor.submit(- _download_document_to_file,- rest_client,- dataset_id,- document_id,- tmp_path / f"download_concurrent_{i}_{source_path.name}",- )- for i, (source_path, document_id) in enumerate(uploaded)- ]- _responses = list(as_completed(futures))- assert len(_responses) == len(uploaded), _responses- for i, (source_path, _document_id) in enumerate(uploaded):- downloaded_path = tmp_path / f"download_concurrent_{i}_{source_path.name}"- assert downloaded_path.exists(), source_path.name- assert compare_by_hash(source_path, downloaded_path), source_path.name---def test_documents_table_parser_chat_patterns(rest_client, clear_datasets, tmp_path):- create_dataset_res = rest_client.post(- "/datasets",- json={"name": f"table_parser_dataset_contract_{uuid.uuid4().hex[:8]}", "chunk_method": "table"},- )- assert create_dataset_res.status_code == 200- create_dataset_payload = create_dataset_res.json()- assert create_dataset_payload["code"] == 0, create_dataset_payload- dataset_id = create_dataset_payload["data"]["id"]-- excel_a = _create_table_excel(- tmp_path / "table_a.xlsx",- [- ["employee_id", "name", "department", "salary"],- ["E001", "Alice Johnson", "Engineering", "95000"],- ["E002", "Bob Smith", "Marketing", "65000"],- ["E003", "Carol Williams", "Engineering", "88000"],- ],- )- excel_b = _create_table_excel(- tmp_path / "table_b.xlsx",- [- ["product", "price", "category"],- ["Laptop", "999", "Electronics"],- ["Keyboard", "79", "Electronics"],- ["Desk", "299", "Furniture"],- ],- )- with ExitStack() as stack:- files = [- ("file", (excel_a.name, stack.enter_context(excel_a.open("rb")))),- ("file", (excel_b.name, stack.enter_context(excel_b.open("rb")))),- ]- upload_res = rest_client.post(f"/datasets/{dataset_id}/documents", files=files)- assert upload_res.status_code == 200- upload_payload = upload_res.json()- assert upload_payload["code"] == 0, upload_payload- document_ids = [doc["id"] for doc in upload_payload["data"]]-- parse_res = rest_client.post(- f"/datasets/{dataset_id}/documents/parse",- json={"document_ids": document_ids},- timeout=60,- )- assert parse_res.status_code == 200- parse_payload = parse_res.json()- assert parse_payload["code"] == 0, parse_payload- _wait_document_runs(rest_client, dataset_id, document_ids, expected_run="DONE")-- chat_payload = {- "name": f"table_parser_chat_{uuid.uuid4().hex[:8]}",- "dataset_ids": [dataset_id],- "prompt_config": {- "system": "Use table knowledge to answer questions.",- "parameters": [{"key": "knowledge", "optional": True, "value": "Answer with table evidence."}],- },- }- create_chat_res = rest_client.post("/chats", json=chat_payload)- assert create_chat_res.status_code == 200- create_chat_payload = create_chat_res.json()- assert create_chat_payload["code"] == 0, create_chat_payload- chat_id = create_chat_payload["data"]["id"]-- create_session_res = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "table_parser_session"})- assert create_session_res.status_code == 200- create_session_payload = create_session_res.json()- assert create_session_payload["code"] == 0, create_session_payload- session_id = create_session_payload["data"]["id"]-- questions = [- "show me column of product",- "which product has price 79",- "How many rows in the dataset?",- "Show me all employees in Engineering department",- ]- for question in questions:- completion_res = rest_client.post(- "/chat/completions",- json={- "chat_id": chat_id,- "session_id": session_id,- "messages": [{"role": "user", "content": question}],- "stream": False,- },- timeout=60,- )- assert completion_res.status_code == 200, (question, completion_res.text)- completion_payload = completion_res.json()- assert completion_payload["code"] == 0, (question, completion_payload)- answer = completion_payload["data"]["answer"]- assert isinstance(answer, str) and answer.strip(), (question, completion_payload)diff --git a/test/testcases/restful_api/test_files.py b/test/testcases/restful_api/test_files.pydeleted file mode 100644index 291a74df8e..0000000000--- a/test/testcases/restful_api/test_files.py+++ /dev/null@@ -1,101 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import uuid--import pytest--from test.testcases.utils.file_utils import create_txt_file---def _assert_ok(res, *, expect_list=False):- assert res.status_code == 200, res.text- payload = res.json()- assert payload["code"] == 0, payload- data = payload["data"]- if expect_list:- assert isinstance(data, dict), payload- assert "files" in data and "total" in data, payload- return data---def test_files_list_create_folder_and_upload_to_root(rest_client, tmp_path):- """Exercise GET/POST /files so connector offload paths run on the live server."""- created_ids: list[str] = []- suffix = uuid.uuid4().hex[:12]- folder_name = f"rf_folder_{suffix}"- upload_name = f"rf_upload_{suffix}.txt"-- try:- list_data = _assert_ok(rest_client.get("/files"), expect_list=True)- assert isinstance(list_data["files"], list), list_data-- folder = _assert_ok(rest_client.post("/files", json={"name": folder_name, "type": "folder"}))- folder_id = folder.get("id")- assert folder_id, folder- created_ids.append(folder_id)- assert folder.get("name") == folder_name-- fp = create_txt_file(tmp_path / upload_name)- with fp.open("rb") as file_obj:- upload_res = rest_client.post("/files", files=[("file", (fp.name, file_obj))])- uploaded = _assert_ok(upload_res)- assert isinstance(uploaded, list) and uploaded, uploaded- file_id = uploaded[0].get("id")- assert file_id, uploaded- created_ids.append(file_id)- assert uploaded[0].get("name") == upload_name- finally:- if created_ids:- rest_client.delete("/files", json={"ids": created_ids})---def test_files_folder_create_fallback_and_rename_duplicate(rest_client):- created_ids: list[str] = []- suffix = uuid.uuid4().hex[:12]- folder_name = f"rf_dup_folder_{suffix}"-- try:- first = _assert_ok(rest_client.post("/files", json={"name": folder_name, "type": "folder"}))- created_ids.append(first["id"])- assert first["name"] == folder_name, first-- # A duplicate create falls back to a numbered name instead of failing.- second = _assert_ok(rest_client.post("/files", json={"name": folder_name, "type": "folder"}))- created_ids.append(second["id"])- assert second["name"] == f"{folder_name}(1)", second-- # Renaming onto another entry's name is rejected.- duplicate_rename = rest_client.post(- "/files/move",- json={"src_file_ids": [second["id"]], "new_name": folder_name},- )- assert duplicate_rename.status_code == 200, duplicate_rename.text- duplicate_rename_payload = duplicate_rename.json()- assert duplicate_rename_payload["code"] != 0, duplicate_rename_payload- assert "duplicated file name" in duplicate_rename_payload["message"], duplicate_rename_payload-- # A case-only rename of the same entry is allowed.- case_only_rename = rest_client.post(- "/files/move",- json={"src_file_ids": [second["id"]], "new_name": second["name"].upper()},- )- assert _assert_ok(case_only_rename) is True, case_only_rename.text- finally:- if created_ids:- rest_client.delete("/files", json={"ids": created_ids})diff --git a/test/testcases/restful_api/test_langfuse_routes.py b/test/testcases/restful_api/test_langfuse_routes.pydeleted file mode 100644index deda7fbe3e..0000000000--- a/test/testcases/restful_api/test_langfuse_routes.py+++ /dev/null@@ -1,37 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import pytest---def test_langfuse_api_key_routes_require_auth(rest_client_noauth):- for method in ("get", "post", "put", "delete"):- requester = getattr(rest_client_noauth, method)- kwargs = {"json": {"secret_key": "s", "public_key": "p", "host": "http://example.com"}} if method in {"post", "put"} else {}- res = requester("/langfuse/api-key", **kwargs)- assert res.status_code == 401- payload = res.json()- assert payload["code"] == 401, (method, payload)---def test_langfuse_api_key_missing_required_fields(rest_client):- res = rest_client.post("/langfuse/api-key", json={"secret_key": "", "public_key": "pub", "host": "http://host"})- assert res.status_code == 200- payload = res.json()- assert payload["code"] in (101, 102), payload- assert "required" in payload["message"].lower() or "missing" in payload["message"].lower(), payloaddiff --git a/test/testcases/restful_api/test_memories_messages.py b/test/testcases/restful_api/test_memories_messages.pydeleted file mode 100644index 69bde501f6..0000000000--- a/test/testcases/restful_api/test_memories_messages.py+++ /dev/null@@ -1,236 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import time-import uuid--import pytest---def memory_cleanup(rest_client):- created_ids: list[str] = []-- def _cleanup():- cleanup_errors = []- for memory_id in created_ids:- delete_res = rest_client.delete(f"/memories/{memory_id}")- if delete_res.status_code != 200:- cleanup_errors.append((memory_id, delete_res.status_code, delete_res.text))- continue- delete_payload = delete_res.json()- if delete_payload["code"] not in (0, 404):- cleanup_errors.append((memory_id, delete_res.status_code, delete_payload))- assert not cleanup_errors, f"Memory cleanup failed: {cleanup_errors}"-- yield created_ids- _cleanup()---def create_memory_resource(rest_client, memory_cleanup):- def _create(name_prefix: str = "restful_memory") -> str:- payload = {- "name": f"{name_prefix}_{uuid.uuid4().hex[:8]}",- "memory_type": ["raw"],- "embd_id": "BAAI/bge-small-en-v1.5@Local@Builtin",- "llm_id": "glm-4-flash@CI@ZHIPU-AI",- }- res = rest_client.post("/memories", json=payload)- assert res.status_code == 200- res_payload = res.json()- assert res_payload["code"] == 0, res_payload- memory_id = res_payload["data"]["id"]- memory_cleanup.append(memory_id)- return memory_id-- yield _create---def _add_message(rest_client, memory_id: str, user_input: str, agent_response: str) -> None:- add_res = rest_client.post(- "/messages",- json={- "memory_id": [memory_id],- "agent_id": uuid.uuid4().hex,- "session_id": uuid.uuid4().hex,- "user_id": uuid.uuid4().hex,- "user_input": user_input,- "agent_response": agent_response,- },- )- assert add_res.status_code == 200- add_payload = add_res.json()- assert add_payload["code"] == 0, add_payload---def _wait_for_memory_messages(rest_client, memory_id: str, timeout: float = 10, interval: float = 0.2) -> list[dict]:- deadline = time.time() + timeout- last_payload = None- while time.time() < deadline:- res = rest_client.get(f"/memories/{memory_id}")- if res.status_code == 200:- payload = res.json()- last_payload = payload- if payload.get("code") == 0:- message_list = payload.get("data", {}).get("messages", {}).get("message_list", [])- if message_list:- return message_list- time.sleep(interval)- pytest.fail(f"Timed out waiting for memory messages: {last_payload}")---def test_memory_crud_cycle(rest_client, create_memory_resource):- memory_id = create_memory_resource("restful_memory_crud")-- list_res = rest_client.get("/memories")- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert any(item["id"] == memory_id for item in list_payload["data"]["memory_list"]), list_payload-- config_res = rest_client.get(f"/memories/{memory_id}/config")- assert config_res.status_code == 200- config_payload = config_res.json()- assert config_payload["code"] == 0, config_payload- assert config_payload["data"]["id"] == memory_id, config_payload-- update_res = rest_client.put(- f"/memories/{memory_id}",- json={"name": f"updated_{uuid.uuid4().hex[:6]}", "permissions": "me"},- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload-- delete_res = rest_client.delete(f"/memories/{memory_id}")- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload---def test_memory_create_missing_required_fields(rest_client):- res = rest_client.post("/memories", json={"name": "missing_models", "memory_type": ["raw"]})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 400, payload---def test_messages_add_list_recent_content_update_forget(rest_client, create_memory_resource):- memory_id = create_memory_resource("restful_message_memory")- _add_message(- rest_client,- memory_id,- user_input="what is coriander?",- agent_response="coriander can refer to leaves or seeds",- )-- message_list = _wait_for_memory_messages(rest_client, memory_id)-- message_id = message_list[0]["message_id"]-- recent_res = rest_client.get("/messages", params={"memory_id": memory_id, "limit": 10})- assert recent_res.status_code == 200- recent_payload = recent_res.json()- assert recent_payload["code"] == 0, recent_payload- assert any(item["message_id"] == message_id for item in recent_payload["data"]), recent_payload-- content_res = rest_client.get(f"/messages/{memory_id}:{message_id}/content")- assert content_res.status_code == 200- content_payload = content_res.json()- assert content_payload["code"] == 0, content_payload- assert content_payload["data"]["content"], content_payload-- update_res = rest_client.put(f"/messages/{memory_id}:{message_id}", json={"status": False})- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload-- forget_res = rest_client.delete(f"/messages/{memory_id}:{message_id}")- assert forget_res.status_code == 200- forget_payload = forget_res.json()- assert forget_payload["code"] == 0, forget_payload---def test_message_keeps_the_caller_supplied_user_id(rest_client, create_memory_resource):- """An API key belongs to an agent backend talking on behalf of its own end- users, so the `user_id` it sends is the subject the memory is written- against rather than the key owner's tenant id."""- memory_id = create_memory_resource("restful_message_user_id")- external_user_id = f"external-{uuid.uuid4().hex}"-- add_res = rest_client.post(- "/messages",- json={- "memory_id": [memory_id],- "agent_id": uuid.uuid4().hex,- "session_id": uuid.uuid4().hex,- "user_id": external_user_id,- "user_input": "who am I?",- "agent_response": "you are the one asking",- },- )- assert add_res.status_code == 200- assert add_res.json()["code"] == 0, add_res.json()-- message_list = _wait_for_memory_messages(rest_client, memory_id)- assert {message["user_id"] for message in message_list} == {external_user_id}, message_list---def test_message_status_validation_requires_boolean(rest_client, create_memory_resource):- memory_id = create_memory_resource("restful_message_status_validation")- _add_message(rest_client, memory_id, user_input="hello", agent_response="hello")-- message_id = _wait_for_memory_messages(rest_client, memory_id)[0]["message_id"]-- invalid_update = rest_client.put(f"/messages/{memory_id}:{message_id}", json={"status": "false"})- assert invalid_update.status_code == 200- invalid_payload = invalid_update.json()- assert invalid_payload["code"] == 101, invalid_payload- assert "Status must be a boolean." in invalid_payload["message"], invalid_payload---def test_messages_recent_requires_memory_ids(rest_client):- res = rest_client.get("/messages")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- assert "memory_ids is required" in payload["message"], payload---def test_message_search_route_contract(rest_client, create_memory_resource):- memory_id = create_memory_resource("restful_message_search")- _add_message(- rest_client,- memory_id,- user_input="what is pineapple?",- agent_response="pineapple is a tropical fruit",- )-- _wait_for_memory_messages(rest_client, memory_id)-- res = rest_client.get("/messages/search", params={"memory_id": memory_id, "query": "pineapple", "top_n": 3})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert isinstance(payload["data"], list), payloaddiff --git a/test/testcases/restful_api/test_memory_messages.py b/test/testcases/restful_api/test_memory_messages.pydeleted file mode 100644index e0546341be..0000000000--- a/test/testcases/restful_api/test_memory_messages.py+++ /dev/null@@ -1,165 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import uuid--import pytest---def _memory_payload(name: str) -> dict:- return {- "name": name,- "memory_type": ["raw"],- "embd_id": "BAAI/bge-small-en-v1.5@Local@Builtin",- "llm_id": "glm-4-flash@CI@ZHIPU-AI",- }---def _create_memory(rest_client, name: str) -> dict:- res = rest_client.post("/memories", json=_memory_payload(name))- assert res.status_code == 200- payload = res.json()- if payload["code"] == 0:- return payload["data"]-- pytest.fail(f"Failed to create memory: {payload}")---def memory_resource(rest_client):- memory = _create_memory(rest_client, f"restful_memory_{uuid.uuid4().hex[:8]}")- memory_id = memory["id"]- try:- yield memory- finally:- delete_res = rest_client.delete(f"/memories/{memory_id}")- assert delete_res.status_code == 200, delete_res.text- delete_payload = delete_res.json()- assert delete_payload["code"] in (0, 404), delete_payload---def test_memory_and_message_routes_require_auth(rest_client_noauth):- memory_res = rest_client_noauth.get("/memories")- assert memory_res.status_code == 401- memory_payload = memory_res.json()- assert memory_payload["code"] == 401, memory_payload-- msg_list_res = rest_client_noauth.get("/messages")- assert msg_list_res.status_code == 401- msg_list_payload = msg_list_res.json()- assert msg_list_payload["code"] == 401, msg_list_payload-- msg_search_res = rest_client_noauth.get("/messages/search")- assert msg_search_res.status_code == 401- msg_search_payload = msg_search_res.json()- assert msg_search_payload["code"] == 401, msg_search_payload---def test_memory_crud_and_config(rest_client):- memory = _create_memory(rest_client, f"restful_memory_crud_{uuid.uuid4().hex[:8]}")- memory_id = memory["id"]- try:- config_res = rest_client.get(f"/memories/{memory_id}/config")- assert config_res.status_code == 200- config_payload = config_res.json()- assert config_payload["code"] == 0, config_payload- assert config_payload["data"]["id"] == memory_id, config_payload-- list_res = rest_client.get("/memories", params={"keywords": memory["name"]})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert any(item["id"] == memory_id for item in list_payload["data"]["memory_list"]), list_payload-- update_res = rest_client.put(f"/memories/{memory_id}", json={"name": "restful_memory_updated"})- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload- finally:- delete_res = rest_client.delete(f"/memories/{memory_id}")- assert delete_res.status_code == 200, delete_res.text- delete_payload = delete_res.json()- assert delete_payload["code"] in (0, 404), delete_payload---def test_memory_update_invalid_name(rest_client, memory_resource):- memory_id = memory_resource["id"]- res = rest_client.put(f"/memories/{memory_id}", json={"name": " "})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- assert "cannot be empty" in payload["message"], payload---def test_messages_list_and_search_validation_contracts(rest_client, memory_resource):- memory_id = memory_resource["id"]-- list_res = rest_client.get("/messages", params={"memory_id": memory_id, "limit": 10})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert isinstance(list_payload["data"], list), list_payload-- missing_memory_res = rest_client.get("/messages")- assert missing_memory_res.status_code == 200- missing_memory_payload = missing_memory_res.json()- assert missing_memory_payload["code"] == 101, missing_memory_payload- assert "memory_ids is required" in missing_memory_payload["message"], missing_memory_payload-- search_res = rest_client.get("/messages/search", params={"memory_id": memory_id, "query": "coriander"})- assert search_res.status_code == 200- search_payload = search_res.json()- assert search_payload["code"] == 0, search_payload- assert isinstance(search_payload["data"], list), search_payload-- search_no_memory = rest_client.get("/messages/search", params={"query": "coriander"})- assert search_no_memory.status_code == 200- search_no_memory_payload = search_no_memory.json()- assert search_no_memory_payload["code"] == 0, search_no_memory_payload- assert isinstance(search_no_memory_payload["data"], list), search_no_memory_payload---def test_message_update_forget_and_content_error_contracts(rest_client, memory_resource):- memory_id = memory_resource["id"]-- invalid_status_res = rest_client.put(- f"/messages/{memory_id}:1",- json={"status": "false"},- )- assert invalid_status_res.status_code == 200- invalid_status_payload = invalid_status_res.json()- assert invalid_status_payload["code"] == 101, invalid_status_payload- assert "Status must be a boolean" in invalid_status_payload["message"], invalid_status_payload-- missing_content_res = rest_client.get(f"/messages/{memory_id}:999999/content")- assert missing_content_res.status_code == 200- missing_content_payload = missing_content_res.json()- assert missing_content_payload["code"] == 404, missing_content_payload-- invalid_memory_forget = rest_client.delete("/messages/missing_memory_id:1")- assert invalid_memory_forget.status_code == 200- invalid_memory_forget_payload = invalid_memory_forget.json()- assert invalid_memory_forget_payload["code"] == 404, invalid_memory_forget_payload-- invalid_memory_update = rest_client.put("/messages/missing_memory_id:1", json={"status": False})- assert invalid_memory_update.status_code == 200- invalid_memory_update_payload = invalid_memory_update.json()- assert invalid_memory_update_payload["code"] == 404, invalid_memory_update_payloaddiff --git a/test/testcases/restful_api/test_openai_compatible.py b/test/testcases/restful_api/test_openai_compatible.pydeleted file mode 100644index d5e9f85fba..0000000000--- a/test/testcases/restful_api/test_openai_compatible.py+++ /dev/null@@ -1,242 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import json--import pytest---def _sse_events(response_text: str) -> list[str]:- return [line[5:] for line in response_text.splitlines() if line.startswith("data:")]---def skip_if_go_proxy_upstream_error(choice_message: dict) -> None:- if choice_message.get("reference") is None and choice_message.get("content", "").startswith("**ERROR**"):- pytest.skip("Go OpenAI-compatible completion could not reach the configured chat model")--- "payload, expected_message",- [- (- {- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "extra_body": "invalid_extra_body",- },- "extra_body must be an object.",- ),- (- {- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "extra_body": {"reference_metadata": "invalid_reference_metadata"},- },- "reference_metadata must be an object.",- ),- (- {- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "extra_body": {"reference_metadata": {"fields": "author"}},- },- "reference_metadata.fields must be an array.",- ),- (- {- "model": "model",- "messages": [],- },- "You have to provide messages.",- ),- (- {- "model": "model",- "messages": [{"role": "assistant", "content": "hello"}],- },- "The last content of this conversation is not from user.",- ),- ],-)-def test_openai_compatible_validation_payloads(rest_client, create_chat, payload, expected_message):- chat_id = create_chat("restful_openai_validation_chat")- res = rest_client.post(f"/openai/{chat_id}/chat/completions", json=payload)- assert res.status_code == 200- data = res.json()- assert data["code"] != 0, data- assert expected_message in data.get("message", ""), data---def test_openai_compatible_metadata_condition_requires_object(rest_client, create_chat):- chat_id = create_chat("restful_openai_metadata_condition_chat")- res = rest_client.post(- f"/openai/{chat_id}/chat/completions",- json={- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "extra_body": {"metadata_condition": "invalid"},- },- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- assert "metadata_condition must be an object." in payload["message"], payload---def test_openai_compatible_invalid_chat(rest_client):- res = rest_client.post(- "/openai/invalid_chat_id/chat/completions",- json={- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- },- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 404, payload- assert payload["message"] == "Resource not found", payload---def test_openai_compatible_nonstream_shape(rest_client, create_chat):- chat_id = create_chat("restful_openai_nonstream_chat")- res = rest_client.post(- f"/openai/{chat_id}/chat/completions",- json={- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- },- timeout=120,- )- assert res.status_code == 200- payload = res.json()-- assert payload.get("object") == "chat.completion", payload- assert isinstance(payload["choices"], list) and payload["choices"], payload- first_choice = payload["choices"][0]- assert first_choice.get("finish_reason") == "stop", payload- assert first_choice.get("message", {}).get("role") == "assistant", payload- assert "content" in first_choice.get("message", {}), payload-- usage = payload.get("usage", {})- assert "prompt_tokens" in usage, usage- assert "completion_tokens" in usage, usage- assert "total_tokens" in usage, usage- assert usage["prompt_tokens"] > 0, usage- assert usage["completion_tokens"] > 0, usage- assert usage["total_tokens"] == usage["prompt_tokens"] + usage["completion_tokens"], usage---def test_openai_compatible_defaults_to_nonstream_when_stream_is_missing(rest_client, create_chat):- chat_id = create_chat("restful_openai_default_nonstream_chat")- res = rest_client.post(- f"/openai/{chat_id}/chat/completions",- json={- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- },- timeout=120,- )- assert res.status_code == 200- assert "application/json" in res.headers.get("Content-Type", ""), res.headers.get("Content-Type", "")-- payload = res.json()- assert payload["object"] == "chat.completion", payload- assert isinstance(payload["choices"], list) and payload["choices"], payload- assert payload["choices"][0].get("finish_reason") == "stop", payload---def test_openai_compatible_nonstream_with_reference_output_shape(rest_client, create_chat):- chat_id = create_chat("restful_openai_reference_chat")- res = rest_client.post(- f"/openai/{chat_id}/chat/completions",- json={- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- "extra_body": {- "reference": True,- "reference_metadata": {"include": True, "fields": ["author"]},- },- },- timeout=120,- )- assert res.status_code == 200- payload = res.json()- choice_msg = payload["choices"][0]["message"]- skip_if_go_proxy_upstream_error(choice_msg)- assert "reference" in choice_msg, payload- assert isinstance(choice_msg["reference"], list), payload---def test_openai_compatible_stream_shape_and_done_semantics(rest_client, create_chat):- chat_id = create_chat("restful_openai_stream_chat")- res = rest_client.post(- f"/openai/{chat_id}/chat/completions",- json={- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "stream": True,- "extra_body": {"reference": True},- },- timeout=60,- )- assert res.status_code == 200- content_type = res.headers.get("Content-Type", "")- assert "text/event-stream" in content_type, content_type-- events = _sse_events(res.text)- assert events, res.text- assert events[-1].strip() == "[DONE]", events[-1]-- json_events = [json.loads(evt) for evt in events if evt.strip() != "[DONE]"]- assert json_events, events- assert any(evt.get("object") == "chat.completion.chunk" for evt in json_events), json_events- assert any(evt.get("choices", [{}])[0].get("finish_reason") == "stop" for evt in json_events), json_events---def test_openai_compatible_reference_metadata_fields_filter_accepts_array(rest_client, create_chat):- chat_id = create_chat("restful_openai_reference_fields_array_chat")- res = rest_client.post(- f"/openai/{chat_id}/chat/completions",- json={- "model": "model",- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- "extra_body": {- "reference": True,- "reference_metadata": {"include": True, "fields": ["author", "year"]},- },- },- timeout=60,- )- assert res.status_code == 200- payload = res.json()- assert payload.get("choices"), payload- choice_msg = payload["choices"][0]["message"]- skip_if_go_proxy_upstream_error(choice_msg)- assert "reference" in choice_msg, payload- print(payload)- assert isinstance(choice_msg["reference"], list), payloaddiff --git a/test/testcases/restful_api/test_plugin_tools.py b/test/testcases/restful_api/test_plugin_tools.pydeleted file mode 100644index 8c637b00b9..0000000000--- a/test/testcases/restful_api/test_plugin_tools.py+++ /dev/null@@ -1,76 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import importlib.util-import sys-from pathlib import Path-from types import ModuleType--import pytest---def test_plugin_tools_requires_auth(rest_client_noauth):- res = rest_client_noauth.get("/plugin/tools")- assert res.status_code == 401- payload = res.json()- assert payload["code"] == 401, payload---def test_plugin_tools_contract(rest_client):- res = rest_client.get("/plugin/tools")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert isinstance(payload["data"], list), payload---class _DummyManager:- def route(self, *_args, **_kwargs):- def decorator(func):- return func-- return decorator---def _load_plugin_module(monkeypatch):- repo_root = Path(__file__).resolve().parents[3]-- common_pkg = ModuleType("common")- common_pkg.__path__ = [str(repo_root / "common")]- monkeypatch.setitem(sys.modules, "common", common_pkg)-- stub_apps = ModuleType("api.apps")- stub_apps.login_required = lambda func: func- monkeypatch.setitem(sys.modules, "api.apps", stub_apps)-- stub_plugin = ModuleType("agent.plugin")-- class _StubGlobalPluginManager:- @staticmethod- def get_llm_tools():- return []-- stub_plugin.GlobalPluginManager = _StubGlobalPluginManager- monkeypatch.setitem(sys.modules, "agent.plugin", stub_plugin)-- module_path = repo_root / "api" / "apps" / "restful_apis" / "plugin_api.py"- spec = importlib.util.spec_from_file_location("restful_plugin_api_unit", module_path)- module = importlib.util.module_from_spec(spec)- module.manager = _DummyManager()- spec.loader.exec_module(module)- return modulediff --git a/test/testcases/restful_api/test_retrieval.py b/test/testcases/restful_api/test_retrieval.pydeleted file mode 100644index 0908a4925a..0000000000--- a/test/testcases/restful_api/test_retrieval.py+++ /dev/null@@ -1,420 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--from concurrent.futures import ThreadPoolExecutor-import pytest-from test.testcases.configs import INVALID_API_TOKEN-from test.testcases.restful_api.helpers.client import RestClient-from test.testcases.utils import wait_for---def test_dataset_search_rest_endpoint(rest_client, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- request_body = {"question": "test TXT file", "top_k": 5}- res = rest_client.post(- f"/datasets/{dataset_id}/search",- json=request_body,- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert "chunks" in payload["data"], payload- retrieval_payload = rest_client.post("/retrieval", json={"dataset_ids": [dataset_id], **request_body}).json()- assert payload == retrieval_payload---def test_multi_dataset_search_rest_endpoint(rest_client, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- request_body = {"dataset_ids": [dataset_id], "question": "test TXT file", "top_k": 5}- res = rest_client.post(- "/datasets/search",- json=request_body,- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert "chunks" in payload["data"], payload- for chunk in payload["data"]["chunks"]:- assert "id" in chunk, payload- assert "content" in chunk, payload- assert "document_id" in chunk, payload- assert "dataset_id" in chunk, payload- retrieval_payload = rest_client.post("/retrieval", json=request_body).json()- assert payload == retrieval_payload---def test_multi_dataset_search_with_metadata_filter(rest_client, ensure_parsed_document):- dataset_id, document_id = ensure_parsed_document()- meta_res = rest_client.patch(- f"/datasets/{dataset_id}/documents/metadatas",- json={- "selector": {"document_ids": [document_id]},- "updates": [{"key": "author", "value": "qa_batch2"}],- "deletes": [],- },- )- assert meta_res.status_code == 200- meta_payload = meta_res.json()- assert meta_payload["code"] == 0, meta_payload-- res = rest_client.post(- "/datasets/search",- json={- "dataset_ids": [dataset_id],- "question": "test TXT file",- "metadata_condition": {- "logic": "and",- "conditions": [{"name": "author", "comparison_operator": "=", "value": "qa_batch2"}],- },- },- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert "chunks" in payload["data"], payload---def test_retrieval_endpoint(rest_client, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- res = rest_client.post(- "/retrieval",- json={"dataset_ids": [dataset_id], "question": "test TXT file", "top_k": 5},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert "chunks" in payload["data"], payload---def test_retrieval_compatibility_requires_dataset_ids(rest_client):- res = rest_client.post("/retrieval", json={"question": "test"})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert payload["message"] == "`dataset_ids` is required.", payload---def test_retrieval_compatibility_requires_auth(rest_client_noauth):- res = rest_client_noauth.post("/retrieval", json={"question": "test", "dataset_ids": ["x"]})- assert res.status_code == 401- payload = res.json()- assert payload["code"] == 401, payload- assert payload["message"] == "<Unauthorized '401: Unauthorized'>", payload---@wait_for(20, 1, "Retrieval indexing timeout in RESTful batch 10 tests")-def _retrieval_has_question(rest_client, dataset_id, question):- res = rest_client.post("/retrieval", json={"question": question, "dataset_ids": [dataset_id]})- if res.status_code != 200:- return False- payload = res.json()- if payload["code"] != 0:- return False- return len(payload["data"]["chunks"]) > 0---@wait_for(20, 1, "Retrieval indexing timeout waiting for chunk presence in RESTful batch 10 tests")-def _retrieval_has_chunks(rest_client, dataset_id, question, chunk_ids):- res = rest_client.post("/retrieval", json={"question": question, "dataset_ids": [dataset_id]})- if res.status_code != 200:- return False- payload = res.json()- if payload["code"] != 0:- return False- retrieved_ids = {chunk["id"] for chunk in payload["data"]["chunks"]}- expected_ids = set(chunk_ids)- return expected_ids.issubset(retrieved_ids)---@wait_for(20, 1, "Retrieval indexing timeout waiting for chunk deletion in RESTful batch 10 tests")-def _retrieval_lacks_chunks(rest_client, dataset_id, question, chunk_ids):- res = rest_client.post("/retrieval", json={"question": question, "dataset_ids": [dataset_id]})- if res.status_code != 200:- return False- payload = res.json()- if payload["code"] != 0:- return False- retrieved_ids = {chunk["id"] for chunk in payload["data"]["chunks"]}- expected_ids = set(chunk_ids)- return expected_ids.isdisjoint(retrieved_ids)---def test_retrieval_requires_auth_contract():- for scenario_name, token, expected_code, expected_message in (- ("missing token", None, 401, "<Unauthorized '401: Unauthorized'>"),- ("invalid token", INVALID_API_TOKEN, 401, "<Unauthorized '401: Unauthorized'>"),- ):- client = RestClient(token=token)- res = client.post("/retrieval", json={"question": "chunk", "dataset_ids": ["x"]})- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (scenario_name, payload)- assert payload["message"] == expected_message, (scenario_name, payload)---def test_retrieval_page_and_page_size_contract(rest_client, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- cases = [- ("page none", {"question": "chunk", "dataset_ids": [dataset_id], "page": None, "page_size": 2}, 100, "TypeError"),- ("page zero", {"question": "chunk", "dataset_ids": [dataset_id], "page": 0, "page_size": 2}, 0, ""),- ("page two", {"question": "chunk", "dataset_ids": [dataset_id], "page": 2, "page_size": 2}, 0, ""),- ("page three", {"question": "chunk", "dataset_ids": [dataset_id], "page": 3, "page_size": 2}, 0, ""),- ("page str", {"question": "chunk", "dataset_ids": [dataset_id], "page": "3", "page_size": 2}, 0, ""),- ("page negative", {"question": "chunk", "dataset_ids": [dataset_id], "page": -1, "page_size": 2}, 0, ""),- ("page alpha", {"question": "chunk", "dataset_ids": [dataset_id], "page": "a", "page_size": 2}, 100, "invalid literal for int()"),- ("page_size none", {"question": "chunk", "dataset_ids": [dataset_id], "page_size": None}, 100, "TypeError"),- ("page_size one", {"question": "chunk", "dataset_ids": [dataset_id], "page_size": 1}, 0, ""),- ("page_size five", {"question": "chunk", "dataset_ids": [dataset_id], "page_size": 5}, 0, ""),- ("page_size str", {"question": "chunk", "dataset_ids": [dataset_id], "page_size": "1"}, 0, ""),- ("page_size alpha", {"question": "chunk", "dataset_ids": [dataset_id], "page_size": "a"}, 100, "invalid literal for int()"),- ]- for scenario_name, payload, expected_code, expected_message in cases:- res = rest_client.post("/retrieval", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code != 0:- assert expected_message in body["message"], (scenario_name, body)---def test_retrieval_highlight_keyword_and_invalid_params_contract(rest_client, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()-- highlight_cases = [- ("highlight true", True, True),- ("highlight true str", "True", True),- ("highlight false", False, False),- ("highlight false str", "False", False),- ("highlight none", None, False),- ]- for scenario_name, highlight_value, expect_highlight in highlight_cases:- res = rest_client.post(- "/retrieval",- json={"question": "chunk", "dataset_ids": [dataset_id], "highlight": highlight_value},- )- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == 0, (scenario_name, body)- for chunk in body["data"]["chunks"]:- if expect_highlight:- assert "highlight" in chunk, (scenario_name, body)- else:- assert "highlight" not in chunk, (scenario_name, body)-- invalid_highlight = rest_client.post(- "/retrieval",- json={"question": "chunk", "dataset_ids": [dataset_id], "highlight": "not_bool"},- )- assert invalid_highlight.status_code == 200- invalid_highlight_payload = invalid_highlight.json()- assert invalid_highlight_payload["code"] == 102, invalid_highlight_payload- assert invalid_highlight_payload["message"] == "`highlight` should be a boolean", invalid_highlight_payload-- for scenario_name, keyword_value in (- ("keyword true", True),- ("keyword true str", "True"),- ("keyword false", False),- ("keyword false str", "False"),- ("keyword none", None),- ):- keyword_res = rest_client.post(- "/retrieval",- json={"question": "chunk test", "dataset_ids": [dataset_id], "keyword": keyword_value},- )- assert keyword_res.status_code == 200, (scenario_name, keyword_res.text)- keyword_payload = keyword_res.json()- assert keyword_payload["code"] == 0, (scenario_name, keyword_payload)- assert isinstance(keyword_payload["data"]["chunks"], list), (scenario_name, keyword_payload)-- invalid_params_res = rest_client.post(- "/retrieval",- json={"question": "chunk", "dataset_ids": [dataset_id], "a": "b"},- )- assert invalid_params_res.status_code == 200- invalid_params_payload = invalid_params_res.json()- assert invalid_params_payload["code"] == 0, invalid_params_payload---def test_retrieval_vector_similarity_and_top_k_contract(rest_client, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- cases = [- ("vector 0", {"vector_similarity_weight": 0}, 0, ""),- ("vector 0.5", {"vector_similarity_weight": 0.5}, 0, ""),- ("vector 10", {"vector_similarity_weight": 10}, 0, ""),- ("vector alpha", {"vector_similarity_weight": "a"}, 102, "`vector_similarity_weight` should be a number"),- ("top_k 10", {"top_k": 10}, 0, ""),- ("top_k 1", {"top_k": 1}, 0, ""),- ("top_k -1", {"top_k": -1}, 102, "`top_k` must be greater than 0"),- ("top_k alpha", {"top_k": "a"}, 102, "`top_k` should be an integer"),- ]- for scenario_name, updates, expected_code, expected_message in cases:- payload = {"question": "chunk", "dataset_ids": [dataset_id]}- payload.update(updates)- res = rest_client.post("/retrieval", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code != 0:- assert expected_message in body["message"], (scenario_name, body)---def test_retrieval_document_ids_and_metadata_condition_contract(rest_client, ensure_parsed_document):- dataset_id, document_id = ensure_parsed_document()-- invalid_doc_ids_res = rest_client.post(- "/retrieval",- json={"question": "chunk", "dataset_ids": [dataset_id], "document_ids": "bad"},- )- assert invalid_doc_ids_res.status_code == 200- invalid_doc_ids_payload = invalid_doc_ids_res.json()- assert invalid_doc_ids_payload["code"] == 102, invalid_doc_ids_payload- assert invalid_doc_ids_payload["message"] == "`documents` should be a list", invalid_doc_ids_payload-- not_owned_doc_res = rest_client.post(- "/retrieval",- json={"question": "chunk", "dataset_ids": [dataset_id], "document_ids": ["not-owned"]},- )- assert not_owned_doc_res.status_code == 200- not_owned_doc_payload = not_owned_doc_res.json()- assert not_owned_doc_payload["code"] == 102, not_owned_doc_payload- assert not_owned_doc_payload["message"] == "The datasets don't own the document not-owned", not_owned_doc_payload-- metadata_condition_res = rest_client.post(- "/retrieval",- json={- "question": "chunk",- "dataset_ids": [dataset_id],- "metadata_condition": {- "logic": "and",- "conditions": [{"name": "author", "comparison_operator": "is", "value": "missing"}],- },- },- )- assert metadata_condition_res.status_code == 200- metadata_condition_payload = metadata_condition_res.json()- assert metadata_condition_payload["code"] == 0, metadata_condition_payload- assert metadata_condition_payload["data"]["chunks"] == [], metadata_condition_payload---def test_retrieval_rerank_unknown_contract(rest_client, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- res = rest_client.post(- "/retrieval",- json={"question": "chunk", "dataset_ids": [dataset_id], "rerank_id": "unknown"},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] != 0, payload- assert payload["message"], payload---def test_retrieval_concurrent_contract(rest_client, ensure_parsed_document):- dataset_id, _ = ensure_parsed_document()- payload = {"question": "chunk", "dataset_ids": [dataset_id]}- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(executor.map(lambda _: rest_client.post("/retrieval", json=payload).json(), range(20)))- assert len(results) == 20, results- assert all(result["code"] == 0 for result in results), results---def test_deleted_chunk_not_in_retrieval_contract(rest_client, create_document):- dataset_id, document_id = create_document("retrieval_deleted_chunk.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- content = "UNIQUE_TEST_CONTENT_12520_REST"-- add_res = rest_client.post(base_path, json={"content": content})- assert add_res.status_code == 200- add_payload = add_res.json()- assert add_payload["code"] == 0, add_payload- chunk_id = add_payload["data"]["chunk"]["id"]-- _retrieval_has_chunks(rest_client, dataset_id, content, [chunk_id])-- delete_res = rest_client.delete(base_path, json={"chunk_ids": [chunk_id]})- assert delete_res.status_code == 200- assert delete_res.json()["code"] == 0- _retrieval_lacks_chunks(rest_client, dataset_id, content, [chunk_id])---def test_deleted_chunks_batch_not_in_retrieval_contract(rest_client, create_document):- dataset_id, document_id = create_document("retrieval_deleted_chunks_batch.txt")- base_path = f"/datasets/{dataset_id}/documents/{document_id}/chunks"- chunk_ids = []- for index in range(3):- content = f"BATCH_DELETE_TEST_CHUNK_{index}_REST_12520"- add_res = rest_client.post(base_path, json={"content": content})- assert add_res.status_code == 200- add_payload = add_res.json()- assert add_payload["code"] == 0, add_payload- chunk_ids.append(add_payload["data"]["chunk"]["id"])- _retrieval_has_chunks(rest_client, dataset_id, "BATCH_DELETE_TEST_CHUNK", chunk_ids)-- delete_res = rest_client.delete(base_path, json={"chunk_ids": chunk_ids})- assert delete_res.status_code == 200- assert delete_res.json()["code"] == 0- _retrieval_lacks_chunks(rest_client, dataset_id, "BATCH_DELETE_TEST_CHUNK", chunk_ids)---def test_related_questions_contract(rest_client, rest_client_noauth):- tokens_res = rest_client.get("/system/tokens")- assert tokens_res.status_code == 200, tokens_res.text- tokens_payload = tokens_res.json()- assert tokens_payload["code"] == 0, tokens_payload- assert tokens_payload["data"], tokens_payload- beta_token = tokens_payload["data"][0]["beta"]-- success_client = RestClient(token=beta_token)- success_res = success_client.post("/searchbots/related_questions", json={"question": "ragflow", "industry": "search"})- assert success_res.status_code == 200- success_payload = success_res.json()- assert success_payload["code"] == 0, success_payload- assert isinstance(success_payload["data"], list), success_payload-- missing_res = success_client.post("/searchbots/related_questions", json={"industry": "search"})- assert missing_res.status_code == 200- missing_payload = missing_res.json()- assert missing_payload["code"] == 101, missing_payload- assert "question" in missing_payload["message"], missing_payload-- invalid_auth_res = rest_client_noauth.post(- "/searchbots/related_questions",- json={"question": "ragflow", "industry": "search"},- headers={"Authorization": "invalid"},- )- assert invalid_auth_res.status_code == 200- invalid_auth_payload = invalid_auth_res.json()- assert invalid_auth_payload["code"] == 102, invalid_auth_payload- assert invalid_auth_payload["message"].strip() in {- "Authorization is not valid!",- 'Authentication error: API key is invalid!"',- "Authentication error: API key is invalid!",- }, invalid_auth_payloaddiff --git a/test/testcases/restful_api/test_router_contracts.py b/test/testcases/restful_api/test_router_contracts.pydeleted file mode 100644index 72683bad8f..0000000000--- a/test/testcases/restful_api/test_router_contracts.py+++ /dev/null@@ -1,28 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import pytest-from configs import VERSION---def test_route_not_found_returns_json(rest_client_noauth):- res = rest_client_noauth.get("/__missing_route__")- assert res.status_code == 404- payload = res.json()- assert payload["code"] == 404, payload- assert payload["error"] == "Not Found", payload- assert payload["message"] == f"Not Found: /api/{VERSION}/__missing_route__", payloaddiff --git a/test/testcases/restful_api/test_search_datasets_consistency.py b/test/testcases/restful_api/test_search_datasets_consistency.pydeleted file mode 100644index 3837e777e3..0000000000--- a/test/testcases/restful_api/test_search_datasets_consistency.py+++ /dev/null@@ -1,613 +0,0 @@-#-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#-"""-Search-datasets consistency tests between Python (port 9380) and Go (port 9380) servers.-Compares /api/v1/datasets/search endpoint responses for consistency between Python and Go.--When an LLM is involved (rerank_id, keyword, or cross_languages is set), both sides-call the LLM independently and the chunk *count*, ordering, and scores can drift-across runs and between Python/Go. The test logs the per-chunk chunk_id + similarity-for human inspection but skips strict count/order/score comparisons in those cases.--When no LLM is involved, both servers run the same deterministic retrieval path-with no non-deterministic dependencies, so the responses are expected to be-byte-identical: same chunk count, same chunk order, and the same per-field-values (with the empty-value normalization done in compare_chunks).--All datasets and documents are created once at module level, then each test unit-runs against the pre-built data. Cleanup happens automatically at module teardown.-"""--import logging-import os-import sys-import tempfile-import time-import uuid--import pytest-import requests--# Logging setup-# Default is silent. Set LOG_LEVEL=INFO locally to see logger.info() messages.-_LOG_LEVEL = os.environ.get("LOG_LEVEL", "WARNING").upper()-_log_handler = logging.StreamHandler(sys.stderr)-_log_handler.setFormatter(logging.Formatter("%(levelname)s: %(message)s"))-logger = logging.getLogger("search_datasets_consistency")-logger.setLevel(getattr(logging, _LOG_LEVEL, logging.WARNING))-logger.addHandler(_log_handler)-logger.propagate = False--from test.testcases.utils import wait_for--PYTHON_HOST = "http://localhost:9380"-GO_HOST = "http://localhost:9380"--# ----------------------------------------------------------------------------# Shared test documents-# ----------------------------------------------------------------------------THREE_KINGDOMS_TXT = """-曹操(155 年—220 年)-作为曹魏政权的奠基者,曹操展现出卓越的政治、军事和文学才能。在政治上,-他挟天子以令诸侯,稳固自身政治地位,推行一系列政策,如推行屯田制,不仅-解决了粮食短缺问题,还使大量流民得以安置,促进了农业生产的恢复与发展 ;-在军事方面,曹操一生征战无数,官渡之战以少胜多击败袁绍,统一北方,压制-匈奴等异族势力,为北方地区带来相对稳定的局面 。然而,曹操的评价褒贬不-一,"治世能臣"体现他在治理国家、施展政治抱负方面的才能;"乱世奸雄"-则反映他在乱世中为达目的不择手段的一面 。--刘备(161 年—223 年)-蜀汉开国皇帝刘备,以"仁德"著称。他的一生充满传奇色彩,早年与关羽、张-飞桃园结义,奠定兄弟情谊,三人患难与共,为兴复汉室而努力 。刘备求贤若-渴,三顾茅庐请出诸葛亮,得到这位智谋之士的辅佐,为蜀汉政权的建立与发展-奠定根基 。刘备善于笼络人心,凭借自身的人格魅力吸引了众多人才,在乱世-中逐渐崛起,建立蜀汉,与曹魏、东吴形成三足鼎立之势 。--孙权(182 年—252 年)-东吴的建立者孙权,年少继承父兄基业,展现出非凡的领导才能 。他擅长用人-制衡,麾下人才济济,周瑜、鲁肃、陆逊等皆是东吴的栋梁之才 。孙权依托长-江天险,制定了稳健的战略,致力于开发江南经济与海外贸易,使东吴在三国中-占据重要地位 。在赤壁之战中,孙权与刘备联军击败曹操,奠定三国鼎立的基-础;之后又在夷陵之战中击败刘备,巩固了东吴在江南的统治 。--赵云(?—229 年)-赵云一生未败,长坂坡单骑救主,在曹操大军中七进七出,如入无人之境,成功-救出刘备之子刘禅,被誉为"常胜将军" 。赵云跟随刘备多年,忠心耿耿,多-次在危难时刻挺身而出,保护刘备及其家人的安全 。他武艺高强,为人正直,-深受刘备和蜀军将士的敬重 。-"""--WATER_MARGIN_TXT = """-宋江(?—?)-宋江是梁山泊一百零八将之首,人称"及时雨"。他原为郓城县押司,因仗义疏财、-济人贫苦,在江湖上广结英雄豪杰。宋江性格矛盾,一方面讲究忠义,一心想着-招安报国;另一方面又领导梁山好汉对抗朝廷。他带领梁山好汉两赢童贯、三败-高俅,最终接受朝廷招安,率军征讨辽国、田虎、王庆、方腊,立下赫赫战功。--武松(?—?)-武松因其排行第二,人称"武二郎"。景阳冈打虎,使他一举成名。武松武艺高强,-力大无穷,是梁山泊步军头领。他因兄长武大郎被西门庆、潘金莲毒害,怒杀二-人,被发配孟州。在孟州,他醉打蒋门神,帮助施恩夺回快活林。大闹飞云浦、-血溅鸳鸯楼后,武松走上反抗道路,最终加入梁山。--李逵(?—?)-李逵是梁山泊好汉中最具反抗精神的代表,人称"黑旋风"。他性格鲁莽直率,-嫉恶如仇,对宋江忠心耿耿。李逵使用两把板斧,在战场上所向披靡,屡立战功。-他曾在江州劫法场救宋江,在沂岭杀四虎,展现了惊人的勇气和力量。-"""--ENGLISH_DOCS = """-Artificial Intelligence-Artificial intelligence (AI) is intelligence demonstrated by machines, in contrast to-the natural intelligence displayed by humans and animals. Leading AI textbooks define-the field as the study of "intelligent agents": any system that perceives its environment-and takes actions that maximize its chance of successfully achieving its goals.-AI applications include advanced web search engines (e.g., Google Search), recommendation-systems (used by YouTube, Amazon, and Netflix), understanding human speech (such as Siri-and Alexa), self-driving cars (e.g., Waymo), generative and creative tools (ChatGPT and-AI art), and superhuman play and analysis in strategy games (such as chess and Go).--Machine Learning-Machine learning (ML) is a field of inquiry devoted to understanding and building methods-that "learn" – that is, methods that leverage data to improve performance on some set of-tasks. It is seen as a part of artificial intelligence. Machine learning algorithms build-a model based on sample data, known as training data, in order to make predictions or-decisions without being explicitly programmed to do so. Machine learning algorithms are-used in a wide variety of applications, such as in medicine, email filtering, speech-recognition, agriculture, and computer vision, where it is difficult or unfeasible to-develop conventional algorithms to perform the needed tasks.--Deep Learning-Deep learning is part of a broader family of machine learning methods based on artificial-neural networks with representation learning. Learning can be supervised, semi-supervised-or unsupervised. Deep-learning architectures such as deep neural networks, deep belief-networks, deep reinforcement learning, recurrent neural networks, convolutional neural-networks and transformers have been applied to fields including computer vision, speech-recognition, natural language processing, machine translation, bioinformatics, drug design,-medical image analysis, climate science, material inspection and board game programs,-where they have produced results comparable to and in some cases surpassing human expert-performance.-"""---# ----------------------------------------------------------------------------# Helpers-# ----------------------------------------------------------------------------def compare_chunks(python_chunk, go_chunk):- """Compare a single chunk between Python and Go responses."""- all_keys = set(python_chunk.keys()) | set(go_chunk.keys())-- for field in all_keys:- p_val = python_chunk.get(field)- g_val = go_chunk.get(field)-- # Normalize empty values to None so the comparison treats- # "no value" shapes interchangeably across engines:- # Python Infinity: [] (keyword list, infinity_conn.py:793-794)- # Python Elasticsearch: "" (keyword, es_conn.py:626-644)- # Go (both engines): None (intentional — see retrieval.go:403-424)- if isinstance(p_val, (list, str)) and not p_val:- p_val = None- if isinstance(g_val, (list, str)) and not g_val:- g_val = None-- if p_val is None or g_val is None:- if p_val != g_val:- raise AssertionError(f"Field '{field}': python={p_val}, go={g_val}")- continue-- if field in ("similarity", "term_similarity", "vector_similarity"):- if p_val != g_val:- raise AssertionError(f"Field '{field}' differs: python={p_val}, go={g_val}, diff={abs(p_val - g_val)}")- elif isinstance(p_val, (list, dict)):- if p_val != g_val:- raise AssertionError(f"Field '{field}' mismatch")- else:- if p_val != g_val:- raise AssertionError(f"Field '{field}': python={p_val}, go={g_val}")---def search_and_compare(rest_client, dataset_ids, cfg):- """Perform search on both servers and compare results.-- dataset_ids can be a single dataset ID string or a list of dataset IDs.- cfg is a dict with keys matching the search payload:- question (required), top_k (default 5), rerank_id, search_id, keyword,- vector_similarity_weight, similarity_threshold, use_kg, cross_languages,- page, size, meta_data_filter- """- headers = {"Authorization": f"Bearer {rest_client.token}"}- if isinstance(dataset_ids, str):- ids = [dataset_ids]- else:- ids = list(dataset_ids)-- search_payload = {- "dataset_ids": ids,- "question": cfg["question"],- "top_k": cfg.get("top_k", 5),- }- optional_fields = [- "rerank_id",- "search_id",- "keyword",- "vector_similarity_weight",- "similarity_threshold",- "use_kg",- "cross_languages",- "page",- "size",- "meta_data_filter",- "doc_ids",- ]- for field in optional_fields:- value = cfg.get(field)- if value is not None:- search_payload[field] = value-- # Call Python server- python_res = rest_client.post("/datasets/search", json=search_payload)- assert python_res.status_code == 200, f"Python server error: {python_res.status_code}, body: {python_res.text}"- python_data = python_res.json()- assert python_data["code"] == 0, f"Python payload error: {python_data}"-- # Call Go server with same auth- go_res = requests.post(- f"{GO_HOST}/api/v1/datasets/search",- json=search_payload,- headers=headers,- timeout=30,- )- assert go_res.status_code == 200, f"Go server error: {go_res.status_code}, body: {go_res.text}"- go_data = go_res.json()- assert go_data["code"] == 0, f"Go payload error: {go_data}"-- python_chunks = python_data["data"]["chunks"]- go_chunks = go_data["data"]["chunks"]-- logger.info(f"python_chunks={len(python_chunks)}, go_chunks={len(go_chunks)}")- logger.info(f" Python chunks: {[(c.get('chunk_id', '?'), c.get('similarity', 0)) for c in python_chunks]}")- logger.info(f" Go chunks: {[(c.get('chunk_id', '?'), c.get('similarity', 0)) for c in go_chunks]}")-- llm_involved = bool(cfg.get("rerank_id") or cfg.get("keyword") or cfg.get("cross_languages"))- if not llm_involved:- assert len(python_chunks) == len(go_chunks), f"Chunk count differs: python={len(python_chunks)}, go={len(go_chunks)}"- for i, (p_chunk, g_chunk) in enumerate(zip(python_chunks, go_chunks)):- try:- compare_chunks(p_chunk, g_chunk)- except AssertionError as e:- raise AssertionError(f"Chunk {i} comparison failed: {e}")- python_total = python_data["data"].get("total", 0)- go_total = go_data["data"].get("total", 0)- if python_total != go_total:- raise AssertionError(f"total differs: python={python_total}, go={go_total}")-- return len(python_chunks), len(python_chunks)---def _upload_and_parse(rest_client, dataset_id, text, filename="doc.txt"):- """Upload text as a file and wait for parsing to complete. Returns document_id."""- with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False, encoding="utf-8") as f:- f.write(text)- temp_path = f.name-- with open(temp_path, "rb") as f:- files = [("file", (filename, f))]- upload_res = rest_client.post(f"/datasets/{dataset_id}/documents", files=files)- assert upload_res.status_code == 200, f"Failed to upload {filename}: {upload_res.text}"- doc_id = upload_res.json()["data"][0]["id"]-- parse_res = rest_client.post(- f"/datasets/{dataset_id}/documents/parse",- json={"document_ids": [doc_id]},- )- assert parse_res.status_code == 200, f"Failed to start parsing {filename}: {parse_res.text}"-- @wait_for(120, 2, f"Document parsing timeout for {filename}")- def check_parsed():- doc_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"id": doc_id})- if doc_res.status_code != 200:- return False- docs = doc_res.json()["data"]["docs"]- return bool(docs) and docs[0].get("run") == "DONE"-- check_parsed()- return doc_id---def _get_chunk_count(rest_client, dataset_id, doc_id):- """Return the number of chunks for a parsed document."""- doc_res = rest_client.get(f"/datasets/{dataset_id}/documents", params={"id": doc_id})- docs = doc_res.json()["data"]["docs"]- return docs[0].get("chunk_count", 0)---def _set_metadata(rest_client, dataset_id, doc_id, updates):- """Set metadata key-value pairs on a document."""- headers = {"Authorization": f"Bearer {rest_client.token}"}- formatted_updates = [{"key": k, "value": v} for k, v in updates.items()]- res = requests.patch(- f"{PYTHON_HOST}/api/v1/datasets/{dataset_id}/documents/metadatas",- json={- "selector": {"document_ids": [doc_id]},- "updates": formatted_updates,- },- headers=headers,- timeout=30,- )- assert res.status_code == 200, f"Failed to set metadata: {res.text}"- assert res.json()["code"] == 0, f"Metadata update error: {res.json()}"- meta_str = ", ".join(f"{k}={v}" for k, v in updates.items())- logger.info(f" metadata set on doc {doc_id}: {meta_str}")---# ----------------------------------------------------------------------------# Module-level fixture: create all datasets once, delete at the end-# ---------------------------------------------------------------------------[email protected](scope="module")-def all_datasets(rest_client):- """Set up all datasets and documents once for the module.-- Returns a dict with:- ds_chinese : 1 dataset with 2 files (Three Kingdoms + Water Margin, with metadata)- ds_chinese_doc1 : doc_id for Three Kingdoms- ds_chinese_doc2 : doc_id for Water Margin-- ds_chinese_2 : 1 dataset with Three Kingdoms only- ds_3k_doc : doc_id-- ds_english : 1 dataset with English text- ds_english_doc : doc_id- """- logger.info("\n[SETUP] Creating all datasets for search-datasets consistency tests...")-- data = {}-- # ------------------------------------------------------------------------ # 1) 1 dataset with 2 files (Chinese)- # ------------------------------------------------------------------------ create_res = rest_client.post(- "/datasets",- json={- "name": "consistency_chinese",- "embedding_model": "BAAI/bge-small-en-v1.5@Builtin",- "parser_config": {"chunk_token_num": 1, "delimiter": "`\n\n`"},- },- )- assert create_res.status_code == 200, create_res.text- assert create_res.json()["code"] == 0, create_res.json()- ds_chinese_id = create_res.json()["data"]["id"]-- doc1 = _upload_and_parse(rest_client, ds_chinese_id, THREE_KINGDOMS_TXT, "three_kingdoms.txt")- doc2 = _upload_and_parse(rest_client, ds_chinese_id, WATER_MARGIN_TXT, "water_margin.txt")- logger.info(f" ds_chinese: {ds_chinese_id} (3k={doc1}, wm={doc2})")- logger.info(f" 3K chunks: {_get_chunk_count(rest_client, ds_chinese_id, doc1)}")- logger.info(f" WM chunks: {_get_chunk_count(rest_client, ds_chinese_id, doc2)}")-- # Set metadata on individual documents- _set_metadata(rest_client, ds_chinese_id, doc1, {"era": 220, "source": "luo", "character": ["曹操", "刘备", "孙权", "赵云"]})- _set_metadata(rest_client, ds_chinese_id, doc2, {"era": 960, "source": "shi", "character": ["宋江", "武松", "李逵"]})-- data["ds_chinese"] = ds_chinese_id- data["ds_chinese_doc1"] = doc1- data["ds_chinese_doc2"] = doc2-- # ------------------------------------------------------------------------ # 2) 1 dataset with Three Kingdoms only- # ------------------------------------------------------------------------ create_res = rest_client.post(- "/datasets",- json={- "name": "consistency_three_kingdoms",- "embedding_model": "BAAI/bge-small-en-v1.5@Builtin",- "parser_config": {"chunk_token_num": 1, "delimiter": "`\n\n`"},- },- )- assert create_res.status_code == 200, create_res.text- assert create_res.json()["code"] == 0, create_res.json()- ds_3k_id = create_res.json()["data"]["id"]-- doc_3k = _upload_and_parse(rest_client, ds_3k_id, THREE_KINGDOMS_TXT, "three_kingdoms.txt")- logger.info(f" ds_chinese_2: {ds_3k_id} (doc={doc_3k})")- logger.info(f" chunks: {_get_chunk_count(rest_client, ds_3k_id, doc_3k)}")-- data["ds_chinese_2"] = ds_3k_id- data["ds_3k_doc"] = doc_3k-- # Wait for metadata to be indexed- time.sleep(2)-- # ------------------------------------------------------------------------ # 3) 1 dataset with English text- # ------------------------------------------------------------------------ create_res = rest_client.post(- "/datasets",- json={- "name": "consistency_english",- "embedding_model": "BAAI/bge-small-en-v1.5@Builtin",- "parser_config": {"chunk_token_num": 1, "delimiter": "`\n\n`"},- },- )- assert create_res.status_code == 200, create_res.text- assert create_res.json()["code"] == 0, create_res.json()- ds_en_id = create_res.json()["data"]["id"]-- doc_en = _upload_and_parse(rest_client, ds_en_id, ENGLISH_DOCS, "english_docs.txt")- logger.info(f" ds_english: {ds_en_id} (doc={doc_en})")- logger.info(f" chunks: {_get_chunk_count(rest_client, ds_en_id, doc_en)}")-- data["ds_english"] = ds_en_id- data["ds_english_doc"] = doc_en-- logger.info("[SETUP] All datasets ready.\n")-- yield data-- # Teardown: delete all datasets- logger.info("\n[TEARDOWN] Deleting all datasets...")- all_ids = [- data["ds_chinese"],- data["ds_chinese_2"],- data["ds_english"],- ]- res = rest_client.delete("/datasets", json={"ids": all_ids})- assert res.status_code == 200, f"Teardown failed: {res.text}"- logger.info("[TEARDOWN] Done.")---# Skip every test in this module from CI. Remove the next line to re-enable.-pytestmark = pytest.mark.skipif(- os.getenv("CI") == "true",- reason="GO server is not started in CI",-)---# ----------------------------------------------------------------------------# Test Unit 1: Search consistency — 1 dataset with 2 files-# ----------------------------------------------------------------------------def test_search_datasets_consistency_basic(rest_client, all_datasets):- """- Compare /api/v1/datasets/search responses between Python and Go servers for consistency.- Tests the single dataset (ds_chinese) which contains 2 files.- """- dataset_id = all_datasets["ds_chinese"]- doc1 = all_datasets["ds_chinese_doc1"]- doc2 = all_datasets["ds_chinese_doc2"]- logger.info(f"Using dataset (Chinese, 2 files): {dataset_id} (doc1={doc1}, doc2={doc2})")-- search_configs = [- {"question": "曹操"},- {"question": "曹操", "page": 2, "size": 2},- {"question": "曹操", "top_k": 2},- {"question": "曹操", "similarity_threshold": 0.0},- {"question": "曹操", "similarity_threshold": 0.5},- {"question": "曹操", "vector_similarity_weight": 0.0},- {"question": "曹操", "vector_similarity_weight": 0.7},- {"question": "曹操", "keyword": True},- {"question": "努力发展农业", "keyword": True},- {"question": "political status", "cross_languages": ["Chinese"]},- {"question": "诸葛亮"},- {"question": "努力发展农业"},- {"question": "曹操 诸葛亮 周瑜"},- {"question": "曹操", "top_k": 3, "rerank_id": "BAAI/bge-reranker-v2-m3@CI@SILICONFLOW"},- {"question": "曹操", "doc_ids": [doc1]},- {"question": "曹操", "doc_ids": [doc2]},- {"question": "曹操", "doc_ids": []},- ]-- for cfg in search_configs:- cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())- logger.info(f"\n--- Testing: {cfg_str} ---")- total, chunk_count = search_and_compare(rest_client, dataset_id, cfg)- logger.info(f"SUCCESS: Python and Go responses match for {chunk_count} chunks, total={total}")---# ----------------------------------------------------------------------------# Test Unit 2: Metadata filter consistency-# ----------------------------------------------------------------------------def test_search_datasets_consistency_metadata_filter(rest_client, all_datasets):- """- Compare Python vs Go search with metadata filtering on the Chinese dataset for consistency.- Uses ds_chinese which has 2 documents with metadata:- - doc1: era=220, source=luo, character=[曹操,刘备,孙权,赵云]- - doc2: era=960, source=shi, character=[宋江,武松,李逵]- """- dataset_id = all_datasets["ds_chinese"]- logger.info(f"Using dataset (Chinese, metadata): {dataset_id}")-- search_configs = [- # Manual filters- {"question": "打虎", "meta_data_filter": {"method": "manual", "manual": [{"key": "era", "op": "=", "value": 960}]}},- {"question": "曹操", "meta_data_filter": {"method": "manual", "manual": [{"key": "source", "op": "=", "value": "luo"}]}},- {"question": "打虎", "meta_data_filter": {"method": "manual", "manual": [{"key": "era", "op": "≠", "value": 960}]}},- {"question": "打虎", "meta_data_filter": {"method": "manual", "manual": [{"key": "era", "op": ">", "value": 220}]}},- {"question": "曹操", "meta_data_filter": {"method": "manual", "manual": [{"key": "source", "op": "contains", "value": "luo"}]}},- {"question": "努力发展农业", "meta_data_filter": {"method": "manual", "manual": [{"key": "character", "op": "in", "value": ["曹操", "孙权"]}]}},- {"question": "打虎", "meta_data_filter": {"method": "manual", "manual": [{"key": "character", "op": "=", "value": "武松"}]}},- ]-- for cfg in search_configs:- cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())- logger.info(f"\n--- Metadata filter: {cfg_str} ---")- total, chunk_count = search_and_compare(rest_client, dataset_id, cfg)- logger.info(f"SUCCESS: {chunk_count} chunks, total={total}")---# ----------------------------------------------------------------------------# Test Unit 3: Search with search_id-# ----------------------------------------------------------------------------def test_search_datasets_consistency_with_search_id(rest_client, all_datasets):- """- Compare Python vs Go with search_id parameter for consistency.- Creates a search config with multiple parameters set, then tests that- both servers honor the stored config overrides.- """- dataset_id = all_datasets["ds_chinese"]- logger.info(f"Using dataset (Chinese): {dataset_id}")-- # Create a search config first- search_name = f"consistency_search_{uuid.uuid4().hex[:8]}"- search_res = rest_client.post("/searches", json={"name": search_name, "description": "consistency test search"})- assert search_res.status_code == 200, f"Failed to create search: {search_res.text}"- search_payload = search_res.json()- assert search_payload["code"] == 0, f"Search creation error: {search_payload}"- search_id = search_payload["data"]["search_id"]- logger.info(f"Created search_id: {search_id}")-- # Update the search config with multiple parameters- search_config = {- "similarity_threshold": 0.2,- "vector_similarity_weight": 0.7,- "top_k": 3,- "use_kg": False,- "keyword": False,- }- update_res = rest_client.put(- f"/searches/{search_id}",- json={"name": search_name, "search_config": search_config},- )- assert update_res.status_code == 200, f"Failed to update search config: {update_res.text}"- assert update_res.json()["code"] == 0, f"Search config update error: {update_res.json()}"- logger.info(f"Updated search config: {search_config}")-- # Search with search_id — config overrides should apply- search_configs = [- {"question": "曹操", "search_id": search_id},- {"question": "曹操 诸葛亮", "search_id": search_id},- ]-- for cfg in search_configs:- cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())- logger.info(f"\n--- Testing with search_id: {cfg_str} ---")- total, chunk_count = search_and_compare(rest_client, dataset_id, cfg)- logger.info(f"SUCCESS: Python and Go responses match for {chunk_count} chunks, total={total}")---# ----------------------------------------------------------------------------# Test Unit 4: Multi-dataset search-# ----------------------------------------------------------------------------def test_search_datasets_consistency_multi_dataset(rest_client, all_datasets):- """- Compare Python vs Go when searching across 2 datasets simultaneously for consistency.- Uses ds_chinese and ds_chinese_2.- """- both_ids = [all_datasets["ds_chinese"], all_datasets["ds_chinese_2"]]- logger.info(f"Using datasets (multi): ds_chinese={all_datasets['ds_chinese']}, ds_chinese_2={all_datasets['ds_chinese_2']}")-- search_configs = [- {"question": "武松打虎"},- {"question": "努力发展农业"},- {"question": "曹操 宋江"},- ]-- for cfg in search_configs:- cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())- logger.info(f"\n--- Multi-dataset Testing: {cfg_str} ---")- total, chunk_count = search_and_compare(rest_client, both_ids, cfg)- logger.info(f"SUCCESS: Python and Go responses match for {chunk_count} chunks, total={total}")---# ----------------------------------------------------------------------------# Test Unit 5: English search consistency-# ----------------------------------------------------------------------------def test_search_datasets_consistency_english(rest_client, all_datasets):- """- Compare Python vs Go with English text documents for consistency.- Uses ds_english.- """- dataset_id = all_datasets["ds_english"]- logger.info(f"Using English dataset: {dataset_id}")-- search_configs = [- {"question": "artificial intelligence"},- {"question": "neural networks and deep learning"},- {"question": "neural networks", "keyword": True},- {"question": "convolutional neural networks", "keyword": True},- {"question": "人工智能", "cross_languages": ["English"]},- {"question": "机器学习", "cross_languages": ["English"]},- ]-- for cfg in search_configs:- cfg_str = ", ".join(f"{k}={v}" for k, v in cfg.items())- logger.info(f"\n--- Testing (English): {cfg_str} ---")- total, chunk_count = search_and_compare(rest_client, dataset_id, cfg)- logger.info(f"SUCCESS: Python and Go responses match for {chunk_count} chunks, total={total}")diff --git a/test/testcases/restful_api/test_searches.py b/test/testcases/restful_api/test_searches.pydeleted file mode 100644index f81aec8bb9..0000000000--- a/test/testcases/restful_api/test_searches.py+++ /dev/null@@ -1,150 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import uuid--import pytest---def search_resource(rest_client):- name = f"restful_search_{uuid.uuid4().hex[:8]}"- create_res = rest_client.post("/searches", json={"name": name, "description": "restful search"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- search_id = create_payload["data"]["search_id"]-- try:- yield search_id- finally:- delete_res = rest_client.delete(f"/searches/{search_id}")- assert delete_res.status_code == 200, delete_res.text- delete_payload = delete_res.json()- assert delete_payload["code"] in (0, 109), delete_payload---def _sse_events(response_text: str) -> list[str]:- return [line[5:] for line in response_text.splitlines() if line.startswith("data:")]---def test_search_routes_require_auth(rest_client_noauth):- create_res = rest_client_noauth.post("/searches", json={"name": "search_noauth"})- assert create_res.status_code == 401- create_payload = create_res.json()- assert create_payload["code"] == 401, create_payload-- list_res = rest_client_noauth.get("/searches")- assert list_res.status_code == 401- list_payload = list_res.json()- assert list_payload["code"] == 401, list_payload---def test_search_crud_contract(rest_client, search_resource):- search_id = search_resource-- list_res = rest_client.get("/searches")- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert any(item.get("id") == search_id for item in list_payload["data"]["search_apps"]), list_payload-- detail_res = rest_client.get(f"/searches/{search_id}")- assert detail_res.status_code == 200- detail_payload = detail_res.json()- assert detail_payload["code"] == 0, detail_payload- assert detail_payload["data"]["id"] == search_id, detail_payload-- new_name = f"search_updated_{uuid.uuid4().hex[:6]}"- update_res = rest_client.put(- f"/searches/{search_id}",- json={"name": new_name, "search_config": {"top_k": 3}},- )- assert update_res.status_code == 200- update_payload = update_res.json()- assert update_payload["code"] == 0, update_payload- assert update_payload["data"]["name"] == new_name, update_payload---def test_search_create_invalid_name(rest_client):- res = rest_client.post("/searches", json={"name": ""})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert "empty" in payload["message"], payload---def test_search_update_invalid_search_id(rest_client):- res = rest_client.put(- "/searches/invalid_search_id",- json={"name": "invalid", "search_config": {}},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 404, payload- assert payload["message"] == "Resource not found", payload---def test_search_completion_requires_question(rest_client, search_resource):- search_id = search_resource- expected_message = "question is required"-- completion_res = rest_client.post(f"/searches/{search_id}/completions", json={})- assert completion_res.status_code == 200- completion_payload = completion_res.json()- assert completion_payload["code"] == 101, completion_payload- assert expected_message in completion_payload["message"], completion_payload-- completions_res = rest_client.post(f"/searches/{search_id}/completions", json={})- assert completions_res.status_code == 200- completions_payload = completions_res.json()- assert completions_payload["code"] == 101, completions_payload- assert expected_message in completions_payload["message"], completions_payload---def test_search_completion_requires_kb_ids(rest_client, search_resource):- search_id = search_resource- for path in ["completions"]:- res = rest_client.post(- f"/searches/{search_id}/{path}",- json={"question": "what is coriander?"},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert "`kb_ids` is required" in payload["message"], payload---def test_search_completion_sse_shape_when_kb_ids_provided(rest_client, search_resource):- search_id = search_resource- # Even with kb_ids provided, runtime may return an error event in-stream, but- # contract remains SSE with JSON data lines and terminal boolean event.- res = rest_client.post(- f"/searches/{search_id}/completions",- json={"question": "what is coriander?", "kb_ids": ["nonexistent_dataset"]},- timeout=60,- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 404, payload- assert payload["message"] == "Resource not found", payloaddiff --git a/test/testcases/restful_api/test_sessions.py b/test/testcases/restful_api/test_sessions.pydeleted file mode 100644index 880f60ff28..0000000000--- a/test/testcases/restful_api/test_sessions.py+++ /dev/null@@ -1,755 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import json-from concurrent.futures import ThreadPoolExecutor--import pytest--from test.testcases.configs import INVALID_API_TOKEN, INVALID_ID_32, SESSION_WITH_CHAT_NAME_LIMIT-from test.testcases.restful_api.helpers.assertions import assert_auth_error-from test.testcases.restful_api.helpers.client import RestClient-from test.testcases.utils import is_sorted---def _sse_events(response_text: str) -> list[str]:- return [line[5:] for line in response_text.splitlines() if line.startswith("data:")]---def _session_names(payload):- return [session["name"] for session in payload["data"]]---def _seed_sessions(rest_client, create_chat, prefix, count=5):- chat_id = create_chat(f"{prefix}_chat")- sessions = []- for index in range(count):- name = f"{prefix}_{index}"- res = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": name})- assert res.status_code == 200, (prefix, index, res.text)- payload = res.json()- assert payload["code"] == 0, (prefix, index, payload)- sessions.append(payload["data"])- return chat_id, sessions---def test_session_crud_cycle(rest_client, create_chat):- chat_id = create_chat("restful_session_crud_chat")-- create_res = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "session_a"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- session_id = create_payload["data"]["id"]- assert create_payload["data"]["chat_id"] == chat_id, create_payload-- list_res = rest_client.get(f"/chats/{chat_id}/sessions")- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert any(item["id"] == session_id for item in list_payload["data"]), list_payload-- get_res = rest_client.get(f"/chats/{chat_id}/sessions/{session_id}")- assert get_res.status_code == 200- get_payload = get_res.json()- assert get_payload["code"] == 0, get_payload- assert get_payload["data"]["id"] == session_id, get_payload-- patch_res = rest_client.patch(- f"/chats/{chat_id}/sessions/{session_id}",- json={"name": "session_a_updated"},- )- assert patch_res.status_code == 200- patch_payload = patch_res.json()- assert patch_payload["code"] == 0, patch_payload- assert patch_payload["data"]["name"] == "session_a_updated", patch_payload-- delete_res = rest_client.delete(f"/chats/{chat_id}/sessions", json={"ids": [session_id]})- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload-- list_after_delete = rest_client.get(f"/chats/{chat_id}/sessions")- assert list_after_delete.status_code == 200- list_after_delete_payload = list_after_delete.json()- assert list_after_delete_payload["code"] == 0, list_after_delete_payload- assert all(item["id"] != session_id for item in list_after_delete_payload["data"]), list_after_delete_payload---def test_session_create_name_validation(rest_client, create_chat):- chat_id = create_chat("restful_session_name_validation_chat")-- res = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": " "})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert "`name` can not be empty" in payload["message"], payload---def test_session_update_blocks_messages_and_reference(rest_client, create_chat):- chat_id = create_chat("restful_session_guard_chat")- create_res = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "session_guard"})- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- session_id = create_payload["data"]["id"]-- msg_res = rest_client.patch(f"/chats/{chat_id}/sessions/{session_id}", json={"messages": []})- assert msg_res.status_code == 200- msg_payload = msg_res.json()- assert msg_payload["code"] == 102, msg_payload- assert "`messages` cannot be changed" in msg_payload["message"], msg_payload-- ref_res = rest_client.patch(f"/chats/{chat_id}/sessions/{session_id}", json={"reference": []})- assert ref_res.status_code == 200- ref_payload = ref_res.json()- assert ref_payload["code"] == 102, ref_payload- assert "`reference` cannot be changed" in ref_payload["message"], ref_payload---def test_session_create_requires_auth_and_invalid_chat_contract():- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.post("/chats/chat_id/sessions", json={"name": "x"})- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_session_create_validation_and_deleted_chat_contract(rest_client, create_chat):- chat_id = create_chat("restful_session_create_contract")-- empty_path_res = rest_client.post("/chats//sessions", json={"name": "valid_name"})- assert empty_path_res.status_code == 200- empty_path_payload = empty_path_res.json()- assert empty_path_payload["code"] == 100, empty_path_payload- assert empty_path_payload["message"] == "<MethodNotAllowed '405: Method Not Allowed'>", empty_path_payload-- invalid_chat_res = rest_client.post("/chats/invalid_chat_assistant_id/sessions", json={"name": "valid_name"})- assert invalid_chat_res.status_code == 200- invalid_chat_payload = invalid_chat_res.json()- assert invalid_chat_payload["code"] == 404, invalid_chat_payload- assert invalid_chat_payload["message"] == "Resource not found", invalid_chat_payload-- for scenario_name, payload in (- ("valid", {"name": "valid_name"}),- ("empty", {"name": ""}),- ("space", {"name": " "}),- ("numeric", {"name": 1}),- ):- res = rest_client.post(f"/chats/{chat_id}/sessions", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- if scenario_name == "valid":- assert body["code"] == 0, (scenario_name, body)- assert body["data"]["name"] == "valid_name", (scenario_name, body)- assert body["data"]["chat_id"] == chat_id, (scenario_name, body)- else:- assert body["code"] == 102, (scenario_name, body)- assert body["message"] == "`name` can not be empty", (scenario_name, body)-- duplicate_first = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "duplicated_name"}).json()- duplicate_second = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "duplicated_name"}).json()- assert duplicate_first["code"] == 0, duplicate_first- assert duplicate_second["code"] == 0, duplicate_second- assert duplicate_second["data"]["name"] == "duplicated_name", duplicate_second-- upper_case = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "CASE INSENSITIVE"}).json()- lower_case = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "case insensitive"}).json()- assert upper_case["code"] == 0, upper_case- assert lower_case["code"] == 0, lower_case- assert upper_case["data"]["name"] == "CASE INSENSITIVE", upper_case- assert lower_case["data"]["name"] == "case insensitive", lower_case-- long_name = "a" * (SESSION_WITH_CHAT_NAME_LIMIT + 1)- long_name_res = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": long_name})- assert long_name_res.status_code == 200- long_name_payload = long_name_res.json()- assert long_name_payload["code"] == 0, long_name_payload- assert long_name_payload["data"]["name"] == long_name[:SESSION_WITH_CHAT_NAME_LIMIT], long_name_payload-- delete_res = rest_client.delete("/chats", json={"ids": [chat_id]})- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload-- create_after_delete = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "after_delete"})- assert create_after_delete.status_code == 200- create_after_delete_payload = create_after_delete.json()- assert create_after_delete_payload["code"] == 404, create_after_delete_payload- assert create_after_delete_payload["message"] == "Resource not found", create_after_delete_payload---def test_session_create_concurrent_contract(rest_client, create_chat):- chat_id = create_chat("restful_session_create_concurrent")-- def _create(index):- return rest_client.post(f"/chats/{chat_id}/sessions", json={"name": f"session create {index}"}).json()-- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(executor.map(_create, range(20)))-- assert len(results) == 20, results- assert all(result["code"] == 0 for result in results), results-- list_res = rest_client.get(f"/chats/{chat_id}/sessions", params={"page_size": 30})- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert len(list_payload["data"]) == 20, list_payload---def test_session_delete_requires_auth_and_invalid_target_contract(rest_client, create_chat):- chat_id = create_chat("restful_session_delete_auth")- create_res = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "session_a"})- assert create_res.status_code == 200- session_id = create_res.json()["data"]["id"]-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.delete(f"/chats/{chat_id}/sessions", json={"ids": [session_id]})- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)-- invalid_chat_res = rest_client.delete("/chats/invalid_chat_assistant_id/sessions", json={"ids": [session_id]})- assert invalid_chat_res.status_code == 200- invalid_chat_payload = invalid_chat_res.json()- assert invalid_chat_payload["code"] == 404, invalid_chat_payload- assert invalid_chat_payload["message"] == "Resource not found", invalid_chat_payload---def test_session_delete_basic_scenarios(rest_client, create_chat):- cases = [- ("none payload", None, 0, 5, {}),- ("invalid only", {"ids": ["invalid_id"]}, 102, 5, "The chat doesn't own the session invalid_id"),- ("not json", "not json", 101, 5, "Malformed JSON syntax: Missing commas/brackets or invalid encoding"),- ("single id", lambda sessions: {"ids": [sessions[0]["id"]]}, 0, 4, True),- ("all ids", lambda sessions: {"ids": [session["id"] for session in sessions]}, 0, 0, True),- ("delete all", {"delete_all": True}, 0, 0, True),- ("empty ids", {"ids": []}, 0, 5, {}),- ]-- for scenario_name, payload, expected_code, expected_remaining, expected_data in cases:- chat_id, sessions = _seed_sessions(rest_client, create_chat, f"delete_basic_{scenario_name.replace(' ', '_')}")- if callable(payload):- payload = payload(sessions)- if scenario_name == "not json":- res = rest_client.delete(- f"/chats/{chat_id}/sessions",- headers={"Content-Type": "application/json"},- data=payload,- )- else:- res = rest_client.delete(f"/chats/{chat_id}/sessions", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code == 0:- assert body["data"] == expected_data, (scenario_name, body)- else:- assert body["message"] == expected_data, (scenario_name, body)-- list_res = rest_client.get(f"/chats/{chat_id}/sessions", params={"page_size": 30})- assert list_res.status_code == 200, (scenario_name, list_res.text)- list_payload = list_res.json()- assert list_payload["code"] == 0, (scenario_name, list_payload)- assert len(list_payload["data"]) == expected_remaining, (scenario_name, list_payload)---def test_session_delete_error_and_repeat_contract(rest_client, create_chat):- partial_cases = [- ("invalid first", lambda sessions: {"ids": ["invalid_id"] + [session["id"] for session in sessions]}),- ("invalid middle", lambda sessions: {"ids": [sessions[0]["id"], "invalid_id", *[session["id"] for session in sessions[1:]]]}),- ("invalid last", lambda sessions: {"ids": [session["id"] for session in sessions] + ["invalid_id"]}),- ]- for scenario_name, payload_builder in partial_cases:- chat_id, sessions = _seed_sessions(rest_client, create_chat, f"delete_partial_{scenario_name.replace(' ', '_')}")- res = rest_client.delete(f"/chats/{chat_id}/sessions", json=payload_builder(sessions))- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == 0, (scenario_name, payload)- assert payload["data"]["success_count"] == len(sessions), (scenario_name, payload)- assert payload["data"]["errors"] == ["The chat doesn't own the session invalid_id"], (scenario_name, payload)- remaining = rest_client.get(f"/chats/{chat_id}/sessions", params={"page_size": 30}).json()- assert remaining["code"] == 0, (scenario_name, remaining)- assert remaining["data"] == [], (scenario_name, remaining)-- duplicate_chat_id, duplicate_sessions = _seed_sessions(rest_client, create_chat, "delete_duplicate")- duplicate_id = duplicate_sessions[0]["id"]- duplicate_res = rest_client.delete(f"/chats/{duplicate_chat_id}/sessions", json={"ids": [duplicate_id, duplicate_id]})- assert duplicate_res.status_code == 200- duplicate_payload = duplicate_res.json()- assert duplicate_payload["code"] == 0, duplicate_payload- assert duplicate_payload["data"]["success_count"] == 1, duplicate_payload- assert duplicate_payload["data"]["errors"] == [f"Duplicate session ids: {duplicate_id}"], duplicate_payload-- repeated_chat_id, repeated_sessions = _seed_sessions(rest_client, create_chat, "delete_repeated")- repeated_ids = [session["id"] for session in repeated_sessions]- first_res = rest_client.delete(f"/chats/{repeated_chat_id}/sessions", json={"ids": repeated_ids})- assert first_res.status_code == 200- first_payload = first_res.json()- assert first_payload["code"] == 0, first_payload- assert first_payload["data"] is True, first_payload-- second_res = rest_client.delete(f"/chats/{repeated_chat_id}/sessions", json={"ids": repeated_ids})- assert second_res.status_code == 200- second_payload = second_res.json()- assert second_payload["code"] == 102, second_payload- for session_id in repeated_ids:- assert f"The chat doesn't own the session {session_id}" in second_payload["message"], second_payload---def test_session_delete_concurrent_and_bulk_contract(rest_client, create_chat):- concurrent_chat_id, concurrent_sessions = _seed_sessions(rest_client, create_chat, "delete_concurrent", count=20)-- def _delete(session):- return rest_client.delete(f"/chats/{concurrent_chat_id}/sessions", json={"ids": [session["id"]]}).json()-- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(executor.map(_delete, concurrent_sessions))-- assert len(results) == 20, results- assert all(result["code"] == 0 for result in results), results- assert all(result["data"] is True for result in results), results-- list_after_concurrent = rest_client.get(f"/chats/{concurrent_chat_id}/sessions", params={"page_size": 30}).json()- assert list_after_concurrent["code"] == 0, list_after_concurrent- assert list_after_concurrent["data"] == [], list_after_concurrent-- bulk_chat_id, bulk_sessions = _seed_sessions(rest_client, create_chat, "delete_bulk", count=100)- bulk_res = rest_client.delete(- f"/chats/{bulk_chat_id}/sessions",- json={"ids": [session["id"] for session in bulk_sessions]},- )- assert bulk_res.status_code == 200- bulk_payload = bulk_res.json()- assert bulk_payload["code"] == 0, bulk_payload- assert bulk_payload["data"] is True, bulk_payload---def test_session_list_requires_auth_and_invalid_target_contract():- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.get("/chats/chat_id/sessions")- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)---def test_session_list_filter_and_deleted_chat_contract(rest_client, create_chat):- chat_id, sessions = _seed_sessions(rest_client, create_chat, "list_filter")- session_ids = [session["id"] for session in sessions]- session_names = [session["name"] for session in sessions]-- default_res = rest_client.get(f"/chats/{chat_id}/sessions", params={"page_size": 30})- assert default_res.status_code == 200- default_payload = default_res.json()- assert default_payload["code"] == 0, default_payload- assert len(default_payload["data"]) == 5, default_payload-- for scenario_name, params, expected_names in (- ("id none", {"id": None, "page_size": 30}, session_names),- ("id empty", {"id": "", "page_size": 30}, session_names),- ("valid id", {"id": session_ids[0], "page_size": 30}, [session_names[0]]),- ("unknown id", {"id": "unknown", "page_size": 30}, []),- ("name none", {"name": None, "page_size": 30}, session_names),- ("name empty", {"name": "", "page_size": 30}, session_names),- ("name exact", {"name": session_names[1], "page_size": 30}, [session_names[1]]),- ("name unknown", {"name": "unknown", "page_size": 30}, []),- ("name and id match", {"id": session_ids[0], "name": session_names[0], "page_size": 30}, [session_names[0]]),- ("name and id mismatch", {"id": session_ids[0], "name": "session_with_chat_assistant_100", "page_size": 30}, []),- ("name and invalid id", {"id": "id", "name": session_names[0], "page_size": 30}, []),- ("invalid params ignored", {"a": "b", "page_size": 30}, session_names),- ):- res = rest_client.get(f"/chats/{chat_id}/sessions", params=params)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == 0, (scenario_name, payload)- assert set(_session_names(payload)) == set(expected_names), (scenario_name, payload)-- invalid_chat_res = rest_client.get(f"/chats/{INVALID_ID_32}/sessions")- assert invalid_chat_res.status_code == 200- invalid_chat_payload = invalid_chat_res.json()- assert invalid_chat_payload["code"] == 404, invalid_chat_payload- assert invalid_chat_payload["message"] == "Resource not found", invalid_chat_payload-- delete_chat_res = rest_client.delete("/chats", json={"ids": [chat_id]})- assert delete_chat_res.status_code == 200- delete_chat_payload = delete_chat_res.json()- assert delete_chat_payload["code"] == 0, delete_chat_payload-- deleted_list_res = rest_client.get(f"/chats/{chat_id}/sessions", params={"page_size": 30})- assert deleted_list_res.status_code == 200- deleted_list_payload = deleted_list_res.json()- assert deleted_list_payload["code"] == 404, deleted_list_payload- assert deleted_list_payload["message"] == "Resource not found", deleted_list_payload---def test_session_list_page_and_sort_contract(rest_client, create_chat):- chat_id, sessions = _seed_sessions(rest_client, create_chat, "list_page_sort")- created_names = [session["name"] for session in sessions]- descending_names = list(reversed(created_names))-- page_cases = [- ("page none", {"page": None, "page_size": 2}, 0, 2, ""),- ("page zero", {"page": 0, "page_size": 2}, 0, 2, ""),- ("page two", {"page": 2, "page_size": 2}, 0, 2, ""),- ("page three", {"page": 3, "page_size": 2}, 0, 1, ""),- ("page string", {"page": "3", "page_size": 2}, 0, 1, ""),- ("page negative", {"page": -1, "page_size": 2}, 0, 2, ""),- ("page alpha", {"page": "a", "page_size": 2}, 0, 2, ""),- ("page_size none", {"page_size": None}, 0, 5, ""),- ("page_size zero", {"page_size": 0}, 0, 0, ""),- ("page_size one", {"page_size": 1}, 0, 1, ""),- ("page_size six", {"page_size": 6}, 0, 5, ""),- ("page_size negative", {"page_size": -1}, 0, 5, ""),- ("page_size alpha", {"page_size": "a"}, 0, 5, ""),- ]- for scenario_name, params, expected_code, expected_count, expected_message in page_cases:- res = rest_client.get(f"/chats/{chat_id}/sessions", params=params)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- assert payload["code"] == expected_code, (scenario_name, payload)- if expected_code == 0:- assert len(payload["data"]) == expected_count, (scenario_name, payload)- else:- assert expected_message in payload["message"], (scenario_name, payload)-- sort_cases = [- ("orderby none", {"orderby": None, "page_size": 30}, "create_time", True, descending_names, ""),- ("orderby create", {"orderby": "create_time", "page_size": 30}, "create_time", True, descending_names, ""),- ("orderby update", {"orderby": "update_time", "page_size": 30}, "update_time", True, descending_names, ""),- ("orderby name ascending", {"orderby": "name", "desc": "False", "page_size": 30}, "name", False, created_names, ""),- ("orderby unknown", {"orderby": "unknown", "page_size": 30}, None, None, None, "invalid orderby field"),- ("desc none", {"desc": None, "page_size": 30}, "create_time", True, descending_names, ""),- ("desc true", {"desc": "true", "page_size": 30}, "create_time", True, descending_names, ""),- ("desc True", {"desc": "True", "page_size": 30}, "create_time", True, descending_names, ""),- ("desc false", {"desc": "false", "page_size": 30}, "create_time", False, created_names, ""),- ("desc False", {"desc": "False", "page_size": 30}, "create_time", False, created_names, ""),- ("desc false update_time", {"desc": "False", "orderby": "update_time", "page_size": 30}, "update_time", False, created_names, ""),- ("desc unknown", {"desc": "unknown", "page_size": 30}, "create_time", True, descending_names, ""),- ]- for scenario_name, params, field, descending, expected_names, expected_message in sort_cases:- res = rest_client.get(f"/chats/{chat_id}/sessions", params=params)- assert res.status_code == 200, (scenario_name, res.text)- payload = res.json()- expected_code = 0 if expected_names is not None else 101- assert payload["code"] == expected_code, (scenario_name, payload)- if expected_code == 0:- assert is_sorted(payload["data"], field, descending), (scenario_name, payload)- assert _session_names(payload) == expected_names, (scenario_name, payload)- else:- assert expected_message in payload["message"], (scenario_name, payload)---def test_session_list_concurrent_contract(rest_client, create_chat):- chat_id, _sessions = _seed_sessions(rest_client, create_chat, "list_concurrent")-- def _list(_):- return rest_client.get(f"/chats/{chat_id}/sessions", params={"page_size": 30}).json()-- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(executor.map(_list, range(10)))-- assert len(results) == 10, results- assert all(result["code"] == 0 for result in results), results- assert all(len(result["data"]) == 5 for result in results), results---def test_session_update_requires_auth_and_invalid_target_contract(rest_client, create_chat):- chat_id = create_chat("restful_session_update_auth")- create_res = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "session_update_auth"})- assert create_res.status_code == 200- session_id = create_res.json()["data"]["id"]-- for scenario_name, client in (("missing token", RestClient(token=None)), ("invalid token", RestClient(token=INVALID_API_TOKEN))):- res = client.patch(f"/chats/{chat_id}/sessions/{session_id}", json={"name": "x"})- assert res.status_code == 401, (scenario_name, res.text)- payload = res.json()- assert_auth_error(payload, scenario_name)-- invalid_chat_res = rest_client.patch(f"/chats/{INVALID_ID_32}/sessions/{session_id}", json={"name": "x"})- assert invalid_chat_res.status_code == 200- invalid_chat_payload = invalid_chat_res.json()- assert invalid_chat_payload["code"] == 404, invalid_chat_payload- assert invalid_chat_payload["message"] == "Resource not found", invalid_chat_payload-- empty_session_res = rest_client.patch(f"/chats/{chat_id}/sessions/", json={"name": "x"})- assert empty_session_res.status_code == 200- empty_session_payload = empty_session_res.json()- assert empty_session_payload["code"] == 100, empty_session_payload- assert empty_session_payload["message"] == "<MethodNotAllowed '405: Method Not Allowed'>", empty_session_payload-- invalid_session_res = rest_client.patch(f"/chats/{chat_id}/sessions/invalid_session_id", json={"name": "x"})- assert invalid_session_res.status_code == 200- invalid_session_payload = invalid_session_res.json()- assert invalid_session_payload["code"] == 102, invalid_session_payload- assert invalid_session_payload["message"] == "Session not found!", invalid_session_payload---def test_session_update_name_and_param_contract(rest_client, create_chat):- chat_id, sessions = _seed_sessions(rest_client, create_chat, "update_contract")- session_id = sessions[0]["id"]-- for scenario_name, payload, expected_code, expected_name_or_message in (- ("valid", {"name": "valid_name"}, 0, "valid_name"),- ("empty", {"name": ""}, 102, "`name` can not be empty"),- ("numeric", {"name": 1}, 102, "`name` can not be empty"),- ("duplicate", {"name": "duplicated_name"}, 0, "duplicated_name"),- ("case insensitive upper", {"name": "CASE INSENSITIVE UPDATE"}, 0, "CASE INSENSITIVE UPDATE"),- ("case insensitive lower", {"name": "case insensitive update"}, 0, "case insensitive update"),- ("long name", {"name": "a" * (SESSION_WITH_CHAT_NAME_LIMIT + 1)}, 0, "a" * SESSION_WITH_CHAT_NAME_LIMIT),- ):- res = rest_client.patch(f"/chats/{chat_id}/sessions/{session_id}", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == expected_code, (scenario_name, body)- if expected_code == 0:- assert body["data"]["name"] == expected_name_or_message, (scenario_name, body)- else:- assert body["message"] == expected_name_or_message, (scenario_name, body)-- for scenario_name, payload in (("empty payload", {}), ("none payload", None)):- res = rest_client.patch(f"/chats/{chat_id}/sessions/{session_id}", json=payload)- assert res.status_code == 200, (scenario_name, res.text)- body = res.json()- assert body["code"] == 0, (scenario_name, body)- assert body["data"]["id"] == session_id, (scenario_name, body)-- delete_res = rest_client.delete("/chats", json={"ids": [chat_id]})- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload-- update_after_delete_res = rest_client.patch(f"/chats/{chat_id}/sessions/{session_id}", json={"name": "after_delete"})- assert update_after_delete_res.status_code == 200- update_after_delete_payload = update_after_delete_res.json()- assert update_after_delete_payload["code"] == 404, update_after_delete_payload- assert update_after_delete_payload["message"] == "Resource not found", update_after_delete_payload---def test_session_update_repeated_and_concurrent_contract(rest_client, create_chat):- chat_id, sessions = _seed_sessions(rest_client, create_chat, "update_repeated")- session_ids = [session["id"] for session in sessions]-- first_res = rest_client.patch(f"/chats/{chat_id}/sessions/{session_ids[0]}", json={"name": "valid_name_1"})- assert first_res.status_code == 200- assert first_res.json()["code"] == 0, first_res.json()-- second_res = rest_client.patch(f"/chats/{chat_id}/sessions/{session_ids[0]}", json={"name": "valid_name_2"})- assert second_res.status_code == 200- assert second_res.json()["code"] == 0, second_res.json()-- def _update(index):- return rest_client.patch(- f"/chats/{chat_id}/sessions/{session_ids[index % len(session_ids)]}",- json={"name": f"update session test {index}"},- ).json()-- with ThreadPoolExecutor(max_workers=5) as executor:- results = list(executor.map(_update, range(20)))-- assert len(results) == 20, results- assert all(result["code"] == 0 for result in results), results---def test_chat_recommendation_requires_question(rest_client):- res = rest_client.post("/chat/recommendation", json={})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- expected_message = "question is required"- assert expected_message in payload["message"], payload---def test_related_questions_compatibility_requires_auth(rest_client_noauth):- # /api/v1/searchbots/related_questions is an SDK compatibility endpoint.- res = rest_client_noauth.post(- "/searchbots/related_questions",- json={"question": "ragflow"},- headers={"Authorization": "invalid"},- )- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 102, payload- assert payload["message"] == "Authorization is not valid!", payload---def test_chat_completion_nonstream_with_session(rest_client, create_chat):- chat_id = create_chat("restful_completion_nonstream_chat")- create_session_res = rest_client.post(f"/chats/{chat_id}/sessions", json={"name": "session_for_completion"})- assert create_session_res.status_code == 200- create_session_payload = create_session_res.json()- assert create_session_payload["code"] == 0, create_session_payload- session_id = create_session_payload["data"]["id"]-- completion_res = rest_client.post(- "/chat/completions",- json={- "chat_id": chat_id,- "session_id": session_id,- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- },- timeout=60,- )- assert completion_res.status_code == 200- completion_payload = completion_res.json()- assert completion_payload["code"] == 0, completion_payload- assert isinstance(completion_payload["data"], dict), completion_payload- for key in ["answer", "reference", "audio_binary", "id", "session_id"]:- assert key in completion_payload["data"], completion_payload- assert completion_payload["data"]["session_id"] == session_id, completion_payload---def test_chat_completion_nonstream_with_chat_without_session(rest_client, create_chat):- chat_id = create_chat("restful_completion_nonstream_without_session_chat")-- completion_res = rest_client.post(- "/chat/completions",- json={- "chat_id": chat_id,- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- },- timeout=60,- )- assert completion_res.status_code == 200- completion_payload = completion_res.json()- assert completion_payload["code"] == 0, completion_payload- assert isinstance(completion_payload["data"], dict), completion_payload- assert completion_payload["data"]["session_id"], completion_payload---def test_chat_completion_nonstream_without_chat(rest_client):- completion_res = rest_client.post(- "/chat/completions",- json={- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- },- timeout=60,- )- assert completion_res.status_code == 200- completion_payload = completion_res.json()- assert completion_payload["code"] == 0, completion_payload- assert isinstance(completion_payload["data"], dict), completion_payload- assert "answer" in completion_payload["data"], completion_payload---def test_chat_completion_stream_events(rest_client, create_chat):- chat_id = create_chat("restful_completion_stream_chat")- stream_res = rest_client.post(- "/chat/completions",- json={- "chat_id": chat_id,- "messages": [{"role": "user", "content": "hello"}],- "stream": True,- },- timeout=60,- )- assert stream_res.status_code == 200- content_type = stream_res.headers.get("Content-Type", "")- assert "text/event-stream" in content_type, content_type-- events = _sse_events(stream_res.text)- assert events, stream_res.text- parsed_events = []- for event in events:- parsed = json.loads(event)- parsed_events.append(parsed)-- assert any(evt.get("code") == 0 and isinstance(evt.get("data"), dict) for evt in parsed_events), parsed_events- assert parsed_events[-1].get("data") is True, parsed_events[-1]---def test_chat_completion_validation_errors(rest_client, create_chat):- chat_id = create_chat("restful_completion_validation_chat")-- missing_messages = rest_client.post(- "/chat/completions",- json={"chat_id": chat_id, "stream": False},- )- assert missing_messages.status_code == 200- missing_messages_payload = missing_messages.json()- assert missing_messages_payload["code"] == 101, missing_messages_payload- assert missing_messages_payload["message"] in {- "required argument are missing: messages",- "messages: is required",- }, missing_messages_payload-- missing_chat_for_session = rest_client.post(- "/chat/completions",- json={- "session_id": "some_session",- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- },- )- assert missing_chat_for_session.status_code == 200- missing_chat_for_session_payload = missing_chat_for_session.json()- assert missing_chat_for_session_payload["code"] == 102, missing_chat_for_session_payload- assert "`chat_id` is required when `session_id` is provided." in missing_chat_for_session_payload["message"], missing_chat_for_session_payload-- invalid_session = rest_client.post(- "/chat/completions",- json={- "chat_id": chat_id,- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- "session_id": "invalid_session",- },- )- assert invalid_session.status_code == 200- invalid_session_payload = invalid_session.json()- assert invalid_session_payload["code"] == 102, invalid_session_payload- assert "Session not found!" in invalid_session_payload["message"], invalid_session_payload-- invalid_chat = rest_client.post(- "/chat/completions",- json={- "chat_id": "invalid_chat_id",- "session_id": "invalid_session",- "messages": [{"role": "user", "content": "hello"}],- "stream": False,- },- )- assert invalid_chat.status_code == 200- invalid_chat_payload = invalid_chat.json()- assert invalid_chat_payload["code"] == 404, invalid_chat_payload- assert invalid_chat_payload["message"] == "Resource not found", invalid_chat_payloaddiff --git a/test/testcases/restful_api/test_system.py b/test/testcases/restful_api/test_system.pydeleted file mode 100644index 8b07f78e88..0000000000--- a/test/testcases/restful_api/test_system.py+++ /dev/null@@ -1,160 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import pytest--from test.testcases.restful_api.helpers.assertions import assert_auth_error---def test_system_ping(rest_client):- res = rest_client.get("/system/ping")- assert res.status_code == 200- assert res.text == "pong"---def test_system_version(rest_client):- res = rest_client.get("/system/version")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert payload["data"], payload---def test_system_status_requires_auth(rest_client_noauth):- res = rest_client_noauth.get("/system/status")- assert res.status_code == 401- payload = res.json()- assert_auth_error(payload, "missing token")---def test_system_status_contract(rest_client):- res = rest_client.get("/system/status")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- for key in ("doc_engine", "storage", "database", "redis"):- assert key in payload["data"], payload---def test_system_config_no_auth_required(rest_client_noauth):- res = rest_client_noauth.get("/system/config")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert "registerEnabled" in payload["data"], payload- assert "disablePasswordLogin" in payload["data"], payload---def test_system_healthz_contract(rest_client_noauth):- res = rest_client_noauth.get("/system/healthz")- assert res.status_code in (200, 500)- payload = res.json()- assert isinstance(payload, dict), payload- assert payload, payload---def test_system_tokens_auth_and_crud(rest_client, rest_client_noauth):- unauth_list = rest_client_noauth.get("/system/tokens")- assert unauth_list.status_code == 401- unauth_list_payload = unauth_list.json()- assert unauth_list_payload["code"] == 401, unauth_list_payload-- create_res = rest_client.post("/system/tokens")- assert create_res.status_code == 200- create_payload = create_res.json()- assert create_payload["code"] == 0, create_payload- token = create_payload["data"]["token"]-- list_res = rest_client.get("/system/tokens")- assert list_res.status_code == 200- list_payload = list_res.json()- assert list_payload["code"] == 0, list_payload- assert isinstance(list_payload["data"], list), list_payload- assert any(item.get("token") == token for item in list_payload["data"]), list_payload-- delete_res = rest_client.delete(f"/system/tokens/{token}")- assert delete_res.status_code == 200- delete_payload = delete_res.json()- assert delete_payload["code"] == 0, delete_payload- assert delete_payload["data"] is True, delete_payload-- delete_missing = rest_client.delete("/system/tokens/missing_token")- assert delete_missing.status_code == 200- delete_missing_payload = delete_missing.json()- assert delete_missing_payload["code"] == 0, delete_missing_payload- assert delete_missing_payload["data"] is True, delete_missing_payload---def test_system_stats_auth_and_shape(rest_client, rest_client_noauth):- unauth_res = rest_client_noauth.get("/system/stats")- assert unauth_res.status_code == 401- unauth_payload = unauth_res.json()- assert unauth_payload["code"] == 401, unauth_payload-- res = rest_client.get("/system/stats")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- data = payload["data"]- for key in ("pv", "uv", "speed", "tokens", "round", "thumb_up"):- assert key in data, payload- assert isinstance(data[key], list), payload---def test_system_oceanbase_status_auth_contract(rest_client, rest_client_noauth):- unauth = rest_client_noauth.get("/system/oceanbase/status")- assert unauth.status_code == 401- assert unauth.json()["code"] == 401-- res = rest_client.get("/system/oceanbase/status")- assert res.status_code == 200- payload = res.json()- assert payload["code"] in (0, 500), payload- assert "data" in payload, payload---def test_system_log_config_routes_auth_and_validation(rest_client, rest_client_noauth):- unauth = rest_client_noauth.get("/system/config/log")- assert unauth.status_code == 401- assert unauth.json()["code"] == 401-- levels = rest_client.get("/system/config/log")- assert levels.status_code == 200- levels_payload = levels.json()- assert levels_payload["code"] == 0, levels_payload- assert isinstance(levels_payload["data"], dict), levels_payload-- missing_body = rest_client.put("/system/config/log", json={})- assert missing_body.status_code == 200- missing_payload = missing_body.json()- assert missing_payload["code"] == 102, missing_payload- assert "pkg_name and level are required" in missing_payload["message"], missing_payload-- invalid_level = rest_client.put("/system/config/log", json={"pkg_name": "rag", "level": "NOT_A_LEVEL"})- assert invalid_level.status_code == 200- invalid_payload = invalid_level.json()- assert invalid_payload["code"] == 102, invalid_payload- assert "Invalid log level" in invalid_payload["message"], invalid_payloaddiff --git a/test/testcases/restful_api/test_task_routes.py b/test/testcases/restful_api/test_task_routes.pydeleted file mode 100644index 13a0fc8a9d..0000000000--- a/test/testcases/restful_api/test_task_routes.py+++ /dev/null@@ -1,48 +0,0 @@-#-# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.-#-# Licensed under the Apache License, Version 2.0 (the "License");-# you may not use this file except in compliance with the License.-# You may obtain a copy of the License at-#-# http://www.apache.org/licenses/LICENSE-2.0-#-# Unless required by applicable law or agreed to in writing, software-# distributed under the License is distributed on an "AS IS" BASIS,-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.-# See the License for the specific language governing permissions and-# limitations under the License.-#--import pytest---def test_task_routes_require_auth(rest_client_noauth):- cancel_res = rest_client_noauth.post("/tasks/missing_task/cancel")- assert cancel_res.status_code == 401- cancel_payload = cancel_res.json()- assert cancel_payload["code"] == 401, cancel_payload-- patch_res = rest_client_noauth.patch("/tasks/missing_task", json={"action": "stop"})- assert patch_res.status_code == 401- patch_payload = patch_res.json()- assert patch_payload["code"] == 401, patch_payload---def test_patch_task_rejects_unsupported_action(rest_client):- res = rest_client.patch("/tasks/missing_task", json={"action": "pause"})- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 101, payload- assert "Only 'stop' is supported" in payload["message"], payload---def test_cancel_missing_task_sets_cancel_contract(rest_client):- res = rest_client.post("/tasks/missing_task/cancel")- assert res.status_code == 200- payload = res.json()- assert payload["code"] == 0, payload- assert payload["data"] is True, payload