nab 0.8.2

Token-optimized HTTP client for LLMs — fetches any URL as clean markdown
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
//! Auto-login orchestration
//!
//! Combines form detection, credential retrieval, and OTP handling
//! to automate login flows.

use anyhow::{Context, Result};
use tracing::{debug, info, warn};

use crate::auth::{Credential, OnePasswordAuth, OtpRetriever};
use crate::form::Form;
use crate::http_client::AcceleratedClient;
use crate::js_engine::JsEngine;

/// Login flow orchestrator
pub struct LoginFlow {
    client: AcceleratedClient,
    one_password: Option<OnePasswordAuth>,
    cookie_header: Option<String>,
    #[cfg(feature = "browser")]
    use_browser: bool,
}

impl LoginFlow {
    /// Create a new login flow
    pub fn new(
        client: AcceleratedClient,
        use_1password: bool,
        cookie_header: Option<String>,
    ) -> Self {
        let one_password = if use_1password {
            Some(OnePasswordAuth::new(None))
        } else {
            None
        };

        Self {
            client,
            one_password,
            cookie_header,
            #[cfg(feature = "browser")]
            use_browser: false,
        }
    }

    /// Enable browser-based login (requires browser feature)
    #[cfg(feature = "browser")]
    #[must_use]
    pub fn with_browser(mut self, enable: bool) -> Self {
        self.use_browser = enable;
        self
    }

    /// Execute login flow
    ///
    /// 1. Fetch login page
    /// 2. Detect login form
    /// 3. Get credentials from 1Password
    /// 4. Fill and submit form
    /// 5. Handle MFA if needed
    /// 6. Return final page
    #[allow(clippy::too_many_lines)] // Complex login flow; splitting would obscure the flow
    pub async fn login(&self, url: &str) -> Result<LoginResult> {
        // Use browser-based login if enabled
        #[cfg(feature = "browser")]
        if self.use_browser {
            return self.browser_login(url).await;
        }

        info!("Starting login flow for {}", url);

        // Step 1: Fetch the login page
        debug!("Fetching login page...");
        let page_html = if let Some(ref cookie) = self.cookie_header {
            // Fetch with cookies using raw client
            let response = self
                .client
                .inner()
                .get(url)
                .header("Cookie", cookie)
                .send()
                .await?;
            response.text().await?
        } else {
            self.client.fetch_text(url).await?
        };

        // Step 2: Detect CAPTCHA / SPA before form detection
        let captcha = Self::detect_captcha(&page_html);
        let is_spa = Self::detect_spa(&page_html);

        // Step 3: Detect login form
        debug!("Detecting login form...");
        let form_result = Form::find_login_form(&page_html).context("Failed to parse forms")?;

        // If no form found, try QuickJS execution for SPA forms
        let mut form = if let Some(f) = form_result {
            f
        } else {
            // Check if page has inline scripts that might render forms
            let has_inline_scripts = Self::has_inline_scripts(&page_html);

            if has_inline_scripts && is_spa {
                info!(
                    "No static form found, but inline scripts detected. Attempting QuickJS execution..."
                );

                // Try to execute inline scripts and extract rendered DOM
                match JsEngine::new() {
                    Ok(js_engine) => {
                        match js_engine.execute_and_extract_forms(&page_html) {
                            Ok(rendered_html) => {
                                debug!("QuickJS execution completed, re-parsing for forms...");

                                // Try to find form in rendered HTML
                                if let Ok(Some(rendered_form)) =
                                    Form::find_login_form(&rendered_html)
                                {
                                    info!("✓ Found login form after JavaScript execution");
                                    rendered_form
                                } else {
                                    warn!(
                                        "JavaScript executed but no login form found in rendered output"
                                    );

                                    // Try cookie-based auth as fallback
                                    if self.cookie_header.is_some() {
                                        info!("Attempting cookie-based authentication instead...");
                                        return self.cookie_auth_fallback(url).await;
                                    }

                                    #[cfg(feature = "browser")]
                                    anyhow::bail!(
                                        "No login form found (SPA detected, JavaScript executed).\n\
                                         💡 Try browser-based login: nab login <url> --browser\n\
                                         💡 Or log in via your browser, then use: nab fetch <url> --cookies brave"
                                    );
                                    #[cfg(not(feature = "browser"))]
                                    anyhow::bail!(
                                        "No login form found (SPA detected, JavaScript executed).\n\
                                         💡 Log in via your browser, then use: nab fetch <url> --cookies brave"
                                    );
                                }
                            }
                            Err(e) => {
                                warn!("JavaScript execution failed: {}", e);

                                // Try cookie-based auth as fallback
                                if self.cookie_header.is_some() {
                                    info!("Attempting cookie-based authentication instead...");
                                    return self.cookie_auth_fallback(url).await;
                                }

                                #[cfg(feature = "browser")]
                                anyhow::bail!(
                                    "No login form found (JavaScript execution failed).\n\
                                     💡 Try browser-based login: nab login <url> --browser\n\
                                     💡 Or log in via your browser, then use: nab fetch <url> --cookies brave"
                                );
                                #[cfg(not(feature = "browser"))]
                                anyhow::bail!(
                                    "No login form found (JavaScript execution failed).\n\
                                     💡 Log in via your browser, then use: nab fetch <url> --cookies brave"
                                );
                            }
                        }
                    }
                    Err(e) => {
                        warn!("Failed to create JavaScript engine: {}", e);

                        // Try cookie-based auth as fallback
                        if self.cookie_header.is_some() {
                            info!("Attempting cookie-based authentication instead...");
                            return self.cookie_auth_fallback(url).await;
                        }

                        anyhow::bail!(
                            "No login form found and JavaScript engine initialization failed"
                        );
                    }
                }
            } else {
                if is_spa {
                    warn!("No login form found — this appears to be a SPA (React/Vue/Angular)");
                    warn!(
                        "SPA login forms are rendered client-side and not visible to HTTP requests"
                    );
                }

                // Try cookie-based auth as fallback
                if self.cookie_header.is_some() {
                    info!("Attempting cookie-based authentication instead...");
                    return self.cookie_auth_fallback(url).await;
                }

                if is_spa {
                    #[cfg(feature = "browser")]
                    anyhow::bail!(
                        "No login form found (SPA detected).\n\
                         💡 Try browser-based login: nab login <url> --browser\n\
                         💡 Or log in via your browser, then use: nab fetch <url> --cookies brave"
                    );
                    #[cfg(not(feature = "browser"))]
                    anyhow::bail!(
                        "No login form found (SPA detected).\n\
                         💡 Log in via your browser, then use: nab fetch <url> --cookies brave"
                    );
                }

                anyhow::bail!("No login form found on page");
            }
        };

        // If CAPTCHA detected, warn and try cookie fallback or browser
        if let Some(ref captcha_type) = captcha {
            warn!("{} detected on login form", captcha_type);
            if self.cookie_header.is_some() {
                info!("Falling back to browser cookie authentication...");
                return self.cookie_auth_fallback(url).await;
            }
            #[cfg(feature = "browser")]
            warn!("💡 CAPTCHA detected. Try browser-based login: nab login <url> --browser");
            #[cfg(not(feature = "browser"))]
            warn!("💡 CAPTCHA may block login. Try: nab fetch <url> --cookies brave");
        }

        info!("Found login form: {} {}", form.method, form.action);

        // Step 3: Get credentials
        let credential = if let Some(ref op) = self.one_password {
            debug!("Getting credentials from 1Password...");
            op.get_credential_for_url(url)?
                .context("No credentials found in 1Password for this URL")?
        } else {
            anyhow::bail!("1Password authentication required but not enabled");
        };

        info!("Found credential: {}", credential.title);

        // Step 4: Fill form with credentials
        Self::fill_form_with_credential(&mut form, &credential)?;

        // Step 5: Resolve action URL and submit
        let action_url = form.resolve_action(url)?;
        debug!("Submitting form to: {}", action_url);

        let form_data = form.encode_urlencoded();

        // Extract origin from URL for CSRF protection
        let origin = extract_origin(url)?;

        let mut request = self
            .client
            .inner()
            .post(&action_url)
            .header("Content-Type", form.content_type())
            .header("Referer", url)
            .header("Origin", &origin);

        // Add cookies if available
        if let Some(ref cookie) = self.cookie_header {
            request = request.header("Cookie", cookie);
        }

        let response = request.body(form_data).send().await?;

        let final_url = response.url().to_string();
        let mut body = response.text().await?;

        // Step 6: Check for success first, then MFA if login didn't succeed
        let login_succeeded = !final_url.to_lowercase().contains("login")
            && !final_url.to_lowercase().contains("sign")
            || body.to_lowercase().contains("welcome")
            || body.to_lowercase().contains("dashboard")
            || body.to_lowercase().contains("my account");

        if !login_succeeded && Self::detect_mfa_required(&body) {
            info!("MFA required, attempting to get OTP...");
            body = self.handle_mfa(url, &body, &credential).await?;
        }

        Ok(LoginResult {
            success: true,
            final_url,
            body,
            message: "Login successful".to_string(),
        })
    }

    /// Fill form fields with credential data
    ///
    /// Supports both exact field names (`email`) and nested/namespaced names
    /// like Rails (`user_session[email]`), Django (`auth-email`), or
    /// generic patterns (`login_email`).
    #[allow(clippy::unnecessary_wraps)] // API consistency: callers use ? operator
    fn fill_form_with_credential(form: &mut Form, credential: &Credential) -> Result<()> {
        // Common username field name patterns
        let username_patterns = [
            "username",
            "user",
            "email",
            "login",
            "log",
            "user_name",
            "user_email",
            "email_address",
        ];
        // Common password field name patterns
        let password_patterns = ["password", "pass", "passwd", "pwd"];

        // Find and fill username field
        if let Some(ref username) = credential.username
            && let Some(key) = Self::find_matching_field(&form.fields, &username_patterns)
        {
            debug!("Filling username field: {}", key);
            form.fields.insert(key, username.clone());
        }

        // Find and fill password field
        if let Some(ref password) = credential.password
            && let Some(key) = Self::find_matching_field(&form.fields, &password_patterns)
        {
            debug!("Filling password field: {}", key);
            form.fields.insert(key, password.clone());
        }

        Ok(())
    }

    /// Find a form field matching any of the given patterns.
    /// Checks exact match first, then substring match for nested names
    /// like `user_session[email]` or `auth-password`.
    fn find_matching_field(
        fields: &std::collections::HashMap<String, String>,
        patterns: &[&str],
    ) -> Option<String> {
        // Pass 1: exact match (highest priority)
        for pattern in patterns {
            if fields.contains_key(*pattern) {
                return Some(pattern.to_string());
            }
        }
        // Pass 2: field name contains pattern (handles nested/namespaced names)
        for pattern in patterns {
            for key in fields.keys() {
                let key_lower = key.to_lowercase();
                if key_lower.contains(pattern) {
                    return Some(key.clone());
                }
            }
        }
        None
    }

    /// Detect if MFA is required from the response
    /// Only detects MFA if there's a non-login form with a visible OTP/code input field
    fn detect_mfa_required(html: &str) -> bool {
        if let Ok(forms) = Form::parse_all(html) {
            return forms.iter().any(|f| {
                // Skip login forms (they have password fields)
                if f.is_login_form {
                    return false;
                }
                // Check for visible (non-hidden) fields that look like OTP inputs
                f.fields.keys().any(|k| {
                    // Skip hidden fields (CSRF tokens like authenticity_token, csrf_token)
                    if f.hidden_fields.contains_key(k) {
                        return false;
                    }
                    let k_lower = k.to_lowercase();
                    k_lower.contains("otp")
                        || k_lower.contains("totp")
                        || k_lower.contains("2fa")
                        || k_lower.contains("verification_code")
                        || k_lower.contains("security_code")
                        || (k_lower.contains("code")
                            && !k_lower.contains("zip")
                            && !k_lower.contains("postal")
                            && !k_lower.contains("promo")
                            && !k_lower.contains("discount")
                            && !k_lower.contains("country"))
                })
            });
        }
        false
    }

    /// Handle MFA challenge
    async fn handle_mfa(
        &self,
        base_url: &str,
        html: &str,
        credential: &Credential,
    ) -> Result<String> {
        // Try to get OTP code
        let otp_code = if credential.has_totp {
            // Try 1Password TOTP first
            if let Some(ref op) = self.one_password {
                debug!("Getting TOTP from 1Password...");
                if let Ok(Some(cred)) = op.get_credential_for_url(base_url) {
                    if let Some(totp) = cred.totp {
                        info!("Got TOTP from 1Password");
                        totp
                    } else {
                        Self::get_otp_from_other_sources(base_url)?
                    }
                } else {
                    Self::get_otp_from_other_sources(base_url)?
                }
            } else {
                Self::get_otp_from_other_sources(base_url)?
            }
        } else {
            Self::get_otp_from_other_sources(base_url)?
        };

        // Find MFA form
        let forms = Form::parse_all(html)?;
        let mut mfa_form = forms
            .into_iter()
            .find(|f| {
                f.fields.keys().any(|k| {
                    let k_lower = k.to_lowercase();
                    k_lower.contains("code") || k_lower.contains("otp") || k_lower.contains("token")
                })
            })
            .context("No MFA form found")?;

        // Fill OTP code
        for key in mfa_form.fields.clone().keys() {
            let key_lower = key.to_lowercase();
            if key_lower.contains("code")
                || key_lower.contains("otp")
                || key_lower.contains("token")
            {
                debug!("Filling MFA field: {}", key);
                mfa_form.fields.insert(key.clone(), otp_code.clone());
                break;
            }
        }

        // Submit MFA form
        let action_url = mfa_form.resolve_action(base_url)?;
        let form_data = mfa_form.encode_urlencoded();

        debug!("Submitting MFA form to: {}", action_url);
        let mut request = self
            .client
            .inner()
            .post(&action_url)
            .header("Content-Type", mfa_form.content_type());

        // Add cookies if available
        if let Some(ref cookie) = self.cookie_header {
            request = request.header("Cookie", cookie);
        }

        let response = request.body(form_data).send().await?;

        Ok(response.text().await?)
    }

    /// Get OTP from SMS or email sources
    fn get_otp_from_other_sources(domain: &str) -> Result<String> {
        if let Some(otp_code) = OtpRetriever::get_otp_for_domain(domain)? {
            info!("Got OTP from {}", otp_code.source);
            return Ok(otp_code.code);
        }
        anyhow::bail!("No OTP code available")
    }

    /// Detect CAPTCHA presence in HTML
    fn detect_captcha(html: &str) -> Option<String> {
        let html_lower = html.to_lowercase();
        if html_lower.contains("g-recaptcha") || html_lower.contains("grecaptcha") {
            Some("reCAPTCHA".to_string())
        } else if html_lower.contains("h-captcha") || html_lower.contains("hcaptcha") {
            Some("hCaptcha".to_string())
        } else if html_lower.contains("cf-turnstile") {
            Some("Cloudflare Turnstile".to_string())
        } else if html_lower.contains("captcha") && html_lower.contains("<img") {
            Some("Image CAPTCHA".to_string())
        } else {
            None
        }
    }

    /// Detect if page is a SPA (no server-rendered form)
    fn detect_spa(html: &str) -> bool {
        let html_lower = html.to_lowercase();
        let has_spa_markers = html_lower.contains("id=\"root\"")
            || html_lower.contains("id=\"app\"")
            || html_lower.contains("id=\"__next\"")
            || html_lower.contains("id=\"__nuxt\"")
            || html_lower.contains("ng-app")
            || html_lower.contains("data-reactroot");
        let has_no_form = !html_lower.contains("<form");
        has_spa_markers && has_no_form
    }

    /// Check if HTML contains inline scripts (not external src=)
    fn has_inline_scripts(html: &str) -> bool {
        // Look for <script> tags without src attribute
        // Simple heuristic: contains <script> but not all have src=
        let html_lower = html.to_lowercase();
        if !html_lower.contains("<script") {
            return false;
        }

        // Check if there are any script tags without src attribute
        // by looking for <script> followed by > (not src=)
        html_lower.contains("<script>") || html_lower.contains("<script ")
    }

    /// Fallback: use browser cookies to access the target URL directly
    async fn cookie_auth_fallback(&self, url: &str) -> Result<LoginResult> {
        let cookie = self
            .cookie_header
            .as_ref()
            .context("No browser cookies available for fallback")?;

        let response = self
            .client
            .inner()
            .get(url)
            .header("Cookie", cookie)
            .send()
            .await?;

        let final_url = response.url().to_string();
        let status = response.status();
        let body = response.text().await?;

        let success = status.is_success()
            && !final_url.to_lowercase().contains("login")
            && !final_url.to_lowercase().contains("signin");

        Ok(LoginResult {
            success,
            final_url,
            body,
            message: if success {
                "Authenticated via browser cookies".to_string()
            } else {
                "Cookie auth attempted but session may be expired".to_string()
            },
        })
    }

    /// Save session cookies to disk
    pub fn save_session(&self, _url: &str, _save: bool) -> Result<()> {
        // Session cookie saving will use the cookie jar from AcceleratedClient
        // The reqwest client already handles cookies automatically
        // For now, we can skip explicit session saving since cookies are
        // maintained in memory during the client lifetime

        // Future: implement persistent session storage in ~/.nab/sessions/
        warn!("Session saving not yet implemented (cookies maintained in memory)");
        Ok(())
    }

    /// Browser-based login flow (requires browser feature)
    #[cfg(feature = "browser")]
    async fn browser_login(&self, url: &str) -> Result<LoginResult> {
        use crate::browser::BrowserLogin;

        info!("Starting browser-based login for {}", url);

        // Connect to running Chrome instance
        let browser = BrowserLogin::connect(None).await
            .context("Failed to connect to Chrome. Start Chrome with: google-chrome --remote-debugging-port=9222")?;

        // Get credentials from 1Password if available
        let credential = if let Some(ref op) = self.one_password {
            debug!("Getting credentials from 1Password...");
            op.get_credential_for_url(url)?
        } else {
            None
        };

        // Perform browser login
        let cookies = browser
            .login(url, credential.as_ref())
            .await
            .context("Browser login failed")?;

        info!("Browser login successful, got {} cookies", cookies.len());

        // Convert cookies to header format
        let cookie_header = BrowserLogin::cookies_to_header(&cookies);

        // Fetch the page with the authenticated session
        let response = self
            .client
            .inner()
            .get(url)
            .header("Cookie", &cookie_header)
            .send()
            .await?;

        let final_url = response.url().to_string();
        let body = response.text().await?;

        Ok(LoginResult {
            success: true,
            final_url,
            body,
            message: "Browser login successful".to_string(),
        })
    }
}

/// Result of a login attempt
#[derive(Debug, Clone)]
pub struct LoginResult {
    pub success: bool,
    pub final_url: String,
    pub body: String,
    pub message: String,
}

/// Extract origin (scheme + host) from URL for CSRF protection
fn extract_origin(url: &str) -> Result<String> {
    let parsed = url::Url::parse(url).context("Invalid URL")?;
    let scheme = parsed.scheme();
    let host = parsed.host_str().context("No host in URL")?;
    let port = parsed.port();

    if let Some(p) = port {
        Ok(format!("{scheme}://{host}:{p}"))
    } else {
        Ok(format!("{scheme}://{host}"))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_detect_mfa_required() {
        let html_with_mfa = r#"
            <html>
                <body>
                    <form>
                        <input name="verification_code">
                    </form>
                </body>
            </html>
        "#;

        assert!(LoginFlow::detect_mfa_required(html_with_mfa));

        let html_without_mfa = r"
            <html>
                <body>
                    <p>Welcome back!</p>
                </body>
            </html>
        ";

        assert!(!LoginFlow::detect_mfa_required(html_without_mfa));
    }

    #[test]
    fn test_fill_form_with_credential() {
        use std::collections::HashMap;

        let mut form = Form {
            action: "/login".to_string(),
            method: "POST".to_string(),
            enctype: "application/x-www-form-urlencoded".to_string(),
            fields: HashMap::from([
                ("username".to_string(), String::new()),
                ("password".to_string(), String::new()),
            ]),
            hidden_fields: HashMap::new(),
            is_login_form: true,
        };

        let credential = Credential {
            title: "Test Site".to_string(),
            username: Some("testuser".to_string()),
            password: Some("testpass".to_string()),
            url: None,
            totp: None,
            has_totp: false,
            passkey_credential_id: None,
        };

        LoginFlow::fill_form_with_credential(&mut form, &credential).unwrap();

        assert_eq!(form.fields.get("username"), Some(&"testuser".to_string()));
        assert_eq!(form.fields.get("password"), Some(&"testpass".to_string()));
    }

    #[test]
    fn test_has_inline_scripts() {
        // HTML with inline script
        let html_with_inline = r"
            <html>
                <head>
                    <script>console.log('hello');</script>
                </head>
                <body></body>
            </html>
        ";
        assert!(LoginFlow::has_inline_scripts(html_with_inline));

        // HTML with external script only - not tested since has_inline_scripts is a heuristic
        // that checks for any <script> tag; QuickJS execution skips external scripts

        // HTML with no scripts
        let html_no_script = r"
            <html>
                <body><p>No scripts here</p></body>
            </html>
        ";
        assert!(!LoginFlow::has_inline_scripts(html_no_script));
    }

    #[test]
    fn test_detect_spa() {
        // SPA with root div and no form
        let spa_html = r#"
            <html>
                <body>
                    <div id="root"></div>
                </body>
            </html>
        "#;
        assert!(LoginFlow::detect_spa(spa_html));

        // Regular HTML with form
        let regular_html = r#"
            <html>
                <body>
                    <form action="/login">
                        <input name="username">
                    </form>
                </body>
            </html>
        "#;
        assert!(!LoginFlow::detect_spa(regular_html));

        // React app marker
        let react_html = r#"
            <html>
                <body>
                    <div id="app"></div>
                </body>
            </html>
        "#;
        assert!(LoginFlow::detect_spa(react_html));
    }
}