Files
Cyrene_v2/internal/runtime/loop/subagent_task_test.go
T
AskaEth 20160c66d8
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled
feat(security): 代审口径 A 落地(仅子代理发起的敏感调用做实质判定 + fail-closed + 理由进审计与小字)
2026-10-05 12:12:31 +08:00

67 lines
2.0 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 loop
import (
"context"
"strings"
"testing"
)
// TestSubagentTask_RoundTrip:挂载的任务目标能被原样读回(ok=true)。
func TestSubagentTask_RoundTrip(t *testing.T) {
t.Parallel()
ctx := WithSubagentTask(context.Background(), "整理导出文件")
got, ok := SubagentTaskFrom(ctx)
if !ok || got != "整理导出文件" {
t.Fatalf("SubagentTaskFrom = (%q, %v), want (整理导出文件, true)", got, ok)
}
}
// TestSubagentTask_EmptyIsNoop:未派发的运行(主会话自身、后台思考)是
// ok=false —— 这正是「非子代理发起」的语义,判定侧据此不加判定。
func TestSubagentTask_EmptyIsNoop(t *testing.T) {
t.Parallel()
tests := []struct {
name string
ctx context.Context
}{
{name: "bare context", ctx: context.Background()},
{name: "blank goal", ctx: WithSubagentTask(context.Background(), " ")},
{name: "nil context", ctx: nil},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got, ok := SubagentTaskFrom(tt.ctx); ok || got != "" {
t.Fatalf("SubagentTaskFrom = (%q, %v), want (\"\", false)", got, ok)
}
})
}
}
// TestSubagentTask_NilContextIsNoop:nil ctx 是空操作,不 panic。
func TestSubagentTask_NilContextIsNoop(t *testing.T) {
t.Parallel()
if got := WithSubagentTask(nil, "任务"); got != nil {
t.Fatalf("WithSubagentTask(nil, …) = %v, want nil", got)
}
}
// TestSubagentTask_PropagatesThroughChildContexts:派发标记必须能穿过中途派生的
// ctx 抵达工具执行点(准入钩子拿到的就是那一层 ctx);被取消的派生 ctx 同样保留它。
func TestSubagentTask_PropagatesThroughChildContexts(t *testing.T) {
t.Parallel()
parent := WithSubagentTask(context.Background(), "整理导出文件")
child, cancel := context.WithCancel(parent)
defer cancel()
got, ok := SubagentTaskFrom(child)
if !ok || !strings.Contains(got, "整理") {
t.Fatalf("派生 ctx 上的任务 = (%q, %v), want 原任务", got, ok)
}
}