package ingest import ( "os" "path/filepath" "testing" ) func writeTempLog(t *testing.T, content string) string { t.Helper() path := filepath.Join(t.TempDir(), "test.log") if err := os.WriteFile(path, []byte(content), 0o644); err != nil { t.Fatal(err) } return path } // INFO lines that merely contain the substring "timeout" must not be counted as // errors (the old substring matcher flagged them) and must not appear in the // content hash. func TestLogIgnoresInfoTimeoutSubstring(t *testing.T) { content := "" + `timestamp=2026-10-01T00:00:00.000Z level=INFO run=aaaa message=evaluated permission=bash pattern="timeout 120 ssh foo"` + "\n" + `timestamp=2026-10-01T00:00:01.000Z level=INFO run=bbbb message=evaluated permission=bash pattern="timeout 5 curl bar"` + "\n" r, err := NewLogIngester().Ingest(writeTempLog(t, content)) if err != nil { t.Fatal(err) } if r.Summary != "no errors" { t.Fatalf("summary=%q, want 'no errors'", r.Summary) } if r.Provenance["error_count"].(int) != 0 { t.Fatalf("error_count=%v, want 0", r.Provenance["error_count"]) } } // The content hash must be stable across volatile differences (times of day, // run ids, session/message ids, line numbers, extra INFO) within the same day, // so an unchanged error set collapses to a no-op. func TestLogContentHashStableWithinDay(t *testing.T) { base := `[] level=ERROR run=aaaa session.id=ses_abc123 messageID=msg_zzz999 error="Not Found"` + "\n" a := `timestamp=2026-10-01T00:00:00.000Z ` + base // Same error, different volatile values, plus unrelated INFO noise. b := `timestamp=2026-10-01T23:59:59.999Z [] level=ERROR run=ffff session.id=ses_def456 messageID=msg_yyy111 error="Not Found"` + "\n" + `timestamp=2026-10-01T23:59:58.000Z level=INFO run=cccc message="resolved path" arg=timeout` + "\n" l := NewLogIngester() ra, err := l.Ingest(writeTempLog(t, a)) if err != nil { t.Fatal(err) } rb, err := l.Ingest(writeTempLog(t, b)) if err != nil { t.Fatal(err) } if ra.ContentHash != rb.ContentHash { t.Fatalf("hash should be stable within a day: %s != %s", ra.ContentHash, rb.ContentHash) } // A genuinely new distinct error changes the hash. c := a + `timestamp=2026-10-01T00:00:05.000Z level=WARN run=dddd message="server unavailable" key=docker status=failed` + "\n" rc, err := l.Ingest(writeTempLog(t, c)) if err != nil { t.Fatal(err) } if rc.ContentHash == ra.ContentHash { t.Fatal("hash should change when a new distinct error appears") } } // A recurring error must move the hash once it recurs on a new day: the digest // carries the dates, so "still happening" is a content change. func TestLogContentHashTracksNewDays(t *testing.T) { base := `[] level=ERROR run=aaaa session.id=ses_abc123 messageID=msg_zzz999 error="Not Found"` + "\n" l := NewLogIngester() day1, err := l.Ingest(writeTempLog(t, `timestamp=2026-10-01T00:00:00.000Z `+base)) if err != nil { t.Fatal(err) } day2, err := l.Ingest(writeTempLog(t, `timestamp=2026-10-02T12:34:56.789Z `+base)) if err != nil { t.Fatal(err) } if day1.ContentHash == day2.ContentHash { t.Fatal("hash should change when the same error recurs on a new day") } } // The digest must say when an error happened. A signature with volatile values // stripped cannot distinguish a fixed failure from a live one. func TestLogDigestKeepsDates(t *testing.T) { line := `[] level=ERROR run=aaaa session.id=ses_abc123 messageID=msg_zzz999 error="Not Found"` content := "" + `timestamp=2026-10-01T00:00:00.000Z ` + line + "\n" + `timestamp=2026-10-02T12:34:56.789Z ` + line + "\n" + `timestamp=2026-10-02T12:35:00.000Z level=INFO run=cccc message="unrelated"` + "\n" r, err := NewLogIngester().Ingest(writeTempLog(t, content)) if err != nil { t.Fatal(err) } want := `2026-10-01..2026-10-02 [] level=ERROR run= session.id= messageID= error="Not Found"` if r.Content != want { t.Fatalf("content=%q, want %q", r.Content, want) } if got, wantSummary := r.Summary, "levels: E2 W0 | err: "+want; got != wantSummary { t.Fatalf("summary=%q, want %q", got, wantSummary) } // A single-day error renders one date, not a range. single, err := NewLogIngester().Ingest(writeTempLog(t, `timestamp=2026-10-03T05:00:00.000Z `+line+"\n")) if err != nil { t.Fatal(err) } if single.Content != `2026-10-03 [] level=ERROR run= session.id= messageID= error="Not Found"` { t.Fatalf("single-day content=%q", single.Content) } }