//! Schema 149: Cotton Chat's search index (21 Nov 2026). `fts_chat` indexes//! each message's text by any three characters in a row, so a search finds a//! match inside a word in any script: Hindi, whose vowel signs `unicode61`//! splits words at, and Tamil or Thai written without the spaces a word//! index needs. The rise is the index alone: a copy stamped 148 gains it in//! place, EMPTY, and fills it in the background newest first; no column, row//! or rule moves. Invented messages only.use cotton_store::{ create::{apply_schema, fresh_schema, SchemaObject}, fts_fill::{filling, step}, schema::move_intents::{rebuilt_above, INTENTS}, shape_move::{crossing, move_to_current},Driver,};use rusqlite::types::ValueasSqlValue;const INDEX: &str = "fts_chat";const NAMES: [&str; 4] = ["fts_chat", "fts_chat_ai", "fts_chat_ad", "fts_chat_au"];/// A file of this build as 148 left it: no chat index, stamped 148.fn at_148() -> (Driver, Vec<SchemaObject>) {let had: Vec<SchemaObject> = serde_json::from_str(include_str!("fixtures/schema-v148.json")).unwrap(); assert!(!had.iter().any(|o| NAMES.contains(&o.name.as_str())), "148 has no chat index");let db = Driver::open_in_memory().unwrap(); apply_schema(&db).unwrap();for trigger in &NAMES[1..] { db.exec(&format!("DROP TRIGGER {trigger}")).unwrap(); } db.exec(&format!("DROP TABLE {INDEX}")).unwrap(); db.exec("PRAGMA user_version=148").unwrap(); db.exec("INSERT INTO cotton_workspaces(id,name,owner_user_id,created_at) VALUES ('synthetic','Synthetic','person','now'); INSERT INTO cotton_msg_threads(id,workspace_id,kind,title,created_at,updated_at) VALUES ('thread-1','synthetic','group','Invented friends','now','now');", ) .unwrap(); (db, had)}fn add(db: &Driver, id: &str, body: &str) { db.run("INSERT INTO cotton_msg_messages (id, thread_id, workspace_id, direction, channel, origin, sent_at, created_at, body) \ VALUES (?, 'thread-1', 'synthetic', 'in', 'cotton', 'live', '2026-11-21T10:00:00.000Z', '2026-11-21T10:00:00.000Z', ?)", &[SqlValue::Text(id.into()), SqlValue::Text(body.into())], ) .unwrap();}fn found(db: &Driver, text: &str) -> Vec<String> { db.all("SELECT m.id FROM fts_chat f JOIN cotton_msg_messages m ON m.rowid = f.rowid WHERE fts_chat MATCH ? ORDER BY m.id", &[SqlValue::Text(format!("\"{text}\""))], ) .unwrap() .iter() .map(|row| row.text("id").unwrap().to_string()) .collect()}fn rows(db: &Driver) -> Vec<cotton_store::driver::Row> { db.all("SELECT rowid, * FROM cotton_msg_messages ORDER BY rowid", &[]).unwrap()}/// RULE: A copy stamped 148 gains the chat index in place, empty and filling, every message and rowid as it was; filled, it finds text inside a word in any script, case folded, and a message written after the move is found at once.#[test]fn a_148_copy_gains_the_chat_index_and_fills_it() {let (db, had) = at_148(); add(&db, "m-1", "कल शाम को मिलते हैं"); add(&db, "m-2", "நாளை சந்திப்போம்"); add(&db, "m-3", "Dinner at EIGHT?"); add(&db, "m-4", "พรุ่งนี้เจอกัน");let before = rows(&db);let plan = crossing(&had, &fresh_schema().unwrap()).unwrap();let later = rebuilt_above(149); assert_eq!(plan.rebuild.iter().map(String::as_str).collect::<std::collections::BTreeSet<_>>(), later, "only a later rise rebuilds"); assert!(plan.replace.is_empty(), "nothing replaced");letmut created: Vec<&str> = plan .create_tables .iter() .chain(plan.create_rest.iter()) .filter(|o| !later.contains(o.tbl_name.as_str())) .map(|o| o.name.as_str())// 149's own objects (150's indexes stand beside them). .filter(|name| name.starts_with("fts_chat") && (!name.starts_with("fts_chat_") || NAMES.contains(name))) .collect(); created.sort_unstable(); assert_eq!(created, ["fts_chat", "fts_chat_ad", "fts_chat_ai", "fts_chat_au"], "only the chat index is new"); move_to_current(&db).unwrap(); assert_eq!(rows(&db), before, "every message and rowid stays"); assert!(filling(&db, INDEX).unwrap().is_some(), "the index fills in the background"); add(&db, "m-5", "मिलते रहना"); assert_eq!(found(&db, "मिलते"), ["m-5"], "a message after the move is found at once");whileletSome(progress) = step(&db, INDEX, 2).unwrap() {if progress.done {break; } } assert!(filling(&db, INDEX).unwrap().is_none(), "whole"); db.exec("INSERT INTO fts_chat (fts_chat, rank) VALUES ('integrity-check', 1)").expect("the index agrees with its messages");// Inside a word, across a vowel sign and a virama: "िलत" sits inside मिलते. assert_eq!(found(&db, "िलत"), ["m-1", "m-5"]); assert_eq!(found(&db, "சந்தி"), ["m-2"], "Tamil inside a word"); assert_eq!(found(&db, "เจอกัน"), ["m-4"], "Thai, no spaces"); assert_eq!(found(&db, "eight"), ["m-3"], "case folded"); assert_eq!(found(&db, "nner at e"), ["m-3"], "across a space");// Edits and deletions follow. db.run("UPDATE cotton_msg_messages SET body = 'see you at nine' WHERE id = 'm-3'", &[]).unwrap(); assert!(found(&db, "eight").is_empty()); assert_eq!(found(&db, "nine"), ["m-3"]); db.run("DELETE FROM cotton_msg_messages WHERE id = 'm-2'", &[]).unwrap(); assert!(found(&db, "சந்தி").is_empty());}/// RULE: 149 is one rise, additive, and discards no stored value.#[test]fn the_149_rise_is_an_index_and_discards_nothing() {let rises: Vec<_> = INTENTS.iter().filter(|intent| intent.version == 149).collect(); assert_eq!(rises.len(), 1); assert!(rises[0].additive && rises[0].losses.is_empty());}