0696ab849a
Snapshot the chat directory with internal/filediff when a turn starts and compare it when the turn ends; store changed turns (newest 100 per chat), push chat_turn_diffs summaries, attach them to chat_messages replies and serve one turn's patches through chat_turn_diff.
269 lines
9.7 KiB
Go
269 lines
9.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
"github.com/coder/websocket/wsjson"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
func turnDiffHub(t *testing.T, dir string) *chatHub {
|
|
t.Helper()
|
|
h := newChatHub()
|
|
h.turnDiffStore = newOperatorStore(t)
|
|
h.turnDiffDirectory = func(context.Context, string) string { return dir }
|
|
return h
|
|
}
|
|
|
|
func gitRepo(t *testing.T, files map[string]string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
for name, body := range files {
|
|
writeTurnFile(t, dir, name, body)
|
|
}
|
|
for _, args := range [][]string{
|
|
{"init", "-q"},
|
|
{"add", "-A"},
|
|
{"-c", "user.name=t", "-c", "user.email=t@example.com", "commit", "-q", "-m", "base"},
|
|
} {
|
|
cmd := exec.Command("git", args...)
|
|
cmd.Dir = dir
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("git %v: %v %s", args, err, out)
|
|
}
|
|
}
|
|
return dir
|
|
}
|
|
|
|
func writeTurnFile(t *testing.T, dir, name, body string) {
|
|
t.Helper()
|
|
if err := os.WriteFile(filepath.Join(dir, name), []byte(body), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func endDiffTurn(h *chatHub, sid string) {
|
|
h.emit(protocol.ChatEventFrame{SessionID: sid, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded}})
|
|
h.turnDiffWG.Wait()
|
|
}
|
|
|
|
func recordedTurns(t *testing.T, h *chatHub, sid string) []protocol.ChatTurnDiffSummary {
|
|
t.Helper()
|
|
got, err := h.turnDiffStore.ChatTurnDiffs(context.Background(), sid)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
func TestChatTurnDiffRecordsGitWorkspaceChanges(t *testing.T) {
|
|
dir := gitRepo(t, map[string]string{"a.txt": "one\ntwo\nthree\n"})
|
|
h := turnDiffHub(t, dir)
|
|
h.startRoomTurnDiff("ses_1")
|
|
writeTurnFile(t, dir, "a.txt", "one\n2\nthree\nfour\n")
|
|
writeTurnFile(t, dir, "new.txt", "fresh\n")
|
|
h.emit(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_a", Kind: protocol.ChatEventText, Delta: "done"})
|
|
h.emit(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_a", Kind: protocol.ChatEventText, Delta: "!"})
|
|
endDiffTurn(h, "ses_1")
|
|
|
|
turns := recordedTurns(t, h, "ses_1")
|
|
if len(turns) != 1 {
|
|
t.Fatalf("turns = %+v, want one", turns)
|
|
}
|
|
got := turns[0]
|
|
if got.Files != 2 || got.Additions != 3 || got.Deletions != 1 || got.Truncated || got.Status != "complete" {
|
|
t.Fatalf("summary = %+v, want 2 files +3 -1 complete", got)
|
|
}
|
|
if len(got.MessageIDs) != 1 || got.MessageIDs[0] != "msg_a" || got.TurnID == "" || got.EndedAt < got.StartedAt {
|
|
t.Fatalf("summary identity = %+v", got)
|
|
}
|
|
_, changes, found, err := h.turnDiffStore.ChatTurnDiff(context.Background(), "ses_1", got.TurnID)
|
|
if err != nil || !found || len(changes.Files) != 2 || !strings.Contains(changes.Files[0].Patch, "+2") {
|
|
t.Fatalf("changes = %+v found=%v err=%v", changes, found, err)
|
|
}
|
|
}
|
|
|
|
func TestChatTurnDiffNonGitDirectoryUsesWorkspaceSnapshot(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writeTurnFile(t, dir, "notes.md", "a\n")
|
|
h := turnDiffHub(t, dir)
|
|
h.startRoomTurnDiff("ses_1")
|
|
writeTurnFile(t, dir, "notes.md", "a\nb\n")
|
|
endDiffTurn(h, "ses_1")
|
|
turns := recordedTurns(t, h, "ses_1")
|
|
if len(turns) != 1 || turns[0].Files != 1 || turns[0].Additions != 1 || turns[0].Deletions != 0 {
|
|
t.Fatalf("turns = %+v", turns)
|
|
}
|
|
}
|
|
|
|
func TestChatTurnDiffRecordsNothingWithoutChanges(t *testing.T) {
|
|
dir := gitRepo(t, map[string]string{"a.txt": "same\n"})
|
|
h := turnDiffHub(t, dir)
|
|
h.startRoomTurnDiff("ses_1")
|
|
endDiffTurn(h, "ses_1")
|
|
if turns := recordedTurns(t, h, "ses_1"); len(turns) != 0 {
|
|
t.Fatalf("an unchanged turn recorded %+v", turns)
|
|
}
|
|
// A directory that cannot be read yields no diff, never an empty claim.
|
|
h = turnDiffHub(t, filepath.Join(dir, "missing"))
|
|
h.startRoomTurnDiff("ses_1")
|
|
endDiffTurn(h, "ses_1")
|
|
if turns := recordedTurns(t, h, "ses_1"); len(turns) != 0 {
|
|
t.Fatalf("an unavailable capture recorded %+v", turns)
|
|
}
|
|
}
|
|
|
|
func TestChatTurnDiffMarksBoundedPatchesTruncated(t *testing.T) {
|
|
dir := gitRepo(t, map[string]string{"a.txt": "x\n"})
|
|
h := turnDiffHub(t, dir)
|
|
h.startRoomTurnDiff("ses_1")
|
|
writeTurnFile(t, dir, "big.txt", strings.Repeat("line\n", 6000))
|
|
endDiffTurn(h, "ses_1")
|
|
turns := recordedTurns(t, h, "ses_1")
|
|
if len(turns) != 1 || !turns[0].Truncated || turns[0].Files != 1 || turns[0].Status != "partial" {
|
|
t.Fatalf("turns = %+v, want one truncated partial turn", turns)
|
|
}
|
|
_, changes, _, _ := h.turnDiffStore.ChatTurnDiff(context.Background(), "ses_1", turns[0].TurnID)
|
|
if file := changes.Files[0]; file.Patch != "" || !file.Truncated || file.NoPatchReason != "lines" {
|
|
t.Fatalf("file = %+v, want no patch (lines)", file)
|
|
}
|
|
}
|
|
|
|
// A turn the stream announces (no room prompt) is captured from its started
|
|
// frame, for every harness that reports one.
|
|
func TestChatTurnDiffCapturesStreamAnnouncedTurn(t *testing.T) {
|
|
dir := gitRepo(t, map[string]string{"a.txt": "a\n"})
|
|
h := turnDiffHub(t, dir)
|
|
h.mu.Lock()
|
|
h.rooms["ses_1"] = newChatRoom(context.Background(), "ses_1")
|
|
h.mu.Unlock()
|
|
h.emit(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepStarted, TurnID: "native"}})
|
|
h.mu.Lock()
|
|
capture := h.turnCaptures["ses_1"]
|
|
h.mu.Unlock()
|
|
if capture == nil {
|
|
t.Fatal("started frame did not begin a capture")
|
|
}
|
|
<-capture.ready
|
|
writeTurnFile(t, dir, "a.txt", "b\n")
|
|
h.emit(protocol.ChatEventFrame{SessionID: "ses_1", Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: "native"}})
|
|
h.turnDiffWG.Wait()
|
|
if turns := recordedTurns(t, h, "ses_1"); len(turns) != 1 || turns[0].Additions != 1 || turns[0].Deletions != 1 {
|
|
t.Fatalf("turns = %+v", turns)
|
|
}
|
|
}
|
|
|
|
func TestChatTurnDiffPatchLineCounts(t *testing.T) {
|
|
patch := "--- \"a/x\"\n+++ \"b/x\"\n@@ -1,2 +1,2 @@\n--- removed dashes\n+++ added pluses\n same\n\\ No newline at end of file\n"
|
|
if added, removed := patchLineCounts(patch); added != 1 || removed != 1 {
|
|
t.Fatalf("counts = +%d -%d, want +1 -1", added, removed)
|
|
}
|
|
}
|
|
|
|
// editingChat changes the workspace when the prompt arrives and only then
|
|
// streams the scripted turn, so the turn's diff is deterministic.
|
|
type editingChat struct {
|
|
*fakeChat
|
|
edit func()
|
|
prompted chan struct{}
|
|
}
|
|
|
|
func (e *editingChat) Prompt(ctx context.Context, sessionID, text string) error {
|
|
e.edit()
|
|
close(e.prompted)
|
|
return e.fakeChat.Prompt(ctx, sessionID, text)
|
|
}
|
|
|
|
func (e *editingChat) Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error {
|
|
ready()
|
|
select {
|
|
case <-e.prompted:
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
return e.fakeChat.Stream(ctx, sessionID, nil, emit)
|
|
}
|
|
|
|
func TestChatTurnDiffProtocolRoundTrip(t *testing.T) {
|
|
dir := gitRepo(t, map[string]string{"main.go": "package main\n"})
|
|
store := newOperatorStore(t)
|
|
if err := store.RecordOperatorSession(context.Background(), "ses_1", dir, "chat", protocol.ChatSessionTagOperator); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
broker := &editingChat{fakeChat: scriptedChat(), prompted: make(chan struct{}), edit: func() {
|
|
writeTurnFile(t, dir, "main.go", "package main\n\nfunc main() {}\n")
|
|
}}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: broker, OperatorRegistry: store}, store))
|
|
t.Cleanup(func() { srv.Close(); cancel() })
|
|
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { conn.CloseNow() })
|
|
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "p1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "edit"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var push protocol.Message
|
|
for push.Type != protocol.ChatTurnDiffs {
|
|
push = protocol.Message{}
|
|
if err := readTestFrame(ctx, conn, &push); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
list := push.ChatTurnDiffList
|
|
if list == nil || list.SessionID != "ses_1" || len(list.TurnDiffs) != 1 {
|
|
t.Fatalf("push = %+v", push)
|
|
}
|
|
summary := list.TurnDiffs[0]
|
|
if summary.Files != 1 || summary.Additions != 2 || summary.Deletions != 0 || len(summary.MessageIDs) != 1 || summary.MessageIDs[0] != "msg_1" {
|
|
t.Fatalf("summary = %+v", summary)
|
|
}
|
|
|
|
request := protocol.Message{Type: protocol.ChatTurnDiffRequest, ID: "d1", ChatTurnDiff: &protocol.ChatTurnDiff{SessionID: "ses_1", TurnID: summary.TurnID}}
|
|
reply := turnDiffReply(t, ctx, conn, request)
|
|
if reply.Type != protocol.ChatTurnDiffRequest || reply.ChatTurnDiff == nil || reply.ChatTurnDiff.Changes == nil {
|
|
t.Fatalf("reply = %+v", reply)
|
|
}
|
|
files := reply.ChatTurnDiff.Changes.Files
|
|
if len(files) != 1 || files[0].Path != "main.go" || !strings.Contains(files[0].Patch, "+func main() {}") {
|
|
t.Fatalf("files = %+v", files)
|
|
}
|
|
|
|
history := turnDiffReply(t, ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "h1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}})
|
|
if history.ChatTurnDiffList == nil || len(history.ChatTurnDiffList.TurnDiffs) != 1 || history.ChatTurnDiffList.TurnDiffs[0].TurnID != summary.TurnID {
|
|
t.Fatalf("history turn diffs = %+v", history.ChatTurnDiffList)
|
|
}
|
|
|
|
missing := turnDiffReply(t, ctx, conn, protocol.Message{Type: protocol.ChatTurnDiffRequest, ID: "d2", ChatTurnDiff: &protocol.ChatTurnDiff{SessionID: "ses_1", TurnID: "turn_gone"}})
|
|
if missing.Type != protocol.Error || !strings.Contains(missing.Error, "no recorded changes") {
|
|
t.Fatalf("missing turn reply = %+v", missing)
|
|
}
|
|
}
|
|
|
|
// turnDiffReply writes request and returns the response carrying its id.
|
|
func turnDiffReply(t *testing.T, ctx context.Context, conn *websocket.Conn, request protocol.Message) protocol.Message {
|
|
t.Helper()
|
|
if err := wsjson.Write(ctx, conn, request); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for {
|
|
var message protocol.Message
|
|
if err := readTestFrame(ctx, conn, &message); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if message.ID == request.ID {
|
|
return message
|
|
}
|
|
}
|
|
}
|