hs-predict 0.2.0

HS code prediction for chemical products — Akinator-style interactive classification with rule-based and LLM hybrid engine
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
//! Interactive Akinator-style classification session.
//!
//! Instead of requiring all product information upfront, [`ClassificationSession`]
//! asks targeted questions one at a time to progressively narrow down the HS code.
//!
//! # Example
//!
//! ```rust,no_run
//! use hs_predict::session::{ClassificationSession, Answer, SessionResult};
//! use hs_predict::types::Language;
//!
//! let mut session = ClassificationSession::new(); // English prompts
//!
//! let q1 = session.start();
//! println!("{}", q1.prompt());
//!
//! match session.answer(Answer::Text("1310-73-2".to_string())).unwrap() {
//!     SessionResult::NeedMoreInfo { next_question } => {
//!         println!("Next: {}", next_question.prompt());
//!     }
//!     SessionResult::Ready => {
//!         println!("Ready to classify!");
//!     }
//!     SessionResult::RequiresLlm => {
//!         println!("LLM needed");
//!     }
//! }
//! ```

pub mod flow;
pub mod messages;
pub mod question;
pub mod state;

use serde::{Deserialize, Serialize};

pub use question::{Answer, QAPair, Question, QuestionStep, SessionResult};
pub use state::{ClassificationState, PartialComponent};

use crate::error::{HsPredictError, Result};
use crate::session::flow::{
    choice_index_to_intended_use, choice_index_to_organic_inorganic,
    choice_index_to_physical_form, multi_choice_indices_to_functional_groups, next_question,
};
use crate::types::{Language, MixtureComponent, PhysicalForm, ProductDescription, SubstanceIdentifier};

/// Interactive HS code classification session.
///
/// Maintains state across multiple question-answer rounds and builds up a
/// [`ProductDescription`] that can be passed to the classification pipeline.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassificationSession {
    /// Accumulated classification state.
    state: ClassificationState,
    /// Full history of Q&A pairs (used for serialization / resume).
    history: Vec<QAPair>,
    /// The question currently pending an answer.
    current_question: Option<Question>,
    /// The logical step of `current_question` (language-independent).
    current_step: Option<QuestionStep>,
    /// Language used for question prompts.
    language: Language,
}

impl ClassificationSession {
    /// Create a new empty session with English prompts.
    pub fn new() -> Self {
        Self {
            state: ClassificationState::default(),
            history: Vec::new(),
            current_question: None,
            current_step: None,
            language: Language::En,
        }
    }

    /// Create a new empty session with Japanese prompts.
    pub fn new_ja() -> Self {
        Self::new().with_language(Language::Ja)
    }

    /// Set the language for question prompts (builder style).
    ///
    /// Must be called before [`start()`](Self::start).
    pub fn with_language(mut self, language: Language) -> Self {
        self.language = language;
        self
    }

    /// Return the first question and mark it as the active question.
    ///
    /// Must be called once before the first [`answer()`](Self::answer) call.
    pub fn start(&mut self) -> Question {
        let (q, step) = next_question(&self.state, self.language)
            .expect("new session should always have a first question");
        self.current_question = Some(q.clone());
        self.current_step = Some(step);
        q
    }

    /// Submit an answer to the current active question.
    ///
    /// Returns the next [`SessionResult`]:
    /// - [`SessionResult::NeedMoreInfo`] — more questions remain
    /// - [`SessionResult::Ready`] — call [`to_product_description()`](Self::to_product_description)
    /// - [`SessionResult::RequiresLlm`] — insufficient info for rule engine alone
    ///
    /// # Errors
    /// - [`HsPredictError::NoActiveQuestion`] — called before [`start()`](Self::start).
    /// - [`HsPredictError::AnswerTypeMismatch`] — answer type doesn't match the question.
    /// - [`HsPredictError::InvalidChoiceIndex`] — choice index out of range.
    /// - [`HsPredictError::NumberOutOfRange`] — number outside `[min, max]`.
    pub fn answer(&mut self, answer: Answer) -> Result<SessionResult> {
        let question = self
            .current_question
            .clone()
            .ok_or(HsPredictError::NoActiveQuestion)?;

        // Validate answer type and apply to state
        self.validate_and_apply(&question, &answer)?;

        // Record in history
        self.history.push(QAPair {
            question: question.clone(),
            answer,
        });

        // Try to resolve IUPAC name → SMILES
        self.try_resolve_smiles();

        // Determine next step
        match next_question(&self.state, self.language) {
            Some((q, step)) => {
                self.current_question = Some(q.clone());
                self.current_step = Some(step);
                Ok(SessionResult::NeedMoreInfo { next_question: q })
            }
            None => {
                self.state.is_complete = true;
                self.current_question = None;
                self.current_step = None;
                if self.state.confidence_estimate() < 0.25 {
                    Ok(SessionResult::RequiresLlm)
                } else {
                    Ok(SessionResult::Ready)
                }
            }
        }
    }

    /// Convert the accumulated session state into a [`ProductDescription`].
    ///
    /// Call after receiving [`SessionResult::Ready`] or [`SessionResult::RequiresLlm`].
    pub fn to_product_description(&self) -> ProductDescription {
        let mixture_components = if self.state.is_mixture == Some(true) {
            Some(
                self.state
                    .components
                    .iter()
                    .map(|c| MixtureComponent {
                        substance: c.identifier.clone(),
                        weight_fraction_pct: c.weight_fraction_pct,
                        volume_fraction_pct: None,
                        is_solvent: c.is_solvent,
                    })
                    .collect(),
            )
        } else {
            None
        };

        ProductDescription {
            identifier: self.state.identifier.clone(),
            physical_form: self.state.physical_form.clone(),
            purity_pct: self.state.purity_pct,
            purity_type: None,
            mixture_components,
            intended_use: self.state.intended_use.clone(),
            additional_context: None,
        }
    }

    /// Current session state (read-only).
    pub fn state(&self) -> &ClassificationState {
        &self.state
    }

    /// Full Q&A history.
    pub fn history(&self) -> &[QAPair] {
        &self.history
    }

    /// Number of questions answered so far.
    pub fn question_count(&self) -> usize {
        self.history.len()
    }

    /// Whether the session has collected enough information.
    pub fn is_complete(&self) -> bool {
        self.state.is_complete
    }

    /// The language used for question prompts.
    pub fn language(&self) -> Language {
        self.language
    }

    /// The logical step of the current active question, if any.
    pub fn current_step(&self) -> Option<QuestionStep> {
        self.current_step
    }

    // ─── Private: validate & apply ────────────────────────────────────

    fn validate_and_apply(&mut self, question: &Question, answer: &Answer) -> Result<()> {
        match (question, answer) {
            // Text input
            (Question::Text { .. }, Answer::Text(text)) => {
                self.apply_identifier_input(text);
            }
            (Question::Text { .. }, Answer::Skip) => {
                if !self.state.has_identifier()
                    && self.current_step != Some(QuestionStep::ComponentIdentifier)
                {
                    return Err(HsPredictError::MissingIdentifier);
                }
            }

            // Yes/No
            (Question::YesNo { .. }, Answer::YesNo(val)) => {
                self.apply_yes_no(*val);
            }

            // Number
            (Question::Number { min, max, .. }, Answer::Number(val)) => {
                if *val < *min || *val > *max {
                    return Err(HsPredictError::NumberOutOfRange {
                        value: *val,
                        min: *min,
                        max: *max,
                    });
                }
                self.apply_number(*val);
            }

            // Single choice
            (Question::Choice { options, .. }, Answer::Choice(idx)) => {
                if *idx >= options.len() {
                    return Err(HsPredictError::InvalidChoiceIndex {
                        index: *idx,
                        max: options.len() - 1,
                    });
                }
                self.apply_choice(*idx);
            }

            // Multi choice
            (Question::MultiChoice { options, .. }, Answer::MultiChoice(indices)) => {
                for &idx in indices {
                    if idx >= options.len() {
                        return Err(HsPredictError::InvalidChoiceIndex {
                            index: idx,
                            max: options.len() - 1,
                        });
                    }
                }
                self.apply_multi_choice(indices);
            }

            // Type mismatch
            _ => {
                return Err(HsPredictError::AnswerTypeMismatch {
                    expected: question_kind_name(question),
                    got: answer.kind_name(),
                });
            }
        }
        Ok(())
    }

    /// Parse and store an identifier string.
    fn apply_identifier_input(&mut self, input: &str) {
        let input = input.trim();

        let in_mixture = self.state.is_mixture == Some(true)
            && self.state.current_component_index < self.state.component_count.unwrap_or(0);

        if in_mixture {
            let idx = self.state.current_component_index;
            while self.state.components.len() <= idx {
                self.state.components.push(PartialComponent::default());
            }
            self.state.components[idx].identifier = parse_identifier(input);
        } else {
            self.state.identifier = parse_identifier(input);
        }
    }

    fn apply_yes_no(&mut self, val: bool) {
        match self.current_step {
            Some(QuestionStep::IsMixture) => {
                self.state.is_mixture = Some(val);
            }
            _ => {}
        }
    }

    fn apply_number(&mut self, val: f64) {
        match self.current_step {
            Some(QuestionStep::ComponentCount) => {
                self.state.component_count = Some(val as usize);
            }
            Some(QuestionStep::ComponentFraction) => {
                let idx = self.state.current_component_index;
                if idx < self.state.components.len() {
                    self.state.components[idx].weight_fraction_pct =
                        if val > 0.0 { Some(val) } else { None };
                    self.state.current_component_index += 1;
                }
            }
            Some(QuestionStep::SolutionConcentration) => {
                if let Some(PhysicalForm::Solution { concentration_pct_ww, .. }) =
                    &mut self.state.physical_form
                {
                    *concentration_pct_ww = if val > 0.0 { Some(val) } else { None };
                }
            }
            _ => {}
        }
    }

    fn apply_choice(&mut self, idx: usize) {
        match self.current_step {
            Some(QuestionStep::PhysicalForm) => {
                self.state.physical_form = Some(choice_index_to_physical_form(idx));
            }
            Some(QuestionStep::IntendedUse) => {
                self.state.intended_use = Some(choice_index_to_intended_use(idx));
            }
            Some(QuestionStep::OrganicInorganic) => {
                self.state.organic_inorganic = Some(choice_index_to_organic_inorganic(idx));
            }
            _ => {}
        }
    }

    fn apply_multi_choice(&mut self, indices: &[usize]) {
        self.state.detected_functional_groups = multi_choice_indices_to_functional_groups(indices);
    }

    /// Attempt to resolve IUPAC name → SMILES (silent on failure).
    fn try_resolve_smiles(&mut self) {
        if self.state.identifier.smiles.is_none() {
            if let Some(ref iupac) = self.state.identifier.iupac_name.clone() {
                if let Some(smiles) = resolve_iupac_to_smiles(iupac) {
                    self.state.identifier.smiles = Some(smiles);
                }
            }
        }
        for comp in &mut self.state.components {
            if comp.identifier.smiles.is_none() {
                if let Some(ref iupac) = comp.identifier.iupac_name.clone() {
                    if let Some(smiles) = resolve_iupac_to_smiles(iupac) {
                        comp.identifier.smiles = Some(smiles);
                    }
                }
            }
        }
    }
}

impl Default for ClassificationSession {
    fn default() -> Self {
        Self::new()
    }
}

// ─── Free functions ────────────────────────────────────────────────────────────

fn parse_identifier(input: &str) -> SubstanceIdentifier {
    let s = input.trim();

    if is_cas_format(s) {
        return SubstanceIdentifier::from_cas(s);
    }
    if is_inchi_key_format(s) {
        return SubstanceIdentifier {
            inchi_key: Some(s.to_string()),
            ..Default::default()
        };
    }
    if s.starts_with("InChI=") {
        return SubstanceIdentifier {
            inchi: Some(s.to_string()),
            ..Default::default()
        };
    }
    if !s.contains(' ')
        && s.chars()
            .any(|c| matches!(c, '(' | ')' | '=' | '#' | '[' | ']' | '+' | '-'))
    {
        return SubstanceIdentifier::from_smiles(s);
    }
    SubstanceIdentifier::from_iupac_name(s)
}

fn is_cas_format(s: &str) -> bool {
    let parts: Vec<&str> = s.split('-').collect();
    parts.len() == 3
        && parts[0].len() >= 2
        && parts[0].chars().all(|c| c.is_ascii_digit())
        && parts[1].len() == 2
        && parts[1].chars().all(|c| c.is_ascii_digit())
        && parts[2].len() == 1
        && parts[2].chars().all(|c| c.is_ascii_digit())
}

fn is_inchi_key_format(s: &str) -> bool {
    let parts: Vec<&str> = s.split('-').collect();
    parts.len() == 3
        && parts[0].len() == 14
        && parts[1].len() == 10
        && parts[2].len() == 1
        && s.chars().all(|c| c.is_ascii_uppercase() || c == '-')
}

fn resolve_iupac_to_smiles(iupac_name: &str) -> Option<String> {
    chem_name_resolver::resolve(iupac_name)
        .ok()
        .map(|r| r.smiles)
}

fn question_kind_name(q: &Question) -> &'static str {
    match q {
        Question::Text { .. } => "text",
        Question::Choice { .. } => "choice",
        Question::YesNo { .. } => "yes_no",
        Question::Number { .. } => "number",
        Question::MultiChoice { .. } => "multi_choice",
    }
}

// ─── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{IntendedUse, OrganicInorganic, PhysicalForm};

    /// Helper: unwrap the next_question from SessionResult::NeedMoreInfo.
    fn next_q(result: SessionResult) -> Question {
        match result {
            SessionResult::NeedMoreInfo { next_question } => next_question,
            other => panic!("expected NeedMoreInfo, got {:?}", std::mem::discriminant(&other)),
        }
    }

    // ─── English session flow ─────────────────────────────────────────

    #[test]
    fn session_starts_with_identifier_question() {
        let mut session = ClassificationSession::new();
        let q = session.start();
        assert!(matches!(q, Question::Text { .. }));
        assert_eq!(session.current_step(), Some(QuestionStep::Identifier));
    }

    #[test]
    fn session_pure_cas_inorganic_full_flow() {
        // CAS input, pure substance, solid, industrial, inorganic → Ready
        let mut session = ClassificationSession::new();
        session.start();

        // Q1 identifier → Q2 is_mixture?
        let r = session.answer(Answer::Text("1310-73-2".to_string())).unwrap();
        assert!(matches!(next_q(r), Question::YesNo { .. }));
        assert_eq!(session.current_step(), Some(QuestionStep::IsMixture));

        // Q2 not mixture → Q3 physical form
        let r = session.answer(Answer::YesNo(false)).unwrap();
        assert!(matches!(next_q(r), Question::Choice { .. }));
        assert_eq!(session.current_step(), Some(QuestionStep::PhysicalForm));

        // Q3 solid (index 0) → Q4 intended use
        let r = session.answer(Answer::Choice(0)).unwrap();
        assert!(matches!(next_q(r), Question::Choice { .. }));
        assert_eq!(session.current_step(), Some(QuestionStep::IntendedUse));

        // Q4 industrial (index 0) → Q5 organic/inorganic (CAS has no SMILES)
        let r = session.answer(Answer::Choice(0)).unwrap();
        assert!(matches!(next_q(r), Question::Choice { .. }));
        assert_eq!(session.current_step(), Some(QuestionStep::OrganicInorganic));

        // Q5 inorganic (index 1) → Ready
        let r = session.answer(Answer::Choice(1)).unwrap();
        assert!(matches!(r, SessionResult::Ready));

        // Verify accumulated state
        let product = session.to_product_description();
        assert_eq!(product.identifier.cas.as_deref(), Some("1310-73-2"));
        assert!(matches!(product.physical_form, Some(PhysicalForm::Solid)));
        assert_eq!(product.intended_use, Some(IntendedUse::Industrial));
        assert_eq!(session.question_count(), 5);
        assert!(session.is_complete());
    }

    #[test]
    fn session_smiles_input_skips_organic_inorganic_question() {
        // SMILES input: organic/inorganic and functional-group questions are skipped.
        let mut session = ClassificationSession::new();
        session.start();

        // Identifier: SMILES string for NaOH → smiles is set
        let r = session.answer(Answer::Text("[Na+].[OH-]".to_string())).unwrap();
        assert!(matches!(next_q(r), Question::YesNo { .. }));

        // Not mixture
        let r = session.answer(Answer::YesNo(false)).unwrap();
        assert!(matches!(next_q(r), Question::Choice { .. })); // physical form

        // Liquid (index 3)
        let r = session.answer(Answer::Choice(3)).unwrap();
        assert!(matches!(next_q(r), Question::Choice { .. })); // intended use

        // Industrial
        let r = session.answer(Answer::Choice(0)).unwrap();
        // SMILES is set → organic/inorganic skipped → Ready
        assert!(matches!(r, SessionResult::Ready));
        assert_eq!(session.question_count(), 4);

        let product = session.to_product_description();
        assert!(product.identifier.smiles.is_some());
    }

    #[test]
    fn session_organic_cas_asks_functional_groups() {
        // CAS input + organic → functional-group question appears.
        let mut session = ClassificationSession::new();
        session.start();

        session.answer(Answer::Text("108-88-3".to_string())).unwrap(); // toluene CAS
        session.answer(Answer::YesNo(false)).unwrap();         // not mixture
        session.answer(Answer::Choice(0)).unwrap();            // solid
        session.answer(Answer::Choice(0)).unwrap();            // industrial
        let r = session.answer(Answer::Choice(0)).unwrap();    // organic (index 0)

        // Must be functional-group MultiChoice next
        let q = next_q(r);
        assert!(matches!(q, Question::MultiChoice { .. }));
        assert_eq!(session.current_step(), Some(QuestionStep::FunctionalGroups));

        // Select aromatic (index 10) and no others
        let r = session.answer(Answer::MultiChoice(vec![10])).unwrap();
        assert!(matches!(r, SessionResult::Ready));

        let state = session.state();
        assert_eq!(state.organic_inorganic, Some(OrganicInorganic::Organic));
        assert!(state.detected_functional_groups.contains(&"aromatic".to_string()));
    }

    #[test]
    fn session_solution_asks_concentration() {
        let mut session = ClassificationSession::new();
        session.start();

        session.answer(Answer::Text("7647-01-0".to_string())).unwrap(); // HCl CAS
        session.answer(Answer::YesNo(false)).unwrap();

        // Solution (index 4)
        let r = session.answer(Answer::Choice(4)).unwrap();
        let q = next_q(r);
        assert!(matches!(q, Question::Number { .. }));
        assert_eq!(session.current_step(), Some(QuestionStep::SolutionConcentration));

        // Concentration 35%
        let r = session.answer(Answer::Number(35.0)).unwrap();
        assert!(matches!(next_q(r), Question::Choice { .. })); // intended use

        session.answer(Answer::Choice(0)).unwrap(); // industrial
        let r = session.answer(Answer::Choice(1)).unwrap(); // inorganic → Ready
        assert!(matches!(r, SessionResult::Ready));

        let product = session.to_product_description();
        assert_eq!(
            product.physical_form,
            Some(PhysicalForm::Solution {
                solvent: None,
                concentration_pct_ww: Some(35.0),
            })
        );
    }

    // ─── Mixture flow ─────────────────────────────────────────────────

    #[test]
    fn session_mixture_two_components() {
        let mut session = ClassificationSession::new();
        session.start();

        // Step 1: main identifier
        session.answer(Answer::Text("7664-93-9".to_string())).unwrap(); // H2SO4

        // Step 2: is mixture → yes
        session.answer(Answer::YesNo(true)).unwrap();
        assert_eq!(session.current_step(), Some(QuestionStep::ComponentCount));

        // Step 3: 2 components
        session.answer(Answer::Number(2.0)).unwrap();
        assert_eq!(session.current_step(), Some(QuestionStep::ComponentIdentifier));

        // Component 1 identifier
        session.answer(Answer::Text("7664-93-9".to_string())).unwrap();
        assert_eq!(session.current_step(), Some(QuestionStep::ComponentFraction));

        // Component 1 fraction
        session.answer(Answer::Number(70.0)).unwrap();
        assert_eq!(session.current_step(), Some(QuestionStep::ComponentIdentifier));

        // Component 2 identifier
        session.answer(Answer::Text("7732-18-5".to_string())).unwrap(); // water
        assert_eq!(session.current_step(), Some(QuestionStep::ComponentFraction));

        // Component 2 fraction → done
        let r = session.answer(Answer::Number(30.0)).unwrap();
        assert!(matches!(r, SessionResult::Ready | SessionResult::RequiresLlm));

        let product = session.to_product_description();
        let comps = product.mixture_components.unwrap();
        assert_eq!(comps.len(), 2);
        assert_eq!(comps[0].substance.cas.as_deref(), Some("7664-93-9"));
        assert_eq!(comps[0].weight_fraction_pct, Some(70.0));
        assert_eq!(comps[1].substance.cas.as_deref(), Some("7732-18-5"));
        assert_eq!(comps[1].weight_fraction_pct, Some(30.0));
    }

    // ─── Error handling ───────────────────────────────────────────────

    #[test]
    fn error_no_active_question_before_start() {
        let mut session = ClassificationSession::new();
        let err = session.answer(Answer::Text("1310-73-2".to_string())).unwrap_err();
        assert!(matches!(err, HsPredictError::NoActiveQuestion));
    }

    #[test]
    fn error_answer_type_mismatch() {
        let mut session = ClassificationSession::new();
        session.start(); // Q1 is a Text question
        let err = session.answer(Answer::YesNo(true)).unwrap_err();
        assert!(matches!(err, HsPredictError::AnswerTypeMismatch { .. }));
    }

    #[test]
    fn error_choice_index_out_of_range() {
        let mut session = ClassificationSession::new();
        session.start();
        session.answer(Answer::Text("1310-73-2".to_string())).unwrap();
        session.answer(Answer::YesNo(false)).unwrap(); // physical form question
        let err = session.answer(Answer::Choice(99)).unwrap_err();
        assert!(matches!(err, HsPredictError::InvalidChoiceIndex { .. }));
    }

    #[test]
    fn error_number_out_of_range() {
        let mut session = ClassificationSession::new();
        session.start();
        session.answer(Answer::Text("1310-73-2".to_string())).unwrap();
        session.answer(Answer::YesNo(true)).unwrap(); // component count question
        let err = session.answer(Answer::Number(1.0)).unwrap_err(); // min is 2
        assert!(matches!(err, HsPredictError::NumberOutOfRange { .. }));
    }

    // ─── Japanese language ────────────────────────────────────────────

    #[test]
    fn japanese_session_prompts_are_in_japanese() {
        let mut session = ClassificationSession::new_ja();
        let q = session.start();
        // The Japanese identifier prompt contains Japanese characters
        assert!(q.prompt().chars().any(|c| c as u32 > 0x7F));
    }

    #[test]
    fn japanese_session_completes_same_as_english() {
        // Logic is language-independent; only prompts differ.
        let mut session = ClassificationSession::new_ja();
        session.start();

        session.answer(Answer::Text("1310-73-2".to_string())).unwrap();
        session.answer(Answer::YesNo(false)).unwrap();
        session.answer(Answer::Choice(0)).unwrap(); // solid
        session.answer(Answer::Choice(0)).unwrap(); // industrial
        let r = session.answer(Answer::Choice(1)).unwrap(); // inorganic

        assert!(matches!(r, SessionResult::Ready));
        let product = session.to_product_description();
        assert_eq!(product.identifier.cas.as_deref(), Some("1310-73-2"));
    }

    // ─── Serialization round-trip ─────────────────────────────────────

    #[test]
    fn session_serializes_and_deserializes() {
        let mut session = ClassificationSession::new();
        session.start();
        session.answer(Answer::Text("1310-73-2".to_string())).unwrap();

        let json = serde_json::to_string(&session).unwrap();
        let restored: ClassificationSession = serde_json::from_str(&json).unwrap();

        assert_eq!(
            restored.state().identifier.cas.as_deref(),
            Some("1310-73-2")
        );
        assert_eq!(restored.language(), Language::En);
        assert_eq!(restored.current_step(), Some(QuestionStep::IsMixture));
    }
}