@@ -127,10 +127,9 @@ func TestReplaceCustomData(t *testing.T) {
127
127
t .Parallel ()
128
128
129
129
testCases := []struct {
130
- name string
131
- data string
132
- replaceData string
133
- expected string
130
+ name string
131
+ data string
132
+ expected string
134
133
}{
135
134
{
136
135
name : "no replacement necessary" ,
@@ -139,10 +138,10 @@ func TestReplaceCustomData(t *testing.T) {
139
138
},
140
139
{
141
140
name : "valid json with replacement" ,
142
- data : "{ \ " foo\" : \ " bar\" , \ " custom_channel_data\" : \" " +
141
+ data : `{ "foo": "bar", "custom_channel_data":"` +
143
142
hex .EncodeToString ([]byte (
144
- "{ \ " bar\" : \ " baz\" }" ,
145
- )) + " \" }" ,
143
+ `{ "bar": "baz"}` ,
144
+ )) + `"}` ,
146
145
expected : `{
147
146
"foo": "bar",
148
147
"custom_channel_data": {
@@ -152,10 +151,10 @@ func TestReplaceCustomData(t *testing.T) {
152
151
},
153
152
{
154
153
name : "valid json with replacement and space" ,
155
- data : "{ \ " foo\" : \ " bar\" , \ " custom_channel_data\ " : \" " +
154
+ data : `{ "foo": "bar", "custom_channel_data": "` +
156
155
hex .EncodeToString ([]byte (
157
- "{ \ " bar\" : \ " baz\" }" ,
158
- )) + " \" }" ,
156
+ `{ "bar": "baz"}` ,
157
+ )) + `"}` ,
159
158
expected : `{
160
159
"foo": "bar",
161
160
"custom_channel_data": {
@@ -178,9 +177,11 @@ func TestReplaceCustomData(t *testing.T) {
178
177
"\" custom_channel_data\" :\" a\" " ,
179
178
},
180
179
{
181
- name : "valid json, invalid hex, just formatted" ,
182
- data : "{\" custom_channel_data\" :\" f\" }" ,
183
- expected : "{\n \" custom_channel_data\" : \" f\" \n }" ,
180
+ name : "valid json, invalid hex, just formatted" ,
181
+ data : `{"custom_channel_data":"f"}` ,
182
+ expected : `{
183
+ "custom_channel_data": "f"
184
+ }` ,
184
185
},
185
186
}
186
187
@@ -191,3 +192,139 @@ func TestReplaceCustomData(t *testing.T) {
191
192
})
192
193
}
193
194
}
195
+
196
+ // TestReplaceAndAppendScid tests whether chan_id is replaced with scid and
197
+ // scid_str in the JSON console output.
198
+ func TestReplaceAndAppendScid (t * testing.T ) {
199
+ t .Parallel ()
200
+
201
+ testCases := []struct {
202
+ name string
203
+ data string
204
+ expected string
205
+ }{
206
+ {
207
+ name : "no replacement necessary" ,
208
+ data : "foo" ,
209
+ expected : "foo" ,
210
+ },
211
+ {
212
+ name : "valid json with replacement" ,
213
+ data : `{"foo":"bar","chan_id":"829031767408640"}` ,
214
+ expected : `{
215
+ "foo": "bar",
216
+ "scid": "829031767408640",
217
+ "scid_str": "754x1x0"
218
+ }` ,
219
+ },
220
+ {
221
+ name : "valid json with replacement and space" ,
222
+ data : `{"foo":"bar","chan_id": "829031767408640"}` ,
223
+ expected : `{
224
+ "foo": "bar",
225
+ "scid": "829031767408640",
226
+ "scid_str": "754x1x0"
227
+ }` ,
228
+ },
229
+ {
230
+ name : "doesn't match pattern, returned identical" ,
231
+ data : "this ain't even json, and no chan_id " +
232
+ "either" ,
233
+ expected : "this ain't even json, and no chan_id " +
234
+ "either" ,
235
+ },
236
+ {
237
+ name : "invalid json" ,
238
+ data : "this ain't json, " +
239
+ "\" chan_id\" :\" 18446744073709551616\" " ,
240
+ expected : "this ain't json, " +
241
+ "\" chan_id\" :\" 18446744073709551616\" " ,
242
+ },
243
+ {
244
+ name : "valid json, invalid uint, just formatted" ,
245
+ data : `{"chan_id":"18446744073709551616"}` ,
246
+ expected : `{
247
+ "chan_id": "18446744073709551616"
248
+ }` ,
249
+ },
250
+ }
251
+
252
+ for _ , tc := range testCases {
253
+ t .Run (tc .name , func (t * testing.T ) {
254
+ result := replaceAndAppendScid ([]byte (tc .data ))
255
+ require .Equal (t , tc .expected , string (result ))
256
+ })
257
+ }
258
+ }
259
+
260
+ // TestAppendChanID tests whether chan_id (BOLT02) is appended
261
+ // to the JSON console output.
262
+ func TestAppendChanID (t * testing.T ) {
263
+ t .Parallel ()
264
+
265
+ testCases := []struct {
266
+ name string
267
+ data string
268
+ expected string
269
+ }{
270
+ {
271
+ name : "no amendment necessary" ,
272
+ data : "foo" ,
273
+ expected : "foo" ,
274
+ },
275
+ {
276
+ name : "valid json with amendment" ,
277
+ data : `{"foo":"bar","channel_point":"6ab312e3b744e` +
278
+ `1b80a33a6541697df88766515c31c08e839bf11dc` +
279
+ `9fcc036a19:0"}` ,
280
+ expected : `{
281
+ "foo": "bar",
282
+ "channel_point": "6ab312e3b744e1b80a33a6541697df88766515c31c` +
283
+ `08e839bf11dc9fcc036a19:0",
284
+ "chan_id": "196a03cc9fdc11bf39e8081cc315657688df971654a` +
285
+ `6330ab8e144b7e312b36a"
286
+ }` ,
287
+ },
288
+ {
289
+ name : "valid json with amendment and space" ,
290
+ data : `{"foo":"bar","channel_point": "6ab312e3b744e` +
291
+ `1b80a33a6541697df88766515c31c08e839bf11dc` +
292
+ `9fcc036a19:0"}` ,
293
+ expected : `{
294
+ "foo": "bar",
295
+ "channel_point": "6ab312e3b744e1b80a33a6541697df88766515c31c` +
296
+ `08e839bf11dc9fcc036a19:0",
297
+ "chan_id": "196a03cc9fdc11bf39e8081cc315657688df971654a` +
298
+ `6330ab8e144b7e312b36a"
299
+ }` ,
300
+ },
301
+ {
302
+ name : "doesn't match pattern, returned identical" ,
303
+ data : "this ain't even json, and no channel_point " +
304
+ "either" ,
305
+ expected : "this ain't even json, and no channel_point" +
306
+ " either" ,
307
+ },
308
+ {
309
+ name : "invalid json" ,
310
+ data : "this ain't json, " +
311
+ "\" channel_point\" :\" f:0\" " ,
312
+ expected : "this ain't json, " +
313
+ "\" channel_point\" :\" f:0\" " ,
314
+ },
315
+ {
316
+ name : "valid json with invalid outpoint, formatted" ,
317
+ data : `{"channel_point":"f:0"}` ,
318
+ expected : `{
319
+ "channel_point": "f:0"
320
+ }` ,
321
+ },
322
+ }
323
+
324
+ for _ , tc := range testCases {
325
+ t .Run (tc .name , func (t * testing.T ) {
326
+ result := appendChanID ([]byte (tc .data ))
327
+ require .Equal (t , tc .expected , string (result ))
328
+ })
329
+ }
330
+ }
0 commit comments