56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package logweb
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestLogFilePath_rejectsInvalid(t *testing.T) {
|
|
dir := t.TempDir()
|
|
for _, tc := range []struct{ kind, date string }{
|
|
{"evil", "2026-05-22"},
|
|
{"app", "2026/05/22"},
|
|
{"app", "../../../etc/passwd"},
|
|
} {
|
|
if _, err := logFilePath(dir, tc.kind, tc.date); err == nil {
|
|
t.Fatalf("expected error for %v", tc)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseLogLines(t *testing.T) {
|
|
raw := []string{
|
|
"======== BEGIN sync ========",
|
|
"[pull] 2026/05/22 08:28:44 batch created step=consult batch_id=1",
|
|
"[push] 2026/05/22 08:28:46 ok biz_key=x code=200",
|
|
"plain line fail something",
|
|
}
|
|
lines := ParseLogLines(raw)
|
|
if !lines[0].IsSeparator {
|
|
t.Fatal("sep")
|
|
}
|
|
if lines[1].Tag != "pull" || lines[1].Time == "" {
|
|
t.Fatalf("pull line: %+v", lines[1])
|
|
}
|
|
if lines[2].Level != "ok" {
|
|
t.Fatalf("ok level: %s", lines[2].Level)
|
|
}
|
|
if lines[3].Level != "error" {
|
|
t.Fatalf("fail level: %s", lines[3].Level)
|
|
}
|
|
}
|
|
|
|
func TestListLogDates(t *testing.T) {
|
|
dir := t.TempDir()
|
|
_ = os.WriteFile(filepath.Join(dir, "app-2026-05-20.log"), []byte("x"), 0o644)
|
|
_ = os.WriteFile(filepath.Join(dir, "app-2026-05-22.log"), []byte("x"), 0o644)
|
|
dates, err := ListLogDates(dir, "app")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(dates) != 2 || dates[0] != "2026-05-22" {
|
|
t.Fatalf("dates=%v", dates)
|
|
}
|
|
}
|