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.community.post",
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.community.post",
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.feed.vote",
97 "subject": {
98 "uri": "at://did:plc:test/social.coves.community.post/abc123",
99 "cid": "bafyreigj3fwnwjuzr35k2kuzmb5dixxczrzjhqkr5srlqplsh6gq3bj3si"
100 },
101 "direction": "up",
102 "createdAt": "2024-01-01T00:00:00Z"
103 }`
104
105 if err := validator.ValidateRecord(jsonString, "social.coves.feed.vote"); err != nil {
106 t.Errorf("Failed to validate JSON string: %v", err)
107 }
108
109 // Test with JSON bytes
110 jsonBytes := []byte(jsonString)
111 if err := validator.ValidateRecord(jsonBytes, "social.coves.feed.vote"); err != nil {
112 t.Errorf("Failed to validate JSON bytes: %v", err)
113 }
114}
115
116func TestStrictValidation(t *testing.T) {
117 // Create validator with strict mode
118 validator, err := NewLexiconValidator("../../internal/atproto/lexicon", true)
119 if err != nil {
120 t.Fatalf("Failed to create validator: %v", err)
121 }
122
123 // Profile with datetime missing timezone (should fail in strict mode)
124 profile := map[string]interface{}{
125 "$type": "social.coves.actor.profile",
126 "handle": "test.example.com",
127 "createdAt": "2024-01-01T00:00:00", // Missing Z
128 }
129
130 if err := validator.ValidateActorProfile(profile); err == nil {
131 t.Error("Expected strict validation to fail on datetime without timezone")
132 }
133}