package claude import ( "context" "fmt" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" ) // An oversized session file used to refuse the whole history. Now only its // newest maxTranscriptBytes are read, starting at a whole line, and the page // opens with a notice that earlier turns are left out. func TestHistoryOfAnOversizedTranscriptShowsItsNewestTurns(t *testing.T) { const id = "ses_oversized" var entries []map[string]any for i := 0; i < 40; i++ { entries = append(entries, map[string]any{ "type": "assistant", "uuid": fmt.Sprintf("a-%d", i), "timestamp": fmt.Sprintf("2026-11-30T10:%02d:00Z", i), "message": map[string]any{ "id": fmt.Sprintf("msg_%d", i), "role": "assistant", "content": textBlocks(fmt.Sprintf("answer %d %s", i, strings.Repeat("x", 200))), }, }) } writeTranscript(t, id, entries) defer func(limit int64) { maxTranscriptBytes = limit }(maxTranscriptBytes) maxTranscriptBytes = 2 << 10 chat := New(context.Background()) s := chat.sessionFor(id, t.TempDir()) page, err := s.transcriptMessages(200, "") if err != nil { t.Fatalf("oversized transcript refused: %v", err) } if len(page.Messages) < 3 { t.Fatalf("got %d messages", len(page.Messages)) } first := page.Messages[0] if first.Kind != protocol.ChatMessageKindNotice || !strings.Contains(first.Parts[0].Text, "Earlier history is not shown") { t.Fatalf("first message = %+v", first) } last := page.Messages[len(page.Messages)-1] if !strings.HasPrefix(last.Parts[0].Text, "answer 39 ") { t.Fatalf("newest turn missing: %q", last.Parts[0].Text[:20]) } for _, message := range page.Messages[1:] { if strings.HasPrefix(message.Parts[0].Text, "answer 0 ") { t.Fatal("the oldest turn was read past the bound") } } } // A file within the bound is read whole, with no notice. func TestHistoryOfASmallTranscriptHasNoNotice(t *testing.T) { const id = "ses_small" writeTranscript(t, id, []map[string]any{ {"type": "assistant", "uuid": "a-1", "message": map[string]any{"id": "msg_1", "role": "assistant", "content": textBlocks("hello")}}, }) chat := New(context.Background()) page, err := chat.sessionFor(id, t.TempDir()).transcriptMessages(50, "") if err != nil || len(page.Messages) != 1 || page.Messages[0].Kind != "" { t.Fatalf("page = %+v err=%v", page.Messages, err) } }