109 lines
3.1 KiB
Go
109 lines
3.1 KiB
Go
|
|
package server
|
||
|
|
|
||
|
|
import (
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"github.com/daniel/cursor-adapter/internal/types"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestDetectCallerWorkspace_ClaudeCodeEnvBlock(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
corpus := "<env>\nWorking directory: " + dir + "\nIs directory a git repo: Yes\n</env>"
|
||
|
|
got := detectCallerWorkspace(corpus)
|
||
|
|
if got != dir {
|
||
|
|
t.Fatalf("got %q, want %q", got, dir)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDetectCallerWorkspace_RejectsNonExistentSandboxPath(t *testing.T) {
|
||
|
|
corpus := "Working directory: /sessions/gracious-magical-franklin/proj"
|
||
|
|
got := detectCallerWorkspace(corpus)
|
||
|
|
if got != "" {
|
||
|
|
t.Fatalf("expected empty (path doesn't exist on host), got %q", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDetectCallerWorkspace_RejectsRelativePath(t *testing.T) {
|
||
|
|
corpus := "cwd: src/"
|
||
|
|
got := detectCallerWorkspace(corpus)
|
||
|
|
if got != "" {
|
||
|
|
t.Fatalf("expected empty for relative path, got %q", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDetectCallerWorkspace_RejectsFilePath(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
f := filepath.Join(dir, "file.txt")
|
||
|
|
if err := os.WriteFile(f, []byte("x"), 0o644); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
corpus := "Working directory: " + f
|
||
|
|
got := detectCallerWorkspace(corpus)
|
||
|
|
if got != "" {
|
||
|
|
t.Fatalf("expected empty for file path, got %q", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDetectAnthropicCwd_FromSystemBlock(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
req := types.AnthropicMessagesRequest{
|
||
|
|
System: []types.AnthropicBlock{
|
||
|
|
{Type: "text", Text: "<env>\nWorking directory: " + dir + "\n</env>"},
|
||
|
|
},
|
||
|
|
Messages: []types.AnthropicMessage{
|
||
|
|
{Role: "user", Content: []types.AnthropicBlock{{Type: "text", Text: "hi"}}},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
if got := detectAnthropicCwd(req); got != dir {
|
||
|
|
t.Fatalf("got %q, want %q", got, dir)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDetectAnthropicCwd_FromUserMessage(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
req := types.AnthropicMessagesRequest{
|
||
|
|
Messages: []types.AnthropicMessage{
|
||
|
|
{Role: "user", Content: []types.AnthropicBlock{
|
||
|
|
{Type: "text", Text: "<system-reminder>Current working directory: " + dir + "</system-reminder>\nHelp me"},
|
||
|
|
}},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
if got := detectAnthropicCwd(req); got != dir {
|
||
|
|
t.Fatalf("got %q, want %q", got, dir)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDetectAnthropicCwd_TrimsTrailingPunctuation(t *testing.T) {
|
||
|
|
dir := t.TempDir()
|
||
|
|
corpus := "Working directory: " + dir + "."
|
||
|
|
if got := detectCallerWorkspace(corpus); got != dir {
|
||
|
|
t.Fatalf("got %q, want %q (trailing dot should be stripped)", got, dir)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDetectAnthropicCwd_NoneFound(t *testing.T) {
|
||
|
|
req := types.AnthropicMessagesRequest{
|
||
|
|
Messages: []types.AnthropicMessage{
|
||
|
|
{Role: "user", Content: []types.AnthropicBlock{{Type: "text", Text: "just a question"}}},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
if got := detectAnthropicCwd(req); got != "" {
|
||
|
|
t.Fatalf("got %q, want empty", got)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Sanity check that none of our regexes mis-eat absolute paths inside
|
||
|
|
// regular sentences without a cwd marker.
|
||
|
|
func TestDetectCallerWorkspace_IgnoresUnmarkedAbsolutePaths(t *testing.T) {
|
||
|
|
corpus := "I edited /tmp/foo earlier."
|
||
|
|
if !strings.HasPrefix(corpus, "I edited") { // keep the import used
|
||
|
|
t.Fatal("test fixture changed")
|
||
|
|
}
|
||
|
|
if got := detectCallerWorkspace(corpus); got != "" {
|
||
|
|
t.Fatalf("got %q, want empty (no cwd marker)", got)
|
||
|
|
}
|
||
|
|
}
|