Skip to content

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

3.2 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 conf

import "testing"

func TestAgentTimeoutDefaults(t *testing.T) {
	if DefaultAgentSessionTimeout != 1800 {
		t.Fatalf("default session timeout changed: %d", DefaultAgentSessionTimeout)
	}
	if MaxAgentSessionTimeout != 3600 {
		t.Fatalf("max session timeout changed: %d", MaxAgentSessionTimeout)
	}
	if DefaultAgentConfirmTimeout != 600 {
		t.Fatalf("default confirmation timeout changed: %d", DefaultAgentConfirmTimeout)
	}
	if DefaultAgentSessionTimeout > MaxAgentSessionTimeout {
		t.Fatalf("default session timeout %d exceeds the max %d", DefaultAgentSessionTimeout, MaxAgentSessionTimeout)
	}
}

func TestAINormalizeAgentTimeoutsAndRetries(t *testing.T) {
	tests := []struct {
		name           string
		agent          *Agent
		wantSession    int
		wantStreamIdle int
		wantMaxRetries int
	}{
		{name: "defaults", agent: nil, wantSession: DefaultAgentSessionTimeout, wantStreamIdle: 120, wantMaxRetries: 3},
		{name: "zero means unlimited session and no retries", agent: &Agent{}, wantSession: 0, wantStreamIdle: 120, wantMaxRetries: 0},
		{name: "clamps upper bounds", agent: &Agent{SessionTimeout: 7200, StreamIdleTimeout: 900, MaxRetries: 20}, wantSession: MaxAgentSessionTimeout, wantStreamIdle: 600, wantMaxRetries: 10},
		{name: "normalizes negative values", agent: &Agent{SessionTimeout: -1, StreamIdleTimeout: -1, MaxRetries: -1}, wantSession: 0, wantStreamIdle: 120, wantMaxRetries: 0},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			ai := &AI{Agent: test.agent}
			ai.Normalize()
			if ai.Agent.SessionTimeout != test.wantSession {
				t.Errorf("session timeout = %d, want %d", ai.Agent.SessionTimeout, test.wantSession)
			}
			if ai.Agent.StreamIdleTimeout != test.wantStreamIdle {
				t.Errorf("stream idle timeout = %d, want %d", ai.Agent.StreamIdleTimeout, test.wantStreamIdle)
			}
			if ai.Agent.MaxRetries != test.wantMaxRetries {
				t.Errorf("max retries = %d, want %d", ai.Agent.MaxRetries, test.wantMaxRetries)
			}
		})
	}
}

func TestNewAIReadsAgentStreamIdleTimeoutFromEnvironment(t *testing.T) {
	t.Setenv("SIYUAN_OPENAI_AGENT_STREAM_IDLE_TIMEOUT", "240")
	ai := NewAI()
	ApplyAIEnvironment(ai)
	if got := ai.Agent.StreamIdleTimeout; got != 240 {
		t.Fatalf("stream idle timeout = %d, want 240", got)
	}
}

func TestAINormalizeProviderRequestTimeout(t *testing.T) {
	ai := &AI{Providers: []*Provider{{RequestTimeout: -1}, {RequestTimeout: 0}, {RequestTimeout: 30}, {RequestTimeout: 601}}}
	ai.Normalize()
	want := []int{120, 120, 30, 600}
	for i, provider := range ai.Providers {
		if provider.RequestTimeout != want[i] {
			t.Errorf("provider %d request timeout = %d, want %d", i, provider.RequestTimeout, want[i])
		}
	}
}

func TestNewAIDefaultProviderRequestTimeout(t *testing.T) {
	t.Setenv("SIYUAN_OPENAI_API_KEY", "test")
	t.Setenv("SIYUAN_OPENAI_API_MODEL", "test-model")
	t.Setenv("SIYUAN_OPENAI_API_BASE_URL", "https://example.com/v1")
	ai := NewAI()
	ApplyAIEnvironment(ai)
	if len(ai.Providers) != 1 || ai.Providers[0].RequestTimeout != 120 {
		t.Fatalf("unexpected default provider request timeout: %#v", ai.Providers)
	}
}