yao/agent/assistant/hook/create_nested_test.go
Max a96946d8bb Refactor Assistant context handling to improve options management
- Updated the Assistant methods to accept an Options parameter, enhancing flexibility in context management.
- Removed the Connector field from the context and related structures, transitioning to a more streamlined options-based approach.
- Adjusted various tests to accommodate the new options handling, ensuring comprehensive coverage of the updated functionality.
- Enhanced the Create and Next hooks to return options alongside responses, improving the overall usability of the API.
- Cleaned up deprecated fields and improved context initialization for better maintainability.
2025-12-04 10:43:32 +08:00

117 lines
2.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package hook_test
import (
"sync"
"testing"
"github.com/yaoapp/yao/agent/assistant"
"github.com/yaoapp/yao/agent/context"
"github.com/yaoapp/yao/agent/testutils"
)
// TestNestedScriptCall tests nested script calls with V8 context sharing
// This test calls: hook -> scripts.tests.create.NestedCall -> GetRoles/GetRole -> models
func TestNestedScriptCall(t *testing.T) {
testutils.Prepare(t)
defer testutils.Clean(t)
agent, err := assistant.Get("tests.create")
if err != nil {
t.Fatalf("Failed to get assistant: %s", err.Error())
}
if agent.Script == nil {
t.Fatalf("Assistant has no script")
}
// Create context
ctx := newTestContext("test-nested-call", "tests.create")
// Call with deep_nested_call scenario
// This will: hook -> scripts.tests.create.NestedCall -> GetRoles -> model
res, _, err := agent.Script.Create(ctx, []context.Message{
{Role: "user", Content: "deep_nested_call"},
})
if err != nil {
t.Fatalf("Nested call failed: %s", err.Error())
}
if res == nil {
t.Fatal("Expected non-nil response")
}
// Verify messages
if len(res.Messages) == 0 {
t.Fatal("Expected messages in response")
}
t.Logf("✓ Nested script call completed successfully")
t.Logf(" Messages count: %d", len(res.Messages))
if res.Metadata != nil {
t.Logf(" Metadata: %+v", res.Metadata)
}
}
// TestNestedScriptCallConcurrent tests nested script calls under high concurrency
// Simulates 100 concurrent users making nested script calls
func TestNestedScriptCallConcurrent(t *testing.T) {
testutils.Prepare(t)
defer testutils.Clean(t)
agent, err := assistant.Get("tests.create")
if err != nil {
t.Fatalf("Failed to get assistant: %s", err.Error())
}
if agent.Script == nil {
t.Fatalf("Assistant has no script")
}
// High concurrency test: 100 concurrent users (testing race condition)
concurrency := 100
iterations := 1 // Each user makes 1 call
var wg sync.WaitGroup
errors := make(chan error, concurrency*iterations)
t.Logf("Starting concurrent test: %d users × %d iterations = %d total calls",
concurrency, iterations, concurrency*iterations)
for i := 0; i < concurrency; i++ {
wg.Add(1)
go func(userID int) {
defer wg.Done()
for j := 0; j < iterations; j++ {
ctx := newTestContext("test-concurrent", "tests.create")
_, _, err := agent.Script.Create(ctx, []context.Message{
{Role: "user", Content: "deep_nested_call"},
})
if err != nil {
errors <- err
return
}
}
}(i)
}
// Wait for all goroutines to complete
wg.Wait()
close(errors)
// Check for errors
errorCount := 0
for err := range errors {
errorCount++
t.Errorf("Concurrent call failed: %s", err.Error())
}
if errorCount > 0 {
t.Fatalf("Failed with %d errors out of %d total calls", errorCount, concurrency*iterations)
}
t.Logf("✓ All %d concurrent nested calls completed successfully", concurrency*iterations)
}