A community based topic aggregation platform built on atproto
1package validation
2
3import (
4 "testing"
5)
6
7func TestNewLexiconValidator(t *testing.T) {
8 // Test creating validator with valid schema path
9 validator, err := NewLexiconValidator("../../internal/atproto/lexicon", false)
10 if err != nil {
11 t.Fatalf("Failed to create validator: %v", err)
12 }
13 if validator == nil {
14 t.Fatal("Expected validator to be non-nil")
15 }
16
17 // Test creating validator with invalid schema path
18 _, err = NewLexiconValidator("/nonexistent/path", false)
19 if err == nil {
20 t.Error("Expected error when creating validator with invalid path")
21 }
22}
23
24func TestValidateActorProfile(t *testing.T) {
25 validator, err := NewLexiconValidator("../../internal/atproto/lexicon", false)
26 if err != nil {
27 t.Fatalf("Failed to create validator: %v", err)
28 }
29
30 // Valid profile
31 validProfile := map[string]interface{}{
32 "$type": "social.coves.actor.profile",
33 "handle": "test.example.com",
34 "displayName": "Test User",
35 "createdAt": "2024-01-01T00:00:00Z",
36 }
37
38 if err := validator.ValidateActorProfile(validProfile); err != nil {
39 t.Errorf("Valid profile failed validation: %v", err)
40 }
41
42 // Invalid profile - missing required field
43 invalidProfile := map[string]interface{}{
44 "$type": "social.coves.actor.profile",
45 "displayName": "Test User",
46 }
47
48 if err := validator.ValidateActorProfile(invalidProfile); err == nil {
49 t.Error("Invalid profile passed validation when it should have failed")
50 }
51}
52
53func TestValidatePost(t *testing.T) {
54 validator, err := NewLexiconValidator("../../internal/atproto/lexicon", false)
55 if err != nil {
56 t.Fatalf("Failed to create validator: %v", err)
57 }
58
59 // Valid post
60 validPost := map[string]interface{}{
61 "$type": "social.coves.post.record",
62 "community": "did:plc:test123",
63 "author": "did:plc:author123",
64 "title": "Test Post",
65 "content": "This is a test",
66 "createdAt": "2024-01-01T00:00:00Z",
67 }
68
69 if err := validator.ValidatePost(validPost); err != nil {
70 t.Errorf("Valid post failed validation: %v", err)
71 }
72
73 // Invalid post - missing required field (author)
74 invalidPost := map[string]interface{}{
75 "$type": "social.coves.post.record",
76 "community": "did:plc:test123",
77 // Missing required "author" field
78 "title": "Test Post",
79 "content": "This is a test",
80 "createdAt": "2024-01-01T00:00:00Z",
81 }
82
83 if err := validator.ValidatePost(invalidPost); err == nil {
84 t.Error("Invalid post passed validation when it should have failed")
85 }
86}
87
88func TestValidateRecordWithDifferentInputTypes(t *testing.T) {
89 validator, err := NewLexiconValidator("../../internal/atproto/lexicon", false)
90 if err != nil {
91 t.Fatalf("Failed to create validator: %v", err)
92 }
93
94 // Test with JSON string
95 jsonString := `{
96 "$type": "social.coves.interaction.vote",
97 "subject": "at://did:plc:test/social.coves.post.text/abc123",
98 "createdAt": "2024-01-01T00:00:00Z"
99 }`
100
101 if err := validator.ValidateRecord(jsonString, "social.coves.interaction.vote"); err != nil {
102 t.Errorf("Failed to validate JSON string: %v", err)
103 }
104
105 // Test with JSON bytes
106 jsonBytes := []byte(jsonString)
107 if err := validator.ValidateRecord(jsonBytes, "social.coves.interaction.vote"); err != nil {
108 t.Errorf("Failed to validate JSON bytes: %v", err)
109 }
110}
111
112func TestStrictValidation(t *testing.T) {
113 // Create validator with strict mode
114 validator, err := NewLexiconValidator("../../internal/atproto/lexicon", true)
115 if err != nil {
116 t.Fatalf("Failed to create validator: %v", err)
117 }
118
119 // Profile with datetime missing timezone (should fail in strict mode)
120 profile := map[string]interface{}{
121 "$type": "social.coves.actor.profile",
122 "handle": "test.example.com",
123 "createdAt": "2024-01-01T00:00:00", // Missing Z
124 }
125
126 if err := validator.ValidateActorProfile(profile); err == nil {
127 t.Error("Expected strict validation to fail on datetime without timezone")
128 }
129}