~bigbes/confluence-md-utilities

ref: 3c0272ac2232ee3a0edd0c259b71e9fd3384f211 confluence-md-utilities/api/comments_test.go -rw-r--r-- 9.5 KiB
3c0272ac — Eugene Blikh fix: round-trip fidelity for heading comments, literal tags, and emphasis a month ago
                                                                                
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package api

import (
	"encoding/json"
	"fmt"
	"net/http"
	"net/http/httptest"
	"strings"
	"sync/atomic"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// commentJSON builds a single comment node as a generic map. Helpers below
// compose these into a paginated list response.
type commentJSON map[string]any

func footerComment(id, body, displayName string, replies ...commentJSON) commentJSON {
	return commentJSON{
		"id":     id,
		"type":   "comment",
		"status": "current",
		"body": map[string]any{
			"storage": map[string]any{
				"value":          body,
				"representation": "storage",
			},
		},
		"version": map[string]any{
			"number": 1,
			"when":   "2025-12-02T09:00:00.000Z",
		},
		"history": map[string]any{
			"createdDate": "2025-12-02T09:00:00.000Z",
			"createdBy": map[string]any{
				"username":    "u-" + id,
				"displayName": displayName,
				"userKey":     "key-" + id,
			},
		},
		"extensions": map[string]any{
			"location": "footer",
		},
		"children": childrenWrap(replies),
	}
}

func inlineComment(id, body, markerRef, resolution, displayName string, replies ...commentJSON) commentJSON {
	ext := map[string]any{
		"location": "inline",
		"inlineProperties": map[string]any{
			"markerRef": markerRef,
		},
	}
	if resolution != "" {
		ext["resolution"] = map[string]any{"status": resolution}
	}
	return commentJSON{
		"id":     id,
		"type":   "comment",
		"status": "current",
		"body": map[string]any{
			"storage": map[string]any{
				"value":          body,
				"representation": "storage",
			},
		},
		"version": map[string]any{
			"number": 1,
			"when":   "2025-12-01T11:00:00.000Z",
		},
		"history": map[string]any{
			"createdDate": "2025-12-01T10:15:00.000Z",
			"createdBy": map[string]any{
				"username":    "u-" + id,
				"displayName": displayName,
			},
		},
		"extensions": ext,
		"children":   childrenWrap(replies),
	}
}

func childrenWrap(replies []commentJSON) map[string]any {
	results := make([]any, 0, len(replies))
	for _, r := range replies {
		results = append(results, r)
	}
	return map[string]any{
		"comment": map[string]any{
			"results": results,
			"size":    len(results),
		},
	}
}

func listResponse(results []commentJSON, next string) map[string]any {
	out := make([]any, 0, len(results))
	for _, r := range results {
		out = append(out, r)
	}
	resp := map[string]any{
		"results": out,
		"size":    len(out),
		"limit":   200,
		"start":   0,
		"_links":  map[string]any{},
	}
	if next != "" {
		resp["_links"] = map[string]any{"next": next}
	}
	return resp
}

func writeJSON(t *testing.T, w http.ResponseWriter, payload any) {
	t.Helper()
	w.Header().Set("Content-Type", "application/json")
	require.NoError(t, json.NewEncoder(w).Encode(payload))
}

func TestGetComments_Empty(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		assert.Equal(t, "/rest/api/content/12345/child/comment", r.URL.Path)
		writeJSON(t, w, listResponse(nil, ""))
	}))
	defer server.Close()

	client := NewClient(server.URL, "test-token")
	result, err := client.GetComments("12345")
	require.NoError(t, err)
	assert.Empty(t, result)
}

func TestGetComments_FooterOnly(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		writeJSON(t, w, listResponse([]commentJSON{
			footerComment("100", "<p>LGTM</p>", "Bob"),
			footerComment("101", "<p>nice</p>", "Eve"),
		}, ""))
	}))
	defer server.Close()

	client := NewClient(server.URL, "test-token")
	result, err := client.GetComments("12345")
	require.NoError(t, err)
	require.Len(t, result, 2)
	assert.Equal(t, "footer", result[0].Location)
	assert.Empty(t, result[0].MarkerRef)
	assert.Equal(t, "<p>LGTM</p>", result[0].BodyStorage)
	assert.Equal(t, "Bob", result[0].Author.DisplayName)
}

func TestGetComments_InlineWithMarker(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		writeJSON(t, w, listResponse([]commentJSON{
			inlineComment("200", "<p>clarify?</p>", "uuid-A", "open", "Jane"),
		}, ""))
	}))
	defer server.Close()

	client := NewClient(server.URL, "test-token")
	result, err := client.GetComments("12345")
	require.NoError(t, err)
	require.Len(t, result, 1)
	assert.Equal(t, "inline", result[0].Location)
	assert.Equal(t, "uuid-A", result[0].MarkerRef)
	assert.False(t, result[0].Resolved)
	assert.Equal(t, "open", result[0].Resolution)
	assert.Equal(t, "Jane", result[0].Author.DisplayName)
}

func TestGetComments_Resolved(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		writeJSON(t, w, listResponse([]commentJSON{
			inlineComment("300", "<p>fixed</p>", "uuid-B", "resolved", "Jane"),
		}, ""))
	}))
	defer server.Close()

	client := NewClient(server.URL, "test-token")
	result, err := client.GetComments("12345")
	require.NoError(t, err)
	require.Len(t, result, 1)
	assert.True(t, result[0].Resolved)
	assert.Equal(t, "resolved", result[0].Resolution)
}

func TestGetComments_NestedReplies(t *testing.T) {
	grandchild := inlineComment("403", "<p>thanks</p>", "uuid-C", "open", "Jane")
	child1 := inlineComment("401", "<p>agreed</p>", "uuid-C", "open", "Alice", grandchild)
	child2 := inlineComment("402", "<p>also</p>", "uuid-C", "open", "Bob")
	parent := inlineComment("400", "<p>q?</p>", "uuid-C", "open", "Jane", child1, child2)

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		writeJSON(t, w, listResponse([]commentJSON{parent}, ""))
	}))
	defer server.Close()

	client := NewClient(server.URL, "test-token")
	result, err := client.GetComments("12345")
	require.NoError(t, err)
	require.Len(t, result, 1)
	assert.Equal(t, "400", result[0].ID)
	require.Len(t, result[0].Replies, 2)
	assert.Equal(t, "401", result[0].Replies[0].ID)
	require.Len(t, result[0].Replies[0].Replies, 1)
	assert.Equal(t, "403", result[0].Replies[0].Replies[0].ID)
	assert.Equal(t, "402", result[0].Replies[1].ID)
}

func TestGetComments_PaginationRelative(t *testing.T) {
	var hits int32
	var server *httptest.Server
	server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		n := atomic.AddInt32(&hits, 1)
		switch n {
		case 1:
			writeJSON(t, w, listResponse([]commentJSON{
				footerComment("1", "<p>a</p>", "A"),
			}, "/rest/api/content/12345/child/comment?start=1&limit=200"))
		case 2:
			assert.Equal(t, "1", r.URL.Query().Get("start"))
			writeJSON(t, w, listResponse([]commentJSON{
				footerComment("2", "<p>b</p>", "B"),
			}, ""))
		default:
			t.Fatalf("unexpected request to %s", r.URL.String())
		}
		_ = server
	}))
	defer server.Close()

	client := NewClient(server.URL, "test-token")
	result, err := client.GetComments("12345")
	require.NoError(t, err)
	require.Len(t, result, 2)
	assert.Equal(t, int32(2), atomic.LoadInt32(&hits))
}

func TestGetComments_PaginationAbsolute(t *testing.T) {
	var hits int32
	var serverURL string
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		n := atomic.AddInt32(&hits, 1)
		switch n {
		case 1:
			next := serverURL + "/rest/api/content/12345/child/comment?start=1"
			writeJSON(t, w, listResponse([]commentJSON{
				footerComment("1", "<p>a</p>", "A"),
			}, next))
		case 2:
			assert.Equal(t, "1", r.URL.Query().Get("start"))
			writeJSON(t, w, listResponse([]commentJSON{
				footerComment("2", "<p>b</p>", "B"),
			}, ""))
		}
	}))
	defer server.Close()
	serverURL = server.URL

	client := NewClient(server.URL, "test-token")
	result, err := client.GetComments("12345")
	require.NoError(t, err)
	require.Len(t, result, 2)
	assert.Equal(t, int32(2), atomic.LoadInt32(&hits))
}

func TestGetComments_ExpandParam(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		assert.Equal(t, "all", r.URL.Query().Get("depth"))
		assert.Equal(t, fmt.Sprintf("%d", commentLimit), r.URL.Query().Get("limit"))
		expand := r.URL.Query().Get("expand")
		assert.Equal(t, commentExpand, expand)
		assert.True(t, strings.Contains(expand, "extensions.inlineProperties"))
		assert.True(t, strings.Contains(expand, "children.comment.children.comment"))
		writeJSON(t, w, listResponse(nil, ""))
	}))
	defer server.Close()

	client := NewClient(server.URL, "test-token")
	_, err := client.GetComments("12345")
	require.NoError(t, err)
}

func TestGetComments_NotFound(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(http.StatusNotFound)
	}))
	defer server.Close()

	client := NewClient(server.URL, "test-token")
	_, err := client.GetComments("12345")
	require.Error(t, err)
	assert.Contains(t, err.Error(), "not found")
}

func TestGetComments_MissingOptionalFields(t *testing.T) {
	// Footer comment without resolution/inlineProperties; minimum viable shape.
	c := commentJSON{
		"id":     "999",
		"type":   "comment",
		"status": "current",
		"body": map[string]any{
			"storage": map[string]any{"value": "<p>hi</p>", "representation": "storage"},
		},
		"extensions": map[string]any{"location": "footer"},
	}
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		writeJSON(t, w, listResponse([]commentJSON{c}, ""))
	}))
	defer server.Close()

	client := NewClient(server.URL, "test-token")
	result, err := client.GetComments("12345")
	require.NoError(t, err)
	require.Len(t, result, 1)
	assert.Equal(t, "footer", result[0].Location)
	assert.Empty(t, result[0].MarkerRef)
	assert.False(t, result[0].Resolved)
	assert.Empty(t, result[0].Resolution)
	assert.Empty(t, result[0].Author.DisplayName)
}