Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

1.4 KB
Raw Blame
// 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"
)

func TestAPIContractNotificationResponses(t *testing.T) {
	engine := gin.New()
	engine.POST("/api/notification/pushMsg", pushMsg)
	engine.POST("/api/notification/pushErrMsg", pushErrMsg)
	for _, path := range []string{"/api/notification/pushMsg", "/api/notification/pushErrMsg"} {
		for _, entry := range []struct {
			body string
			code int
		}{
			{`{"msg":" hello "}`, 0},
			{`{"msg":"hello","timeout":null}`, 0},
			{`{"msg":"hello","timeout":1.9}`, 0},
			{`{"msg":"hello","timeout":-1}`, 0},
			{`{"msg":null}`, -1},
			{`{}`, -1},
		} {
			recorder := httptest.NewRecorder()
			engine.ServeHTTP(recorder, httptest.NewRequest("POST", path, strings.NewReader(entry.body)))
			requireAPIContract(t, "POST", path, recorder)
			var response struct {
				Code int `json:"code"`
				Data *struct {
					ID string `json:"id"`
				} `json:"data"`
			}
			if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil || response.Code != entry.code {
				t.Fatalf("%s %s: %s, %v", path, entry.body, recorder.Body.String(), err)
			}
			if entry.code == 0 && (response.Data == nil || response.Data.ID == "") {
				t.Fatalf("missing message ID: %s", recorder.Body.String())
			}
		}
	}
}