Files
pipelines/internal/server/chat_open_workspace_test.go
T
alan 4f8e99f316 Serialize open-chat sync writes by server revision and fan out prunes
Server: chat_open_workspace is a compare-and-swap on base_revision; a stale
write is refused and answered with the current snapshot (rejected: true).
Each stored change increments the revision; identical writes store nothing.
A request with no body reads the set. Seeds apply only to a never-stored set.
Validation uses the operator registry (recorded, not closed). chat_close and
connection-time pruning store a new revision and push it to every
connection; otherwise a new connection gets the current set to catch up.

App: ChatOpenWorkspaceController keeps one write in flight, coalesces later
changes, sends from the last replied revision and ignores lower revisions;
pushes and rejected replies win over unsent changes. It starts after the
local layout restores (seed or read), keeps a deep-linked chat, and is
disposed on server/gateway rebind. Remote membership is applied by
reconcileChatWorkspaceTree, keeping pane ids and split sizes. Federated
lists keep their local layout.

Tests: Go store/handler fan-out tests; app controller, reconcile and screen
sync tests. Notes record a live two-client scratch check.
2026-11-12 03:23:22 +00:00

148 lines
5.9 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))
}
}