four-word-networking 2.7.0

Convert IP addresses to memorable, family-friendly word groups. Interactive TUI with real-time autocomplete. IPv4 = 4 words, IPv6 = 6-12 words. Perfect reconstruction with progressive hints.
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
//! Adaptive four-word encoder that handles both IPv4 and IPv6 addresses.
//!
//! This is the main public API for four-word networking.

use crate::dictionary4k::DICTIONARY;
use crate::error::{FourWordError, Result};
use crate::four_word_encoder::FourWordEncoder;
use crate::four_word_ipv6_encoder::{FourWordIpv6Encoder, Ipv6FourWordGroupEncoding};
use crate::validation::{AutocompleteHelper, ValidationResult};
use std::net::{IpAddr, SocketAddr};

/// The main four-word networking encoder interface
pub struct FourWordAdaptiveEncoder {
    ipv4_encoder: FourWordEncoder,
    ipv6_encoder: FourWordIpv6Encoder,
}

impl FourWordAdaptiveEncoder {
    /// Creates a new four-word adaptive encoder
    pub fn new() -> Result<Self> {
        Ok(FourWordAdaptiveEncoder {
            ipv4_encoder: FourWordEncoder::new(),
            ipv6_encoder: FourWordIpv6Encoder::new(),
        })
    }

    /// Encodes any IP address into words
    /// - IPv4: Always exactly 4 words
    /// - IPv6: 6, 9, or 12 words based on compression
    pub fn encode(&self, input: &str) -> Result<String> {
        let addr = self.parse_address(input)?;

        match addr {
            SocketAddr::V4(_) => {
                let encoded = self.ipv4_encoder.encode(addr)?;
                Ok(encoded.to_string())
            }
            SocketAddr::V6(v6) => {
                let encoded = self.ipv6_encoder.encode(&v6)?;
                Ok(encoded.to_string())
            }
        }
    }

    /// Decodes words back to an IP address
    /// Port 65535 is treated as "no port specified" and omitted from output
    pub fn decode(&self, words: &str) -> Result<String> {
        // Determine separator and count words appropriately
        let word_count = if words.contains(' ') {
            // For space-separated words, filter out empty strings from trailing spaces
            words.split(' ').filter(|s| !s.is_empty()).count()
        } else if words.contains('.') {
            // Dot-separated (IPv4 or backwards compatibility)
            words.split('.').filter(|s| !s.is_empty()).count()
        } else if words.contains('-') {
            // Dash-separated (IPv6)
            words.split('-').filter(|s| !s.is_empty()).count()
        } else {
            // Single word or other format
            1
        };

        match word_count {
            4 => {
                // IPv4
                let addr = self.ipv4_encoder.decode(words)?;
                // If port is 65535, omit it (special marker for "no port specified")
                if addr.port() == 65535 {
                    Ok(addr.ip().to_string())
                } else {
                    Ok(addr.to_string())
                }
            }
            6 | 9 | 12 => {
                // IPv6 (6, 9, or 12 words including padding)
                let groups = self.parse_ipv6_groups(words)?;
                let decoded = self.ipv6_encoder.decode(&groups)?;
                let socket_addr = SocketAddr::V6(decoded);
                // If port is 65535, omit it (special marker for "no port specified")
                if socket_addr.port() == 65535 {
                    Ok(socket_addr.ip().to_string())
                } else {
                    Ok(socket_addr.to_string())
                }
            }
            _ => Err(FourWordError::InvalidWordCount {
                expected: 4, // or 6/8/12 for IPv6
                actual: word_count,
            }),
        }
    }

    /// Returns information about the encoding
    pub fn analyze(&self, input: &str) -> Result<String> {
        let addr = self.parse_address(input)?;

        match addr {
            SocketAddr::V4(v4) => Ok(format!(
                "IPv4 Address: {v4}\nEncoding: 4 words\nMethod: Perfect reconstruction"
            )),
            SocketAddr::V6(v6) => {
                let encoded = self.ipv6_encoder.encode(&v6)?;
                Ok(format!(
                    "IPv6 Address: {v6}\nCategory: {:?}\nEncoding: {} words\nMethod: Category-based compression",
                    encoded.category(),
                    encoded.word_count()
                ))
            }
        }
    }

    /// Parses an IP address string into a SocketAddr
    /// Uses port 65535 as a special marker for "no port specified"
    fn parse_address(&self, input: &str) -> Result<SocketAddr> {
        // Try parsing as socket address first
        if let Ok(addr) = input.parse::<SocketAddr>() {
            return Ok(addr);
        }

        // Try parsing as IP address (use port 65535 as marker for "no port specified")
        if let Ok(ip) = input.parse::<IpAddr>() {
            return Ok(match ip {
                IpAddr::V4(v4) => SocketAddr::new(IpAddr::V4(v4), 65535),
                IpAddr::V6(v6) => SocketAddr::new(IpAddr::V6(v6), 65535),
            });
        }

        Err(FourWordError::InvalidInput(format!(
            "Invalid IP address format: {input}"
        )))
    }

    /// Parses IPv6 word groups from a string
    fn parse_ipv6_groups(&self, words: &str) -> Result<Ipv6FourWordGroupEncoding> {
        use crate::four_word_ipv6_encoder::FourWordGroup;
        use crate::ipv6_compression::Ipv6Category;

        // Parse words and filter out empty strings
        let all_words: Vec<String> = if words.contains(' ') {
            words
                .split(' ')
                .filter(|s| !s.is_empty())
                .map(|s| s.to_string())
                .collect()
        } else if words.contains('.') {
            words
                .split('.')
                .filter(|s| !s.is_empty())
                .map(|s| s.to_string())
                .collect()
        } else {
            words
                .split('-')
                .filter(|s| !s.is_empty())
                .map(|s| s.to_string())
                .collect()
        };

        // IPv6 can have 6, 9, or 12 words
        if all_words.len() != 6 && all_words.len() != 9 && all_words.len() != 12 {
            return Err(FourWordError::InvalidWordCount {
                expected: 6, // or 9/12
                actual: all_words.len(),
            });
        }

        let mut groups = Vec::new();

        // Create groups of 4 words, handling 6 and 9 word cases properly
        match all_words.len() {
            6 => {
                // For 6 words, create 1 group of 4 and keep the remaining 2 words separately
                // This is handled by creating only 1.5 groups but since we can't have half groups,
                // we'll encode the extra 2 words by creating a partial second group
                groups.push(FourWordGroup::new(
                    all_words[0].clone(),
                    all_words[1].clone(),
                    all_words[2].clone(),
                    all_words[3].clone(),
                ));
                // For the remaining 2 words, we need to store them in the compressed data
                // rather than creating empty padding. The IPv6 encoder will handle this.
                // We'll use a special marker approach in the FourWordGroup structure
                groups.push(FourWordGroup::new(
                    all_words[4].clone(),
                    all_words[5].clone(),
                    "__MARKER_END__".to_string(), // Special marker to indicate end of data
                    "__MARKER_END__".to_string(), // Special marker to indicate end of data
                ));
            }
            9 => {
                // For 9 words, create 2 groups of 4 and keep the remaining 1 word separately
                groups.push(FourWordGroup::new(
                    all_words[0].clone(),
                    all_words[1].clone(),
                    all_words[2].clone(),
                    all_words[3].clone(),
                ));
                groups.push(FourWordGroup::new(
                    all_words[4].clone(),
                    all_words[5].clone(),
                    all_words[6].clone(),
                    all_words[7].clone(),
                ));
                // For the remaining 1 word, create a partial third group
                groups.push(FourWordGroup::new(
                    all_words[8].clone(),
                    "__MARKER_END__".to_string(), // Special marker to indicate end of data
                    "__MARKER_END__".to_string(), // Special marker to indicate end of data
                    "__MARKER_END__".to_string(), // Special marker to indicate end of data
                ));
            }
            12 => {
                // For 12 words, create groups of 4
                for chunk in all_words.chunks(4) {
                    groups.push(FourWordGroup::new(
                        chunk.first().cloned().unwrap_or_default(),
                        chunk.get(1).cloned().unwrap_or_default(),
                        chunk.get(2).cloned().unwrap_or_default(),
                        chunk.get(3).cloned().unwrap_or_default(),
                    ));
                }
            }
            _ => {
                return Err(FourWordError::InvalidWordCount {
                    expected: 6,
                    actual: all_words.len(),
                });
            }
        };

        // For decoding, we don't know the category yet, so use a placeholder
        // The actual category will be extracted during decoding from the encoded data
        Ok(Ipv6FourWordGroupEncoding::new(
            groups,
            Ipv6Category::GlobalUnicast, // placeholder - will be replaced during decoding
        ))
    }

    // ========== Autocomplete & Hints API ==========

    /// Get word hints for a given prefix
    ///
    /// Returns all dictionary words that start with the given prefix.
    /// With 5 characters, typically returns exactly one word due to unique prefix guarantee.
    ///
    /// # Examples
    ///
    /// ```
    /// use four_word_networking::FourWordAdaptiveEncoder;
    ///
    /// let encoder = FourWordAdaptiveEncoder::new().unwrap();
    ///
    /// // Get hints for 3-character prefix
    /// let hints = encoder.get_word_hints("bea");
    /// assert!(!hints.is_empty());
    /// assert!(hints.iter().all(|w| w.starts_with("bea")));
    ///
    /// // With 5 characters, should get unique match
    /// let hints = encoder.get_word_hints("beach");
    /// assert!(hints.len() <= 1);
    /// ```
    pub fn get_word_hints(&self, prefix: &str) -> Vec<String> {
        AutocompleteHelper::get_word_hints(prefix)
    }

    /// Validate partial input and provide suggestions
    ///
    /// Analyzes partial word input to determine validity and provide completions.
    /// Returns information about word count, validity, and possible completions.
    ///
    /// # Examples
    ///
    /// ```
    /// use four_word_networking::FourWordAdaptiveEncoder;
    ///
    /// let encoder = FourWordAdaptiveEncoder::new().unwrap();
    ///
    /// // Validate partial input
    /// let result = encoder.validate_partial_input("about abo").unwrap();
    /// assert!(result.is_valid_prefix);
    /// assert_eq!(result.word_count_so_far, 1);
    /// assert!(!result.possible_completions.is_empty());
    /// ```
    pub fn validate_partial_input(&self, partial: &str) -> Result<ValidationResult> {
        AutocompleteHelper::validate_partial_input(partial)
    }

    /// Suggest completions for partial word sequences
    ///
    /// Returns up to 10 complete suggestions based on partial input.
    /// Useful for implementing dropdown autocomplete in UIs.
    ///
    /// # Examples
    ///
    /// ```
    /// use four_word_networking::FourWordAdaptiveEncoder;
    ///
    /// let encoder = FourWordAdaptiveEncoder::new().unwrap();
    ///
    /// // Get suggestions for partial input with complete word + partial
    /// let suggestions = encoder.suggest_completions("about ab").unwrap();
    /// assert!(!suggestions.is_empty());
    ///
    /// // Get suggestions for just a partial word
    /// let partial_suggestions = encoder.suggest_completions("abo").unwrap();
    /// assert!(!partial_suggestions.is_empty());
    /// ```
    pub fn suggest_completions(&self, partial_words: &str) -> Result<Vec<String>> {
        AutocompleteHelper::suggest_completions(partial_words)
    }

    /// Auto-complete a word if it has a unique 5-character prefix
    ///
    /// Returns the complete word if the prefix uniquely identifies it.
    /// This enables instant completion when users type 5 characters.
    ///
    /// # Examples
    ///
    /// ```
    /// use four_word_networking::FourWordAdaptiveEncoder;
    ///
    /// let encoder = FourWordAdaptiveEncoder::new().unwrap();
    ///
    /// // With 5+ unique characters
    /// let word = encoder.auto_complete_at_five("beach");
    /// assert_eq!(word, Some("beach".to_string()));
    ///
    /// // With less than 5 characters
    /// let word = encoder.auto_complete_at_five("bea");
    /// assert_eq!(word, None);
    /// ```
    pub fn auto_complete_at_five(&self, prefix: &str) -> Option<String> {
        AutocompleteHelper::auto_complete_at_five(prefix)
    }

    /// Suggest corrections for potentially misspelled words
    ///
    /// Returns suggested corrections for words not found in the dictionary.
    /// Uses prefix matching to find similar words.
    ///
    /// # Examples
    ///
    /// ```
    /// use four_word_networking::FourWordAdaptiveEncoder;
    ///
    /// let encoder = FourWordAdaptiveEncoder::new().unwrap();
    ///
    /// // Get corrections for misspelled word
    /// let corrections = encoder.suggest_corrections("aboot");
    /// assert!(!corrections.is_empty());
    ///
    /// // Valid word returns itself
    /// let corrections = encoder.suggest_corrections("about");
    /// assert_eq!(corrections.first(), Some(&"about".to_string()));
    /// ```
    pub fn suggest_corrections(&self, word: &str) -> Vec<String> {
        AutocompleteHelper::suggest_corrections(word)
    }

    /// Check if a string is a valid word prefix
    ///
    /// Returns true if the prefix matches at least one dictionary word.
    ///
    /// # Examples
    ///
    /// ```
    /// use four_word_networking::FourWordAdaptiveEncoder;
    ///
    /// let encoder = FourWordAdaptiveEncoder::new().unwrap();
    ///
    /// assert!(encoder.is_valid_prefix("abo"));
    /// assert!(encoder.is_valid_prefix("about"));
    /// assert!(!encoder.is_valid_prefix("xyz"));
    /// ```
    pub fn is_valid_prefix(&self, prefix: &str) -> bool {
        DICTIONARY.is_valid_prefix(prefix)
    }

    /// Get statistics about possible completions
    ///
    /// Returns information about how many words match a given prefix.
    /// Useful for UI feedback about autocomplete possibilities.
    ///
    /// # Examples
    ///
    /// ```
    /// use four_word_networking::FourWordAdaptiveEncoder;
    ///
    /// let encoder = FourWordAdaptiveEncoder::new().unwrap();
    ///
    /// let (count, unique_at_five) = encoder.get_completion_stats("abo");
    /// assert!(count > 0);
    /// // unique_at_five indicates if typing 2 more chars will give unique match
    /// ```
    pub fn get_completion_stats(&self, prefix: &str) -> (usize, bool) {
        let hints = self.get_word_hints(prefix);
        let count = hints.len();

        // Check if extending to 5 characters would give unique results
        let unique_at_five = if prefix.len() < 5 {
            // Check if all hints have unique 5-character prefixes
            let mut five_char_prefixes = std::collections::HashSet::new();
            for hint in &hints {
                if hint.len() >= 5 {
                    five_char_prefixes.insert(&hint[..5]);
                }
            }
            five_char_prefixes.len() == hints.len()
        } else {
            count <= 1
        };

        (count, unique_at_five)
    }

    /// Generate random words from the dictionary
    ///
    /// Returns a vector of randomly selected words from the 4096-word dictionary.
    /// These are just random dictionary words, NOT necessarily valid IP encodings.
    /// Useful for generating passphrases, test data, or other applications that
    /// need random English words.
    ///
    /// Each word is independently selected, so duplicates are possible.
    ///
    /// # Arguments
    ///
    /// * `count` - The number of random words to generate
    ///
    /// # Examples
    ///
    /// ```
    /// use four_word_networking::FourWordAdaptiveEncoder;
    ///
    /// let encoder = FourWordAdaptiveEncoder::new().unwrap();
    ///
    /// // Generate 4 random words (not necessarily a valid IP encoding)
    /// let words = encoder.get_random_words(4);
    /// assert_eq!(words.len(), 4);
    ///
    /// // Generate any number of random words
    /// let passphrase = encoder.get_random_words(6);
    /// println!("Random passphrase: {}", passphrase.join("-"));
    ///
    /// // All generated words are valid dictionary words
    /// for word in &words {
    ///     assert!(encoder.is_valid_word(word));
    /// }
    /// ```
    pub fn get_random_words(&self, count: usize) -> Vec<String> {
        use rand::seq::SliceRandom;
        let mut rng = rand::thread_rng();

        // Get all words from dictionary
        let all_words = DICTIONARY.get_all_words();

        // Sample random words
        let mut result = Vec::new();
        for _ in 0..count {
            if let Some(word) = all_words.choose(&mut rng) {
                result.push(word.clone());
            }
        }

        result
    }

    /// Check if a word is valid in the dictionary
    ///
    /// Validates whether a given word exists in the 4096-word dictionary.
    /// The check is case-insensitive - uppercase and mixed-case words are
    /// automatically converted to lowercase before validation.
    ///
    /// # Arguments
    ///
    /// * `word` - The word to validate
    ///
    /// # Returns
    ///
    /// * `true` if the word exists in the dictionary
    /// * `false` if the word is not found or contains invalid characters
    ///
    /// # Examples
    ///
    /// ```
    /// use four_word_networking::FourWordAdaptiveEncoder;
    ///
    /// let encoder = FourWordAdaptiveEncoder::new().unwrap();
    ///
    /// // Valid words return true
    /// assert!(encoder.is_valid_word("about"));
    /// assert!(encoder.is_valid_word("a"));  // Single-letter words are valid
    ///
    /// // Case-insensitive validation
    /// assert!(encoder.is_valid_word("About"));
    /// assert!(encoder.is_valid_word("ABOUT"));
    ///
    /// // Invalid words return false
    /// assert!(!encoder.is_valid_word("xyz123"));
    /// assert!(!encoder.is_valid_word("not-a-word"));
    /// assert!(!encoder.is_valid_word(""));
    /// ```
    pub fn is_valid_word(&self, word: &str) -> bool {
        DICTIONARY.get_index(word).is_some()
    }
}

impl Default for FourWordAdaptiveEncoder {
    fn default() -> Self {
        Self::new().expect("Failed to create encoder")
    }
}

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

    #[test]
    fn test_ipv4_encoding() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        let test_cases = vec![
            "192.168.1.1:443",
            "10.0.0.1:80",
            "127.0.0.1:8080",
            "172.16.0.1:22",
        ];

        for addr in test_cases {
            let encoded = encoder.encode(addr).unwrap();
            let word_count = encoded.split_whitespace().count();
            assert_eq!(word_count, 4);

            let decoded = encoder.decode(&encoded).unwrap();
            assert_eq!(addr, decoded);
        }
    }

    #[test]
    fn test_ipv6_encoding() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        let test_cases = vec!["[::1]:443", "[fe80::1]:22", "[2001:db8::1]:8080"];

        for addr in test_cases {
            let encoded = encoder.encode(addr).unwrap();
            let word_count = encoded.split_whitespace().count();
            assert!(word_count == 6 || word_count == 9 || word_count == 12);
            assert!(word_count == 6 || word_count == 9 || word_count == 12); // 6, 9, or 12 words

            let decoded = encoder.decode(&encoded).unwrap();
            // Compare just the IP and port (not the full format)
            let original: SocketAddr = addr.parse().unwrap();
            let decoded_addr: SocketAddr = decoded.parse().unwrap();
            assert_eq!(original.ip(), decoded_addr.ip());
            assert_eq!(original.port(), decoded_addr.port());
        }
    }

    #[test]
    fn test_analyze() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        let analysis = encoder.analyze("192.168.1.1:443").unwrap();
        assert!(analysis.contains("IPv4"));
        assert!(analysis.contains("4 words"));

        let analysis = encoder.analyze("[::1]:443").unwrap();
        assert!(analysis.contains("IPv6"));
        assert!(analysis.contains("words"));
    }

    #[test]
    fn test_get_random_words_basic() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        // Test requesting different counts
        let words_4 = encoder.get_random_words(4);
        assert_eq!(words_4.len(), 4);

        let words_1 = encoder.get_random_words(1);
        assert_eq!(words_1.len(), 1);

        let words_10 = encoder.get_random_words(10);
        assert_eq!(words_10.len(), 10);

        // Test edge cases
        let words_0 = encoder.get_random_words(0);
        assert_eq!(words_0.len(), 0);

        let words_100 = encoder.get_random_words(100);
        assert_eq!(words_100.len(), 100);
    }

    #[test]
    fn test_random_words_are_valid() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        // Generate random words and verify they're all valid
        let words = encoder.get_random_words(50);
        for word in &words {
            assert!(
                encoder.is_valid_word(word),
                "Random word '{word}' should be valid"
            );
        }
    }

    #[test]
    fn test_random_words_randomness() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        // Generate multiple sets and check they're different
        let set1 = encoder.get_random_words(10);
        let set2 = encoder.get_random_words(10);
        let set3 = encoder.get_random_words(10);

        // At least one set should be different from another
        // (statistically, they should all be different)
        assert!(
            set1 != set2 || set2 != set3 || set1 != set3,
            "Random word sets should vary"
        );
    }

    #[test]
    fn test_is_valid_word_basic() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        // Test with known valid words from the dictionary
        assert!(encoder.is_valid_word("a"));
        assert!(encoder.is_valid_word("about"));

        // Test case insensitivity
        assert!(encoder.is_valid_word("About"));
        assert!(encoder.is_valid_word("ABOUT"));

        // Test invalid words
        assert!(!encoder.is_valid_word("xyz123"));
        assert!(!encoder.is_valid_word("notaword"));
        assert!(!encoder.is_valid_word(""));
        assert!(!encoder.is_valid_word("123"));
        assert!(!encoder.is_valid_word("test-word"));
    }

    #[test]
    fn test_is_valid_word_all_dictionary_words() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        // Get all dictionary words and verify each is valid
        use crate::dictionary4k::DICTIONARY;
        for i in 0..4096 {
            if let Some(word) = DICTIONARY.get_word(i) {
                assert!(
                    encoder.is_valid_word(word),
                    "Dictionary word '{word}' at index {i} should be valid"
                );
            }
        }
    }

    #[test]
    fn test_is_valid_word_with_special_chars() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        // Words with special characters should be invalid
        assert!(!encoder.is_valid_word("hello-world"));
        assert!(!encoder.is_valid_word("hello_world"));
        assert!(!encoder.is_valid_word("hello.world"));
        assert!(!encoder.is_valid_word("hello world"));
        assert!(!encoder.is_valid_word("hello@world"));
        assert!(!encoder.is_valid_word("hello!"));
    }

    #[test]
    fn test_random_words_are_just_dictionary_words() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        // get_random_words() should return random dictionary words
        // These are NOT necessarily valid IP encodings - just random words for other uses
        // like generating passphrases, test data, etc.

        let words_4 = encoder.get_random_words(4);
        assert_eq!(words_4.len(), 4);

        // Verify they're all from the dictionary
        for word in &words_4 {
            assert!(encoder.is_valid_word(word));
        }

        // Can generate any number of random words
        let words_7 = encoder.get_random_words(7);
        assert_eq!(words_7.len(), 7);

        let words_20 = encoder.get_random_words(20);
        assert_eq!(words_20.len(), 20);

        // These random words are NOT meant to be decoded as IP addresses
        // They're just random dictionary words for general use
    }

    #[test]
    fn test_is_valid_word_edge_cases() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();

        // Test single-letter words (should have some valid ones like "a", "i")
        assert!(encoder.is_valid_word("a"));

        // Test very long invalid strings
        let long_word = "a".repeat(100);
        assert!(!encoder.is_valid_word(&long_word));

        // Test whitespace
        assert!(!encoder.is_valid_word(" "));
        assert!(!encoder.is_valid_word("\t"));
        assert!(!encoder.is_valid_word("\n"));

        // Test words with leading/trailing spaces (should work due to lowercase conversion)
        // The dictionary lookup uses to_lowercase which doesn't trim
        assert!(!encoder.is_valid_word(" about "));
        assert!(!encoder.is_valid_word("about "));
        assert!(!encoder.is_valid_word(" about"));
    }

    #[test]
    fn test_random_words_distribution() {
        let encoder = FourWordAdaptiveEncoder::new().unwrap();
        use std::collections::HashSet;

        // Generate a large sample and check distribution
        let sample_size = 1000;
        let words = encoder.get_random_words(sample_size);

        // Convert to set to count unique words
        let unique_words: HashSet<_> = words.iter().collect();

        // We should see a reasonable variety of words
        // With 4096 words and 1000 samples, we expect to see several hundred unique words
        assert!(
            unique_words.len() > 100,
            "Random selection should produce variety, got {} unique words from {} samples",
            unique_words.len(),
            sample_size
        );

        // But not all unique (that would suggest non-random behavior)
        assert!(
            unique_words.len() < sample_size,
            "Some repetition expected in random selection"
        );
    }
}