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 "postType": "text",
64 "title": "Test Post",
65 "text": "This is a test",
66 "tags": []string{"test"},
67 "language": "en",
68 "contentWarnings": []string{},
69 "createdAt": "2024-01-01T00:00:00Z",
70 }
71
72 if err := validator.ValidatePost(validPost); err != nil {
73 t.Errorf("Valid post failed validation: %v", err)
74 }
75
76 // Invalid post - invalid enum value
77 invalidPost := map[string]interface{}{
78 "$type": "social.coves.post.record",
79 "community": "did:plc:test123",
80 "postType": "invalid",
81 "title": "Test Post",
82 "text": "This is a test",
83 "tags": []string{"test"},
84 "language": "en",
85 "contentWarnings": []string{},
86 "createdAt": "2024-01-01T00:00:00Z",
87 }
88
89 if err := validator.ValidatePost(invalidPost); err == nil {
90 t.Error("Invalid post passed validation when it should have failed")
91 }
92}
93
94func TestValidateRecordWithDifferentInputTypes(t *testing.T) {
95 validator, err := NewLexiconValidator("../../internal/atproto/lexicon", false)
96 if err != nil {
97 t.Fatalf("Failed to create validator: %v", err)
98 }
99
100 // Test with JSON string
101 jsonString := `{
102 "$type": "social.coves.interaction.vote",
103 "subject": "at://did:plc:test/social.coves.post.text/abc123",
104 "createdAt": "2024-01-01T00:00:00Z"
105 }`
106
107 if err := validator.ValidateRecord(jsonString, "social.coves.interaction.vote"); err != nil {
108 t.Errorf("Failed to validate JSON string: %v", err)
109 }
110
111 // Test with JSON bytes
112 jsonBytes := []byte(jsonString)
113 if err := validator.ValidateRecord(jsonBytes, "social.coves.interaction.vote"); err != nil {
114 t.Errorf("Failed to validate JSON bytes: %v", err)
115 }
116}
117
118func TestStrictValidation(t *testing.T) {
119 // Create validator with strict mode
120 validator, err := NewLexiconValidator("../../internal/atproto/lexicon", true)
121 if err != nil {
122 t.Fatalf("Failed to create validator: %v", err)
123 }
124
125 // Profile with datetime missing timezone (should fail in strict mode)
126 profile := map[string]interface{}{
127 "$type": "social.coves.actor.profile",
128 "handle": "test.example.com",
129 "createdAt": "2024-01-01T00:00:00", // Missing Z
130 }
131
132 if err := validator.ValidateActorProfile(profile); err == nil {
133 t.Error("Expected strict validation to fail on datetime without timezone")
134 }
135}