Files

58 lines
1.8 KiB
Go

package alert
import (
"image"
"image/color"
"os"
"path/filepath"
"strings"
"testing"
"time"
"silverpose/v2/internal/fall"
)
func TestDispatchWritesOnePNGAndRedactedJSONL(t *testing.T) {
dispatcher := NewDispatcher(t.TempDir(), "lobby-camera-01")
event := fall.Event{
EventID: "FALL-run-000001", TrackID: "P-0001", ConfigVersion: "cfg-safe",
SuspectedAtMonotonic: 10, ConfirmedAtMonotonic: 11.8, LatencySeconds: 1.8, State: fall.Confirmed,
}
canvas := image.NewRGBA(image.Rect(0, 0, 2, 2))
canvas.SetRGBA(0, 0, color.RGBA{R: 1, G: 2, B: 3, A: 255})
now := time.Date(2026, 7, 22, 8, 0, 0, 0, time.UTC)
first, err := dispatcher.Dispatch(event, canvas, now)
if err != nil || !first.Written {
t.Fatalf("first dispatch = %#v, %v", first, err)
}
second, err := dispatcher.Dispatch(event, canvas, now)
if err != nil || second.Written {
t.Fatalf("duplicate dispatch = %#v, %v", second, err)
}
if extension := filepath.Ext(first.ScreenshotPath); extension != ".png" {
t.Fatalf("screenshot extension = %q", extension)
}
if _, err := os.Stat(first.ScreenshotPath); err != nil {
t.Fatal(err)
}
jsonl, err := os.ReadFile(first.LogPath)
if err != nil {
t.Fatal(err)
}
if lines := strings.Count(strings.TrimSpace(string(jsonl)), "\n") + 1; lines != 1 {
t.Fatalf("JSONL lines = %d: %s", lines, jsonl)
}
if strings.Contains(string(jsonl), "rtsp") || strings.Contains(string(jsonl), "secret") {
t.Fatalf("JSONL leaked source details: %s", jsonl)
}
}
func TestDispatchRejectsNonConfirmedEvent(t *testing.T) {
dispatcher := NewDispatcher(t.TempDir(), "lobby-camera-01")
_, err := dispatcher.Dispatch(fall.Event{EventID: "FALL-run-000001", State: fall.Suspect}, image.NewRGBA(image.Rect(0, 0, 1, 1)), time.Now())
if err == nil {
t.Fatal("expected non-confirmed event error")
}
}