package server import ( "context" "strings" "testing" "time" "github.com/coder/websocket/wsjson" "git.a-wan.net/alan/pipelines/internal/claude" "git.a-wan.net/alan/pipelines/internal/codex" "git.a-wan.net/alan/pipelines/internal/protocol" ) func chatNewSessionRequest(id, session string) protocol.Message { return protocol.Message{Type: protocol.ChatNewSession, ID: id, ChatNewSession: &protocol.ChatNewSessionRequest{SessionID: session}} } // TestChatNewSessionKeepsTheChatOnEveryHarness is the /new evidence for all // three harnesses: the same chat (title, colour, star, directory, backend, // mode) moves to a fresh native session of its own harness, the reply and the // other devices' push name the replaced id, the list shows the one chat under // its new id, and the resume command names the new native session while the // old row still resolves. func TestChatNewSessionKeepsTheChatOnEveryHarness(t *testing.T) { cases := []struct { backend, oldID, newID string }{ {"opencode", "ses_old", "ses_fresh"}, {codex.Backend, "11111111-1111-4111-8111-111111111111", "22222222-2222-4222-8222-222222222222"}, {claude.Backend, "33333333-3333-4333-8333-333333333333", "44444444-4444-4444-8444-444444444444"}, } for _, tc := range cases { t.Run(tc.backend, func(t *testing.T) { store := newOperatorStore(t) open, codexFake, claudeFake := scriptedChat(), scriptedChat(), scriptedChat() owner := map[string]*fakeChat{"opencode": open, codex.Backend: codexFake, claude.Backend: claudeFake}[tc.backend] owner.created = protocol.ChatSession{ID: tc.newID, Title: "Feature"} owner.sessionFound = true for _, fake := range []*fakeChat{open, codexFake, claudeFake} { fake.sessions = []protocol.ChatSession{{ID: tc.oldID}, {ID: tc.newID}} } manager := newChatManager(context.Background(), open, store, (&fakeHarness{available: true, client: codexFake}).probe(), (&fakeHarness{available: true, client: claudeFake}).probe(), time.Now) ctx, _, conns := dialChatConns(t, Options{Chat: manager, OperatorRegistry: store}, 2) if err := store.RecordOperatorSession(ctx, tc.oldID, "/work/a", "Feature", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } if err := store.SetOperatorSessionBackend(ctx, tc.oldID, tc.backend); err != nil { t.Fatal(err) } if err := store.SetOperatorSessionColor(ctx, tc.oldID, "teal"); err != nil { t.Fatal(err) } if err := store.SetOperatorSessionStar(ctx, tc.oldID, true); err != nil { t.Fatal(err) } if err := store.SetChatMode(ctx, tc.oldID, "plan"); err != nil { t.Fatal(err) } if err := wsjson.Write(ctx, conns[1], protocol.Message{Type: protocol.Ping, ID: "p1"}); err != nil { t.Fatal(err) } if pong := readMessage(t, ctx, conns[1]); pong.ID != "p1" { t.Fatalf("ping reply = %+v", pong) } if err := wsjson.Write(ctx, conns[0], chatNewSessionRequest("n1", tc.oldID)); err != nil { t.Fatal(err) } reply := readMessage(t, ctx, conns[0]) got := reply.ChatSession if reply.Type != protocol.ChatNewSession || reply.ID != "n1" || got == nil { t.Fatalf("chat_new_session reply = %+v", reply) } if got.ID != tc.newID || got.PreviousID != tc.oldID || got.Title != "Feature" || got.Directory != "/work/a" || got.Backend != tc.backend || got.Color != "teal" || !got.Starred || got.Closed { t.Fatalf("new session row = %+v", got) } if tc.backend != "opencode" && !strings.Contains(got.ResumeCommand, tc.newID) { t.Fatalf("resume command %q does not name the new session", got.ResumeCommand) } if calls, title, directory := owner.createQuery(); calls != 1 || title != "Feature" || directory != "/work/a" { t.Fatalf("harness create = %d %q %q", calls, title, directory) } pushed := readMessage(t, ctx, conns[1]) if pushed.Type != protocol.ChatSessionChanged || pushed.ChatSession == nil || pushed.ChatSession.ID != tc.newID || pushed.ChatSession.PreviousID != tc.oldID { t.Fatalf("watcher push = %+v", pushed) } if mode := manager.CurrentMode(ctx, tc.newID); mode != "plan" { t.Fatalf("stored mode after /new = %q, want plan", mode) } if err := wsjson.Write(ctx, conns[0], protocol.Message{Type: protocol.ChatSessions, ID: "l1"}); err != nil { t.Fatal(err) } listed := readMessage(t, ctx, conns[0]) if len(listed.ChatSessions) != 1 || listed.ChatSessions[0].ID != tc.newID || listed.ChatSessions[0].PreviousID != "" { t.Fatalf("chat_sessions after /new = %+v", listed.ChatSessions) } if old, found, err := store.OperatorSession(ctx, tc.oldID); err != nil || !found || old.Title != "Feature" { t.Fatalf("old row = %+v found=%v err=%v", old, found, err) } }) } } // TestChatNewSessionRefusesWithoutCreating proves a refused /new never starts // a native session: a foreign id, a running turn, and a second /new on a chat // already moved on. func TestChatNewSessionRefusesWithoutCreating(t *testing.T) { store := newOperatorStore(t) broker := scriptedChat() broker.block = true broker.started = make(chan struct{}) broker.created = protocol.ChatSession{ID: "ses_fresh"} ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store}) if err := store.RecordOperatorSession(ctx, "ses_1", "/work/a", "A", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } if err := wsjson.Write(ctx, conn, chatNewSessionRequest("x", "ses_unknown")); err != nil { t.Fatal(err) } if reply := readMessage(t, ctx, conn); reply.Type != protocol.Error || !strings.Contains(reply.Error, "not an operator chat") { t.Fatalf("foreign reply = %+v", reply) } if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "long"}}); err != nil { t.Fatal(err) } if ack := readMessage(t, ctx, conn); ack.Type != protocol.ChatPrompt { t.Fatalf("prompt ack = %+v", ack) } <-broker.started if err := wsjson.Write(ctx, conn, chatNewSessionRequest("busy", "ses_1")); err != nil { t.Fatal(err) } for { reply := readMessage(t, ctx, conn) if reply.ID != "busy" { continue } if reply.Type != protocol.Error || !strings.Contains(reply.Error, "turn is still running") { t.Fatalf("busy reply = %+v", reply) } break } if calls, _, _ := broker.createQuery(); calls != 0 { t.Fatalf("a refused /new created %d sessions", calls) } } // TestReplaceOperatorSessionMovesEveryField pins the registry re-key: the new // row takes every shared field, the old row is kept but unlisted, and a second // replace of the same chat is refused. func TestReplaceOperatorSessionMovesEveryField(t *testing.T) { store := newOperatorStore(t) ctx := context.Background() if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a", "Title", protocol.ChatSessionTagOperator); err != nil { t.Fatal(err) } _ = store.SetOperatorSessionBackend(ctx, "ses_a", claude.Backend) _ = store.SetOperatorSessionColor(ctx, "ses_a", "red") _ = store.SetOperatorSessionStar(ctx, "ses_a", true) _ = store.CloseOperatorSession(ctx, "ses_a") if err := store.SetChatModelSelection(ctx, "ses_a", "anthropic", "opus"); err != nil { t.Fatal(err) } before, _, _ := store.OperatorSession(ctx, "ses_a") if err := store.ReplaceOperatorSession(ctx, "ses_a", "ses_b"); err != nil { t.Fatal(err) } after, found, err := store.OperatorSession(ctx, "ses_b") if err != nil || !found { t.Fatalf("new row found=%v err=%v", found, err) } if after.Title != "Title" || after.Directory != "/work/a" || after.Backend != claude.Backend || after.Color != "red" || after.StarredAt != before.StarredAt || after.CreatedAt != before.CreatedAt || after.ClosedAt != 0 { t.Fatalf("new row = %+v, old = %+v", after, before) } if provider, model, _ := store.ChatModelSelection(ctx, "ses_b"); provider != "anthropic" || model != "opus" { t.Fatalf("model selection = %q %q", provider, model) } rows, _ := store.OperatorSessions(ctx) if len(rows) != 1 || rows[0].ID != "ses_b" { t.Fatalf("listed rows = %+v", rows) } if _, found, _ := store.OperatorSession(ctx, "ses_a"); !found { t.Fatal("the replaced row was deleted") } if err := store.ReplaceOperatorSession(ctx, "ses_a", "ses_c"); err == nil { t.Fatal("a chat already moved on was replaced again") } if err := store.ReplaceOperatorSession(ctx, "ses_missing", "ses_d"); err == nil { t.Fatal("an unknown chat was replaced") } }