New test packages: - config/loader_test.go: 5 tests (valid, minimal, missing file, pulsar-only, defaults) - serial/serial_test.go: 6 tests (interface, read, EOF, close, empty) - app/app_test.go: 5 tests (New, dispatch store/send, error handling, ctx cancellation) Enhanced test packages: - storage/store_test.go: +3 tests (concurrent insert, close/reopen, init clears) - telegram/parser_test.go: +5 tests (concurrency, rawLog, empty, multiline, removeEmpty) - transport/transport_test.go: +7 tests, +fixes Fixes: - app.go: store field type *storage.Store -> storage.Repository (supports mocking) - transport_test: fix net.Pipe() sync deadlock in SetConnClosesPrevious - transport_test: fix MultiSender_ErrorPropagation expectations Results: 6 packages, 30+ tests, all pass with -race
200 lines
4.3 KiB
Go
200 lines
4.3 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func writeTempConfig(t *testing.T, content string) string {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
path := filepath.Join(dir, "telegram.yaml")
|
|
if err := os.WriteFile(path, []byte(content), 0644); err != nil {
|
|
t.Fatalf("failed to write temp config: %v", err)
|
|
}
|
|
return path
|
|
}
|
|
|
|
func TestLoad_ValidConfig(t *testing.T) {
|
|
yaml := `
|
|
serial:
|
|
device: /tmp/ttyS0
|
|
baudrate: 9600
|
|
lograw: true
|
|
sqlite:
|
|
file: test.db
|
|
init: false
|
|
socket:
|
|
address: 127.0.0.1:6000
|
|
pulsar:
|
|
url: pulsar://localhost:6650
|
|
topic: telegrams
|
|
name: reader
|
|
telegram:
|
|
tcp: true
|
|
pulsar: true
|
|
`
|
|
path := writeTempConfig(t, yaml)
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load() failed: %v", err)
|
|
}
|
|
|
|
if cfg.Serial.Device != "/tmp/ttyS0" {
|
|
t.Errorf("expected device /tmp/ttyS0, got %s", cfg.Serial.Device)
|
|
}
|
|
if cfg.Serial.Baudrate != 9600 {
|
|
t.Errorf("expected baudrate 9600, got %d", cfg.Serial.Baudrate)
|
|
}
|
|
if !cfg.Serial.LogRaw {
|
|
t.Error("expected lograw=true")
|
|
}
|
|
if cfg.SQLite.File != "test.db" {
|
|
t.Errorf("expected sqlite file test.db, got %s", cfg.SQLite.File)
|
|
}
|
|
if cfg.SQLite.Init {
|
|
t.Error("expected sqlite init=false")
|
|
}
|
|
if cfg.Socket.Address != "127.0.0.1:6000" {
|
|
t.Errorf("expected socket address 127.0.0.1:6000, got %s", cfg.Socket.Address)
|
|
}
|
|
if cfg.Pulsar.URL != "pulsar://localhost:6650" {
|
|
t.Errorf("expected pulsar url, got %s", cfg.Pulsar.URL)
|
|
}
|
|
if cfg.Pulsar.Topic != "telegrams" {
|
|
t.Errorf("expected pulsar topic telegrams, got %s", cfg.Pulsar.Topic)
|
|
}
|
|
if cfg.Pulsar.Name != "reader" {
|
|
t.Errorf("expected pulsar name reader, got %s", cfg.Pulsar.Name)
|
|
}
|
|
if !cfg.Telegram.TCP {
|
|
t.Error("expected telegram tcp=true")
|
|
}
|
|
if !cfg.Telegram.Pulsar {
|
|
t.Error("expected telegram pulsar=true")
|
|
}
|
|
}
|
|
|
|
func TestLoad_MinimalConfig(t *testing.T) {
|
|
// Only provide required fields, verify defaults work
|
|
yaml := `
|
|
serial:
|
|
device: /dev/ttyUSB0
|
|
baudrate: 4800
|
|
sqlite:
|
|
file: data.db
|
|
socket:
|
|
address: :7000
|
|
pulsar:
|
|
url: pulsar://host:6650
|
|
topic: raw
|
|
name: test
|
|
`
|
|
path := writeTempConfig(t, yaml)
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load() failed: %v", err)
|
|
}
|
|
|
|
if cfg.Serial.Device != "/dev/ttyUSB0" {
|
|
t.Errorf("expected device, got %s", cfg.Serial.Device)
|
|
}
|
|
if cfg.Serial.Baudrate != 4800 {
|
|
t.Errorf("expected baudrate 4800, got %d", cfg.Serial.Baudrate)
|
|
}
|
|
if cfg.Serial.LogRaw {
|
|
t.Error("expected lograw default false")
|
|
}
|
|
if cfg.Telegram.TCP {
|
|
t.Error("expected tcp default false")
|
|
}
|
|
if cfg.Telegram.Pulsar {
|
|
t.Error("expected pulsar default false")
|
|
}
|
|
// Log defaults
|
|
if cfg.Log.Dir != "./logs" {
|
|
t.Errorf("expected log dir ./logs, got %s", cfg.Log.Dir)
|
|
}
|
|
if cfg.Log.MaxAge != 60 {
|
|
t.Errorf("expected max age 60, got %d", cfg.Log.MaxAge)
|
|
}
|
|
}
|
|
|
|
func TestLoad_MissingFile(t *testing.T) {
|
|
cfg, err := Load("/nonexistent/path/config.yaml")
|
|
if err == nil {
|
|
t.Fatal("expected error for missing file, got nil")
|
|
}
|
|
if cfg != nil {
|
|
t.Fatal("expected nil config on error")
|
|
}
|
|
}
|
|
|
|
func TestLoad_PulsarOnlyConfig(t *testing.T) {
|
|
// Verify the exact config from telegram.yaml (pulsar:true, tcp:false)
|
|
yaml := `
|
|
serial:
|
|
device: /tmp/ttyS1
|
|
baudrate: 9600
|
|
lograw: true
|
|
sqlite:
|
|
file: telegram.db
|
|
init: true
|
|
socket:
|
|
address: 127.0.0.1:6000
|
|
pulsar:
|
|
url: pulsar://yzjc.gzzn.dev:6650
|
|
topic: telegram-raw
|
|
name: serial-reader
|
|
telegram:
|
|
tcp: false
|
|
pulsar: true
|
|
`
|
|
path := writeTempConfig(t, yaml)
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load() failed: %v", err)
|
|
}
|
|
|
|
if cfg.Telegram.TCP {
|
|
t.Error("expected tcp=false")
|
|
}
|
|
if !cfg.Telegram.Pulsar {
|
|
t.Error("expected pulsar=true")
|
|
}
|
|
if cfg.SQLite.File != "telegram.db" {
|
|
t.Errorf("expected db file telegram.db, got %s", cfg.SQLite.File)
|
|
}
|
|
}
|
|
|
|
func TestLoad_DefaultLogValues(t *testing.T) {
|
|
yaml := `
|
|
serial:
|
|
device: /tmp/ttyS0
|
|
baudrate: 9600
|
|
sqlite:
|
|
file: test.db
|
|
socket:
|
|
address: :6000
|
|
pulsar:
|
|
url: pulsar://localhost:6650
|
|
topic: t
|
|
name: n
|
|
`
|
|
path := writeTempConfig(t, yaml)
|
|
cfg, err := Load(path)
|
|
if err != nil {
|
|
t.Fatalf("Load() failed: %v", err)
|
|
}
|
|
|
|
if cfg.Log.Dir != "./logs" {
|
|
t.Errorf("expected log dir ./logs, got %q", cfg.Log.Dir)
|
|
}
|
|
if cfg.Log.MaxAge != 60 {
|
|
t.Errorf("expected log max age 60, got %d", cfg.Log.MaxAge)
|
|
}
|
|
if cfg.Log.RotateHour != 1 {
|
|
t.Errorf("expected log rotate hour 1, got %d", cfg.Log.RotateHour)
|
|
}
|
|
} |