porting all github actions from bluesky-social/indigo to tangled CI
at main 2.1 kB view raw
1package syntax 2 3import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8) 9 10func TestJSONEncoding(t *testing.T) { 11 assert := assert.New(t) 12 13 type AllTogether struct { 14 Handle Handle `json:"handle"` 15 Aturi ATURI `json:"aturi"` 16 Did DID `json:"did"` 17 Atid *AtIdentifier `json:"atid"` 18 Rkey RecordKey `json:"rkey"` 19 Col *NSID `json:"col"` // demonstrating a pointer 20 } 21 fullJSON := `{ 22 "handle": "handle.example.com", 23 "aturi": "at://handle.example.com/not.atproto.thing/abc123", 24 "did": "did:abc:123", 25 "atid": "did:abc:123", 26 "rkey": "abc123", 27 "col": "not.atproto.thing" 28 }` 29 assert.Equal(json.Valid([]byte(fullJSON)), true) 30 31 handle, err := ParseHandle("handle.example.com") 32 assert.NoError(err) 33 aturi, err := ParseATURI("at://handle.example.com/not.atproto.thing/abc123") 34 assert.NoError(err) 35 did, err := ParseDID("did:abc:123") 36 assert.NoError(err) 37 atid, err := ParseAtIdentifier("did:abc:123") 38 assert.NoError(err) 39 rkey, err := ParseRecordKey("abc123") 40 assert.NoError(err) 41 col, err := ParseNSID("not.atproto.thing") 42 assert.NoError(err) 43 44 fullStruct := AllTogether{ 45 Handle: handle, 46 Aturi: aturi, 47 Did: did, 48 Atid: atid, 49 Rkey: rkey, 50 Col: &col, 51 } 52 53 _, err = json.Marshal(fullStruct) 54 assert.NoError(err) 55 56 var parseStruct AllTogether 57 err = json.Unmarshal([]byte(fullJSON), &parseStruct) 58 assert.NoError(err) 59 assert.Equal(fullStruct, parseStruct) 60 61 badJSON := `{"handle": 12343}` 62 err = json.Unmarshal([]byte(badJSON), &parseStruct) 63 assert.Error(err) 64 65 wrongJSON := `{"handle": "asdf"}` 66 err = json.Unmarshal([]byte(wrongJSON), &parseStruct) 67 assert.Error(err) 68 69 okJSON := `{"handle": "blah.com"}` 70 err = json.Unmarshal([]byte(okJSON), &parseStruct) 71 assert.NoError(err) 72} 73 74func TestJSONHandle(t *testing.T) { 75 assert := assert.New(t) 76 77 blob := `["atproto.com", "bsky.app"]` 78 var handleList []Handle 79 if err := json.Unmarshal([]byte(blob), &handleList); err != nil { 80 t.Fatal(err) 81 } 82 assert.Equal(Handle("atproto.com"), handleList[0]) 83 assert.Equal(Handle("bsky.app"), handleList[1]) 84}