lightdom-test 0.1.3

A lightweight Rust library for testing HTML interactions without browser automation
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
use anyhow::Result;
use lightdom_test::{Dom, HttpRequest, HttpResponse, HttpTransport, StatusCode};
use std::sync::{Arc, Mutex};

/// モック Transport 実装
#[derive(Clone, Debug)]
struct MockTransport {
    captured_requests: Arc<Mutex<Vec<HttpRequest>>>,
    response: HttpResponse,
}

impl MockTransport {
    fn new(response: HttpResponse) -> Self {
        Self {
            captured_requests: Arc::new(Mutex::new(Vec::new())),
            response,
        }
    }
}

#[async_trait::async_trait]
impl HttpTransport for MockTransport {
    async fn send(&self, req: HttpRequest) -> Result<HttpResponse> {
        self.captured_requests.lock().unwrap().push(req.clone());
        Ok(self.response.clone())
    }
}

fn default_response() -> HttpResponse {
    HttpResponse {
        status: StatusCode(200),
        headers: Default::default(),
        body: "OK".to_string(),
    }
}

// ============================================
// Element API Tests
// ============================================

#[tokio::test]
async fn test_element_by_id() -> Result<()> {
    let html = r#"
        <div id="content">Hello World</div>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let element = dom.element("#content")?;

    assert_eq!(element.text().trim(), "Hello World");
    Ok(())
}

#[tokio::test]
async fn test_element_by_test_id() -> Result<()> {
    let html = r#"
        <div test-id="main-content">Test Content</div>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let element = dom.element("@main-content")?;

    assert_eq!(element.text().trim(), "Test Content");
    Ok(())
}

#[tokio::test]
async fn test_element_by_class() -> Result<()> {
    let html = r#"
        <div class="container">Container Content</div>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let element = dom.element(".container")?;

    assert_eq!(element.text().trim(), "Container Content");
    Ok(())
}

#[tokio::test]
async fn test_elements_multiple() -> Result<()> {
    let html = r#"
        <div class="item">Item 1</div>
        <div class="item">Item 2</div>
        <div class="item">Item 3</div>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let elements = dom.elements(".item");

    assert_eq!(elements.len(), 3);
    assert_eq!(elements[0].text().trim(), "Item 1");
    assert_eq!(elements[1].text().trim(), "Item 2");
    assert_eq!(elements[2].text().trim(), "Item 3");
    Ok(())
}

#[tokio::test]
async fn test_element_attr() -> Result<()> {
    let html = r#"
        <a id="link" href="/page" title="Go to page">Link</a>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let element = dom.element("#link")?;

    assert_eq!(element.attr("href"), Some("/page".to_string()));
    assert_eq!(element.attr("title"), Some("Go to page".to_string()));
    assert_eq!(element.attr("nonexistent"), None);
    Ok(())
}

#[tokio::test]
async fn test_element_has_class() -> Result<()> {
    let html = r#"
        <div id="box" class="container primary active">Content</div>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let element = dom.element("#box")?;

    assert!(element.has_class("container"));
    assert!(element.has_class("primary"));
    assert!(element.has_class("active"));
    assert!(!element.has_class("hidden"));
    Ok(())
}

#[tokio::test]
async fn test_element_inner_html() -> Result<()> {
    let html = r#"
        <div id="wrapper"><span>Hello</span> <strong>World</strong></div>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let element = dom.element("#wrapper")?;

    let inner = element.inner_html();
    assert!(inner.contains("<span>Hello</span>"));
    assert!(inner.contains("<strong>World</strong>"));
    Ok(())
}

#[tokio::test]
async fn test_element_not_found() {
    let html = r#"
        <div id="content">Content</div>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string()).unwrap();
    let result = dom.element("#nonexistent");

    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Element not found"));
}

// ============================================
// Text API Tests
// ============================================

#[tokio::test]
async fn test_text_single() -> Result<()> {
    let html = r#"
        <p id="message">Hello World</p>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let text = dom.text("#message")?;

    assert_eq!(text.trim(), "Hello World");
    Ok(())
}

#[tokio::test]
async fn test_texts_multiple() -> Result<()> {
    let html = r#"
        <p class="para">First paragraph</p>
        <p class="para">Second paragraph</p>
        <p class="para">Third paragraph</p>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let texts = dom.texts(".para");

    assert_eq!(texts.len(), 3);
    assert_eq!(texts[0].trim(), "First paragraph");
    assert_eq!(texts[1].trim(), "Second paragraph");
    assert_eq!(texts[2].trim(), "Third paragraph");
    Ok(())
}

#[tokio::test]
async fn test_inner_html_from_dom() -> Result<()> {
    let html = r#"
        <div id="content"><h1>Title</h1><p>Paragraph</p></div>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let inner = dom.inner_html("#content")?;

    assert!(inner.contains("<h1>Title</h1>"));
    assert!(inner.contains("<p>Paragraph</p>"));
    Ok(())
}

// ============================================
// Table API Tests
// ============================================

#[tokio::test]
async fn test_table_headers() -> Result<()> {
    let html = r#"
        <table id="users">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Age</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Alice</td>
                    <td>alice@example.com</td>
                    <td>25</td>
                </tr>
            </tbody>
        </table>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let table = dom.table("#users")?;

    let headers = table.headers();
    assert_eq!(headers.len(), 3);
    assert_eq!(headers[0], "Name");
    assert_eq!(headers[1], "Email");
    assert_eq!(headers[2], "Age");
    Ok(())
}

#[tokio::test]
async fn test_table_rows() -> Result<()> {
    let html = r#"
        <table id="data">
            <tr>
                <th>A</th>
                <th>B</th>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>3</td>
                <td>4</td>
            </tr>
        </table>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let table = dom.table("#data")?;

    let rows = table.rows();
    assert_eq!(rows.len(), 2);

    let row0_cells = rows[0].cells();
    assert_eq!(row0_cells[0], "1");
    assert_eq!(row0_cells[1], "2");

    let row1_cells = rows[1].cells();
    assert_eq!(row1_cells[0], "3");
    assert_eq!(row1_cells[1], "4");
    Ok(())
}

#[tokio::test]
async fn test_table_row() -> Result<()> {
    let html = r#"
        <table id="products">
            <tr>
                <th>Product</th>
                <th>Price</th>
            </tr>
            <tr>
                <td>Apple</td>
                <td>$1.00</td>
            </tr>
            <tr>
                <td>Banana</td>
                <td>$0.50</td>
            </tr>
        </table>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let table = dom.table("#products")?;

    let row = table.row(0)?;
    assert_eq!(row.cell(0)?, "Apple");
    assert_eq!(row.cell(1)?, "$1.00");
    Ok(())
}

#[tokio::test]
async fn test_table_cell() -> Result<()> {
    let html = r#"
        <table id="grid">
            <tr>
                <td>A1</td>
                <td>A2</td>
            </tr>
            <tr>
                <td>B1</td>
                <td>B2</td>
            </tr>
        </table>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let table = dom.table("#grid")?;

    assert_eq!(table.cell(0, 0)?, "A1");
    assert_eq!(table.cell(0, 1)?, "A2");
    assert_eq!(table.cell(1, 0)?, "B1");
    assert_eq!(table.cell(1, 1)?, "B2");
    Ok(())
}

#[tokio::test]
async fn test_table_row_get_by_column_name() -> Result<()> {
    let html = r#"
        <table id="users">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Bob</td>
                    <td>bob@example.com</td>
                </tr>
            </tbody>
        </table>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let table = dom.table("#users")?;

    let row = table.row(0)?;
    assert_eq!(row.get("Name")?, "Bob");
    assert_eq!(row.get("Email")?, "bob@example.com");
    Ok(())
}

#[tokio::test]
async fn test_table_find_row() -> Result<()> {
    let html = r#"
        <table id="employees">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Department</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>001</td>
                    <td>Alice</td>
                    <td>Engineering</td>
                </tr>
                <tr>
                    <td>002</td>
                    <td>Bob</td>
                    <td>Sales</td>
                </tr>
                <tr>
                    <td>003</td>
                    <td>Charlie</td>
                    <td>Engineering</td>
                </tr>
            </tbody>
        </table>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let table = dom.table("#employees")?;

    let row = table.find_row("Name", "Bob")?;
    assert_eq!(row.get("ID")?, "002");
    assert_eq!(row.get("Department")?, "Sales");
    Ok(())
}

#[tokio::test]
async fn test_table_not_found() {
    let html = r#"
        <table id="data">
            <tr><td>Test</td></tr>
        </table>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string()).unwrap();
    let result = dom.table("#nonexistent");

    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("Table not found"));
}

#[tokio::test]
async fn test_table_row_out_of_bounds() {
    let html = r#"
        <table id="data">
            <tr><td>A</td></tr>
        </table>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string()).unwrap();
    let table = dom.table("#data").unwrap();
    let result = table.row(5);

    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("out of bounds"));
}

#[tokio::test]
async fn test_table_by_class() -> Result<()> {
    let html = r#"
        <table class="data-table">
            <tr>
                <th>Col1</th>
                <th>Col2</th>
            </tr>
            <tr>
                <td>Val1</td>
                <td>Val2</td>
            </tr>
        </table>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let table = dom.table(".data-table")?;

    let headers = table.headers();
    assert_eq!(headers.len(), 2);
    assert_eq!(headers[0], "Col1");
    assert_eq!(headers[1], "Col2");
    Ok(())
}

// ============================================
// List API Tests
// ============================================

#[tokio::test]
async fn test_list_items() -> Result<()> {
    let html = r#"
        <ul id="fruits">
            <li>Apple</li>
            <li>Banana</li>
            <li>Orange</li>
        </ul>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let list = dom.list("#fruits")?;

    let items = list.items();
    assert_eq!(items.len(), 3);
    assert_eq!(items[0], "Apple");
    assert_eq!(items[1], "Banana");
    assert_eq!(items[2], "Orange");
    Ok(())
}

#[tokio::test]
async fn test_list_item() -> Result<()> {
    let html = r#"
        <ol id="steps">
            <li>First</li>
            <li>Second</li>
            <li>Third</li>
        </ol>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let list = dom.list("#steps")?;

    assert_eq!(list.item(0)?, "First");
    assert_eq!(list.item(1)?, "Second");
    assert_eq!(list.item(2)?, "Third");
    Ok(())
}

#[tokio::test]
async fn test_list_len() -> Result<()> {
    let html = r#"
        <ul id="colors">
            <li>Red</li>
            <li>Green</li>
            <li>Blue</li>
            <li>Yellow</li>
        </ul>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let list = dom.list("#colors")?;

    assert_eq!(list.len(), 4);
    assert!(!list.is_empty());
    Ok(())
}

#[tokio::test]
async fn test_list_is_empty() -> Result<()> {
    let html = r#"
        <ul id="empty"></ul>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let list = dom.list("#empty")?;

    assert_eq!(list.len(), 0);
    assert!(list.is_empty());
    Ok(())
}

#[tokio::test]
async fn test_list_contains() -> Result<()> {
    let html = r#"
        <ul id="tasks">
            <li>Buy milk</li>
            <li>Walk the dog</li>
            <li>Read a book</li>
        </ul>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let list = dom.list("#tasks")?;

    assert!(list.contains("Buy milk"));
    assert!(list.contains("Walk the dog"));
    assert!(!list.contains("Write code"));
    Ok(())
}

#[tokio::test]
async fn test_list_not_found() {
    let html = r#"
        <ul id="items">
            <li>Item 1</li>
        </ul>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string()).unwrap();
    let result = dom.list("#nonexistent");

    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("List not found"));
}

#[tokio::test]
async fn test_list_item_out_of_bounds() {
    let html = r#"
        <ul id="items">
            <li>Item 1</li>
        </ul>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string()).unwrap();
    let list = dom.list("#items").unwrap();
    let result = list.item(5);

    assert!(result.is_err());
    assert!(result.unwrap_err().to_string().contains("out of bounds"));
}

#[tokio::test]
async fn test_list_by_class() -> Result<()> {
    let html = r#"
        <ul class="menu-items">
            <li>Home</li>
            <li>About</li>
            <li>Contact</li>
        </ul>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let list = dom.list(".menu-items")?;

    assert_eq!(list.len(), 3);
    assert!(list.contains("Home"));
    assert!(list.contains("About"));
    assert!(list.contains("Contact"));
    Ok(())
}

#[tokio::test]
async fn test_list_by_test_id() -> Result<()> {
    let html = r#"
        <ol test-id="ranking">
            <li>First Place</li>
            <li>Second Place</li>
            <li>Third Place</li>
        </ol>
    "#;

    let transport = MockTransport::new(default_response());
    let dom = Dom::new(transport).parse(html.to_string())?;
    let list = dom.list("@ranking")?;

    assert_eq!(list.len(), 3);
    assert_eq!(list.item(0)?, "First Place");
    Ok(())
}