An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:white_check_mark: Cover validation response envelopes https://github.com/siyuan-note/siyuan/pull/21031
Danielb174bf9a2954
diff --git a/kernel/api/contract_error_response_test.go b/kernel/api/contract_error_response_test.gonew file mode 100644index 0000000000..6d20355271--- /dev/null+++ b/kernel/api/contract_error_response_test.go@@ -0,0 +1,45 @@+// SiYuan - https://github.com/siyuan-note/siyuan+// Copyright (c) 2020-present, b3log.org+// SPDX-License-Identifier: AGPL-3.0-or-later++package api++import (+ "encoding/json"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/gin-gonic/gin"+ "github.com/siyuan-note/siyuan/kernel/model"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestAPIContractRemovedEntriesKeepNullSuccessData(t *testing.T) {+ previousDir, previousConfDir, previousConf := util.DataDir, util.ConfDir, model.Conf+ util.DataDir, util.ConfDir, model.Conf = t.TempDir(), t.TempDir(), model.NewAppConf()+ t.Cleanup(func() { util.DataDir, util.ConfDir, model.Conf = previousDir, previousConfDir, previousConf })+ engine := gin.New()+ engine.POST("/api/ai/editor/removeAction", removeAIEditorAction)+ engine.POST("/api/storage/removeViewState", removeViewState)+ for _, scenario := range []struct{ path, body string }{+ {"/api/ai/editor/removeAction", `{"id":"20261028000000-abcdefg"}`},+ {"/api/storage/removeViewState", `{"key":"review-fixture"}`},+ } {+ t.Run(scenario.path, func(t *testing.T) {+ recorder := httptest.NewRecorder()+ engine.ServeHTTP(recorder, httptest.NewRequest("POST", scenario.path, strings.NewReader(scenario.body)))+ requireAPIContract(t, "POST", scenario.path, recorder)+ var envelope struct {+ Code int `json:"code"`+ Data json.RawMessage `json:"data"`+ }+ if err := json.Unmarshal(recorder.Body.Bytes(), &envelope); err != nil {+ t.Fatal(err)+ }+ if envelope.Code != 0 || string(envelope.Data) != "null" {+ t.Fatalf("success envelope changed: %s", recorder.Body.String())+ }+ })+ }+}diff --git a/kernel/apicontract/response_rejection_test.go b/kernel/apicontract/response_rejection_test.gonew file mode 100644index 0000000000..3ed811f8cc--- /dev/null+++ b/kernel/apicontract/response_rejection_test.go@@ -0,0 +1,45 @@+// SiYuan - https://github.com/siyuan-note/siyuan+// Copyright (c) 2020-present, b3log.org+// SPDX-License-Identifier: AGPL-3.0-or-later++package apicontract++import (+ "encoding/json"+ "errors"+ "fmt"+ "testing"+)++func TestRejectionResponsePreservesCodeAndNullData(t *testing.T) {+ for _, scenario := range []struct {+ err error+ code int+ msg string+ }{+ {Reject("invalid"), -1, "invalid"},+ {RejectWith(403, "Forbidden"), 403, "Forbidden"},+ {fmt.Errorf("context: %w", RejectWith(-3, "locked")), -3, "locked"},+ {errors.New("ordinary error"), -1, "ordinary error"},+ } {+ raw, err := json.Marshal(FailureFrom[Null](scenario.err))+ if err != nil {+ t.Fatal(err)+ }+ var envelope struct {+ Code int `json:"code"`+ Msg string `json:"msg"`+ Data json.RawMessage `json:"data"`+ }+ if err = json.Unmarshal(raw, &envelope); err != nil {+ t.Fatal(err)+ }+ if envelope.Code != scenario.code || envelope.Msg != scenario.msg || string(envelope.Data) != "null" {+ t.Fatalf("unexpected rejection envelope: %s", raw)+ }+ }+ raw, err := json.Marshal(SuccessWithoutData[Null]())+ if err != nil || string(raw) != `{"code":0,"msg":"","data":null}` {+ t.Fatalf("unexpected empty success: %s, %v", raw, err)+ }+}