Files
William Valentin 0e2734be23 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>
2026-01-17 01:55:24 -08:00

82 lines
1.7 KiB
Go

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")
}
}