Auto-indexing service and GraphQL API for AT Protocol Records
quickslice.slices.network/
atproto
gleam
graphql
1/// Tests for Lexicon Reference Resolver
2///
3/// Resolves ref types in lexicon definitions to their actual types
4import gleam/dict
5import gleam/option.{None, Some}
6import gleeunit/should
7import lexicon_graphql/internal/lexicon/ref_resolver
8import lexicon_graphql/types
9
10// Test resolving a local reference (within same lexicon)
11pub fn resolve_local_ref_test() {
12 // Lexicon with a main record that references another type in the same lexicon
13 let lexicon =
14 types.Lexicon(
15 id: "xyz.statusphere.post",
16 defs: types.Defs(
17 main: Some(
18 types.RecordDef(type_: "record", key: None, properties: [
19 #(
20 "text",
21 types.Property(
22 type_: "string",
23 required: True,
24 format: None,
25 ref: None,
26 refs: None,
27 items: None,
28 ),
29 ),
30 #(
31 "embed",
32 types.Property(
33 type_: "ref",
34 required: False,
35 format: None,
36 ref: None,
37 refs: None,
38 items: None,
39 ),
40 ),
41 ]),
42 ),
43 others: dict.new(),
44 ),
45 )
46
47 // Reference should resolve to a type within the same lexicon
48 let result = ref_resolver.resolve_ref("xyz.statusphere.post#embed", [lexicon])
49
50 should.be_ok(result)
51}
52
53// Test resolving an external reference (different lexicon)
54pub fn resolve_external_ref_test() {
55 let post_lexicon =
56 types.Lexicon(
57 id: "xyz.statusphere.post",
58 defs: types.Defs(
59 main: Some(
60 types.RecordDef(type_: "record", key: None, properties: [
61 #(
62 "text",
63 types.Property(
64 type_: "string",
65 required: True,
66 format: None,
67 ref: None,
68 refs: None,
69 items: None,
70 ),
71 ),
72 #(
73 "author",
74 types.Property(
75 type_: "ref",
76 required: False,
77 format: None,
78 ref: None,
79 refs: None,
80 items: None,
81 ),
82 ),
83 ]),
84 ),
85 others: dict.new(),
86 ),
87 )
88
89 let profile_lexicon =
90 types.Lexicon(
91 id: "xyz.statusphere.profile",
92 defs: types.Defs(
93 main: Some(
94 types.RecordDef(type_: "record", key: None, properties: [
95 #(
96 "displayName",
97 types.Property(
98 type_: "string",
99 required: True,
100 format: None,
101 ref: None,
102 refs: None,
103 items: None,
104 ),
105 ),
106 ]),
107 ),
108 others: dict.new(),
109 ),
110 )
111
112 // Reference to different lexicon
113 let result =
114 ref_resolver.resolve_ref("xyz.statusphere.profile", [
115 post_lexicon,
116 profile_lexicon,
117 ])
118
119 should.be_ok(result)
120}
121
122// Test error when reference not found
123pub fn resolve_nonexistent_ref_test() {
124 let lexicon =
125 types.Lexicon(
126 id: "xyz.statusphere.post",
127 defs: types.Defs(
128 main: Some(
129 types.RecordDef(type_: "record", key: None, properties: [
130 #(
131 "text",
132 types.Property(
133 type_: "string",
134 required: True,
135 format: None,
136 ref: None,
137 refs: None,
138 items: None,
139 ),
140 ),
141 ]),
142 ),
143 others: dict.new(),
144 ),
145 )
146
147 // Try to resolve a reference that doesn't exist
148 let result =
149 ref_resolver.resolve_ref("xyz.statusphere.nonexistent", [lexicon])
150
151 should.be_error(result)
152}
153
154// Test parsing ref URI format
155pub fn parse_ref_uri_test() {
156 // Test parsing full NSID
157 ref_resolver.parse_ref_uri("xyz.statusphere.profile")
158 |> should.equal(#("xyz.statusphere.profile", "main"))
159
160 // Test parsing NSID with fragment
161 ref_resolver.parse_ref_uri("xyz.statusphere.post#embed")
162 |> should.equal(#("xyz.statusphere.post", "embed"))
163}