115 lines
3.5 KiB
Go
115 lines
3.5 KiB
Go
package telegram
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestParser_SingleTelegram(t *testing.T) {
|
||
|
|
p := New(nil)
|
||
|
|
telegrams := p.Append("ZCZC TEST MESSAGE NNNN")
|
||
|
|
if len(telegrams) != 1 {
|
||
|
|
t.Fatalf("expected 1 telegram, got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
if !strings.Contains(telegrams[0], "ZCZC TEST MESSAGE NNNN") {
|
||
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParser_MultipleTelegrams(t *testing.T) {
|
||
|
|
p := New(nil)
|
||
|
|
// Simulate two telegrams arriving in one burst
|
||
|
|
telegrams := p.Append("ZCZC MSG1 NNNN ZCZC MSG2 NNNN")
|
||
|
|
if len(telegrams) != 1 {
|
||
|
|
t.Fatalf("expected 1 telegram (non-greedy stops at first NNNN), got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
if !strings.Contains(telegrams[0], "ZCZC MSG1 NNNN") {
|
||
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
||
|
|
}
|
||
|
|
|
||
|
|
// Second telegram should be in buffer
|
||
|
|
telegrams = p.Append("")
|
||
|
|
if len(telegrams) != 1 {
|
||
|
|
t.Fatalf("expected 1 telegram from remaining buffer, got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
if !strings.Contains(telegrams[0], "ZCZC MSG2 NNNN") {
|
||
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParser_NNNNInBufferStart(t *testing.T) {
|
||
|
|
p := New(nil)
|
||
|
|
// Simulate leftover NNNN from previous truncation
|
||
|
|
telegrams := p.Append("NNNN ZCZC TEST NNNN")
|
||
|
|
if len(telegrams) != 1 {
|
||
|
|
t.Fatalf("expected 1 telegram, got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
if !strings.Contains(telegrams[0], "ZCZC TEST NNNN") {
|
||
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParser_NoTelegram(t *testing.T) {
|
||
|
|
p := New(nil)
|
||
|
|
telegrams := p.Append("some random noise without markers")
|
||
|
|
if len(telegrams) != 0 {
|
||
|
|
t.Fatalf("expected 0 telegrams, got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParser_PartialTelegram(t *testing.T) {
|
||
|
|
p := New(nil)
|
||
|
|
// First line: start of telegram
|
||
|
|
telegrams := p.Append("ZCZC PARTIAL")
|
||
|
|
if len(telegrams) != 0 {
|
||
|
|
t.Fatalf("expected 0 telegrams (incomplete), got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
// Second line: completion
|
||
|
|
telegrams = p.Append("CONTINUES HERE NNNN")
|
||
|
|
if len(telegrams) != 1 {
|
||
|
|
t.Fatalf("expected 1 telegram after completion, got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
if !strings.Contains(telegrams[0], "ZCZC PARTIAL") || !strings.Contains(telegrams[0], "CONTINUES HERE NNNN") {
|
||
|
|
t.Errorf("unexpected telegram content: %s", telegrams[0])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParser_BufferOverflow(t *testing.T) {
|
||
|
|
p := New(nil)
|
||
|
|
// Fill buffer beyond maxBufferSize
|
||
|
|
largeData := strings.Repeat("A", maxBufferSize+100)
|
||
|
|
telegrams := p.Append(largeData)
|
||
|
|
if len(telegrams) != 0 {
|
||
|
|
t.Fatalf("expected 0 telegrams from noise, got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
|
||
|
|
// Buffer should have been truncated; add a valid telegram
|
||
|
|
telegrams = p.Append("ZCZC AFTER OVERFLOW NNNN")
|
||
|
|
if len(telegrams) != 1 {
|
||
|
|
t.Fatalf("expected 1 telegram after overflow, got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestParser_NonGreedyMatch(t *testing.T) {
|
||
|
|
p := New(nil)
|
||
|
|
// Two telegrams in same line - non-greedy should capture first only
|
||
|
|
telegrams := p.Append("ZCZC FIRST NNNN ZCZC SECOND NNNN")
|
||
|
|
if len(telegrams) != 1 {
|
||
|
|
t.Fatalf("expected 1 telegram, got %d", len(telegrams))
|
||
|
|
}
|
||
|
|
if !strings.Contains(telegrams[0], "ZCZC FIRST NNNN") {
|
||
|
|
t.Errorf("expected first telegram, got: %s", telegrams[0])
|
||
|
|
}
|
||
|
|
if strings.Contains(telegrams[0], "SECOND") {
|
||
|
|
t.Errorf("non-greedy match should not include second telegram: %s", telegrams[0])
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestRemoveEmpty(t *testing.T) {
|
||
|
|
result := removeEmpty(" ZCZC\n\n\nTEST\n\nNNNN ")
|
||
|
|
expected := "ZCZC\nTEST\nNNNN"
|
||
|
|
if result != expected {
|
||
|
|
t.Errorf("removeEmpty(%q) = %q, want %q", " ZCZC\n\n\nTEST\n\nNNNN ", result, expected)
|
||
|
|
}
|
||
|
|
}
|