| 1 | package sessiontool |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "reasonix/internal/agent" |
| 12 | ) |
| 13 | |
| 14 | func writeTitleTestSession(t *testing.T, dir, name string) string { |
| 15 | t.Helper() |
| 16 | path := filepath.Join(dir, name) |
| 17 | if err := os.WriteFile(path, []byte(`{"role":"user","content":"hello"}`+"\n"), 0o600); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | return path |
| 21 | } |
| 22 | |
| 23 | func TestSetSessionTitleUsesHostBoundCurrentSession(t *testing.T) { |
| 24 | dir := t.TempDir() |
| 25 | current := writeTitleTestSession(t, dir, "current.jsonl") |
| 26 | other := writeTitleTestSession(t, dir, "other.jsonl") |
| 27 | var projectedDir, projectedPath, projectedTitle string |
| 28 | tool := NewSetSessionTitleTool(dir, func() string { return current }, func(sessionDir, sessionPath, title string) error { |
| 29 | projectedDir, projectedPath, projectedTitle = sessionDir, sessionPath, title |
| 30 | return nil |
| 31 | }) |
| 32 | |
| 33 | got, err := tool.Execute(context.Background(), json.RawMessage(`{"title":" Current work ","session":"other.jsonl"}`)) |
| 34 | if err != nil { |
| 35 | t.Fatalf("Execute: %v", err) |
| 36 | } |
| 37 | if !strings.Contains(got, "Current work") { |
| 38 | t.Fatalf("result = %q", got) |
| 39 | } |
| 40 | meta, ok, err := agent.LoadBranchMeta(current) |
| 41 | if err != nil || !ok || meta.CustomTitle != "Current work" { |
| 42 | t.Fatalf("current meta = %+v, ok=%v, err=%v", meta, ok, err) |
| 43 | } |
| 44 | if _, ok, err := agent.LoadBranchMeta(other); err != nil || ok { |
| 45 | t.Fatalf("other session changed: ok=%v err=%v", ok, err) |
| 46 | } |
| 47 | if projectedDir != dir || projectedPath != current || projectedTitle != "Current work" { |
| 48 | t.Fatalf("projection = %q %q %q", projectedDir, projectedPath, projectedTitle) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestSetSessionTitleClearsTitle(t *testing.T) { |
| 53 | dir := t.TempDir() |
| 54 | path := writeTitleTestSession(t, dir, "current.jsonl") |
| 55 | if err := agent.RenameSession(path, "Before"); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | tool := NewSetSessionTitleTool(dir, func() string { return path }, nil) |
| 59 | if _, err := tool.Execute(context.Background(), json.RawMessage(`{"title":""}`)); err != nil { |
| 60 | t.Fatalf("Execute: %v", err) |
| 61 | } |
| 62 | meta, ok, err := agent.LoadBranchMeta(path) |
| 63 | if err != nil || !ok || meta.CustomTitle != "" { |
| 64 | t.Fatalf("meta = %+v, ok=%v, err=%v", meta, ok, err) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestSetSessionTitleRejectsUnavailableOrInvalidCurrentPath(t *testing.T) { |
| 69 | dir := t.TempDir() |
| 70 | tests := []struct { |
| 71 | name string |
| 72 | path string |
| 73 | }{ |
| 74 | {name: "empty"}, |
| 75 | {name: "directory", path: dir}, |
| 76 | {name: "non transcript", path: filepath.Join(dir, "notes.txt")}, |
| 77 | } |
| 78 | for _, tt := range tests { |
| 79 | t.Run(tt.name, func(t *testing.T) { |
| 80 | tool := NewSetSessionTitleTool(dir, func() string { return tt.path }, nil) |
| 81 | if _, err := tool.Execute(context.Background(), json.RawMessage(`{"title":"Title"}`)); err == nil { |
| 82 | t.Fatal("expected error") |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestSetSessionTitleRequiresTitleAndRejectsSubagent(t *testing.T) { |
| 89 | dir := t.TempDir() |
| 90 | path := writeTitleTestSession(t, dir, "current.jsonl") |
| 91 | tool := NewSetSessionTitleTool(dir, func() string { return path }, nil) |
| 92 | if _, err := tool.Execute(context.Background(), json.RawMessage(`{}`)); err == nil { |
| 93 | t.Fatal("missing title should fail") |
| 94 | } |
| 95 | tooLong, _ := json.Marshal(map[string]string{"title": strings.Repeat("界", setSessionTitleMaxRunes+1)}) |
| 96 | if _, err := tool.Execute(context.Background(), tooLong); err == nil { |
| 97 | t.Fatal("oversized title should fail") |
| 98 | } |
| 99 | ctx := agent.WithSubagentDepth(context.Background(), 1) |
| 100 | if _, err := tool.Execute(ctx, json.RawMessage(`{"title":"Wrong owner"}`)); err == nil { |
| 101 | t.Fatal("subagent should not rename the parent session") |
| 102 | } |
| 103 | } |
| 104 |