Skip to content

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

2.9 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 apicontract

import (
	"strings"
	"testing"
)

func TestOCRSettingStrictInput(t *testing.T) {
	for _, body := range []string{`{}`, `{"provider":"paddleocr","model":"tiny","auto":null}`, `{"provider":null,"model":"tiny","auto":true}`, `{"provider":"paddleocr","model":"tiny","auto":1}`} {
		if _, err := SetOCRConfig.Decode(strings.NewReader(body)); err == nil {
			t.Fatalf("accepted invalid OCR settings: %s", body)
		}
	}
	request, err := SetOCRConfig.Decode(strings.NewReader(`{"provider":"paddleocr","model":"small","auto":false}`))
	if err != nil || request.Provider != "paddleocr" || request.Model != "small" || request.Auto {
		t.Fatalf("valid settings: %+v %v", request, err)
	}
}

func TestOCRThresholdContract(t *testing.T) {
	for _, fields := range []string{
		`{}`, `{"detection":0.3,"box":0.6}`, `{"detection":"0.3","box":0.6,"recognition":0.5}`,
		`{"detection":false,"box":null,"recognition":null}`,
	} {
		body := `{"provider":"paddleocr","model":"small","auto":false,"thresholds":` + fields + `}`
		if _, err := SetOCRConfig.Decode(strings.NewReader(body)); err == nil {
			t.Fatalf("accepted malformed thresholds: %s", body)
		}
	}
	for _, fields := range []string{`{"detection":null,"box":null,"recognition":null}`, `{"detection":0.3,"box":0.6,"recognition":0}`} {
		body := `{"provider":"paddleocr","model":"small","auto":false,"thresholds":` + fields + `}`
		if _, err := SetOCRConfig.Decode(strings.NewReader(body)); err != nil {
			t.Fatalf("rejected valid thresholds: %s: %v", body, err)
		}
	}
}

func TestOCRAIModelCompatibility(t *testing.T) {
	for _, field := range []string{"", `,"aiModelId":null`, `,"aiModelId":"vision"`} {
		request, err := SetOCRConfig.Decode(strings.NewReader(`{"provider":"ai","model":"tiny","auto":false` + field + `}`))
		if err != nil || field == `,"aiModelId":"vision"` && request.AIModelID != "vision" {
			t.Fatalf("valid independent model selection: %+v, %v", request, err)
		}
	}
	if _, err := SetOCRConfig.Decode(strings.NewReader(`{"provider":"ai","model":"tiny","auto":false,"aiModelId":1}`)); err == nil {
		t.Fatal("accepted numeric model ID")
	}
}

func TestOCRTooltipSettingContract(t *testing.T) {
	for _, key := range []string{"showText", "showEmpty", "showError"} {
		for _, value := range []string{"true", "false", "null"} {
			body := `{"provider":"paddleocr","model":"tiny","auto":false,"` + key + `":` + value + `}`
			if _, err := SetOCRConfig.Decode(strings.NewReader(body)); err != nil {
				t.Fatalf("rejected tooltip preference %s: %v", body, err)
			}
		}
		for _, value := range []string{`"false"`, "1", "{}"} {
			body := `{"provider":"paddleocr","model":"tiny","auto":false,"` + key + `":` + value + `}`
			if _, err := SetOCRConfig.Decode(strings.NewReader(body)); err == nil {
				t.Fatalf("accepted malformed tooltip preference: %s", body)
			}
		}
	}
}