mirror of
https://github.com/tiennm99/goclaw.git
synced 2026-10-20 02:17:24 +00:00
1 parent
501800743f
commit
f9440baca2
15 files changed
+584
-72
No files matched your search
@@ -84,6 +84,9 @@ func makeCronJobHandler(sched *scheduler.Scheduler, msgBus *bus.MessageBus, cfg
|
||||
cronCtx, cancelCron := context.WithTimeout(context.Background(), jobTimeout)
|
||||
defer cancelCron()
|
||||
cronCtx = store.WithTenantID(cronCtx, job.TenantID)
|
||||
if job.Payload.CredentialUserID != "" {
|
||||
cronCtx = store.WithCredentialUserID(cronCtx, job.Payload.CredentialUserID)
|
||||
}
|
||||
|
||||
// Reset session before each cron run to prevent tool errors from previous
|
||||
// runs from polluting the context and blocking future executions (#294).
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/nextlevelbuilder/goclaw/internal/agent"
|
||||
"github.com/nextlevelbuilder/goclaw/internal/config"
|
||||
"github.com/nextlevelbuilder/goclaw/internal/scheduler"
|
||||
"github.com/nextlevelbuilder/goclaw/internal/store"
|
||||
)
|
||||
|
||||
func TestCronJobHandlerInjectsPayloadCredentialUserID(t *testing.T) {
|
||||
wantCredentialUserID := "tenant-user-123"
|
||||
var gotCredentialUserID string
|
||||
|
||||
sched := scheduler.NewScheduler(
|
||||
scheduler.DefaultLanes(),
|
||||
scheduler.QueueConfig{
|
||||
Mode: scheduler.QueueModeQueue,
|
||||
Cap: 1,
|
||||
Drop: scheduler.DropOld,
|
||||
DebounceMs: 0,
|
||||
MaxConcurrent: 1,
|
||||
},
|
||||
func(ctx context.Context, req agent.RunRequest) (*agent.RunResult, error) {
|
||||
gotCredentialUserID = store.CredentialUserIDFromContext(ctx)
|
||||
return &agent.RunResult{Content: "ok"}, nil
|
||||
},
|
||||
)
|
||||
defer sched.Stop()
|
||||
|
||||
handler := makeCronJobHandler(
|
||||
sched,
|
||||
nil,
|
||||
&config.Config{},
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
|
||||
result, err := handler(&store.CronJob{
|
||||
ID: uuid.NewString(),
|
||||
TenantID: uuid.New(),
|
||||
Name: "credentialed-report",
|
||||
AgentID: "reporter",
|
||||
UserID: "group:telegram:-100123",
|
||||
Stateless: true,
|
||||
Payload: store.CronPayload{
|
||||
Kind: "agent_turn",
|
||||
Message: "run gh issue list",
|
||||
CredentialUserID: wantCredentialUserID,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("cron handler returned error: %v", err)
|
||||
}
|
||||
if result == nil || result.Content != "ok" {
|
||||
t.Fatalf("cron result = %#v, want content ok", result)
|
||||
}
|
||||
if gotCredentialUserID != wantCredentialUserID {
|
||||
t.Fatalf("credential user ID in scheduled context = %q, want %q", gotCredentialUserID, wantCredentialUserID)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user