|
| 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