package server import ( "context" "fmt" "net/http/httptest" "strings" "testing" "time" "github.com/coder/websocket" "github.com/coder/websocket/wsjson" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) func dialAckClient(t *testing.T, ctx context.Context, url string) *websocket.Conn { t.Helper() conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(url, "http")+"/connect", nil) if err != nil { t.Fatal(err) } t.Cleanup(func() { conn.CloseNow() }) var welcome protocol.Message if err := wsjson.Read(ctx, conn, &welcome); err != nil { t.Fatal(err) } return conn } func ackExchange(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) } var reply protocol.Message if err := wsjson.Read(ctx, conn, &reply); err != nil { t.Fatal(err) } return reply } func TestAttentionAcksSyncAcrossConnections(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() store := newOperatorStore(t) if _, err := store.Submit(ctx, "acks", pipeline.Template([]string{"one"}, true)); err != nil { t.Fatal(err) } items, err := store.List(ctx) if err != nil { t.Fatal(err) } definition, err := store.Get(ctx, items[0].ID, 1) if err != nil { t.Fatal(err) } run, _, err := store.CreateRun(ctx, definition, "ack-run", t.TempDir(), false, nil, map[string]any{"prompt": "task"}) if err != nil { t.Fatal(err) } if err := store.RecordOperatorSession(ctx, "ses_sync", "/work", "Sync", "operator"); err != nil { t.Fatal(err) } handlerCtx, stop := context.WithCancel(ctx) handler := Handler(handlerCtx, store) defer func() { stop(); WaitRuns(handler) }() srv := httptest.NewServer(handler) defer srv.Close() web := dialAckClient(t, ctx, srv.URL) phone := dialAckClient(t, ctx, srv.URL) initial := ackExchange(t, ctx, phone, protocol.Message{Type: protocol.GetAttentionAcks, ID: "get"}) if initial.Type != protocol.AttentionAcksReply || initial.ID != "get" || initial.AttentionAcks == nil || !initial.AttentionAcks.Empty() { t.Fatalf("initial = %+v", initial) } key := fmt.Sprintf("%d:Failed", run) reply := ackExchange(t, ctx, web, protocol.Message{Type: protocol.AckAttention, ID: "ack", AttentionAcks: &protocol.AttentionAcks{ Dismissed: []string{key}, ChatSeen: map[string]int64{"ses_sync": 1}, }}) if reply.Type != protocol.AttentionAcksReply || reply.ID != "ack" || len(reply.AttentionAcks.Dismissed) != 1 || reply.AttentionAcks.ChatSeen["ses_sync"] == 0 { t.Fatalf("ack reply = %+v", reply) } var pushed protocol.Message if err := wsjson.Read(ctx, phone, &pushed); err != nil { t.Fatal(err) } if pushed.Type != protocol.AttentionAcksChanged || pushed.ID != "" || pushed.AttentionAcks == nil || len(pushed.AttentionAcks.Dismissed) != 1 || pushed.AttentionAcks.Dismissed[0] != key || pushed.AttentionAcks.ChatSeen["ses_sync"] != reply.AttentionAcks.ChatSeen["ses_sync"] { t.Fatalf("pushed = %+v", pushed) } // Malformed acks are refused and push nothing: the next frame the phone // reads is the reply to its own request. for _, bad := range []*protocol.AttentionAcks{nil, {Dismissed: []string{"1:Running"}}, {ChatSeen: map[string]int64{"bad id": 3}}} { refused := ackExchange(t, ctx, web, protocol.Message{Type: protocol.AckAttention, ID: "bad", AttentionAcks: bad}) if refused.Type != protocol.Error || refused.Error == "" || refused.AttentionAcks != nil { t.Fatalf("malformed ack accepted: %+v", refused) } } // A repeated ack changes nothing and pushes nothing. ackExchange(t, ctx, web, protocol.Message{Type: protocol.AckAttention, ID: "again", AttentionAcks: &protocol.AttentionAcks{Dismissed: []string{key}}}) after := ackExchange(t, ctx, phone, protocol.Message{Type: protocol.GetAttentionAcks, ID: "after"}) if after.Type != protocol.AttentionAcksReply || after.ID != "after" || len(after.AttentionAcks.Dismissed) != 1 { t.Fatalf("phone read after malformed/repeated acks = %+v", after) } }