parsoid 0.10.1

Wrapper around Parsoid HTML that provides convenient accessors for processing and manipulation
Documentation
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
Copyright (C) 2020-2021 Kunal Mehta <legoktm@debian.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
use crate::prelude::*;
use crate::tests::test_client;
use crate::{map::IndexMap, Result};

#[tokio::test]
async fn test_templates() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client.get("Main Page").await?.into_mutable();
    let mut found = false;
    for template in code.filter_templates()? {
        if template.name_in_wikitext() == "Wikipedia:What we do on this wiki" {
            found = true;
        }
    }
    assert!(found);
    Ok(())
}

#[tokio::test]
async fn test_more_cases() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
            .transform_to_html(
                "{{1x|param<!--comment-->name=value|normal=value2}}{{#if:{{{1}}}|foo|bar}}",
            )
            .await?.into_mutable();
    let templates = code.filter_templates()?;
    // The second parser function wasn't included
    assert_eq!(templates.len(), 1);
    let temp = &templates[0];
    assert!(temp.is_template());
    assert!(!temp.is_parser_function());
    assert_eq!(temp.raw_name(), "./Template:1x");
    assert_eq!(temp.name(), "Template:1x");
    let mut params = IndexMap::new();
    params.insert("normal".to_string(), "value2".to_string());
    params.insert("paramname".to_string(), "value".to_string());
    assert_eq!(temp.params(), params);
    assert_eq!(temp.param("paramname"), Some("value".to_string()));
    assert_eq!(temp.param("notset"), None);
    assert_eq!(
        temp.param_in_wikitext("paramname"),
        Some("param<!--comment-->name".to_string())
    );
    assert_eq!(temp.param_in_wikitext("normal"), Some("normal".to_string()));
    assert_eq!(temp.param_in_wikitext("notset"), None);
    let pfs = code.filter_parser_functions()?;
    // The previous template wasn't included
    assert_eq!(pfs.len(), 1);
    let pf = &pfs[0];
    assert!(pf.is_parser_function());
    assert!(!pf.is_template());
    assert_eq!(pf.raw_name(), "if");
    Ok(())
}

#[tokio::test]
async fn test_template_mutation() -> Result<()> {
    let client = test_client::testwp_client();
    let original = "{{1x|foo=bar}}";
    let code = client.transform_to_html(original).await?.into_mutable();
    let mut templates = code.filter_templates()?;
    let temp = &mut templates[0];
    temp.set_param("new", "placeholder")?;
    let previous = temp.set_param("new", "wikitext")?;
    assert_eq!(previous, Some("placeholder".to_string()));
    let html = client.transform_to_wikitext(&code).await?;
    assert_eq!(html, "{{1x|foo=bar|new=wikitext}}".to_string());
    let value = temp.remove_param("new")?;
    assert_eq!(value, Some("wikitext".to_string()));
    let new_html = client.transform_to_wikitext(&code).await?;
    assert_eq!(new_html, original.to_string());
    temp.set_params([
        ("baz".to_string(), "blah".to_string()),
        ("1".to_string(), "what".to_string()),
    ])?;
    let html = client.transform_to_wikitext(&code).await?;
    assert_eq!(&html, "{{1x|baz=blah|what}}");
    temp.set_name("2x".to_string())?;
    let html = client.transform_to_wikitext(&code).await?;
    println!("{}", temp);
    assert_eq!(&html, "{{2x|baz=blah|what}}");
    Ok(())
}

#[tokio::test]
async fn test_text_contents() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client.get("Mwbot-rs/Strip code").await?.into_mutable();
    assert_eq!(
        code.text_contents(),
        "This is some formatted code. Also a link.".to_string()
    );
    Ok(())
}

#[tokio::test]
async fn test_wikilinks() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("[[Home Page#some fragment|link text]]")
        .await?
        .into_mutable()
        .without_parsoid_data();
    let links = code.filter_links();
    let link = &links[0];
    assert_eq!(link.raw_target(), "./Home_Page#some_fragment".to_string());
    assert_eq!(link.target(), "Home Page#some fragment".to_string());
    assert_eq!(link.text_contents(), "link text".to_string());
    assert!(link.is_redirect());
    if link.to_string() != "<a rel=\"mw:WikiLink\" href=\"./Home_Page#some_fragment\" title=\"Home Page\" class=\"mw-redirect\" id=\"mwAw\">link text</a>"
    && link.to_string() != "<a rel=\"mw:WikiLink\" href=\"./Home_Page#some_fragment\" class=\"mw-selflink-fragment mw-redirect\" id=\"mwAw\">link text</a>" {
        // the response is currently different between core/extension API and RESTBase API on WMF projects
        assert_eq!(link.to_string(), "");
    }
    // Mutability
    link.set_target("MediaWiki");
    assert_eq!(link.raw_target(), "./MediaWiki".to_string());
    assert_eq!(link.target(), "MediaWiki".to_string());
    assert!(code.to_string().contains("href=\"./MediaWiki\""));
    let wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(wikitext, "[[MediaWiki|link text]]".to_string());
    // Percent encoding
    let code = client
        .transform_to_html("[[Mwbot-rs/Question?#some fragment with ö and ?]]")
        .await?
        .into_mutable();
    let links = code.filter_links();
    let link = &links[0];
    assert_eq!(
        link.raw_target(),
        "./Mwbot-rs/Question%3F#some_fragment_with_ö_and_?".to_string()
    );
    assert_eq!(
        link.target(),
        "Mwbot-rs/Question?#some fragment with ö and ?".to_string()
    );
    Ok(())
}

#[tokio::test]
async fn test_redlinks() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("[[Page does not exist#Section heading]]")
        .await?
        .into_mutable();
    let links = code.filter_links();
    let link = &links[0];
    assert_eq!(
        link.target(),
        "Page does not exist#Section heading".to_string()
    );
    Ok(())
}

#[tokio::test]
async fn test_new_link() -> Result<()> {
    let client = test_client::testwp_client();
    let link = WikiLink::new("Foo", &Wikicode::new_text("bar"));
    assert_eq!(
        &link.to_string(),
        "<a href=\"./Foo\" rel=\"mw:WikiLink\">bar</a>"
    );
    let code = Wikicode::new("");
    code.append(&link);
    let new_wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(new_wikitext, "[[Foo|bar]]".to_string());
    Ok(())
}

#[tokio::test]
async fn test_external_links() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("[https://example.com Link content] ")
        .await?
        .into_mutable()
        .without_parsoid_data();
    println!("{}", *code);
    let links = code.filter_external_links();
    let link = &links[0];
    assert_eq!(link.target(), "https://example.com".to_string());
    assert_eq!(link.text_contents(), "Link content".to_string());
    assert_eq!(
        &link.to_string(),
        "<a rel=\"mw:ExtLink nofollow\" href=\"https://example.com\" class=\"external text\" id=\"mwAw\">Link content</a>"
    );
    // Or via new_from_node
    let found_links = code.inclusive_descendants().any(|node| {
        dbg!(&node);
        matches!(node, Wikinode::ExtLink(_))
    });
    assert!(found_links);
    // Mutability
    link.set_target("https://wiki.example.org/foo?query=1");
    assert_eq!(
        link.target(),
        "https://wiki.example.org/foo?query=1".to_string()
    );
    let wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(
        wikitext,
        "[https://wiki.example.org/foo?query=1 Link content] ".to_string()
    );
    Ok(())
}

#[tokio::test]
async fn test_comments() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("<!--comment-->")
        .await?
        .into_mutable();
    let comments = code.filter_comments();
    let comment = &comments[0];
    assert_eq!(comment.text(), "comment".to_string());
    // Surround with spaces for extra whitespace
    comment.set_text(" new ");
    assert_eq!(comment.text(), " new ".to_string());
    // Change is reflected in Wikicode serialization
    assert!(code.to_string().contains("<!-- new -->"));
    Ok(())
}

#[tokio::test]
async fn test_properties() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client.get("Mwbot-rs/Stable").await?.into_mutable();
    assert_eq!(code.revision_id(), Some(635905));
    assert_eq!(code.title(), Some("Mwbot-rs/Stable".to_string()));
    Ok(())
}

#[tokio::test]
async fn test_iterators() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("This is a [[PageThatDoesntExist|sentence]].")
        .await?
        .into_mutable();
    let link = code
        .descendants()
        .filter_map(|node| {
            dbg!(&node);
            node.as_wikilink()
        })
        .next()
        .unwrap();
    assert_eq!(
        link.raw_target(),
        "./PageThatDoesntExist?action=edit&redlink=1".to_string()
    );
    assert_eq!(link.target(), "PageThatDoesntExist".to_string());
    assert_eq!(link.text_contents(), "sentence".to_string());
    Ok(())
}

#[tokio::test]
async fn test_title() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client.get("Mwbot-rs/DISPLAYTITLE").await?.into_mutable();
    assert_eq!(code.title().unwrap(), "Mwbot-rs/DISPLAYTITLE".to_string());
    // T326490 regression test
    let wikitext = client.transform_to_wikitext(&code).await?;
    assert!(wikitext.starts_with("{{DISPLAYTITLE"));
    Ok(())
}

#[tokio::test]
async fn test_sections() -> Result<()> {
    let client = test_client::testwp_client();
    let wikitext = r#"
...lead section contents...
== foo=bar ==
...section contents...
=== nested ===
...section contents...
"#;
    let code = client
        .transform_to_html(wikitext)
        .await?
        .into_mutable()
        .without_parsoid_data();
    let sections = code.iter_sections();
    {
        let section = &sections[0];
        assert!(section.is_pseudo_section());
        assert_eq!(section.section_id(), 0);
        assert!(section.heading().is_none());
        assert!(section.is_editable());
    }
    {
        let section = &sections[1];
        assert!(!section.is_pseudo_section());
        assert_eq!(section.section_id(), 1);
        let heading = section.heading().unwrap();
        assert_eq!(heading.text_contents(), "foo=bar");
        assert_eq!(heading.level(), 2);
        assert!(section.is_editable());
    }
    {
        let section = &sections[2];
        assert!(!section.is_pseudo_section());
        assert_eq!(section.section_id(), 2);
        let heading = section.heading().unwrap();
        assert_eq!(heading.text_contents(), "nested");
        assert_eq!(heading.level(), 3);
        assert!(section.is_editable());
    }
    let new_heading = Heading::new(3, &Wikicode::new_text("inserted"))?;
    sections[2].insert_before(&new_heading);
    assert_eq!(
        r#"
...lead section contents...

== foo=bar ==
...section contents...

=== inserted ===

=== nested ===
...section contents...
"#,
        client.transform_to_wikitext(&code).await?
    );

    Ok(())
}

#[tokio::test]
async fn test_heading() -> Result<()> {
    let client = test_client::testwp_client();
    let heading = Heading::new(2, &Wikicode::new_text("Some text"))?;
    let code = Wikicode::new("");
    code.append(&heading);
    let wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(&wikitext, "== Some text ==\n");

    Ok(())
}

#[test]
fn test_invalid_heading() {
    let err = Heading::new(7, &Wikicode::new_text("foo")).unwrap_err();
    assert_eq!(
        err.to_string(),
        "Heading levels must be between 1 and 6, '7' was provided"
    );
}

#[tokio::test]
async fn test_category() -> Result<()> {
    let client = test_client::testwp_client();
    let category = Category::new("Category:Foo bar", Some("Bar baz#quux"));
    let code = Wikicode::new("");
    code.append(&category);
    let wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(&wikitext, "[[Category:Foo bar|Bar baz#quux]]");
    let cats: Vec<_> = code
        .inclusive_descendants()
        .filter_map(|node| node.as_category())
        .collect();
    let cat = &cats[0];
    assert_eq!(&cat.category(), "Category:Foo bar");
    assert_eq!(cat.sort_key(), Some("Bar baz#quux".to_string()));
    cat.set_category("Category:Bar baz");
    cat.set_sort_key(None);
    assert_eq!(cat.sort_key(), None);
    assert_eq!(
        &cat.to_string(),
        "<link href=\"./Category:Bar_baz\" rel=\"mw:PageProp/Category\">"
    );
    let new_wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(&new_wikitext, "[[Category:Bar baz]]");

    Ok(())
}

#[tokio::test]
async fn test_language_link() -> Result<()> {
    let client = test_client::testwp_client();
    let link = LanguageLink::new("https://en.wikipedia.org/wiki/Foo");
    let code = Wikicode::new("");
    code.append(&link);
    let wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(&wikitext, "[[en:Foo]]");
    link.set_target("https://de.wikipedia.org/wiki/Foo");
    assert_eq!(&link.target(), "https://de.wikipedia.org/wiki/Foo");
    let wikitext2 = client.transform_to_wikitext(&code).await?;
    assert_eq!(&wikitext2, "[[de:Foo]]");
    Ok(())
}

#[tokio::test]
async fn test_behavior_switch() -> Result<()> {
    let client = test_client::testwp_client();
    let code = Wikicode::new("");
    code.append(&BehaviorSwitch::new("toc"));
    let wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(&wikitext, "__TOC__\n");
    let switches: Vec<_> = code
        .inclusive_descendants()
        .filter_map(|node| node.as_behavior_switch())
        .collect();
    let switch = &switches[0];
    assert_eq!(&switch.property(), "toc");
    Ok(())
}

#[tokio::test]
async fn test_redirect() -> Result<()> {
    let client = test_client::testwp_client();
    let code = Wikicode::new("");
    code.append(&Redirect::new("Foo"));
    let redirect = code.redirect().unwrap();
    assert_eq!(redirect.target(), "Foo".to_string());
    assert_eq!(redirect.raw_target(), "./Foo".to_string());
    assert!(!redirect.is_external());
    let wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(&wikitext, "#REDIRECT [[Foo]]");

    assert_eq!(
        client
            .get("Mwbot-rs/RedirectSym")
            .await?
            .into_mutable()
            .redirect()
            .unwrap()
            .target(),
        "Mwbot-rs/RedirectSym?"
    );

    Ok(())
}

#[tokio::test]
async fn test_external_redirect() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("#REDIRECT [[w:Main Page]]")
        .await?
        .into_mutable();
    dbg!(&code.to_string());
    dbg!(&code.redirect());
    let redirect = code.redirect().expect("not a redirect");
    assert!(redirect.is_external());
    assert_eq!(
        redirect.target(),
        "https://en.wikipedia.org/wiki/Main%20Page".to_string()
    );
    assert_eq!(
        redirect.raw_target(),
        "https://en.wikipedia.org/wiki/Main%20Page".to_string()
    );
    Ok(())
}

#[tokio::test]
async fn test_detach() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("This is a [[link]].")
        .await?
        .into_mutable();
    for link in code.filter_links() {
        link.detach();
    }

    let wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(&wikitext, "This is a .");

    Ok(())
}

#[tokio::test]
async fn test_section_prepend() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("== Section ==\n[[foo]]")
        .await?
        .into_mutable()
        .without_parsoid_data();
    let section = code.iter_sections()[1].clone();
    // Need to insert_after the heading otherwise parsoid will move the content before
    // the <section> tag
    section
        .heading()
        .unwrap()
        .insert_after(&WikiLink::new("./Bar", &Wikicode::new_text("Bar")));
    let wikitext = client.transform_to_wikitext(&code).await?;
    assert_eq!(&wikitext, "== Section ==\n[[Bar]]\n[[foo]]");
    Ok(())
}

#[tokio::test]
async fn test_parser_functions() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client.get("Mwbot-rs/Parser function").await?.into_mutable();
    let templates = code.filter_parser_functions()?;
    let temp = &templates[0];
    assert!(temp.is_parser_function());
    // name is literally the entire function
    assert_eq!(
        temp.name_in_wikitext(),
        "#expr:{{formatnum:{{NUMBEROFUSERS}}|R}}+100"
    );
    assert_eq!(temp.raw_name(), "expr");
    // identical to normalized_name
    assert_eq!(temp.name(), "expr");
    Ok(())
}

#[tokio::test]
async fn test_question() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .get("Mwbot-rs/Question template")
        .await?
        .into_mutable();
    let templates = code.filter_templates()?;
    let temp = &templates[0];
    assert_eq!(&temp.name(), "Template:But why?");
    Ok(())
}

#[tokio::test]
async fn test_includeonly() -> Result<()> {
    let client = test_client::testwp_client();
    let wikitext = "foo<includeonly>bar</includeonly>baz";
    let code = client.transform_to_html(wikitext).await?.into_mutable();
    let includeonlys: Vec<_> = code
        .descendants()
        .filter_map(|node| node.as_includeonly())
        .collect();
    assert_eq!(includeonlys.len(), 1);
    let includeonly = includeonlys[0].clone();
    assert_eq!(includeonly.wikitext()?, "bar");
    includeonly.set_wikitext("yay!")?;
    let new = client.transform_to_wikitext(&code).await?;
    assert_eq!(&new, "foo<includeonly>yay!</includeonly>baz");
    includeonly.detach();
    let detached = client.transform_to_wikitext(&code).await?;
    assert_eq!(&detached, "foobaz");
    let node = IncludeOnly::new("included")?;
    code.append(&node);
    let appended = client.transform_to_wikitext(&code).await?;
    assert_eq!(&appended, "foobaz<includeonly>included</includeonly>");
    Ok(())
}

#[tokio::test]
async fn test_noinclude() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("foo<noinclude>bar</noinclude>baz")
        .await?
        .into_mutable();
    let noincludes = code.filter_noinclude();
    assert_eq!(noincludes.len(), 1);
    let noinclude = noincludes[0].clone();
    let text_contents = {
        let text: Vec<_> = noinclude
            .inclusive_descendants()
            .iter()
            .map(|node| node.text_contents())
            .collect();
        text.join("")
    };
    assert_eq!(&text_contents, "bar");
    noinclude.detach();
    assert_eq!(&client.transform_to_wikitext(&code).await?, "foobaz");
    let new = NoInclude::new(&WikiLink::new(
        "Main Page",
        &Wikicode::new_text("text"),
    ));
    code.append(&new);
    assert_eq!(
        &client.transform_to_wikitext(&code).await?,
        "foobaz<noinclude>[[Main Page|text]]</noinclude>"
    );
    Ok(())
}

#[tokio::test]
async fn test_noinclude_children() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("foo<noinclude>[[bar]] baz</noinclude>!")
        .await?
        .into_mutable();
    let noinclude = code.filter_noinclude()[0].clone();
    let links: Vec<WikiLink> = noinclude
        .inclusive_descendants()
        .iter()
        .filter_map(|node| node.as_wikilink())
        .collect();
    assert_eq!(&links[0].target(), "Bar");
    Ok(())
}

#[tokio::test]
async fn test_onlyinclude() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client
        .transform_to_html("foo<onlyinclude>bar</onlyinclude>baz")
        .await?
        .into_mutable();
    let onlyincludes = code.filter_onlyinclude();
    assert_eq!(onlyincludes.len(), 1);
    let onlyinclude = onlyincludes[0].clone();
    let text_contents = {
        let text: Vec<_> = onlyinclude
            .inclusive_descendants()
            .iter()
            .map(|node| node.text_contents())
            .collect();
        text.join("")
    };
    assert_eq!(&text_contents, "bar");
    onlyinclude.detach();
    assert_eq!(&client.transform_to_wikitext(&code).await?, "foobaz");
    let new = OnlyInclude::new(&WikiLink::new(
        "Main Page",
        &Wikicode::new_text("text"),
    ));
    code.append(&new);
    assert_eq!(
        &client.transform_to_wikitext(&code).await?,
        "foobaz<onlyinclude>[[Main Page|text]]</onlyinclude>"
    );
    Ok(())
}

#[tokio::test]
async fn test_placeholder() -> Result<()> {
    let client = test_client::testwp_client();
    // This was copied out of Parsoid's parser tests
    let code = client
        .transform_to_html(";one</b>two :bad tag fun")
        .await?
        .into_mutable();
    let placeholders: Vec<_> = code
        .descendants()
        .filter_map(|node| node.as_placeholder())
        .collect();
    assert_eq!(placeholders.len(), 1);
    Ok(())
}

#[tokio::test]
async fn test_displayspace() -> Result<()> {
    let client = test_client::testwp_client();
    // This was copied out of Parsoid's parser tests
    let code = client
        .transform_to_html("<nowiki>test : 123</nowiki>")
        .await?
        .into_mutable();
    let displayspaces: Vec<_> = code
        .inclusive_descendants()
        .filter_map(|node| node.as_displayspace())
        .collect();
    assert_eq!(displayspaces.len(), 1);
    Ok(())
}

#[tokio::test]
async fn test_spec_version() -> Result<()> {
    let client = test_client::testwp_client();
    let code = client.get("Main Page").await?.into_mutable();
    let version = code.spec_version().unwrap();
    // The version is 2.x
    assert!(version.starts_with("2."));
    let sp: Vec<_> = version.split('.').collect();
    // and has three parts.
    assert_eq!(sp.len(), 3);
    Ok(())
}

/// Regression test for extension-generated <section> tags
/// https://gitlab.com/mwbot-rs/parsoid/-/issues/14
#[tokio::test]
async fn test_extension_sections() -> Result<()> {
    let client = test_client::mw_client();
    let code = client.get("Help:TemplateData").await?.into_mutable();
    for section in code.iter_sections() {
        // Verify none of these panic
        section.section_id();
        section.is_pseudo_section();
        section.heading();
    }
    for section in code
        .inclusive_descendants()
        .filter_map(|node| node.as_section())
    {
        // Verify none of these panic
        section.section_id();
        section.is_pseudo_section();
        section.heading();
    }
    Ok(())
}

#[tokio::test]
async fn test_magic_links() -> Result<()> {
    let client = test_client::testwp_client();
    // Assert magic links are correctly detected
    let code = client
        .transform_to_html(
            r#"
PMID 1234
ISBN 978-0-307-26395-7
RFC 4321
"#,
        )
        .await?
        .into_mutable();
    let links = code.filter_links();
    assert!(links[0].is_isbn_magic_link());
    let extlinks = code.filter_external_links();
    assert!(extlinks[0].is_magic_link());
    assert!(extlinks[1].is_magic_link());
    // Assert wikitext equivalents aren't
    let code = client
        .transform_to_html(
            r#"
[[pmid:1234|PMID 1234]]
[[Special:BookSources/9780307263957|ISBN 978-0-307-26395-7]]
[[rfc:4321|RFC 4321]]
"#,
        )
        .await?
        .into_mutable();
    let links = code.filter_links();
    assert!(!links[0].is_isbn_magic_link());
    assert_eq!(code.filter_external_links().len(), 0);
    assert_eq!(
        code.descendants()
            .filter_map(|node| node.as_interwiki_link())
            .count(),
        2
    );

    Ok(())
}

#[tokio::test]
async fn test_templatearg() -> Result<()> {
    let client = test_client::enwp_client();
    // Reduced test case of a multi-part template with a "templatearg"
    let wikitext = "{{discussion-top}}{{{1|}}}{{discussion-bottom}}";
    let code = client.transform_to_html(wikitext).await?.into_mutable();
    // No error
    code.filter_templates()?;
    let converted = client.transform_to_wikitext(&code).await?;
    assert_eq!(converted, wikitext);
    // T330371 regression test
    let code = client
        .get_revision("Talk:Signaling of the New York City Subway", 1095492368)
        .await?
        .into_mutable();
    // No error
    code.filter_templates()?;

    Ok(())
}