package server import ( "context" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" ) func TestCompactionRetainedReplayIsolationAndTerminalClear(t *testing.T) { h := newChatHub() room := newChatRoom(context.Background(), "ses_compact") defer room.cancel() room.running, room.nativeTurnID = true, "turn_new" room.turnDone = make(chan struct{}) h.rooms[room.sessionID] = room sub := &chatSub{hub: h, room: room, clientID: "watcher", ch: make(chan protocol.Message, 32), done: make(chan struct{})} room.subs[sub.clientID] = sub emit := func(id, status, nativeID string) { h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, NativeTurnID: nativeID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{ID: id, Status: status}}) } emit("old", "running", "turn_old") if len(sub.ch) != 0 { t.Fatal("old native turn surfaced") } emit("first", "running", "turn_new") <-sub.ch if got := h.snapshot(room.sessionID); len(got) != 0 { t.Fatalf("fake message: %+v", got) } h.replayCompaction(sub) if f := (<-sub.ch).ChatEvent; f == nil || f.Compaction.ID != "first" || f.MessageID != "" { t.Fatalf("replay without assistant: %+v", f) } other := newChatRoom(context.Background(), "ses_other") defer other.cancel() h.rooms[other.sessionID] = other otherSub := &chatSub{hub: h, room: other, clientID: "other", ch: make(chan protocol.Message, 4), done: make(chan struct{})} other.subs[otherSub.clientID] = otherSub h.replayCompaction(otherSub) if len(otherSub.ch) != 0 { t.Fatal("foreign-session replay") } resetOld := h.compactionReset(room.sessionID) emit("second", "running", "turn_new") <-sub.ch resetOld() emit("first", "completed", "turn_new") if len(sub.ch) != 0 || h.turns[room.sessionID].compaction.ID != "second" { t.Fatal("old lifecycle cleared newer compaction") } h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, Intermediate: true}}) <-sub.ch if h.turns[room.sessionID].compaction == nil || !room.running { t.Fatal("provider step settled compaction or turn") } emit("second", "completed", "turn_new") <-sub.ch if !room.running { t.Fatal("compaction completion released turn queue") } emit("third", "running", "turn_new") <-sub.ch h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventStep, Step: &protocol.ChatStep{Status: protocol.ChatStepEnded, TurnID: "turn_new"}}) if f := (<-sub.ch).ChatEvent; f.Compaction == nil || f.Compaction.Status != "failed" { t.Fatalf("terminal cleanup: %+v", f) } <-sub.ch // authoritative step completion if room.running || h.turns[room.sessionID].compaction != nil { t.Fatal("terminal state stuck") } emit("third", "running", "turn_new") if len(sub.ch) != 0 { t.Fatal("late old-turn activity revived label") } } func TestCompactionTransportLossAndInterruptClearDoNotReleaseTurn(t *testing.T) { for _, mode := range []string{"transport", "interrupt", "detach"} { t.Run(mode, func(t *testing.T) { h := newChatHub() room := newChatRoom(context.Background(), "ses_compact") defer room.cancel() h.rooms[room.sessionID] = room room.running = true sub := &chatSub{hub: h, room: room, clientID: "watcher", ch: make(chan protocol.Message, 8), done: make(chan struct{})} room.subs[sub.clientID] = sub h.emitRoom(room, protocol.ChatEventFrame{SessionID: room.sessionID, Kind: protocol.ChatEventCompaction, Compaction: &protocol.ChatCompaction{Status: "running", ID: "compact"}}) <-sub.ch switch mode { case "interrupt": h.compactionReset(room.sessionID)() case "detach": h.detach(room.sessionID, sub) case "transport": result := make(chan error, 1) result <- nil // Cancellation prevents any reconnect I/O; drain still clears retained // activity on the exact room whose stream ended. room.cancel() h.drainStream(room, result) } if h.turns[room.sessionID].compaction != nil || !room.running { t.Fatal("cleanup stuck label or released live turn") } }) } }