summaryrefslogtreecommitdiff
path: root/gemini/gemtext/atomconv/convert_test.go
blob: 8adaa2eb96149c0ac42a7099ded401b03cf7bffa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package atomconv

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"net/url"
	"testing"

	"tildegit.org/tjp/sliderule"
	"tildegit.org/tjp/sliderule/gemini"
	"tildegit.org/tjp/sliderule/gemini/gemtext"
	"tildegit.org/tjp/sliderule/internal/types"
)

func TestConvert(t *testing.T) {
	tests := []struct {
		url    string
		input  string
		output string
	}{
		{
			url: "gemini://sombodys.site/a/page",
			input: `
# This is a gemlog page


## with a subtitle after empty lines

=> ./first-post.gmi 2023-08-25 - This is my first post
`[1:],
			output: `
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>gemini://sombodys.site/a/page</id>
  <link href="gemini://sombodys.site/a/page"/>
  <title>This is a gemlog page</title>
  <subtitle>with a subtitle after empty lines</subtitle>
  <updated>2023-08-25T12:00:00Z</updated>
  <entry>
    <id>./first-post.gmi</id>
    <link rel="alternate" href="./first-post.gmi"/>
    <title>This is my first post</title>
    <updated>2023-08-25T12:00:00Z</updated>
  </entry>
</feed>
`[1:],
		},
	}

	for _, test := range tests {
		t.Run(test.url, func(t *testing.T) {
			doc, err := gemtext.Parse(bytes.NewBufferString(test.input))
			if err != nil {
				t.Fatal(err)
			}
			loc, err := url.Parse(test.url)
			if err != nil {
				t.Fatal(err)
			}
			out := &bytes.Buffer{}
			if err := Convert(out, doc, loc); err != nil {
				t.Fatal(err)
			}
			if out.String() != test.output {
				t.Fatal("mismatched output")
			}
		})
	}
}

func TestAuto(t *testing.T) {
	rout := &sliderule.Router{}

	rout.Route("/foo.gmi", types.HandlerFunc(func(ctx context.Context, request *types.Request) *types.Response {
		return gemini.Success("text/gemini", bytes.NewBufferString(`
# This is my gemini page

## a subtitle

=> ./first-post.gmi 2023-05-17 - My first post
=> ./second-post.gmi 2023-06-02 second-ever post
`[1:]))
	}))

	rout.Route("/bar.gmi", types.HandlerFunc(func(ctx context.Context, request *types.Request) *types.Response {
		return gemini.Success("text/gemini", bytes.NewBufferString(`
# Another homepage

=> ./first-post.gmi 2023-05-17 - first post
=> ./second-post.gmi 2023-06-02 second post
`[1:]))
	}))

	h := Auto(rout.Handler())

	response := h.Handle(context.Background(), &types.Request{URL: &url.URL{
		Scheme: "gemini",
		Host:   "127.0.0.1",
		Path:   "/foo.gmi.atom",
	}})
	if response.Status != gemini.StatusSuccess {
		t.Fatal("bad response code")
	}

	result, err := io.ReadAll(response.Body)
	if err != nil {
		t.Fatal(err)
	}

	target := `
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>gemini://127.0.0.1/foo.gmi.atom</id>
  <link href="gemini://127.0.0.1/foo.gmi.atom"/>
  <title>This is my gemini page</title>
  <subtitle>a subtitle</subtitle>
  <updated>2023-06-02T12:00:00Z</updated>
  <entry>
    <id>./first-post.gmi</id>
    <link rel="alternate" href="./first-post.gmi"/>
    <title>My first post</title>
    <updated>2023-05-17T12:00:00Z</updated>
  </entry>
  <entry>
    <id>./second-post.gmi</id>
    <link rel="alternate" href="./second-post.gmi"/>
    <title>second-ever post</title>
    <updated>2023-06-02T12:00:00Z</updated>
  </entry>
</feed>
`[1:]
	if string(result) != target {
		fmt.Println(target)
		fmt.Println(string(result))
		t.Fatal("response body")
	}
}