From 4bea13f8a801baba251c5bb1e76fab885581482f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 25 Jul 2026 05:06:57 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=87=B9=F0=9F=87=B7=20settings=20li?= =?UTF-8?q?sts=20dedup=20by=20folded=20word,=20not=20ASCII=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - the duplicate check lowercased with Rust to_lowercase, which turns İ into i + U+0307, so İZİN and izin were accepted as two distinct entries in the same list — exactly what the check exists to prevent - fold both sides with the shared text_fold table, so validation and search agree on what "the same word" means - this is stricter: a list holding İZİN and izin (or ÖDEV and odev) now 400s where it used to be accepted, including on a PATCH that only meant to change another field of an already-bad row - reword the error from "entries must be unique (case-insensitive)" to name the field and the remedy, kept as a fixed string so the frontend can localize it - annotate Settings::save as test-only: production merges from a loaded snapshot and goes through the save_if_unchanged compare-and-swap --- src/domain/settings.rs | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/domain/settings.rs b/src/domain/settings.rs index dfa1890..a7ab9bf 100644 --- a/src/domain/settings.rs +++ b/src/domain/settings.rs @@ -22,6 +22,7 @@ use crate::constant::{ MIN_MARK, MIN_MAX_CHATBOT_THREADS, MIN_MAX_CHATBOT_MESSAGE_LEN, MIN_MAX_FILE_BYTES, }; use crate::database::{Database, SETTINGS_TABLE}; +use crate::domain::text_fold; use crate::error::{AppError, ValidationError}; /// The singleton's fixed key: one school per deployment, one settings row. @@ -177,7 +178,7 @@ impl Settings { } /// Validate a full policy. List entries are trimmed; kinds and statuses - /// must be non-empty, unique (case-insensitive), and bounded; the four + /// must be non-empty, unique (folded: case- and Turkish-insensitive), and bounded; the four /// core attendance statuses can never be removed (the attendance rate's /// semantics are defined over them). Bands may be empty (numeric-only /// display), otherwise their mins are unique and one band must start at 0 @@ -344,6 +345,7 @@ impl Settings { /// unconditionally — last write wins. Prefer [`Self::save_if_unchanged`] /// wherever the new policy was merged from a loaded snapshot. pub async fn save(self, db: &Database) -> Result { + // whole-row-save-ok: test-only seeding; every production write merges from a loaded snapshot and goes through save_if_unchanged let saved: Option = db.upsert(Self::record_id()).content(self).await?; saved.ok_or_else(|| AppError::Internal("failed to save settings".into())) } @@ -435,13 +437,17 @@ fn validate_list(field: &'static str, values: Vec) -> Result } trimmed.push(value.to_string()); } - let mut folded: Vec = trimmed.iter().map(|v| v.to_lowercase()).collect(); + // `to_lowercase` is locale-invariant — `İZİN` lowercases to `i̇zin` (with a + // leftover combining dot) and would slip past `izin` as a distinct entry. + // Same folding as search uses, so the whole app agrees on "the same word". + let mut folded: Vec = trimmed.iter().map(|v| text_fold::fold(v)).collect(); folded.sort_unstable(); folded.dedup(); if folded.len() != trimmed.len() { return Err(ValidationError::Invalid { field, - reason: "entries must be unique (case-insensitive)", + reason: "two entries are the same word apart from upper/lower case \ + or Turkish letters — keep only one of them", }); } Ok(trimmed) @@ -527,6 +533,33 @@ mod tests { assert!(with_kinds(too_many).is_err()); } + /// Rust's `to_lowercase` is locale-invariant: `İZİN` becomes `i̇zin` (with a + /// combining dot above), which never equals `izin`, so both spellings of + /// the same word used to land in one list. Teachers type these by hand in + /// Turkish, which is exactly when it happens. + #[tokio::test] + async fn turkish_casing_is_a_duplicate() { + let with_kinds = |exam_kinds| { + Settings::try_new(SettingsParams { + exam_kinds, + ..params() + }) + }; + assert!(with_kinds(kinds(&["İZİN", "izin"])).is_err()); + assert!(with_kinds(kinds(&["SINAV", "sınav"])).is_err()); + // Plain English duplicates still die, genuinely distinct entries live. + assert!(with_kinds(kinds(&["Lab", "LAB"])).is_err()); + assert!(with_kinds(kinds(&["izin", "sınav", "lab"])).is_ok()); + // Same rule on the other guarded list. + assert!( + Settings::try_new(SettingsParams { + attendance_statuses: statuses_with(&["İZİN", "izin"]), + ..params() + }) + .is_err() + ); + } + #[tokio::test] async fn kind_weights_resolve_by_exact_name() { let s = Settings::try_new(SettingsParams {