yao/agent/assistant/hook/create_nested_test.go
Max 0bc0821646 Refactor Assistant script handling to use HookScript
- Replaced all instances of `Script` with `HookScript` in the Assistant and related files to improve clarity and consistency in naming.
- Updated method calls in the Stream, Create, and Next hooks to utilize the new `HookScript` field.
- Adjusted tests and benchmarks to reflect the changes in script handling, ensuring all functionalities remain intact and operational.
- Enhanced the load and initialization processes to accommodate the new HookScript structure, streamlining the assistant's script management.
2025-12-05 14:45:09 +08:00

117 lines
2.9 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.HookScript == 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.HookScript.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.HookScript == 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.HookScript.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)
}