| 1 |
package clnkrapp |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"reflect" |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/clnkr-ai/clnkr" |
| 11 |
) |
| 12 |
|
| 13 |
func TestParseCompactCommand(t *testing.T) { |
| 14 |
tests := []struct { |
| 15 |
input string |
| 16 |
ok bool |
| 17 |
}{ |
| 18 |
{input: "/compact", ok: true}, |
| 19 |
{input: " /compact ", ok: true}, |
| 20 |
{input: "compact"}, |
| 21 |
{input: "/compaction"}, |
| 22 |
{input: "/compactfoo"}, |
| 23 |
{input: "/compact/anything"}, |
| 24 |
{input: "/compact focus on tests"}, |
| 25 |
{input: "/compact focus on tests"}, |
| 26 |
{input: "/compact\tfocus on tests"}, |
| 27 |
} |
| 28 |
|
| 29 |
for _, tt := range tests { |
| 30 |
t.Run(tt.input, func(t *testing.T) { |
| 31 |
got := ParseCompactCommand(tt.input) |
| 32 |
if got != tt.ok { |
| 33 |
t.Fatalf("ParseCompactCommand(%q) = %v, want %v", tt.input, got, tt.ok) |
| 34 |
} |
| 35 |
}) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func TestMalformedCompactCommand(t *testing.T) { |
| 40 |
tests := []struct { |
| 41 |
name string |
| 42 |
input string |
| 43 |
want string |
| 44 |
}{ |
| 45 |
{name: "exact", input: "/compact", want: ""}, |
| 46 |
{name: "surrounding whitespace", input: " /compact ", want: ""}, |
| 47 |
{ |
| 48 |
name: "trailing space", |
| 49 |
input: "/compact focus", |
| 50 |
want: "/compact does not accept arguments", |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "trailing tab", |
| 54 |
input: "/compact\tfocus", |
| 55 |
want: "/compact does not accept arguments", |
| 56 |
}, |
| 57 |
{ |
| 58 |
name: "trailing unicode space", |
| 59 |
input: "/compact\u00a0focus", |
| 60 |
want: "/compact does not accept arguments", |
| 61 |
}, |
| 62 |
{name: "no space suffix", input: "/compactfoo", want: ""}, |
| 63 |
{name: "slash suffix", input: "/compact/anything", want: ""}, |
| 64 |
{name: "compaction word", input: "/compaction", want: ""}, |
| 65 |
{name: "compact no prefix", input: "compact", want: ""}, |
| 66 |
} |
| 67 |
|
| 68 |
for _, tt := range tests { |
| 69 |
t.Run(tt.name, func(t *testing.T) { |
| 70 |
err := MalformedCompactCommand(tt.input) |
| 71 |
if tt.want == "" { |
| 72 |
if err != nil { |
| 73 |
t.Fatalf("MalformedCompactCommand(%q) = %v, want nil", tt.input, err) |
| 74 |
} |
| 75 |
return |
| 76 |
} |
| 77 |
if err == nil || !strings.Contains(err.Error(), tt.want) { |
| 78 |
t.Fatalf("MalformedCompactCommand(%q) = %v, want %q", tt.input, err, tt.want) |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
func TestHandleCompactCommandDoesNotAppendLiteralUserMessage(t *testing.T) { |
| 85 |
agent := clnkr.NewAgent(&fakeModel{}, &fakeExecutor{}, "/tmp") |
| 86 |
if err := agent.AddMessages(compactableMessages()); err != nil { |
| 87 |
t.Fatalf("AddMessages: %v", err) |
| 88 |
} |
| 89 |
|
| 90 |
compactor := &fakeCompactor{summary: "Older work summarized."} |
| 91 |
stats, ran, err := HandleCompactCommand( |
| 92 |
context.Background(), |
| 93 |
agent, |
| 94 |
"/compact", |
| 95 |
func() clnkr.Compactor { |
| 96 |
return compactor |
| 97 |
}, |
| 98 |
) |
| 99 |
if err != nil { |
| 100 |
t.Fatalf("HandleCompactCommand: %v", err) |
| 101 |
} |
| 102 |
if !ran { |
| 103 |
t.Fatal("HandleCompactCommand did not run compact command") |
| 104 |
} |
| 105 |
if got := [2]int{stats.CompactedMessages, stats.KeptMessages}; got != [2]int{2, 4} { |
| 106 |
t.Fatalf("stats = %#v, want 2 compacted and 4 kept", stats) |
| 107 |
} |
| 108 |
if compactor.calls != 1 || len(compactor.messages) != 2 { |
| 109 |
t.Fatalf( |
| 110 |
"compactor got %d calls and %d messages, want 1 call and 2 messages", |
| 111 |
compactor.calls, |
| 112 |
len(compactor.messages), |
| 113 |
) |
| 114 |
} |
| 115 |
|
| 116 |
msgs := agent.Messages() |
| 117 |
if hasUserMessage(msgs, "/compact") { |
| 118 |
t.Fatalf("literal compact command was appended: %#v", msgs) |
| 119 |
} |
| 120 |
if len(msgs) == 0 || !strings.HasPrefix(msgs[0].Content, "[compact]\n") { |
| 121 |
t.Fatalf("expected compact block at start, got %#v", msgs) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
func TestCompactTranscriptRunsCompaction(t *testing.T) { |
| 126 |
agent := clnkr.NewAgent(&fakeModel{}, &fakeExecutor{}, "/tmp") |
| 127 |
if err := agent.AddMessages(compactableMessages()); err != nil { |
| 128 |
t.Fatalf("AddMessages: %v", err) |
| 129 |
} |
| 130 |
|
| 131 |
compactor := &fakeCompactor{summary: "Older work summarized."} |
| 132 |
stats, err := compactTranscript( |
| 133 |
context.Background(), |
| 134 |
agent, |
| 135 |
func() clnkr.Compactor { |
| 136 |
return compactor |
| 137 |
}, |
| 138 |
) |
| 139 |
if err != nil { |
| 140 |
t.Fatalf("CompactTranscript: %v", err) |
| 141 |
} |
| 142 |
if stats.CompactedMessages != 2 || stats.KeptMessages != 4 { |
| 143 |
t.Fatalf("stats = %#v, want 2 compacted and 4 kept", stats) |
| 144 |
} |
| 145 |
if len(agent.Messages()) == 0 || |
| 146 |
!strings.HasPrefix(agent.Messages()[0].Content, "[compact]\n") { |
| 147 |
t.Fatalf("expected compact block at start, got %#v", agent.Messages()) |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func TestHandleCompactCommandFailureLeavesMessagesUnchanged(t *testing.T) { |
| 152 |
agent := clnkr.NewAgent(&fakeModel{}, &fakeExecutor{}, "/tmp") |
| 153 |
if err := agent.AddMessages(compactableMessages()); err != nil { |
| 154 |
t.Fatalf("AddMessages: %v", err) |
| 155 |
} |
| 156 |
before := agent.Messages() |
| 157 |
|
| 158 |
_, _, err := HandleCompactCommand( |
| 159 |
context.Background(), |
| 160 |
agent, |
| 161 |
"/compact", |
| 162 |
func() clnkr.Compactor { |
| 163 |
return &fakeCompactor{err: errors.New("boom")} |
| 164 |
}, |
| 165 |
) |
| 166 |
if err == nil || !strings.Contains(err.Error(), "compact transcript: summarize prefix: boom") { |
| 167 |
t.Fatalf("got %v, want summarize prefix error", err) |
| 168 |
} |
| 169 |
if !reflect.DeepEqual(agent.Messages(), before) { |
| 170 |
t.Fatalf( |
| 171 |
"messages changed on compaction failure: got %#v want %#v", |
| 172 |
agent.Messages(), |
| 173 |
before, |
| 174 |
) |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
func TestHandleCompactCommandRejectsMissingCompactor(t *testing.T) { |
| 179 |
agent := clnkr.NewAgent(&fakeModel{}, &fakeExecutor{}, "/tmp") |
| 180 |
tests := []struct { |
| 181 |
name string |
| 182 |
factory func() clnkr.Compactor |
| 183 |
want string |
| 184 |
}{ |
| 185 |
{name: "factory", want: "no compactor factory configured"}, |
| 186 |
{ |
| 187 |
name: "compactor", |
| 188 |
factory: func() clnkr.Compactor { return nil }, |
| 189 |
want: "no compactor configured", |
| 190 |
}, |
| 191 |
} |
| 192 |
|
| 193 |
for _, tt := range tests { |
| 194 |
t.Run(tt.name, func(t *testing.T) { |
| 195 |
_, _, err := HandleCompactCommand(context.Background(), agent, "/compact", tt.factory) |
| 196 |
if err == nil || !strings.Contains(err.Error(), tt.want) { |
| 197 |
t.Fatalf("got %v, want %q", err, tt.want) |
| 198 |
} |
| 199 |
}) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
func TestRejectCompactCommandOutsideConversation(t *testing.T) { |
| 204 |
tests := []struct { |
| 205 |
input string |
| 206 |
want string |
| 207 |
}{ |
| 208 |
{input: "/compact", want: "/compact is only available at the conversational prompt"}, |
| 209 |
{input: "/compact focus on tests", want: "/compact does not accept arguments"}, |
| 210 |
{input: "/compactfoo"}, |
| 211 |
{input: "/compact/anything"}, |
| 212 |
{input: "/compaction"}, |
| 213 |
} |
| 214 |
|
| 215 |
for _, tt := range tests { |
| 216 |
t.Run(tt.input, func(t *testing.T) { |
| 217 |
err := RejectCompactCommand(tt.input) |
| 218 |
if tt.want == "" { |
| 219 |
if err != nil { |
| 220 |
t.Fatalf("got %v, want nil", err) |
| 221 |
} |
| 222 |
return |
| 223 |
} |
| 224 |
if err == nil || !strings.Contains(err.Error(), tt.want) { |
| 225 |
t.Fatalf("got %v, want %q", err, tt.want) |
| 226 |
} |
| 227 |
}) |
| 228 |
} |
| 229 |
} |