Skip to content

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

1.1 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 TestAIOCRContract(t *testing.T) {
	for _, body := range []string{`{}`, `{"path":null}`, `{"path":42}`} {
		if _, err := AIOCR.Decode(strings.NewReader(body)); err == nil {
			t.Fatalf("accepted invalid OCR request: %s", body)
		}
	}
	request, err := AIOCR.Decode(strings.NewReader(`{"path":"assets/image.png?box=20261003100000-abcdefg#preview"}`))
	if err != nil || !strings.Contains(request.Path, "?box=") {
		t.Fatalf("lost resource identity: %+v, %v", request, err)
	}
	bundle, err := BuildBundle()
	if err != nil {
		t.Fatal(err)
	}
	for _, body := range []string{`{"code":0,"msg":"","data":{"text":"line one\nline two"}}`, `{"code":0,"msg":"","data":{"text":""}}`, `{"code":-1,"msg":"failed","data":null}`} {
		if err := bundle.ValidateResponse("POST", "/api/ai/ocr", []byte(body)); err != nil {
			t.Fatal(err)
		}
	}
	if !RequiresAI(AIOCR.Definition().Path) {
		t.Fatal("OCR bypasses the AI feature flag")
	}
}