🇹🇷 settings lists dedup by folded word, not ASCII case
Browse files at this commit4bea13f8a801baba251c5bb1e76fab885581482fParent: e0f9d63- fix
- 🇹🇷 settings lists dedup by folded word, not ASCII case
1 changed file +36 -3
- src/domain/settings.rs +36 -3
src/domain/settings.rsmodified+36 -3
| @@ -22,6 +22,7 @@ use crate::constant::{ | |||
| 22 | MIN_MARK, MIN_MAX_CHATBOT_THREADS, MIN_MAX_CHATBOT_MESSAGE_LEN, MIN_MAX_FILE_BYTES, | 22 | MIN_MARK, MIN_MAX_CHATBOT_THREADS, MIN_MAX_CHATBOT_MESSAGE_LEN, MIN_MAX_FILE_BYTES, |
| 23 | }; | 23 | }; |
| 24 | use crate::database::{Database, SETTINGS_TABLE}; | 24 | use crate::database::{Database, SETTINGS_TABLE}; |
| 25 | +use crate::domain::text_fold; | ||
| 25 | use crate::error::{AppError, ValidationError}; | 26 | use crate::error::{AppError, ValidationError}; |
| 26 | 27 | ||
| 27 | /// The singleton's fixed key: one school per deployment, one settings row. | 28 | /// The singleton's fixed key: one school per deployment, one settings row. |
| @@ -177,7 +178,7 @@ impl Settings { | |||
| 177 | } | 178 | } |
| 178 | 179 | ||
| 179 | /// Validate a full policy. List entries are trimmed; kinds and statuses | 180 | /// Validate a full policy. List entries are trimmed; kinds and statuses |
| 180 | - /// must be non-empty, unique (case-insensitive), and bounded; the four | 181 | + /// must be non-empty, unique (folded: case- and Turkish-insensitive), and bounded; the four |
| 181 | /// core attendance statuses can never be removed (the attendance rate's | 182 | /// core attendance statuses can never be removed (the attendance rate's |
| 182 | /// semantics are defined over them). Bands may be empty (numeric-only | 183 | /// semantics are defined over them). Bands may be empty (numeric-only |
| 183 | /// display), otherwise their mins are unique and one band must start at 0 | 184 | /// display), otherwise their mins are unique and one band must start at 0 |
| @@ -344,6 +345,7 @@ impl Settings { | |||
| 344 | /// unconditionally — last write wins. Prefer [`Self::save_if_unchanged`] | 345 | /// unconditionally — last write wins. Prefer [`Self::save_if_unchanged`] |
| 345 | /// wherever the new policy was merged from a loaded snapshot. | 346 | /// wherever the new policy was merged from a loaded snapshot. |
| 346 | pub async fn save(self, db: &Database) -> Result<Settings, AppError> { | 347 | pub async fn save(self, db: &Database) -> Result<Settings, AppError> { |
| 348 | + // whole-row-save-ok: test-only seeding; every production write merges from a loaded snapshot and goes through save_if_unchanged | ||
| 347 | let saved: Option<Settings> = db.upsert(Self::record_id()).content(self).await?; | 349 | let saved: Option<Settings> = db.upsert(Self::record_id()).content(self).await?; |
| 348 | saved.ok_or_else(|| AppError::Internal("failed to save settings".into())) | 350 | saved.ok_or_else(|| AppError::Internal("failed to save settings".into())) |
| 349 | } | 351 | } |
| @@ -435,13 +437,17 @@ fn validate_list(field: &'static str, values: Vec<String>) -> Result<Vec<String> | |||
| 435 | } | 437 | } |
| 436 | trimmed.push(value.to_string()); | 438 | trimmed.push(value.to_string()); |
| 437 | } | 439 | } |
| 438 | - let mut folded: Vec<String> = trimmed.iter().map(|v| v.to_lowercase()).collect(); | 440 | + // `to_lowercase` is locale-invariant — `İZİN` lowercases to `i̇zin` (with a |
| 441 | + // leftover combining dot) and would slip past `izin` as a distinct entry. | ||
| 442 | + // Same folding as search uses, so the whole app agrees on "the same word". | ||
| 443 | + let mut folded: Vec<String> = trimmed.iter().map(|v| text_fold::fold(v)).collect(); | ||
| 439 | folded.sort_unstable(); | 444 | folded.sort_unstable(); |
| 440 | folded.dedup(); | 445 | folded.dedup(); |
| 441 | if folded.len() != trimmed.len() { | 446 | if folded.len() != trimmed.len() { |
| 442 | return Err(ValidationError::Invalid { | 447 | return Err(ValidationError::Invalid { |
| 443 | field, | 448 | field, |
| 444 | - reason: "entries must be unique (case-insensitive)", | 449 | + reason: "two entries are the same word apart from upper/lower case \ |
| 450 | + or Turkish letters — keep only one of them", | ||
| 445 | }); | 451 | }); |
| 446 | } | 452 | } |
| 447 | Ok(trimmed) | 453 | Ok(trimmed) |
| @@ -527,6 +533,33 @@ mod tests { | |||
| 527 | assert!(with_kinds(too_many).is_err()); | 533 | assert!(with_kinds(too_many).is_err()); |
| 528 | } | 534 | } |
| 529 | 535 | ||
| 536 | + /// Rust's `to_lowercase` is locale-invariant: `İZİN` becomes `i̇zin` (with a | ||
| 537 | + /// combining dot above), which never equals `izin`, so both spellings of | ||
| 538 | + /// the same word used to land in one list. Teachers type these by hand in | ||
| 539 | + /// Turkish, which is exactly when it happens. | ||
| 540 | + #[tokio::test] | ||
| 541 | + async fn turkish_casing_is_a_duplicate() { | ||
| 542 | + let with_kinds = |exam_kinds| { | ||
| 543 | + Settings::try_new(SettingsParams { | ||
| 544 | + exam_kinds, | ||
| 545 | + ..params() | ||
| 546 | + }) | ||
| 547 | + }; | ||
| 548 | + assert!(with_kinds(kinds(&["İZİN", "izin"])).is_err()); | ||
| 549 | + assert!(with_kinds(kinds(&["SINAV", "sınav"])).is_err()); | ||
| 550 | + // Plain English duplicates still die, genuinely distinct entries live. | ||
| 551 | + assert!(with_kinds(kinds(&["Lab", "LAB"])).is_err()); | ||
| 552 | + assert!(with_kinds(kinds(&["izin", "sınav", "lab"])).is_ok()); | ||
| 553 | + // Same rule on the other guarded list. | ||
| 554 | + assert!( | ||
| 555 | + Settings::try_new(SettingsParams { | ||
| 556 | + attendance_statuses: statuses_with(&["İZİN", "izin"]), | ||
| 557 | + ..params() | ||
| 558 | + }) | ||
| 559 | + .is_err() | ||
| 560 | + ); | ||
| 561 | + } | ||
| 562 | + | ||
| 530 | #[tokio::test] | 563 | #[tokio::test] |
| 531 | async fn kind_weights_resolve_by_exact_name() { | 564 | async fn kind_weights_resolve_by_exact_name() { |
| 532 | let s = Settings::try_new(SettingsParams { | 565 | let s = Settings::try_new(SettingsParams { |