返回 DeepSeek-Reasonix
permission_turn_test.go
根目录 / internal / control / permission_turn_test.go
1 package control
2
3 import (
4 "context"
5 "testing"
6 "time"
7
8 "reasonix/internal/agent"
9 "reasonix/internal/provider"
10 )
11
12 type presetTurnRunner struct {
13 session *agent.Session
14 release chan struct{}
15 }
16
17 func (r presetTurnRunner) Run(ctx context.Context, input string) error {
18 r.session.Add(provider.Message{Role: provider.RoleUser, Content: input})
19 select {
20 case <-r.release:
21 return nil
22 case <-ctx.Done():
23 return ctx.Err()
24 }
25 }
26
27 func TestPermissionPresetChangeCancelsRunningTurnOnlyWhenNotWidening(t *testing.T) {
28 cases := []struct {
29 name string
30 from, to string
31 wantCancel bool
32 }{
33 {"workspace to full access", ToolApprovalWorkspaceWrite, ToolApprovalDangerFullAccess, false},
34 {"read-only to workspace", ToolApprovalReadOnly, ToolApprovalWorkspaceWrite, false},
35 {"full access to workspace", ToolApprovalDangerFullAccess, ToolApprovalWorkspaceWrite, true},
36 {"workspace to read-only", ToolApprovalWorkspaceWrite, ToolApprovalReadOnly, true},
37 }
38 for _, tc := range cases {
39 t.Run(tc.name, func(t *testing.T) {
40 release := make(chan struct{})
41 c := newOwnedTestController(t, Options{Runner: presetTurnRunner{session: agent.NewSession("sys"), release: release}})
42 c.SetToolApprovalMode(tc.from)
43 done := make(chan error, 1)
44 go func() { done <- c.RunTurn(context.Background(), "work") }()
45 waitForRunning(t, c)
46
47 c.SetToolApprovalMode(tc.to)
48 if got := c.ToolApprovalMode(); got != tc.to {
49 t.Fatalf("mode = %q, want %q", got, tc.to)
50 }
51 if tc.wantCancel {
52 waitIdle(t, c)
53 } else {
54 select {
55 case err := <-done:
56 t.Fatalf("widening the preset ended the running turn: %v", err)
57 case <-time.After(200 * time.Millisecond):
58 }
59 if !c.Running() {
60 t.Fatal("widening the preset stopped the running turn")
61 }
62 }
63 close(release)
64 select {
65 case <-done:
66 case <-time.After(30 * time.Second):
67 t.Fatal("turn did not finish")
68 }
69 })
70 }
71 }
72
72 lines GO