Skip to content

Commit 1d952aa

Browse files
committed
added some specs for DID and PLCOperation
1 parent 1bde673 commit 1d952aa

File tree

3 files changed

+334
-8
lines changed

3 files changed

+334
-8
lines changed

spec/did_spec.rb

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
describe DIDKit::DID do
2+
subject { described_class }
3+
4+
let(:plc_did) { 'did:plc:vc7f4oafdgxsihk4cry2xpze' }
5+
let(:web_did) { 'did:web:taylorswift.com' }
6+
7+
describe '#initialize' do
8+
context 'with a valid did:plc' do
9+
it 'should return an initialized DID object' do
10+
did = subject.new(plc_did)
11+
12+
did.should be_a(DIDKit::DID)
13+
did.type.should == :plc
14+
did.did.should be_a(String)
15+
did.did.should == plc_did
16+
did.resolved_by.should be_nil
17+
end
18+
end
19+
20+
context 'with a valid did:web' do
21+
it 'should return an initialized DID object' do
22+
did = subject.new(web_did)
23+
24+
did.should be_a(DIDKit::DID)
25+
did.type.should == :web
26+
did.did.should be_a(String)
27+
did.did.should == web_did
28+
did.resolved_by.should be_nil
29+
end
30+
end
31+
32+
context 'with another DID object' do
33+
it 'should create a copy of the DID' do
34+
other = subject.new(plc_did)
35+
did = subject.new(other)
36+
37+
did.did.should == plc_did
38+
did.type.should == :plc
39+
did.equal?(other).should == false
40+
end
41+
end
42+
43+
context 'with a string that is not a DID' do
44+
it 'should raise an error' do
45+
expect {
46+
subject.new('not-a-did')
47+
}.to raise_error(DIDKit::DIDError)
48+
end
49+
end
50+
51+
context 'when an unrecognized did: type' do
52+
it 'should raise an error' do
53+
expect {
54+
subject.new('did:example:123')
55+
}.to raise_error(DIDKit::DIDError)
56+
end
57+
end
58+
end
59+
60+
describe '#web_domain' do
61+
context 'for a did:web' do
62+
it 'should return the domain part' do
63+
did = subject.new('did:web:site.example.com')
64+
65+
did.web_domain.should == 'site.example.com'
66+
end
67+
end
68+
69+
context 'for a did:plc' do
70+
it 'should return nil' do
71+
did = subject.new('did:plc:yk4dd2qkboz2yv6tpubpc6co')
72+
73+
did.web_domain.should be_nil
74+
end
75+
end
76+
end
77+
78+
describe '#==' do
79+
let(:did_string) { 'did:plc:vc7f4oafdgxsihk4cry2xpze' }
80+
let(:other_string) { 'did:plc:oio4hkxaop4ao4wz2pp3f4cr' }
81+
82+
let(:did) { subject.new(did_string) }
83+
let(:other) { subject.new(other_string) }
84+
85+
context 'given a DID string' do
86+
it 'should compare its string value to the other DID' do
87+
did.should == did_string
88+
did.should_not == other_string
89+
end
90+
end
91+
92+
context 'given another DID object' do
93+
it "should compare its string value to the other DID's string value" do
94+
copy = subject.new(did_string)
95+
96+
did.should == copy
97+
did.should_not == other
98+
end
99+
end
100+
101+
context 'given something that is not a DID' do
102+
it 'should return false' do
103+
did.should_not == :didplc
104+
did.should_not == [did_string]
105+
end
106+
end
107+
end
108+
109+
describe '#to_s' do
110+
it "should return the DID's string value" do
111+
did = subject.new(plc_did)
112+
113+
did.to_s.should be_a(String)
114+
did.to_s.should == plc_did
115+
end
116+
end
117+
end

spec/document_spec.rb

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
describe DIDKit::Document do
22
subject { described_class }
33

4-
let(:did_string) { 'did:plc:yk4dd2qkboz2yv6tpubpc6co' }
5-
let(:did) { DID.new(did_string) }
6-
let(:json_data) { load_did_json('dholms.json') }
4+
let(:did) { DID.new('did:plc:yk4dd2qkboz2yv6tpubpc6co') }
5+
let(:base_json) { load_did_json('dholms.json') }
76

87
describe '#initialize' do
9-
let(:base_json) { json_data }
10-
118
context 'with valid input' do
129
let(:json) { base_json }
1310

@@ -39,7 +36,7 @@
3936
end
4037

4138
context 'when id is missing' do
42-
let(:json) { base_json.dup.tap { |h| h.delete('id') } }
39+
let(:json) { base_json.dup.tap { |h| h.delete('id') }}
4340

4441
it 'should raise a format error' do
4542
expect {
@@ -149,7 +146,7 @@
149146

150147
describe 'service helpers' do
151148
let(:service_json) {
152-
json_data.merge('service' => [
149+
base_json.merge('service' => [
153150
{ 'id' => '#atproto_pds', 'type' => 'AtprotoPersonalDataServer', 'serviceEndpoint' => 'https://pds.dholms.xyz' },
154151
{ 'id' => '#atproto_labeler', 'type' => 'AtprotoLabeler', 'serviceEndpoint' => 'https://labels.dholms.xyz' },
155152
{ 'id' => '#lycan', 'type' => 'LycanService', 'serviceEndpoint' => 'https://lycan.feeds.blue' }
@@ -216,7 +213,7 @@
216213

217214
describe 'if there is no matching service' do
218215
let(:service_json) {
219-
json_data.merge('service' => [
216+
base_json.merge('service' => [
220217
{ 'id' => '#lycan', 'type' => 'LycanService', 'serviceEndpoint' => 'https://lycan.feeds.blue' }
221218
])
222219
}

spec/plc_operation_spec.rb

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
require 'time'
2+
3+
describe DIDKit::PLCOperation do
4+
subject { described_class }
5+
6+
let(:base_json) { load_did_json('bnewbold_log.json').last }
7+
8+
describe '#initialize' do
9+
context 'with a valid plc operation' do
10+
let(:json) { base_json }
11+
12+
it 'should return a PLCOperation with parsed data' do
13+
op = subject.new(json)
14+
15+
op.json.should == json
16+
op.type.should == :plc_operation
17+
op.did.should == 'did:plc:44ybard66vv44zksje25o7dz'
18+
op.cid.should == 'bafyreiaoaelqu32ngmqd2mt3v3zvek7k34cvo7lvmk3kseuuaag5eptg5m'
19+
op.created_at.should be_a(Time)
20+
op.created_at.should == Time.parse("2025-06-06T00:34:40.824Z")
21+
op.handles.should == ['bnewbold.net']
22+
op.services.map(&:key).should == ['atproto_pds']
23+
end
24+
end
25+
26+
context 'when argument is not a hash' do
27+
let(:json) { [base_json] }
28+
29+
it 'should raise a format error' do
30+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
31+
end
32+
end
33+
34+
context 'when did is missing' do
35+
let(:json) { base_json.tap { |h| h.delete('did') }}
36+
37+
it 'should raise a format error' do
38+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
39+
end
40+
end
41+
42+
context 'when did is not a string' do
43+
let(:json) { base_json.merge('did' => 123) }
44+
45+
it 'should raise a format error' do
46+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
47+
end
48+
end
49+
50+
context "when did doesn't start with did:" do
51+
let(:json) { base_json.merge('did' => 'foobar') }
52+
53+
it 'should raise a format error' do
54+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
55+
end
56+
end
57+
58+
context 'when cid is missing' do
59+
let(:json) { base_json.tap { |h| h.delete('cid') }}
60+
61+
it 'should raise a format error' do
62+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
63+
end
64+
end
65+
66+
context 'when cid is not a string' do
67+
let(:json) { base_json.merge('cid' => 700) }
68+
69+
it 'should raise a format error' do
70+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
71+
end
72+
end
73+
74+
context 'when createdAt is missing' do
75+
let(:json) { base_json.tap { |h| h.delete('createdAt') }}
76+
77+
it 'should raise a format error' do
78+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
79+
end
80+
end
81+
82+
context 'when createdAt is invalid' do
83+
let(:json) { base_json.merge('createdAt' => 123) }
84+
85+
it 'should raise a format error' do
86+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
87+
end
88+
end
89+
90+
context 'when operation block is missing' do
91+
let(:json) { base_json.tap { |h| h.delete('operation') }}
92+
93+
it 'should raise a format error' do
94+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
95+
end
96+
end
97+
98+
context 'when operation block is not a hash' do
99+
let(:json) { base_json.merge('operation' => 'invalid') }
100+
101+
it 'should raise a format error' do
102+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
103+
end
104+
end
105+
106+
context 'when operation type is missing' do
107+
let(:json) { base_json.tap { |h| h['operation'].delete('type') }}
108+
109+
it 'should raise a format error' do
110+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
111+
end
112+
end
113+
114+
context 'when operation type is not plc_operation' do
115+
let(:json) { base_json.tap { |h| h['operation']['type'] = 'other' }}
116+
117+
it 'should not raise an error' do
118+
expect { subject.new(json) }.not_to raise_error
119+
end
120+
121+
it 'should return the operation type' do
122+
op = subject.new(json)
123+
op.type.should == :other
124+
end
125+
126+
it 'should not try to parse services' do
127+
json['services'] = nil
128+
129+
expect { subject.new(json) }.not_to raise_error
130+
end
131+
132+
it 'should return nil from services' do
133+
op = subject.new(json)
134+
op.services.should be_nil
135+
end
136+
137+
it 'should not try to parse handles' do
138+
json['alsoKnownAs'] = nil
139+
140+
expect { subject.new(json) }.not_to raise_error
141+
end
142+
143+
it 'should return nil from handles' do
144+
op = subject.new(json)
145+
op.handles.should be_nil
146+
end
147+
end
148+
149+
context 'when alsoKnownAs is not an array' do
150+
let(:json) { base_json.tap { |h| h['operation']['alsoKnownAs'] = 'at://dholms.xyz' }}
151+
152+
it 'should raise an AtHandles format error' do
153+
expect {
154+
subject.new(json)
155+
}.to raise_error(DIDKit::AtHandles::FormatError)
156+
end
157+
end
158+
159+
context 'when alsoKnownAs elements are not strings' do
160+
let(:json) { base_json.tap { |h| h['operation']['alsoKnownAs'] = [666] }}
161+
162+
it 'should raise an AtHandles format error' do
163+
expect {
164+
subject.new(json)
165+
}.to raise_error(DIDKit::AtHandles::FormatError)
166+
end
167+
end
168+
169+
context 'when alsoKnownAs contains multiple handles' do
170+
let(:json) {
171+
base_json.tap { |h|
172+
h['operation']['alsoKnownAs'] = [
173+
'at://dholms.xyz',
174+
'https://example.com',
175+
'at://other.handle'
176+
]
177+
}
178+
}
179+
180+
it 'should pick those starting with at:// and remove the prefixes' do
181+
op = subject.new(json)
182+
op.handles.should == ['dholms.xyz', 'other.handle']
183+
end
184+
end
185+
186+
context 'when services are missing' do
187+
let(:json) { base_json.tap { |h| h['operation'].delete('services') }}
188+
189+
it 'should raise a format error' do
190+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
191+
end
192+
end
193+
194+
context 'when services entry is not a hash' do
195+
let(:json) {
196+
base_json.tap { |h|
197+
h['operation']['services'] = [
198+
{
199+
"id": "#atproto_pds",
200+
"type": "AtprotoPersonalDataServer",
201+
"serviceEndpoint": "https://pds.dholms.xyz"
202+
}
203+
]
204+
}
205+
}
206+
207+
it 'should raise a format error' do
208+
expect { subject.new(json) }.to raise_error(DIDKit::PLCOperation::FormatError)
209+
end
210+
end
211+
end
212+
end

0 commit comments

Comments
 (0)