The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
package dashboard
import (
"path/filepath"
"testing"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
)
func TestReadEventsAppendOrderFallsBackToLineOrderWhenUnsequenced(t *testing.T) {
// Events written before sequence stamping still have to come back in append order.
tie := "2026-05-13T01:00:00Z"
earlier := testSchemaEvent(tie, "claude_code", "metric.observed", "metric", "")
earlier.Message = "line-1"
later := testSchemaEvent(tie, "claude_code", "metric.observed", "metric", "")
later.Message = "line-2"
path := filepath.Join(t.TempDir(), "runtime.jsonl")
writeTestLog(t, path, marshalEvents(t, earlier, later)...)
events, err := ReadEventsAppendOrder(path, EventQuery{})
if err != nil {
t.Fatalf("ReadEventsAppendOrder returned error: %v", err)
}
if len(events) != 2 {
t.Fatalf("read %d events, want 2", len(events))
}
if events[0].Message != "line-1" || events[1].Message != "line-2" {
t.Fatalf("order = %q, %q; want line-1, line-2", events[0].Message, events[1].Message)
}
}
func TestReadEventsAppendOrderTransitiveWithMixedSequences(t *testing.T) {
// Three events at the same timestamp mixing sequenced and unsequenced must sort
// transitively: unsequenced events come before sequenced ones (in line order among
// themselves), and sequenced events sort by sequence among themselves.
tie := "2026-05-13T01:00:00.000000000Z"
highSeq := testSchemaEvent(tie, "claude_code", "metric.observed", "metric", "")
highSeq.Sequence = 10
highSeq.Message = "high-seq"
legacy := testSchemaEvent(tie, "claude_code", "metric.observed", "metric", "")
legacy.Message = "legacy"
lowSeq := testSchemaEvent(tie, "claude_code", "metric.observed", "metric", "")
lowSeq.Sequence = 5
lowSeq.Message = "low-seq"
path := filepath.Join(t.TempDir(), "runtime.jsonl")
writeTestLog(t, path, marshalEvents(t, highSeq, legacy, lowSeq)...)
events, err := ReadEventsAppendOrder(path, EventQuery{})
if err != nil {
t.Fatalf("ReadEventsAppendOrder returned error: %v", err)
}
if len(events) != 3 {
t.Fatalf("read %d events, want 3", len(events))
}
if events[0].Message != "legacy" || events[1].Message != "low-seq" || events[2].Message != "high-seq" {
t.Fatalf("order = %q, %q, %q; want legacy, low-seq, high-seq",
events[0].Message, events[1].Message, events[2].Message)
}
}
func TestBuildSummaryPicksLastEventByInstantNotByString(t *testing.T) {
// A log spanning the move to nanosecond stamping: ":05Z" is the older event but sorts
// after ":05.500000000Z" byte-wise, because 'Z' outranks '.'.
older := testSchemaEvent("2026-05-13T01:00:05Z", "cursor", "tool.started", "tool", "")
newer := testSchemaEvent("2026-05-13T01:00:05.500000000Z", "cursor", "tool.completed", "tool", "")
summary := BuildSummary(EventResult{
TotalMatched: 2,
Events: []EventRecord{{Event: older}, {Event: newer}},
})
if summary.LastEventTime != newer.Timestamp {
t.Fatalf("LastEventTime = %q, want %q", summary.LastEventTime, newer.Timestamp)
}
}
func TestBuildSummarySkipsUnparseableTimestamps(t *testing.T) {
real := testSchemaEvent("2026-05-13T01:00:05.000000000Z", "cursor", "tool.started", "tool", "")
broken := testSchemaEvent("whenever", "cursor", "tool.completed", "tool", "")
summary := BuildSummary(EventResult{
TotalMatched: 2,
Events: []EventRecord{{Event: real}, {Event: broken}},
})
if summary.LastEventTime != real.Timestamp {
t.Fatalf("LastEventTime = %q, want %q", summary.LastEventTime, real.Timestamp)
}
}
func TestStreamEventsCarriesSequenceThrough(t *testing.T) {
// Detection reads the log through StreamEvents, so the ordering key has to survive
// the round trip to be of any use to the sort in front of the rules engine.
event := testSchemaEvent("2026-05-13T01:00:00.000000000Z", "claude_code", "command.executed", "command", "")
event.Sequence = 7
path := filepath.Join(t.TempDir(), "runtime.jsonl")
writeTestLog(t, path, marshalEvents(t, event)...)
var got []schema.Event
if err := StreamEvents(path, func(e schema.Event) error {
got = append(got, e)
return nil
}); err != nil {
t.Fatalf("StreamEvents returned error: %v", err)
}
if len(got) != 1 {
t.Fatalf("streamed %d events, want 1", len(got))
}
if got[0].Sequence != 7 {
t.Fatalf("sequence = %d, want 7", got[0].Sequence)
}
}