Files
2026-10-27 00:40:49 -04:00

84 lines
2.6 KiB
Go

// Copyright (c) 2026 Storm Dragon
// Authors: Storm Dragon, Angus Kola
// SPDX-License-Identifier: GPL-3.0-or-later
package messaging
import (
"fmt"
"strings"
"testing"
)
func TestHistoryOrdersConversationsAndIsolatesApplicationInstances(t *testing.T) {
var history History
for i := 0; i < 12; i++ {
history.Add(Message{Provider: ":1.42", Conversation: fmt.Sprintf("/room%d", i%2), Window: "/window", Text: fmt.Sprintf("Message %d", i)})
}
history.Add(Message{Provider: ":1.43", Conversation: "/room0", Window: "/window", Text: "Other instance"})
for i := 0; i < HistorySize; i++ {
message, ok := history.Previous(":1.42", i)
if !ok || message.Text != fmt.Sprintf("Message %d", 11-i) {
t.Fatalf("slot %d = %+v, %t", i, message, ok)
}
}
for _, index := range []int{-1, 9} {
if _, ok := history.Previous(":1.42", index); ok {
t.Fatal("out-of-range slot exists")
}
}
if _, ok := history.Previous(":1.44", 0); ok {
t.Fatal("restarted provider inherited old history")
}
// Identical messages from real insertions are legitimate separate entries.
for range 2 {
history.Add(Message{Provider: ":1.42", Conversation: "/room0", Window: "/window", Text: "Repeated"})
}
for i := range 2 {
message, _ := history.Previous(":1.42", i)
if message.Text != "Repeated" {
t.Fatal("identical messages collapsed")
}
}
}
func TestHistoryRetirementTypingAndBounds(t *testing.T) {
var history History
message := Message{Provider: ":1.42", Conversation: "/one", Window: "/window1", Text: "First"}
history.Add(message)
message.Conversation, message.Window, message.Text = "/two", "/window2", "Second"
history.Add(message)
message.Typing = true
if history.Add(message) {
t.Fatal("typing entered history")
}
history.Retire(":1.42", "/window1")
if got, _ := history.Previous(":1.42", 0); got.Text != "Second" {
t.Fatal("unrelated window was retired")
}
if _, ok := history.Previous(":1.42", 1); ok {
t.Fatal("destroyed window remains")
}
history.Retire(":1.42", "/two")
if _, ok := history.Previous(":1.42", 0); ok {
t.Fatal("destroyed transcript remains")
}
message.Typing = false
message.Text = "\n" + strings.Repeat("界", 5000) + "\n"
history.Add(message)
if got, _ := history.Previous(":1.42", 0); len([]rune(got.Text)) != 4096 {
t.Fatal("Unicode message is not bounded")
}
for i := 0; i < 64; i++ {
message.Provider = fmt.Sprintf(":1.%d", i+100)
history.Add(message)
}
if len(history.providers) != maxProviders {
t.Fatal("provider count is not bounded")
}
history.Reset()
if _, ok := history.Previous(message.Provider, 0); ok {
t.Fatal("lock retained messages")
}
}