small SPA gleam experiment to fetch and render a single bsky post
1import app/post
2import app/profile
3import gleam/dynamic/decode
4import gleam/json
5import gleeunit
6import gleeunit/should
7
8pub fn main() {
9 gleeunit.main()
10}
11
12fn from_json(
13 json_string: String,
14 decoder: decode.Decoder(a),
15) -> Result(a, json.DecodeError) {
16 json.parse(from: json_string, using: decoder)
17}
18
19pub fn decode_strong_ref_test() {
20 let json_string =
21 "{\"uri\":\"at://did:plc:123/app.bsky.feed.post/abc\",\"cid\":\"bafyre123\"}"
22
23 from_json(json_string, post.decode_strong_ref())
24 |> should.be_ok()
25 |> fn(p) {
26 p.uri |> should.equal("at://did:plc:123/app.bsky.feed.post/abc")
27 p.cid |> should.equal("bafyre123")
28 }
29}
30
31pub fn decode_label_test() {
32 let json_string = "{\"val\":\"nsfw\"}"
33
34 from_json(json_string, post.decode_label())
35 |> should.be_ok()
36 |> fn(l) { l.val |> should.equal("nsfw") }
37}
38
39pub fn decode_facet_test() {
40 let json_string =
41 "{\"index\":{\"byteStart\":0,\"byteEnd\":5},\"features\":[{\"$type\":\"app.bsky.richtext.facet#mention\",\"did\":\"did:plc:456\"}]}"
42
43 from_json(json_string, post.decode_facet())
44 |> should.be_ok()
45 |> fn(f) {
46 f.index.byte_start |> should.equal(0)
47 f.index.byte_end |> should.equal(5)
48 }
49}
50
51pub fn decode_image_test() {
52 let json_string = "{\"alt\":\"A cat image\"}"
53
54 from_json(json_string, post.decode_image())
55 |> should.be_ok()
56 |> fn(i) { i.alt |> should.equal("A cat image") }
57}
58
59pub fn decode_external_test() {
60 let json_string =
61 "{\"uri\":\"https://example.com\",\"title\":\"Example\",\"description\":\"An example site\"}"
62
63 from_json(json_string, post.decode_external())
64 |> should.be_ok()
65 |> fn(e) {
66 e.uri |> should.equal("https://example.com")
67 e.title |> should.equal("Example")
68 e.description |> should.equal("An example site")
69 }
70}
71
72pub fn decode_embed_test() {
73 let json_string =
74 "{\"$type\":\"app.bsky.embed.images\",\"images\":[{\"alt\":\"Image 1\"}]}"
75
76 from_json(json_string, post.decode_embed())
77 |> should.be_ok()
78}
79
80pub fn decode_post_test() {
81 let json_string =
82 "{\"text\":\"Hello world\",\"createdAt\":\"2024-01-01T00:00:00.000Z\"}"
83
84 from_json(json_string, post.decode_post())
85 |> should.be_ok()
86 |> fn(p) {
87 p.text |> should.equal("Hello world")
88 p.created_at |> should.equal("2024-01-01T00:00:00.000Z")
89 }
90}
91
92pub fn decode_mini_doc_test() {
93 let json_string =
94 "{\"did\":\"did:plc:abc123\",\"handle\":\"test.bsky.social\",\"pds\":\"https://example.pds.com\",\"signing_key\":\"zTestKey123\"}"
95
96 from_json(json_string, profile.decode_mini_doc())
97 |> should.be_ok()
98 |> fn(m) {
99 m.did |> should.equal("did:plc:abc123")
100 m.handle |> should.equal("test.bsky.social")
101 m.pds |> should.equal("https://example.pds.com")
102 m.signing_key |> should.equal("zTestKey123")
103 }
104}