Files
pipelines/internal/server/chat_open_workspace_test.go
T
alan 4eb9b2e934 Open chats: the server moves a restarted chat (/new) to its new session
The server-stored open-chat set kept a /new'd chat's old session unless the
restarting device's own write landed, so another device (the phone) came
back to the old conversation. chat_new_session now replaces the session in
the stored set in place (position and floating kept) and pushes the new
revision to every connection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 00:43:33 +00:00

196 lines
8.3 KiB
Go

package server
import (
"context"
"testing"
"github.com/coder/websocket"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func workspaceWrite(id string, base int64, entries ...protocol.ChatOpenWorkspaceEntry) protocol.Message {
return protocol.Message{Type: protocol.ChatOpenWorkspace, ID: id, ChatOpenWorkspaceRequest: &protocol.ChatOpenWorkspaceRequest{Entries: entries, BaseRevision: base}}
}
func workspaceIDs(snapshot *protocol.ChatOpenWorkspaceSnapshot) []string {
ids := []string{}
for _, entry := range snapshot.Entries {
id := entry.SessionID
if entry.Floating {
id += "*"
}
ids = append(ids, id)
}
return ids
}
func sameIDs(a []string, b ...string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// readWorkspacePush reads chat_open_workspace_changed pushes until one carries
// revision; lower revisions (pushed out of order) are skipped.
func readWorkspacePush(t *testing.T, ctx context.Context, conn *websocket.Conn, revision int64) *protocol.ChatOpenWorkspaceSnapshot {
t.Helper()
for {
message := readUntil(t, ctx, conn, protocol.ChatOpenWorkspaceChanged)
got := message.ChatOpenWorkspace
if got == nil {
t.Fatal("push without a snapshot")
}
if got.Revision == revision {
return got
}
if got.Revision > revision {
t.Fatalf("push revision %d, want %d", got.Revision, revision)
}
}
}
// TestChatOpenWorkspaceSyncsEveryConnection is the fan-out evidence: a write
// from one connection is stored at the next revision and pushed to the other;
// a write from a stale base is refused with the current snapshot; closing a
// chat drops it and pushes the result to both connections; and a later write
// cannot put the closed chat back.
func TestChatOpenWorkspaceSyncsEveryConnection(t *testing.T) {
store := newOperatorStore(t)
ctx, _, conns := dialChatConns(t, Options{Chat: scriptedChat(), OperatorRegistry: store}, 2)
for _, id := range []string{"ses_a", "ses_b"} {
if err := store.RecordOperatorSession(ctx, id, "/work", id, protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
}
a, b := protocol.ChatOpenWorkspaceEntry{SessionID: "ses_a"}, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_b", Floating: true}
writeChat(t, ctx, conns[0], workspaceWrite("w1", 0, a, b, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_unknown"}))
reply := readUntil(t, ctx, conns[0], protocol.ChatOpenWorkspace).ChatOpenWorkspace
if reply.Revision != 1 || reply.Rejected || !sameIDs(workspaceIDs(reply), "ses_a", "ses_b*") {
t.Fatalf("first write reply = %+v", reply)
}
if push := readWorkspacePush(t, ctx, conns[1], 1); !sameIDs(workspaceIDs(push), "ses_a", "ses_b*") {
t.Fatalf("second connection got %v", workspaceIDs(push))
}
writeChat(t, ctx, conns[1], protocol.Message{Type: protocol.ChatOpenWorkspace, ID: "r1"})
if read := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace; read.Revision != 1 || !sameIDs(workspaceIDs(read), "ses_a", "ses_b*") {
t.Fatalf("read = %+v", read)
}
// A write derived from revision 0 lands after revision 1: refused.
writeChat(t, ctx, conns[1], workspaceWrite("w2", 0, b))
stale := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace
if !stale.Rejected || stale.Revision != 1 || !sameIDs(workspaceIDs(stale), "ses_a", "ses_b*") {
t.Fatalf("stale write reply = %+v", stale)
}
// Reorder from the second connection reaches the first.
b.Floating = false
writeChat(t, ctx, conns[1], workspaceWrite("w3", 1, b, a))
if got := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace; got.Revision != 2 {
t.Fatalf("reorder reply = %+v", got)
}
if push := readWorkspacePush(t, ctx, conns[0], 2); !sameIDs(workspaceIDs(push), "ses_b", "ses_a") {
t.Fatalf("reorder push = %v", workspaceIDs(push))
}
// Closing ses_a drops it and pushes revision 3 to both connections.
writeChat(t, ctx, conns[0], protocol.Message{Type: protocol.ChatClose, ID: "c1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_a"}})
readUntil(t, ctx, conns[0], protocol.ChatClose)
for i, conn := range conns {
if push := readWorkspacePush(t, ctx, conn, 3); !sameIDs(workspaceIDs(push), "ses_b") {
t.Fatalf("connection %d close push = %v", i, workspaceIDs(push))
}
}
// A client still showing the closed chat cannot restore it.
writeChat(t, ctx, conns[1], workspaceWrite("w4", 3, a, b))
if got := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace; got.Revision != 3 || !sameIDs(workspaceIDs(got), "ses_b") {
t.Fatalf("closed chat restored: %+v", got)
}
}
// TestChatOpenWorkspacePrunesOnConnectForEveryone pins that a chat closed
// behind the workspace's back is pruned when a connection opens, and that the
// pruned snapshot reaches the connections already open, not only the new one.
func TestChatOpenWorkspacePrunesOnConnectForEveryone(t *testing.T) {
store := newOperatorStore(t)
ctx, _, srv, conns := dialChatServer(t, Options{Chat: scriptedChat(), OperatorRegistry: store}, 1)
for _, id := range []string{"ses_a", "ses_b"} {
if err := store.RecordOperatorSession(ctx, id, "/work", id, protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
}
writeChat(t, ctx, conns[0], workspaceWrite("w1", 0, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_a"}, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_b"}))
readUntil(t, ctx, conns[0], protocol.ChatOpenWorkspace)
if err := store.CloseOperatorSession(ctx, "ses_a"); err != nil {
t.Fatal(err)
}
late := dialChatConn(t, ctx, srv)
for i, conn := range []*websocket.Conn{conns[0], late} {
if push := readWorkspacePush(t, ctx, conn, 2); !sameIDs(workspaceIDs(push), "ses_b") {
t.Fatalf("connection %d prune push = %v", i, workspaceIDs(push))
}
}
// With nothing to prune, a new connection alone is brought up to date.
third := dialChatConn(t, ctx, srv)
if push := readWorkspacePush(t, ctx, third, 2); !sameIDs(workspaceIDs(push), "ses_b") {
t.Fatalf("catch-up push = %v", workspaceIDs(push))
}
}
// TestChatOpenWorkspaceFollowsARestartedChat pins that /new moves the open
// chat to its new session on the server and pushes that to every connection,
// so another device reopens the new session even if the restarting device
// never writes its own workspace.
func TestChatOpenWorkspaceFollowsARestartedChat(t *testing.T) {
store := newOperatorStore(t)
chat := scriptedChat()
chat.created = protocol.ChatSession{ID: "ses_fresh", Title: "Feature"}
chat.sessionFound = true
chat.sessions = []protocol.ChatSession{{ID: "ses_old"}, {ID: "ses_b"}, {ID: "ses_fresh"}}
ctx, _, conns := dialChatConns(t, Options{Chat: chat, OperatorRegistry: store}, 2)
for _, id := range []string{"ses_old", "ses_b"} {
if err := store.RecordOperatorSession(ctx, id, "/work", id, protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
}
writeChat(t, ctx, conns[0], workspaceWrite("w1", 0, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_b"}, protocol.ChatOpenWorkspaceEntry{SessionID: "ses_old", Floating: true}))
if got := readUntil(t, ctx, conns[0], protocol.ChatOpenWorkspace).ChatOpenWorkspace; got.Revision != 1 {
t.Fatalf("first write reply = %+v", got)
}
readWorkspacePush(t, ctx, conns[1], 1)
writeChat(t, ctx, conns[0], chatNewSessionRequest("n1", "ses_old"))
// The push is asynchronous, so it may reach the restarting connection
// before or after its reply.
var replied bool
var pushed *protocol.ChatOpenWorkspaceSnapshot
for !replied || pushed == nil {
message := readMessage(t, ctx, conns[0])
switch {
case message.Type == protocol.ChatNewSession && message.ID == "n1":
replied = true
case message.Type == protocol.ChatOpenWorkspaceChanged && message.ChatOpenWorkspace != nil && message.ChatOpenWorkspace.Revision == 2:
pushed = message.ChatOpenWorkspace
}
}
if !sameIDs(workspaceIDs(pushed), "ses_b", "ses_fresh*") {
t.Fatalf("restarting connection after /new = %v", workspaceIDs(pushed))
}
if push := readWorkspacePush(t, ctx, conns[1], 2); !sameIDs(workspaceIDs(push), "ses_b", "ses_fresh*") {
t.Fatalf("other connection after /new = %v", workspaceIDs(push))
}
writeChat(t, ctx, conns[1], protocol.Message{Type: protocol.ChatOpenWorkspace, ID: "r1"})
if read := readUntil(t, ctx, conns[1], protocol.ChatOpenWorkspace).ChatOpenWorkspace; read.Revision != 2 || !sameIDs(workspaceIDs(read), "ses_b", "ses_fresh*") {
t.Fatalf("stored workspace after /new = %+v", read)
}
}