1
// SPDX-FileCopyrightText: 2024 Kunal Mehta <legoktm@debian.org>
2
// SPDX-License-Identifier: GPL-3.0-or-later
3
use crate::prelude::*;
4
use crate::tests::test_client;
5
use anyhow::Result;
6
use std::collections::HashMap;
7

            
8
#[tokio::test]
9
3
async fn test_refs() -> Result<()> {
10
3
    let client = test_client::testwp_client();
11
3
    let code = client.get("Mwbot-rs/Cite").await?.into_mutable();
12
3
    let ref_links = code.filter_reference_links();
13
3
    dbg!(&ref_links);
14
3
    assert_eq!(
15
3
        ref_links.len(),
16
3
        code.descendants()
17
197
            .filter_map(|node| node.as_reference_link())
18
3
            .count()
19
3
    );
20
2

            
21
3
    let all_refs: HashMap<_, _> = code
22
3
        .filter_references()
23
3
        .into_iter()
24
7
        .map(|ref_| (ref_.id(), ref_))
25
3
        .collect();
26
3
    assert_eq!(
27
3
        all_refs.len(),
28
3
        code.descendants()
29
197
            .filter_map(|node| node.as_reference())
30
3
            .count()
31
3
    );
32
2

            
33
3
    assert_eq!(ref_links.len(), 4);
34
2
    // refs[0] == <ref>This is a [[reference]].</ref>
35
3
    assert_eq!(ref_links[0].group()?, None);
36
3
    assert_eq!(ref_links[0].name()?, None);
37
3
    assert_eq!(ref_links[0].text_contents(), "[1]");
38
3
    assert_eq!(
39
3
        ref_links[0].reference_id()?,
40
2
        "mw-reference-text-cite_note-1"
41
2
    );
42
3
    assert_eq!(ref_links[0].id(), "cite_ref-1");
43
3
    assert_eq!(
44
3
        all_refs
45
3
            .get(&ref_links[0].reference_id()?)
46
3
            .unwrap()
47
3
            .contents()
48
3
            .text_contents(),
49
2
        "This is a reference."
50
2
    );
51
3
    assert_eq!(ref_links[1].name()?, Some("foo".to_string()));
52
3
    assert_eq!(
53
3
        ref_links[1].reference_id()?,
54
2
        "mw-reference-text-cite_note-foo-2"
55
2
    );
56
3
    assert_eq!(ref_links[1].id(), "cite_ref-foo_2-0");
57
2
    // first use of the reference, so it isn't reused
58
3
    assert!(!ref_links[1].is_reused()?);
59
3
    assert_eq!(
60
3
        all_refs
61
3
            .get(&ref_links[1].reference_id()?)
62
3
            .unwrap()
63
3
            .contents()
64
3
            .text_contents(),
65
2
        "Named reference."
66
2
    );
67
3
    assert_eq!(ref_links[2].name()?, Some("foo".to_string()));
68
3
    assert_eq!(
69
3
        ref_links[2].reference_id()?,
70
2
        "mw-reference-text-cite_note-foo-2"
71
2
    );
72
3
    assert_eq!(ref_links[2].id(), "cite_ref-foo_2-1");
73
2
    // second use of it, so it is reused
74
3
    assert!(ref_links[2].is_reused()?);
75
3
    assert_eq!(
76
3
        all_refs
77
3
            .get(&ref_links[2].reference_id()?)
78
3
            .unwrap()
79
3
            .contents()
80
3
            .text_contents(),
81
2
        "Named reference."
82
2
    );
83
3
    assert_eq!(ref_links[3].group()?, Some("group".to_string()));
84
3
    assert_eq!(
85
3
        ref_links[3].reference_id()?,
86
2
        "mw-reference-text-cite_note-bar-3"
87
2
    );
88
3
    assert_eq!(ref_links[3].id(), "cite_ref-bar_3-0");
89
3
    assert_eq!(
90
3
        all_refs
91
3
            .get(&ref_links[3].reference_id()?)
92
3
            .unwrap()
93
3
            .contents()
94
3
            .text_contents(),
95
2
        "reference!"
96
2
    );
97
2

            
98
3
    ref_links[0].set_group("new-group".to_string())?;
99
3
    let wikitext = client.transform_to_wikitext(&code).await?;
100
3
    dbg!(&wikitext);
101
3
    assert!(&wikitext
102
3
        .contains("<ref group=\"new-group\">This is a [[reference]].</ref>"));
103
2

            
104
3
    Ok(())
105
2
}
106

            
107
/// Verify you can edit a template inside a <ref> node
108
#[tokio::test]
109
3
async fn test_mutablity() -> Result<()> {
110
3
    let client = test_client::testwp_client();
111
3
    let code = client.get("Mwbot-rs/Cite").await?.into_mutable();
112
3
    let refs_ = code.filter_references();
113
3
    let temps = refs_[0].filter_templates()?;
114
3
    dbg!(&refs_[0]);
115
3
    temps[0].set_param("2", "a second value")?;
116
2

            
117
3
    let wikitext = client.transform_to_wikitext(&code).await?;
118
3
    dbg!(&wikitext);
119
3
    assert!(wikitext.contains("{{1x|reference!|a second value}}"));
120
3
    Ok(())
121
2
}
122

            
123
#[tokio::test]
124
3
async fn test_reference_lists() -> Result<()> {
125
3
    let client = test_client::testwp_client();
126
3
    let code = client.get("Mwbot-rs/Cite").await?.into_mutable();
127
3

            
128
3
    let lists = code.filter_reference_lists();
129
3
    assert_eq!(
130
3
        lists.len(),
131
3
        code.descendants()
132
197
            .filter_map(|node| node.as_reference_list())
133
3
            .count()
134
3
    );
135
2

            
136
3
    dbg!(&lists);
137
3
    assert_eq!(lists.len(), 2);
138
2

            
139
3
    assert_eq!(lists[0].group()?, Some("group".to_string()));
140
3
    assert!(lists[1].is_auto_generated()?);
141
3
    Ok(())
142
2
}