feat: add event validation

Validates required envelope fields: schema, event.id, event.type,
event.ts, and event.source (framework, client_id, host).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
William Valentin
2026-01-17 01:55:24 -08:00
parent 4456997216
commit 0e2734be23
2 changed files with 166 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
package event
import (
"encoding/json"
"testing"
)
func TestValidate_ValidEvent(t *testing.T) {
raw := `{
"schema": {"name": "agentmon.event", "version": 1},
"event": {
"id": "e1",
"type": "run.start",
"ts": "2026-01-17T00:00:00Z",
"source": {"framework": "claude-code", "client_id": "c1", "host": "myhost"}
}
}`
var m map[string]any
_ = json.Unmarshal([]byte(raw), &m)
err := Validate(m)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
}
func TestValidate_MissingEventID(t *testing.T) {
raw := `{
"schema": {"name": "agentmon.event", "version": 1},
"event": {
"type": "run.start",
"ts": "2026-01-17T00:00:00Z",
"source": {"framework": "claude-code", "client_id": "c1", "host": "myhost"}
}
}`
var m map[string]any
_ = json.Unmarshal([]byte(raw), &m)
err := Validate(m)
if err == nil {
t.Fatal("expected error for missing event.id")
}
}
func TestValidate_InvalidType(t *testing.T) {
raw := `{
"schema": {"name": "agentmon.event", "version": 1},
"event": {
"id": "e1",
"type": "invalid.type",
"ts": "2026-01-17T00:00:00Z",
"source": {"framework": "claude-code", "client_id": "c1", "host": "myhost"}
}
}`
var m map[string]any
_ = json.Unmarshal([]byte(raw), &m)
err := Validate(m)
if err == nil {
t.Fatal("expected error for invalid type")
}
}
func TestValidate_WrongSchemaName(t *testing.T) {
raw := `{
"schema": {"name": "wrong.schema", "version": 1},
"event": {
"id": "e1",
"type": "run.start",
"ts": "2026-01-17T00:00:00Z",
"source": {"framework": "claude-code", "client_id": "c1", "host": "myhost"}
}
}`
var m map[string]any
_ = json.Unmarshal([]byte(raw), &m)
err := Validate(m)
if err == nil {
t.Fatal("expected error for wrong schema name")
}
}