Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1447 lines
52 KiB
Rust
1447 lines
52 KiB
Rust
//! SQLite-backed persistence for calls, their transcripts, speaker renames
|
||
//! and merges, and outstanding remote transcription jobs.
|
||
|
||
use std::collections::BTreeMap;
|
||
use std::path::{Path, PathBuf};
|
||
use std::sync::Mutex;
|
||
|
||
use chrono::{DateTime, Local};
|
||
use rusqlite::{params, Connection, Row, TransactionBehavior};
|
||
use thiserror::Error;
|
||
|
||
use crate::merge::{self, MergeError};
|
||
use crate::model::{Call, CallId, CallStatus, Segment, Speaker};
|
||
|
||
/// Longest call title, in characters.
|
||
const MAX_TITLE_CHARS: usize = 120;
|
||
|
||
/// Errors a [`Store`] operation can fail with.
|
||
#[derive(Debug, Error)]
|
||
pub enum StoreError {
|
||
#[error(transparent)]
|
||
Sqlite(#[from] rusqlite::Error),
|
||
|
||
/// A value read back from SQLite could not be interpreted, e.g. an
|
||
/// unknown `status` string, invalid RFC 3339 timestamp or unparsable
|
||
/// speaker key.
|
||
#[error("corrupt stored value: {0}")]
|
||
Corrupt(String),
|
||
|
||
/// A speaker merge that [`merge::check_merge`] refused.
|
||
#[error(transparent)]
|
||
Merge(#[from] MergeError),
|
||
|
||
#[error("Titel zu lang (max. 120 Zeichen)")]
|
||
TitleTooLong,
|
||
}
|
||
|
||
pub type Result<T> = std::result::Result<T, StoreError>;
|
||
|
||
/// Error text stored on calls that were mid-processing when the app last
|
||
/// shut down (or crashed).
|
||
const RECOVERY_ERROR: &str = "Verarbeitung unterbrochen – bitte erneut versuchen";
|
||
|
||
/// What [`Store::startup_recovery`] did to reconcile call states left over
|
||
/// from an unclean shutdown.
|
||
#[derive(Debug, Clone, PartialEq, Default)]
|
||
pub struct RecoveryReport {
|
||
/// Calls moved from `Recording` to `AwaitingDecision`.
|
||
pub interrupted: Vec<CallId>,
|
||
/// Calls moved from `Processing` to `Failed`.
|
||
pub failed: Vec<CallId>,
|
||
}
|
||
|
||
/// Version 1: the first release. `calls.account_id` (the customer mock) and
|
||
/// `summaries` are no longer used; they stay so no migration has to drop
|
||
/// them.
|
||
const SCHEMA_V1_SQL: &str = "
|
||
CREATE TABLE IF NOT EXISTS calls (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
app TEXT NOT NULL,
|
||
started_at TEXT NOT NULL,
|
||
ended_at TEXT,
|
||
status TEXT NOT NULL,
|
||
interrupted INTEGER NOT NULL,
|
||
error TEXT,
|
||
mic_path TEXT NOT NULL,
|
||
out_path TEXT NOT NULL,
|
||
transcribed_at TEXT,
|
||
audio_deleted INTEGER NOT NULL,
|
||
account_id TEXT
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS segments (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
|
||
seq INTEGER NOT NULL,
|
||
speaker TEXT NOT NULL,
|
||
start_ts REAL NOT NULL,
|
||
end_ts REAL NOT NULL,
|
||
text TEXT NOT NULL
|
||
);
|
||
CREATE INDEX IF NOT EXISTS segments_call_id_idx ON segments(call_id);
|
||
|
||
CREATE TABLE IF NOT EXISTS summaries (
|
||
call_id INTEGER PRIMARY KEY REFERENCES calls(id) ON DELETE CASCADE,
|
||
json TEXT NOT NULL,
|
||
edited INTEGER NOT NULL
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS speaker_names (
|
||
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
|
||
speaker_key TEXT NOT NULL,
|
||
name TEXT NOT NULL,
|
||
PRIMARY KEY (call_id, speaker_key)
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS remote_jobs (
|
||
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
|
||
track TEXT NOT NULL,
|
||
remote_id TEXT NOT NULL,
|
||
PRIMARY KEY (call_id, track, remote_id)
|
||
);
|
||
";
|
||
|
||
/// Version 2: call titles and reversible speaker merges.
|
||
const SCHEMA_V2_SQL: &str = "
|
||
ALTER TABLE calls ADD COLUMN title TEXT;
|
||
|
||
CREATE TABLE speaker_merges (
|
||
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
|
||
from_key TEXT NOT NULL,
|
||
into_key TEXT NOT NULL,
|
||
PRIMARY KEY (call_id, from_key)
|
||
);
|
||
";
|
||
|
||
/// Version 3: the last upload of the transcript to the CRM.
|
||
const SCHEMA_V3_SQL: &str = "
|
||
ALTER TABLE calls ADD COLUMN crm_transcript_id TEXT;
|
||
ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
|
||
";
|
||
|
||
/// Version 4: which backend an upload belongs to, so a build whose own
|
||
/// backend differs (see `crate::backend`) does not show someone else's
|
||
/// upload as done (`call_view::uploaded_here`). Every upload before this
|
||
/// version only ever went to the test backend (production never had the
|
||
/// API yet): backfilled here, so existing rows keep meaning what they did.
|
||
const SCHEMA_V4_SQL: &str = "
|
||
ALTER TABLE calls ADD COLUMN crm_base_url TEXT;
|
||
UPDATE calls SET crm_base_url = 'https://test.euvinopro.eu' WHERE crm_transcript_id IS NOT NULL;
|
||
";
|
||
|
||
/// Version 5: the language a call is transcribed in, chosen per call when
|
||
/// its transcription is started. Before this version every transcription
|
||
/// used the then default German: backfilled for the calls one was started
|
||
/// for, so their corrections (`call_view`) stay as they were; a call still
|
||
/// waiting gets its language when the user decides.
|
||
const SCHEMA_V5_SQL: &str = "
|
||
ALTER TABLE calls ADD COLUMN language TEXT;
|
||
UPDATE calls SET language = 'de' WHERE status NOT IN ('recording', 'awaiting_decision');
|
||
";
|
||
|
||
/// Schema steps by `PRAGMA user_version`: entry `i` takes a database from
|
||
/// version `i` to `i + 1`. A new database runs all of them.
|
||
const MIGRATIONS: &[&str] = &[
|
||
SCHEMA_V1_SQL,
|
||
SCHEMA_V2_SQL,
|
||
SCHEMA_V3_SQL,
|
||
SCHEMA_V4_SQL,
|
||
SCHEMA_V5_SQL,
|
||
];
|
||
|
||
/// SQLite-backed store for calls and everything derived from them.
|
||
pub struct Store {
|
||
conn: Mutex<Connection>,
|
||
}
|
||
|
||
impl Store {
|
||
/// Opens (creating if necessary) a database file on disk.
|
||
pub fn open(path: &Path) -> Result<Store> {
|
||
let conn = Connection::open(path)?;
|
||
Self::init(conn)
|
||
}
|
||
|
||
/// Opens a throwaway in-memory database, used by tests.
|
||
pub fn open_in_memory() -> Result<Store> {
|
||
let conn = Connection::open_in_memory()?;
|
||
Self::init(conn)
|
||
}
|
||
|
||
fn init(mut conn: Connection) -> Result<Store> {
|
||
conn.execute_batch("PRAGMA foreign_keys = ON;")?;
|
||
migrate(&mut conn)?;
|
||
Ok(Store {
|
||
conn: Mutex::new(conn),
|
||
})
|
||
}
|
||
|
||
pub fn create_call(
|
||
&self,
|
||
app: &str,
|
||
started_at: DateTime<Local>,
|
||
mic_path: &Path,
|
||
out_path: &Path,
|
||
) -> Result<CallId> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"INSERT INTO calls \
|
||
(app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted) \
|
||
VALUES (?1, ?2, NULL, ?3, 0, NULL, ?4, ?5, NULL, 0)",
|
||
params![
|
||
app,
|
||
dt_to_str(started_at),
|
||
status_to_str(CallStatus::Recording),
|
||
path_to_str(mic_path)?,
|
||
path_to_str(out_path)?,
|
||
],
|
||
)?;
|
||
Ok(conn.last_insert_rowid())
|
||
}
|
||
|
||
pub fn finish_recording(
|
||
&self,
|
||
id: CallId,
|
||
ended_at: DateTime<Local>,
|
||
interrupted: bool,
|
||
) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"UPDATE calls SET ended_at = ?1, interrupted = ?2, status = ?3 WHERE id = ?4",
|
||
params![
|
||
dt_to_str(ended_at),
|
||
interrupted,
|
||
status_to_str(CallStatus::AwaitingDecision),
|
||
id
|
||
],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
/// Sets the track file paths, for callers that derive them from the
|
||
/// call id and so can only know them after [`Store::create_call`].
|
||
pub fn set_paths(&self, id: CallId, mic_path: &Path, out_path: &Path) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"UPDATE calls SET mic_path = ?1, out_path = ?2 WHERE id = ?3",
|
||
params![path_to_str(mic_path)?, path_to_str(out_path)?, id],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
pub fn set_status(&self, id: CallId, status: CallStatus, error: Option<&str>) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"UPDATE calls SET status = ?1, error = ?2 WHERE id = ?3",
|
||
params![status_to_str(status), error, id],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
/// Corrects the end time of an interrupted recording.
|
||
pub fn set_ended_at(&self, id: CallId, at: DateTime<Local>) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"UPDATE calls SET ended_at = ?1 WHERE id = ?2",
|
||
params![dt_to_str(at), id],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
pub fn get_call(&self, id: CallId) -> Result<Option<Call>> {
|
||
let conn = self.conn.lock().unwrap();
|
||
let mut stmt = conn.prepare(SELECT_CALL_COLUMNS_SQL)?;
|
||
let mut rows = stmt.query(params![id])?;
|
||
match rows.next()? {
|
||
Some(row) => Ok(Some(row_to_call(row)?)),
|
||
None => Ok(None),
|
||
}
|
||
}
|
||
|
||
/// Lists all calls, newest (by `started_at`) first.
|
||
pub fn list_calls(&self) -> Result<Vec<Call>> {
|
||
let conn = self.conn.lock().unwrap();
|
||
let sql = format!("{} ORDER BY started_at DESC, id DESC", SELECT_ALL_CALLS_SQL);
|
||
let mut stmt = conn.prepare(&sql)?;
|
||
let mut rows = stmt.query([])?;
|
||
let mut calls = Vec::new();
|
||
while let Some(row) = rows.next()? {
|
||
calls.push(row_to_call(row)?);
|
||
}
|
||
Ok(calls)
|
||
}
|
||
|
||
/// Replaces all stored segments for a call with `segments`.
|
||
pub fn save_segments(&self, id: CallId, segments: &[Segment]) -> Result<()> {
|
||
let mut conn = self.conn.lock().unwrap();
|
||
let tx = conn.transaction()?;
|
||
tx.execute("DELETE FROM segments WHERE call_id = ?1", params![id])?;
|
||
for (seq, segment) in segments.iter().enumerate() {
|
||
tx.execute(
|
||
"INSERT INTO segments (call_id, seq, speaker, start_ts, end_ts, text) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||
params![id, seq as i64, segment.speaker.key(), segment.start, segment.end, segment.text],
|
||
)?;
|
||
}
|
||
tx.commit()?;
|
||
Ok(())
|
||
}
|
||
|
||
pub fn segments(&self, id: CallId) -> Result<Vec<Segment>> {
|
||
let conn = self.conn.lock().unwrap();
|
||
let mut stmt = conn.prepare(
|
||
"SELECT speaker, start_ts, end_ts, text FROM segments WHERE call_id = ?1 ORDER BY seq ASC",
|
||
)?;
|
||
let mut rows = stmt.query(params![id])?;
|
||
let mut segments = Vec::new();
|
||
while let Some(row) = rows.next()? {
|
||
let speaker_key: String = row.get(0)?;
|
||
segments.push(Segment {
|
||
speaker: speaker_from_key(&speaker_key)?,
|
||
start: row.get(1)?,
|
||
end: row.get(2)?,
|
||
text: row.get(3)?,
|
||
});
|
||
}
|
||
Ok(segments)
|
||
}
|
||
|
||
pub fn set_speaker_name(&self, id: CallId, speaker_key: &str, name: &str) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"INSERT INTO speaker_names (call_id, speaker_key, name) VALUES (?1, ?2, ?3) \
|
||
ON CONFLICT(call_id, speaker_key) DO UPDATE SET name = excluded.name",
|
||
params![id, speaker_key, name],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
/// Drops the rename of `speaker_key`, so it shows its default label
|
||
/// again. Not an error if it was never renamed.
|
||
pub fn remove_speaker_name(&self, id: CallId, speaker_key: &str) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"DELETE FROM speaker_names WHERE call_id = ?1 AND speaker_key = ?2",
|
||
params![id, speaker_key],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
pub fn speaker_names(&self, id: CallId) -> Result<BTreeMap<String, String>> {
|
||
let conn = self.conn.lock().unwrap();
|
||
let mut stmt =
|
||
conn.prepare("SELECT speaker_key, name FROM speaker_names WHERE call_id = ?1")?;
|
||
let mut rows = stmt.query(params![id])?;
|
||
let mut names = BTreeMap::new();
|
||
while let Some(row) = rows.next()? {
|
||
names.insert(row.get(0)?, row.get(1)?);
|
||
}
|
||
Ok(names)
|
||
}
|
||
|
||
/// Merges remote speaker `from_key` into `into_key` for call `id`,
|
||
/// replacing an earlier merge of `from_key`. Refused (see
|
||
/// [`merge::check_merge`]) for "me", a speaker merged into itself, and a
|
||
/// merge that would make the call's merges loop.
|
||
pub fn set_speaker_merge(&self, id: CallId, from_key: &str, into_key: &str) -> Result<()> {
|
||
let mut conn = self.conn.lock().unwrap();
|
||
let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||
merge::check_merge(&merges_of(&tx, id)?, from_key, into_key)?;
|
||
tx.execute(
|
||
"INSERT INTO speaker_merges (call_id, from_key, into_key) VALUES (?1, ?2, ?3) \
|
||
ON CONFLICT(call_id, from_key) DO UPDATE SET into_key = excluded.into_key",
|
||
params![id, from_key, into_key],
|
||
)?;
|
||
tx.commit()?;
|
||
Ok(())
|
||
}
|
||
|
||
/// Undoes the merge of `from_key`. Not an error if it was not merged.
|
||
pub fn remove_speaker_merge(&self, id: CallId, from_key: &str) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"DELETE FROM speaker_merges WHERE call_id = ?1 AND from_key = ?2",
|
||
params![id, from_key],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
/// The call's speaker merges, source key → target key as stored (chains
|
||
/// are resolved by [`merge::resolve_merges`]).
|
||
pub fn speaker_merges(&self, id: CallId) -> Result<BTreeMap<String, String>> {
|
||
let conn = self.conn.lock().unwrap();
|
||
merges_of(&conn, id)
|
||
}
|
||
|
||
/// Names the call. The title is trimmed; an empty or missing one clears
|
||
/// it. Longer than 120 characters is refused.
|
||
pub fn set_title(&self, id: CallId, title: Option<&str>) -> Result<()> {
|
||
let title = title.map(str::trim).filter(|t| !t.is_empty());
|
||
if title.is_some_and(|t| t.chars().count() > MAX_TITLE_CHARS) {
|
||
return Err(StoreError::TitleTooLong);
|
||
}
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"UPDATE calls SET title = ?1 WHERE id = ?2",
|
||
params![title, id],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
/// Records that the transcript was uploaded to `crm_base_url` at `at`,
|
||
/// which gave it `crm_transcript_id`; replaces an earlier upload's
|
||
/// record (even one of another backend — one upload slot per call, see
|
||
/// `call_view::uploaded_here`).
|
||
pub fn set_uploaded(
|
||
&self,
|
||
id: CallId,
|
||
crm_transcript_id: &str,
|
||
crm_base_url: &str,
|
||
at: DateTime<Local>,
|
||
) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"UPDATE calls SET crm_transcript_id = ?1, crm_base_url = ?2, uploaded_at = ?3 WHERE id = ?4",
|
||
params![crm_transcript_id, crm_base_url, dt_to_str(at), id],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
/// Sets the language (ISO 639-1 code) the call is transcribed in.
|
||
pub fn set_language(&self, id: CallId, language: &str) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"UPDATE calls SET language = ?1 WHERE id = ?2",
|
||
params![language, id],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
pub fn mark_transcribed(&self, id: CallId, at: DateTime<Local>) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"UPDATE calls SET transcribed_at = ?1 WHERE id = ?2",
|
||
params![dt_to_str(at), id],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
pub fn mark_audio_deleted(&self, id: CallId) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"UPDATE calls SET audio_deleted = 1 WHERE id = ?1",
|
||
params![id],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
/// Deletes a call and every row derived from it (segments, speaker
|
||
/// names and merges, remote jobs; also the `summaries` table of schema
|
||
/// v1, which nothing writes).
|
||
pub fn delete_call(&self, id: CallId) -> Result<()> {
|
||
let mut conn = self.conn.lock().unwrap();
|
||
let tx = conn.transaction()?;
|
||
tx.execute("DELETE FROM segments WHERE call_id = ?1", params![id])?;
|
||
tx.execute("DELETE FROM summaries WHERE call_id = ?1", params![id])?;
|
||
tx.execute("DELETE FROM speaker_names WHERE call_id = ?1", params![id])?;
|
||
tx.execute("DELETE FROM speaker_merges WHERE call_id = ?1", params![id])?;
|
||
tx.execute("DELETE FROM remote_jobs WHERE call_id = ?1", params![id])?;
|
||
tx.execute("DELETE FROM calls WHERE id = ?1", params![id])?;
|
||
tx.commit()?;
|
||
Ok(())
|
||
}
|
||
|
||
pub fn record_remote_job(&self, id: CallId, track: &str, remote_id: &str) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute(
|
||
"INSERT OR REPLACE INTO remote_jobs (call_id, track, remote_id) VALUES (?1, ?2, ?3)",
|
||
params![id, track, remote_id],
|
||
)?;
|
||
Ok(())
|
||
}
|
||
|
||
pub fn remote_jobs(&self, id: CallId) -> Result<Vec<(String, String)>> {
|
||
let conn = self.conn.lock().unwrap();
|
||
let mut stmt =
|
||
conn.prepare("SELECT track, remote_id FROM remote_jobs WHERE call_id = ?1")?;
|
||
let mut rows = stmt.query(params![id])?;
|
||
let mut jobs = Vec::new();
|
||
while let Some(row) = rows.next()? {
|
||
jobs.push((row.get(0)?, row.get(1)?));
|
||
}
|
||
Ok(jobs)
|
||
}
|
||
|
||
/// Calls that still have remote jobs on record (their delete failed or
|
||
/// never ran), ordered by id.
|
||
pub fn calls_with_remote_jobs(&self) -> Result<Vec<CallId>> {
|
||
let conn = self.conn.lock().unwrap();
|
||
let mut stmt =
|
||
conn.prepare("SELECT DISTINCT call_id FROM remote_jobs ORDER BY call_id ASC")?;
|
||
let mut rows = stmt.query([])?;
|
||
let mut ids = Vec::new();
|
||
while let Some(row) = rows.next()? {
|
||
ids.push(row.get(0)?);
|
||
}
|
||
Ok(ids)
|
||
}
|
||
|
||
pub fn clear_remote_jobs(&self, id: CallId) -> Result<()> {
|
||
let conn = self.conn.lock().unwrap();
|
||
conn.execute("DELETE FROM remote_jobs WHERE call_id = ?1", params![id])?;
|
||
Ok(())
|
||
}
|
||
|
||
/// Reconciles call states left over from an unclean shutdown (the app
|
||
/// crashed or was killed mid-recording or mid-processing).
|
||
///
|
||
/// - `Recording` calls were still being recorded: they move to
|
||
/// `AwaitingDecision`, with `interrupted` set and `ended_at` set to now
|
||
/// if it was not already set (a later pass corrects it from the WAV
|
||
/// length).
|
||
/// - `Processing` calls were mid-transcription: they move
|
||
/// to `Failed` with a fixed error message.
|
||
///
|
||
/// Both updates run in a single transaction.
|
||
pub fn startup_recovery(&self) -> Result<RecoveryReport> {
|
||
let now = Local::now();
|
||
let mut conn = self.conn.lock().unwrap();
|
||
let tx = conn.transaction()?;
|
||
|
||
let interrupted = ids_with_status(&tx, CallStatus::Recording)?;
|
||
for id in &interrupted {
|
||
tx.execute(
|
||
"UPDATE calls SET status = ?1, interrupted = 1, ended_at = COALESCE(ended_at, ?2) \
|
||
WHERE id = ?3",
|
||
params![
|
||
status_to_str(CallStatus::AwaitingDecision),
|
||
dt_to_str(now),
|
||
id
|
||
],
|
||
)?;
|
||
}
|
||
|
||
let failed = ids_with_status(&tx, CallStatus::Processing)?;
|
||
for id in &failed {
|
||
tx.execute(
|
||
"UPDATE calls SET status = ?1, error = ?2 WHERE id = ?3",
|
||
params![status_to_str(CallStatus::Failed), RECOVERY_ERROR, id],
|
||
)?;
|
||
}
|
||
|
||
tx.commit()?;
|
||
Ok(RecoveryReport {
|
||
interrupted,
|
||
failed,
|
||
})
|
||
}
|
||
}
|
||
|
||
/// Brings the schema to the newest version in one transaction, so a failed
|
||
/// upgrade leaves the database as it was. Running it on an up-to-date
|
||
/// database changes nothing.
|
||
fn migrate(conn: &mut Connection) -> Result<()> {
|
||
let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||
let version: i64 = tx.query_row("PRAGMA user_version", [], |row| row.get(0))?;
|
||
let done = usize::try_from(version).unwrap_or(0);
|
||
if done < MIGRATIONS.len() {
|
||
for step in &MIGRATIONS[done..] {
|
||
tx.execute_batch(step)?;
|
||
}
|
||
tx.execute_batch(&format!("PRAGMA user_version = {};", MIGRATIONS.len()))?;
|
||
}
|
||
tx.commit()?;
|
||
Ok(())
|
||
}
|
||
|
||
fn merges_of(conn: &Connection, id: CallId) -> Result<BTreeMap<String, String>> {
|
||
let mut stmt =
|
||
conn.prepare("SELECT from_key, into_key FROM speaker_merges WHERE call_id = ?1")?;
|
||
let mut rows = stmt.query(params![id])?;
|
||
let mut merges = BTreeMap::new();
|
||
while let Some(row) = rows.next()? {
|
||
merges.insert(row.get(0)?, row.get(1)?);
|
||
}
|
||
Ok(merges)
|
||
}
|
||
|
||
/// Ids of calls currently in `status`, ordered by id (insertion order).
|
||
fn ids_with_status(conn: &rusqlite::Connection, status: CallStatus) -> Result<Vec<CallId>> {
|
||
let mut stmt = conn.prepare("SELECT id FROM calls WHERE status = ?1 ORDER BY id ASC")?;
|
||
let mut rows = stmt.query(params![status_to_str(status)])?;
|
||
let mut ids = Vec::new();
|
||
while let Some(row) = rows.next()? {
|
||
ids.push(row.get(0)?);
|
||
}
|
||
Ok(ids)
|
||
}
|
||
|
||
const SELECT_ALL_CALLS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title, crm_transcript_id, uploaded_at, crm_base_url, language FROM calls";
|
||
|
||
const SELECT_CALL_COLUMNS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, title, crm_transcript_id, uploaded_at, crm_base_url, language FROM calls WHERE id = ?1";
|
||
|
||
fn row_to_call(row: &Row) -> Result<Call> {
|
||
let id: CallId = row.get(0)?;
|
||
let app: String = row.get(1)?;
|
||
let started_at: String = row.get(2)?;
|
||
let ended_at: Option<String> = row.get(3)?;
|
||
let status: String = row.get(4)?;
|
||
let interrupted: bool = row.get(5)?;
|
||
let error: Option<String> = row.get(6)?;
|
||
let mic_path: String = row.get(7)?;
|
||
let out_path: String = row.get(8)?;
|
||
let transcribed_at: Option<String> = row.get(9)?;
|
||
let audio_deleted: bool = row.get(10)?;
|
||
let title: Option<String> = row.get(11)?;
|
||
let crm_transcript_id: Option<String> = row.get(12)?;
|
||
let uploaded_at: Option<String> = row.get(13)?;
|
||
let crm_base_url: Option<String> = row.get(14)?;
|
||
let language: Option<String> = row.get(15)?;
|
||
|
||
Ok(Call {
|
||
id,
|
||
app,
|
||
started_at: dt_from_str(&started_at)?,
|
||
ended_at: opt_dt_from_str(ended_at)?,
|
||
status: status_from_str(&status)?,
|
||
interrupted,
|
||
error,
|
||
mic_path: PathBuf::from(mic_path),
|
||
out_path: PathBuf::from(out_path),
|
||
transcribed_at: opt_dt_from_str(transcribed_at)?,
|
||
audio_deleted,
|
||
title,
|
||
crm_transcript_id,
|
||
crm_base_url,
|
||
uploaded_at: opt_dt_from_str(uploaded_at)?,
|
||
language,
|
||
})
|
||
}
|
||
|
||
fn path_to_str(path: &Path) -> Result<&str> {
|
||
path.to_str()
|
||
.ok_or_else(|| StoreError::Corrupt(format!("non-utf8 path: {}", path.display())))
|
||
}
|
||
|
||
fn dt_to_str(dt: DateTime<Local>) -> String {
|
||
dt.to_rfc3339()
|
||
}
|
||
|
||
fn dt_from_str(s: &str) -> Result<DateTime<Local>> {
|
||
DateTime::parse_from_rfc3339(s)
|
||
.map(|dt| dt.with_timezone(&Local))
|
||
.map_err(|e| StoreError::Corrupt(format!("invalid timestamp {s:?}: {e}")))
|
||
}
|
||
|
||
fn opt_dt_from_str(s: Option<String>) -> Result<Option<DateTime<Local>>> {
|
||
s.map(|s| dt_from_str(&s)).transpose()
|
||
}
|
||
|
||
fn status_to_str(status: CallStatus) -> &'static str {
|
||
match status {
|
||
CallStatus::Recording => "recording",
|
||
CallStatus::AwaitingDecision => "awaiting_decision",
|
||
CallStatus::Processing => "processing",
|
||
CallStatus::Done => "done",
|
||
CallStatus::Failed => "failed",
|
||
}
|
||
}
|
||
|
||
fn status_from_str(s: &str) -> Result<CallStatus> {
|
||
match s {
|
||
"recording" => Ok(CallStatus::Recording),
|
||
"awaiting_decision" => Ok(CallStatus::AwaitingDecision),
|
||
"processing" => Ok(CallStatus::Processing),
|
||
"done" => Ok(CallStatus::Done),
|
||
"failed" => Ok(CallStatus::Failed),
|
||
other => Err(StoreError::Corrupt(format!(
|
||
"unknown call status: {other:?}"
|
||
))),
|
||
}
|
||
}
|
||
|
||
fn speaker_from_key(key: &str) -> Result<Speaker> {
|
||
Speaker::from_key(key)
|
||
.ok_or_else(|| StoreError::Corrupt(format!("invalid speaker key: {key:?}")))
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use std::collections::BTreeMap;
|
||
use std::path::{Path, PathBuf};
|
||
|
||
use chrono::Local;
|
||
|
||
use crate::model::{CallId, CallStatus, Segment, Speaker};
|
||
use crate::store::Store;
|
||
|
||
/// The schema of user_version 1, exactly as the first release created
|
||
/// it, so the upgrade runs against what users have on disk.
|
||
const RELEASED_V1_SCHEMA_SQL: &str = "
|
||
CREATE TABLE IF NOT EXISTS calls (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
app TEXT NOT NULL,
|
||
started_at TEXT NOT NULL,
|
||
ended_at TEXT,
|
||
status TEXT NOT NULL,
|
||
interrupted INTEGER NOT NULL,
|
||
error TEXT,
|
||
mic_path TEXT NOT NULL,
|
||
out_path TEXT NOT NULL,
|
||
transcribed_at TEXT,
|
||
audio_deleted INTEGER NOT NULL,
|
||
account_id TEXT
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS segments (
|
||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
|
||
seq INTEGER NOT NULL,
|
||
speaker TEXT NOT NULL,
|
||
start_ts REAL NOT NULL,
|
||
end_ts REAL NOT NULL,
|
||
text TEXT NOT NULL
|
||
);
|
||
CREATE INDEX IF NOT EXISTS segments_call_id_idx ON segments(call_id);
|
||
|
||
CREATE TABLE IF NOT EXISTS summaries (
|
||
call_id INTEGER PRIMARY KEY REFERENCES calls(id) ON DELETE CASCADE,
|
||
json TEXT NOT NULL,
|
||
edited INTEGER NOT NULL
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS speaker_names (
|
||
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
|
||
speaker_key TEXT NOT NULL,
|
||
name TEXT NOT NULL,
|
||
PRIMARY KEY (call_id, speaker_key)
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS remote_jobs (
|
||
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
|
||
track TEXT NOT NULL,
|
||
remote_id TEXT NOT NULL,
|
||
PRIMARY KEY (call_id, track, remote_id)
|
||
);
|
||
";
|
||
|
||
/// The schema of user_version 2, exactly as release 0.1.1 created it:
|
||
/// version 1 plus these statements.
|
||
const RELEASED_V2_STEP_SQL: &str = "
|
||
ALTER TABLE calls ADD COLUMN title TEXT;
|
||
|
||
CREATE TABLE speaker_merges (
|
||
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
|
||
from_key TEXT NOT NULL,
|
||
into_key TEXT NOT NULL,
|
||
PRIMARY KEY (call_id, from_key)
|
||
);
|
||
";
|
||
|
||
/// The schema of user_version 3, exactly as release 0.2.0 created it
|
||
/// (copied from `SCHEMA_V3_SQL` at `HEAD`): version 2 plus these
|
||
/// statements.
|
||
const RELEASED_V3_STEP_SQL: &str = "
|
||
ALTER TABLE calls ADD COLUMN crm_transcript_id TEXT;
|
||
ALTER TABLE calls ADD COLUMN uploaded_at TEXT;
|
||
";
|
||
|
||
/// The schema of user_version 4, exactly as release 0.2.2 created it
|
||
/// (copied from `SCHEMA_V4_SQL` at `HEAD`): version 3 plus these
|
||
/// statements.
|
||
const RELEASED_V4_STEP_SQL: &str = "
|
||
ALTER TABLE calls ADD COLUMN crm_base_url TEXT;
|
||
UPDATE calls SET crm_base_url = 'https://test.euvinopro.eu' WHERE crm_transcript_id IS NOT NULL;
|
||
";
|
||
|
||
fn user_version(path: &Path) -> i64 {
|
||
rusqlite::Connection::open(path)
|
||
.unwrap()
|
||
.query_row("PRAGMA user_version", [], |row| row.get(0))
|
||
.unwrap()
|
||
}
|
||
|
||
fn new_call(store: &Store) -> CallId {
|
||
store
|
||
.create_call("teams.exe", Local::now(), Path::new(""), Path::new(""))
|
||
.unwrap()
|
||
}
|
||
|
||
fn merge_map(pairs: &[(&str, &str)]) -> BTreeMap<String, String> {
|
||
pairs
|
||
.iter()
|
||
.map(|(from, into)| (from.to_string(), into.to_string()))
|
||
.collect()
|
||
}
|
||
|
||
#[test]
|
||
fn a_new_database_starts_at_version_5() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let path = dir.path().join("calls.sqlite");
|
||
let store = Store::open(&path).unwrap();
|
||
let id = new_call(&store);
|
||
store.set_title(id, Some("Angebot")).unwrap();
|
||
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
|
||
store
|
||
.set_uploaded(id, "4711", "https://backend.example", Local::now())
|
||
.unwrap();
|
||
drop(store);
|
||
|
||
assert_eq!(user_version(&path), 5);
|
||
}
|
||
|
||
#[test]
|
||
fn a_version_1_database_is_upgraded_keeping_its_data() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let path = dir.path().join("calls.sqlite");
|
||
let started = "2026-11-05T10:14:00+01:00";
|
||
{
|
||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||
conn.execute_batch(RELEASED_V1_SCHEMA_SQL).unwrap();
|
||
conn.execute_batch("PRAGMA user_version = 1;").unwrap();
|
||
conn.execute(
|
||
"INSERT INTO calls (app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id) \
|
||
VALUES ('Microsoft Teams', ?1, ?1, 'done', 0, NULL, '/rec/1-mic.wav', '/rec/1-out.wav', ?1, 0, 'mock-2')",
|
||
rusqlite::params![started],
|
||
)
|
||
.unwrap();
|
||
conn.execute_batch(
|
||
"INSERT INTO segments (call_id, seq, speaker, start_ts, end_ts, text) VALUES \
|
||
(1, 0, 'me', 0.0, 1.5, 'Guten Tag.'), \
|
||
(1, 1, 'remote:2', 2.0, 3.0, 'Hallo.'); \
|
||
INSERT INTO speaker_names (call_id, speaker_key, name) VALUES (1, 'remote:2', 'Frau Probe');",
|
||
)
|
||
.unwrap();
|
||
}
|
||
|
||
let store = Store::open(&path).unwrap();
|
||
|
||
let call = store.get_call(1).unwrap().expect("call kept");
|
||
assert_eq!(call.app, "Microsoft Teams");
|
||
assert_eq!(call.status, CallStatus::Done);
|
||
assert_eq!(call.mic_path, PathBuf::from("/rec/1-mic.wav"));
|
||
assert_eq!(call.title, None);
|
||
let texts: Vec<_> = store
|
||
.segments(1)
|
||
.unwrap()
|
||
.into_iter()
|
||
.map(|s| (s.speaker, s.text))
|
||
.collect();
|
||
assert_eq!(
|
||
texts,
|
||
[
|
||
(Speaker::Me, "Guten Tag.".to_string()),
|
||
(Speaker::Remote(2), "Hallo.".to_string())
|
||
]
|
||
);
|
||
assert_eq!(
|
||
store.speaker_names(1).unwrap(),
|
||
merge_map(&[("remote:2", "Frau Probe")])
|
||
);
|
||
assert_eq!(store.speaker_merges(1).unwrap(), BTreeMap::new());
|
||
|
||
// The new column and table work, and opening again changes nothing.
|
||
store.set_title(1, Some("Lieferung März")).unwrap();
|
||
store.set_speaker_merge(1, "remote:2", "remote:1").unwrap();
|
||
drop(store);
|
||
assert_eq!(user_version(&path), 5);
|
||
let store = Store::open(&path).unwrap();
|
||
let call = store.get_call(1).unwrap().unwrap();
|
||
assert_eq!(call.title.as_deref(), Some("Lieferung März"));
|
||
assert_eq!(
|
||
store.speaker_merges(1).unwrap(),
|
||
merge_map(&[("remote:2", "remote:1")])
|
||
);
|
||
assert_eq!(store.list_calls().unwrap().len(), 1);
|
||
}
|
||
|
||
#[test]
|
||
fn a_version_2_database_is_upgraded_keeping_its_data() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let path = dir.path().join("calls.sqlite");
|
||
let started = "2026-11-05T10:14:00+01:00";
|
||
{
|
||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||
conn.execute_batch(RELEASED_V1_SCHEMA_SQL).unwrap();
|
||
conn.execute_batch(RELEASED_V2_STEP_SQL).unwrap();
|
||
conn.execute_batch("PRAGMA user_version = 2;").unwrap();
|
||
conn.execute(
|
||
"INSERT INTO calls (app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title) \
|
||
VALUES ('Microsoft Teams', ?1, ?1, 'done', 0, NULL, '/rec/1-mic.wav', '/rec/1-out.wav', ?1, 1, NULL, 'Angebot')",
|
||
rusqlite::params![started],
|
||
)
|
||
.unwrap();
|
||
conn.execute_batch(
|
||
"INSERT INTO segments (call_id, seq, speaker, start_ts, end_ts, text) VALUES \
|
||
(1, 0, 'remote:2', 0.0, 1.5, 'Hallo.'); \
|
||
INSERT INTO speaker_merges (call_id, from_key, into_key) VALUES (1, 'remote:2', 'remote:1');",
|
||
)
|
||
.unwrap();
|
||
}
|
||
|
||
let store = Store::open(&path).unwrap();
|
||
|
||
let call = store.get_call(1).unwrap().expect("call kept");
|
||
assert_eq!(call.title.as_deref(), Some("Angebot"));
|
||
assert!(call.audio_deleted);
|
||
assert_eq!(call.crm_transcript_id, None);
|
||
assert_eq!(call.uploaded_at, None);
|
||
assert_eq!(store.segments(1).unwrap().len(), 1);
|
||
assert_eq!(
|
||
store.speaker_merges(1).unwrap(),
|
||
merge_map(&[("remote:2", "remote:1")])
|
||
);
|
||
|
||
// The new columns work, and opening again changes nothing.
|
||
let uploaded = Local::now();
|
||
store
|
||
.set_uploaded(1, "crm-1", "https://backend.example", uploaded)
|
||
.unwrap();
|
||
drop(store);
|
||
assert_eq!(user_version(&path), 5);
|
||
let store = Store::open(&path).unwrap();
|
||
let call = store.get_call(1).unwrap().unwrap();
|
||
assert_eq!(call.crm_transcript_id.as_deref(), Some("crm-1"));
|
||
assert_eq!(
|
||
call.crm_base_url.as_deref(),
|
||
Some("https://backend.example")
|
||
);
|
||
assert_eq!(call.uploaded_at, Some(uploaded));
|
||
}
|
||
|
||
#[test]
|
||
fn a_version_3_database_is_upgraded_backfilling_the_test_backend_for_uploads() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let path = dir.path().join("calls.sqlite");
|
||
let started = "2026-11-05T10:14:00+01:00";
|
||
{
|
||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||
conn.execute_batch(RELEASED_V1_SCHEMA_SQL).unwrap();
|
||
conn.execute_batch(RELEASED_V2_STEP_SQL).unwrap();
|
||
conn.execute_batch(RELEASED_V3_STEP_SQL).unwrap();
|
||
conn.execute_batch("PRAGMA user_version = 3;").unwrap();
|
||
// An already-uploaded call (every pre-v4 upload went to the
|
||
// test backend; production never had the API).
|
||
conn.execute(
|
||
"INSERT INTO calls \
|
||
(app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title, crm_transcript_id, uploaded_at) \
|
||
VALUES ('Microsoft Teams', ?1, ?1, 'done', 0, NULL, '/rec/1-mic.wav', '/rec/1-out.wav', ?1, 0, NULL, NULL, 'crm-1', ?1)",
|
||
rusqlite::params![started],
|
||
)
|
||
.unwrap();
|
||
// A call that was never uploaded.
|
||
conn.execute(
|
||
"INSERT INTO calls \
|
||
(app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title, crm_transcript_id, uploaded_at) \
|
||
VALUES ('MicroSIP', ?1, NULL, 'awaiting_decision', 0, NULL, '/rec/2-mic.wav', '/rec/2-out.wav', NULL, 0, NULL, NULL, NULL, NULL)",
|
||
rusqlite::params![started],
|
||
)
|
||
.unwrap();
|
||
}
|
||
|
||
let store = Store::open(&path).unwrap();
|
||
|
||
let uploaded = store.get_call(1).unwrap().expect("call kept");
|
||
assert_eq!(uploaded.crm_transcript_id.as_deref(), Some("crm-1"));
|
||
assert_eq!(
|
||
uploaded.crm_base_url.as_deref(),
|
||
Some("https://test.euvinopro.eu")
|
||
);
|
||
|
||
let not_uploaded = store.get_call(2).unwrap().expect("call kept");
|
||
assert_eq!(not_uploaded.crm_transcript_id, None);
|
||
assert_eq!(not_uploaded.crm_base_url, None);
|
||
|
||
assert_eq!(user_version(&path), 5);
|
||
}
|
||
|
||
#[test]
|
||
fn a_version_4_database_is_upgraded_backfilling_german_for_started_transcriptions() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let path = dir.path().join("calls.sqlite");
|
||
let started = "2026-11-05T10:14:00+01:00";
|
||
{
|
||
let conn = rusqlite::Connection::open(&path).unwrap();
|
||
conn.execute_batch(RELEASED_V1_SCHEMA_SQL).unwrap();
|
||
conn.execute_batch(RELEASED_V2_STEP_SQL).unwrap();
|
||
conn.execute_batch(RELEASED_V3_STEP_SQL).unwrap();
|
||
conn.execute_batch(RELEASED_V4_STEP_SQL).unwrap();
|
||
conn.execute_batch("PRAGMA user_version = 4;").unwrap();
|
||
for (app, status) in [
|
||
("Microsoft Teams", "done"),
|
||
("Zoom", "failed"),
|
||
("MicroSIP", "awaiting_decision"),
|
||
] {
|
||
conn.execute(
|
||
"INSERT INTO calls \
|
||
(app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted) \
|
||
VALUES (?1, ?2, ?2, ?3, 0, NULL, '/rec/mic.wav', '/rec/out.wav', NULL, 0)",
|
||
rusqlite::params![app, started, status],
|
||
)
|
||
.unwrap();
|
||
}
|
||
}
|
||
|
||
let store = Store::open(&path).unwrap();
|
||
|
||
// Transcriptions started before this version all used the then
|
||
// default German; a call still waiting has no language yet.
|
||
let languages: Vec<_> = (1..=3)
|
||
.map(|id| store.get_call(id).unwrap().unwrap().language)
|
||
.collect();
|
||
assert_eq!(
|
||
languages,
|
||
[Some("de".to_string()), Some("de".to_string()), None]
|
||
);
|
||
assert_eq!(user_version(&path), 5);
|
||
}
|
||
|
||
#[test]
|
||
fn the_language_is_stored_per_call() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = new_call(&store);
|
||
let other = new_call(&store);
|
||
assert_eq!(store.get_call(id).unwrap().unwrap().language, None);
|
||
|
||
store.set_language(id, "fr").unwrap();
|
||
|
||
assert_eq!(
|
||
store.get_call(id).unwrap().unwrap().language.as_deref(),
|
||
Some("fr")
|
||
);
|
||
assert_eq!(store.get_call(other).unwrap().unwrap().language, None);
|
||
}
|
||
|
||
#[test]
|
||
fn an_upload_is_recorded_and_replaced_by_the_next_even_for_another_backend() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = new_call(&store);
|
||
let other = new_call(&store);
|
||
let first = Local::now();
|
||
let second = first + chrono::Duration::minutes(5);
|
||
|
||
store
|
||
.set_uploaded(id, "4711", "https://test.euvinopro.eu", first)
|
||
.unwrap();
|
||
let call = store.get_call(id).unwrap().unwrap();
|
||
assert_eq!(
|
||
(
|
||
call.crm_transcript_id.as_deref(),
|
||
call.crm_base_url.as_deref(),
|
||
call.uploaded_at
|
||
),
|
||
(Some("4711"), Some("https://test.euvinopro.eu"), Some(first))
|
||
);
|
||
|
||
// One upload slot per call: a later upload, even to a different
|
||
// backend, replaces it rather than keeping both.
|
||
store
|
||
.set_uploaded(id, "4712", "https://backend.euvinopro.eu", second)
|
||
.unwrap();
|
||
let listed = store.list_calls().unwrap();
|
||
let call = listed.iter().find(|c| c.id == id).unwrap();
|
||
assert_eq!(
|
||
(
|
||
call.crm_transcript_id.as_deref(),
|
||
call.crm_base_url.as_deref(),
|
||
call.uploaded_at
|
||
),
|
||
(
|
||
Some("4712"),
|
||
Some("https://backend.euvinopro.eu"),
|
||
Some(second)
|
||
)
|
||
);
|
||
let untouched = store.get_call(other).unwrap().unwrap();
|
||
assert_eq!(
|
||
(
|
||
untouched.crm_transcript_id,
|
||
untouched.crm_base_url,
|
||
untouched.uploaded_at
|
||
),
|
||
(None, None, None)
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn speaker_merges_are_set_replaced_and_removed() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = new_call(&store);
|
||
let other = new_call(&store);
|
||
assert_eq!(store.speaker_merges(id).unwrap(), BTreeMap::new());
|
||
|
||
store.set_speaker_merge(id, "remote:3", "remote:2").unwrap();
|
||
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
|
||
store
|
||
.set_speaker_merge(other, "remote:1", "remote:2")
|
||
.unwrap();
|
||
assert_eq!(
|
||
store.speaker_merges(id).unwrap(),
|
||
merge_map(&[("remote:2", "remote:1"), ("remote:3", "remote:2")])
|
||
);
|
||
|
||
store.set_speaker_merge(id, "remote:3", "remote:1").unwrap();
|
||
store.remove_speaker_merge(id, "remote:2").unwrap();
|
||
store.remove_speaker_merge(id, "remote:9").unwrap(); // never merged
|
||
assert_eq!(
|
||
store.speaker_merges(id).unwrap(),
|
||
merge_map(&[("remote:3", "remote:1")])
|
||
);
|
||
assert_eq!(
|
||
store.speaker_merges(other).unwrap(),
|
||
merge_map(&[("remote:1", "remote:2")])
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn a_merge_that_would_loop_or_involve_me_is_not_stored() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = new_call(&store);
|
||
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
|
||
|
||
let err = store
|
||
.set_speaker_merge(id, "remote:1", "remote:2")
|
||
.unwrap_err();
|
||
assert_eq!(
|
||
err.to_string(),
|
||
"Diese Zuordnung würde eine Schleife ergeben"
|
||
);
|
||
assert!(store.set_speaker_merge(id, "me", "remote:1").is_err());
|
||
assert!(store.set_speaker_merge(id, "remote:1", "me").is_err());
|
||
assert_eq!(
|
||
store.speaker_merges(id).unwrap(),
|
||
merge_map(&[("remote:2", "remote:1")])
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn the_title_is_trimmed_cleared_when_empty_and_limited_to_120_chars() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = new_call(&store);
|
||
let title = |store: &Store| store.get_call(id).unwrap().unwrap().title;
|
||
assert_eq!(title(&store), None);
|
||
|
||
store.set_title(id, Some(" Angebot Rioja ")).unwrap();
|
||
assert_eq!(title(&store).as_deref(), Some("Angebot Rioja"));
|
||
assert_eq!(
|
||
store.list_calls().unwrap()[0].title.as_deref(),
|
||
Some("Angebot Rioja")
|
||
);
|
||
|
||
store.set_title(id, Some(" ")).unwrap();
|
||
assert_eq!(title(&store), None);
|
||
store.set_title(id, Some("Angebot")).unwrap();
|
||
store.set_title(id, None).unwrap();
|
||
assert_eq!(title(&store), None);
|
||
|
||
let longest = "ä".repeat(120);
|
||
store.set_title(id, Some(&longest)).unwrap();
|
||
assert_eq!(title(&store), Some(longest.clone()));
|
||
let err = store.set_title(id, Some(&"ä".repeat(121))).unwrap_err();
|
||
assert_eq!(err.to_string(), "Titel zu lang (max. 120 Zeichen)");
|
||
assert_eq!(title(&store), Some(longest));
|
||
}
|
||
|
||
#[test]
|
||
fn create_and_finish_call() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let started = Local::now();
|
||
let id = store
|
||
.create_call(
|
||
"teams.exe",
|
||
started,
|
||
Path::new("/tmp/mic.wav"),
|
||
Path::new("/tmp/out.wav"),
|
||
)
|
||
.unwrap();
|
||
|
||
let call = store.get_call(id).unwrap().expect("call exists");
|
||
assert_eq!(call.app, "teams.exe");
|
||
assert_eq!(call.status, CallStatus::Recording);
|
||
assert_eq!(call.started_at, started);
|
||
assert!(call.ended_at.is_none());
|
||
assert!(!call.interrupted);
|
||
assert!(call.error.is_none());
|
||
|
||
let ended = started + chrono::Duration::seconds(30);
|
||
store.finish_recording(id, ended, true).unwrap();
|
||
|
||
let call = store.get_call(id).unwrap().expect("call exists");
|
||
assert_eq!(call.status, CallStatus::AwaitingDecision);
|
||
assert!(call.interrupted);
|
||
assert_eq!(call.ended_at, Some(ended));
|
||
}
|
||
|
||
#[test]
|
||
fn segments_roundtrip_replaces() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = store
|
||
.create_call(
|
||
"teams.exe",
|
||
Local::now(),
|
||
Path::new("/tmp/mic.wav"),
|
||
Path::new("/tmp/out.wav"),
|
||
)
|
||
.unwrap();
|
||
|
||
let first = vec![
|
||
Segment {
|
||
speaker: Speaker::Me,
|
||
start: 0.0,
|
||
end: 1.5,
|
||
text: "Hallo".to_string(),
|
||
},
|
||
Segment {
|
||
speaker: Speaker::Remote(1),
|
||
start: 1.5,
|
||
end: 3.0,
|
||
text: "Hi".to_string(),
|
||
},
|
||
];
|
||
store.save_segments(id, &first).unwrap();
|
||
assert_eq!(store.segments(id).unwrap(), first);
|
||
|
||
let second = vec![Segment {
|
||
speaker: Speaker::Remote(2),
|
||
start: 0.0,
|
||
end: 2.0,
|
||
text: "Neuer Inhalt".to_string(),
|
||
}];
|
||
store.save_segments(id, &second).unwrap();
|
||
assert_eq!(store.segments(id).unwrap(), second);
|
||
}
|
||
|
||
#[test]
|
||
fn speaker_names_upsert() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = store
|
||
.create_call(
|
||
"teams.exe",
|
||
Local::now(),
|
||
Path::new("/tmp/mic.wav"),
|
||
Path::new("/tmp/out.wav"),
|
||
)
|
||
.unwrap();
|
||
|
||
store
|
||
.set_speaker_name(id, "remote:1", "Herr Mueller")
|
||
.unwrap();
|
||
store.set_speaker_name(id, "me", "Ich").unwrap();
|
||
|
||
let names = store.speaker_names(id).unwrap();
|
||
let mut expected = BTreeMap::new();
|
||
expected.insert("remote:1".to_string(), "Herr Mueller".to_string());
|
||
expected.insert("me".to_string(), "Ich".to_string());
|
||
assert_eq!(names, expected);
|
||
|
||
store
|
||
.set_speaker_name(id, "remote:1", "Frau Mueller")
|
||
.unwrap();
|
||
let names = store.speaker_names(id).unwrap();
|
||
assert_eq!(names.len(), 2);
|
||
assert_eq!(names.get("remote:1").unwrap(), "Frau Mueller");
|
||
}
|
||
|
||
#[test]
|
||
fn a_removed_speaker_name_is_gone() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = store
|
||
.create_call("teams.exe", Local::now(), Path::new(""), Path::new(""))
|
||
.unwrap();
|
||
store
|
||
.set_speaker_name(id, "remote:1", "Frau Probe")
|
||
.unwrap();
|
||
store.set_speaker_name(id, "remote:2", "Herr Test").unwrap();
|
||
|
||
store.remove_speaker_name(id, "remote:1").unwrap();
|
||
store.remove_speaker_name(id, "remote:3").unwrap(); // never renamed
|
||
|
||
let names = store.speaker_names(id).unwrap();
|
||
assert_eq!(names.keys().collect::<Vec<_>>(), ["remote:2"]);
|
||
}
|
||
|
||
#[test]
|
||
fn list_newest_first() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let t0 = Local::now();
|
||
let id1 = store
|
||
.create_call(
|
||
"teams.exe",
|
||
t0,
|
||
Path::new("/tmp/mic1.wav"),
|
||
Path::new("/tmp/out1.wav"),
|
||
)
|
||
.unwrap();
|
||
let id2 = store
|
||
.create_call(
|
||
"chrome.exe",
|
||
t0 + chrono::Duration::seconds(10),
|
||
Path::new("/tmp/mic2.wav"),
|
||
Path::new("/tmp/out2.wav"),
|
||
)
|
||
.unwrap();
|
||
let id3 = store
|
||
.create_call(
|
||
"msedge.exe",
|
||
t0 + chrono::Duration::seconds(20),
|
||
Path::new("/tmp/mic3.wav"),
|
||
Path::new("/tmp/out3.wav"),
|
||
)
|
||
.unwrap();
|
||
|
||
let ids: Vec<CallId> = store
|
||
.list_calls()
|
||
.unwrap()
|
||
.into_iter()
|
||
.map(|c| c.id)
|
||
.collect();
|
||
assert_eq!(ids, vec![id3, id2, id1]);
|
||
}
|
||
|
||
#[test]
|
||
fn startup_recovery_marks_states() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let started = Local::now();
|
||
|
||
let recording_id = store
|
||
.create_call(
|
||
"teams.exe",
|
||
started,
|
||
Path::new("/tmp/mic1.wav"),
|
||
Path::new("/tmp/out1.wav"),
|
||
)
|
||
.unwrap();
|
||
|
||
let processing_id = store
|
||
.create_call(
|
||
"chrome.exe",
|
||
started,
|
||
Path::new("/tmp/mic2.wav"),
|
||
Path::new("/tmp/out2.wav"),
|
||
)
|
||
.unwrap();
|
||
store
|
||
.set_status(processing_id, CallStatus::Processing, None)
|
||
.unwrap();
|
||
|
||
let before = Local::now();
|
||
let report = store.startup_recovery().unwrap();
|
||
let after = Local::now();
|
||
|
||
assert_eq!(report.interrupted, vec![recording_id]);
|
||
assert_eq!(report.failed, vec![processing_id]);
|
||
|
||
let recovered = store.get_call(recording_id).unwrap().unwrap();
|
||
assert_eq!(recovered.status, CallStatus::AwaitingDecision);
|
||
assert!(recovered.interrupted);
|
||
let ended_at = recovered.ended_at.expect("ended_at set by recovery");
|
||
assert!(ended_at >= before && ended_at <= after);
|
||
|
||
let failed_call = store.get_call(processing_id).unwrap().unwrap();
|
||
assert_eq!(failed_call.status, CallStatus::Failed);
|
||
assert_eq!(
|
||
failed_call.error.as_deref(),
|
||
Some("Verarbeitung unterbrochen – bitte erneut versuchen")
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn startup_recovery_keeps_existing_ended_at() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let started = Local::now();
|
||
let id = store
|
||
.create_call(
|
||
"teams.exe",
|
||
started,
|
||
Path::new("/tmp/mic.wav"),
|
||
Path::new("/tmp/out.wav"),
|
||
)
|
||
.unwrap();
|
||
|
||
let original_ended_at = started + chrono::Duration::minutes(5);
|
||
store.set_ended_at(id, original_ended_at).unwrap();
|
||
|
||
let report = store.startup_recovery().unwrap();
|
||
assert_eq!(report.interrupted, vec![id]);
|
||
|
||
let recovered = store.get_call(id).unwrap().unwrap();
|
||
assert_eq!(recovered.ended_at, Some(original_ended_at));
|
||
}
|
||
|
||
#[test]
|
||
fn set_paths_updates_track_paths() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = store
|
||
.create_call("Manuell", Local::now(), Path::new(""), Path::new(""))
|
||
.unwrap();
|
||
|
||
store
|
||
.set_paths(id, Path::new("/rec/1-mic.wav"), Path::new("/rec/1-out.wav"))
|
||
.unwrap();
|
||
|
||
let call = store.get_call(id).unwrap().unwrap();
|
||
assert_eq!(call.mic_path, PathBuf::from("/rec/1-mic.wav"));
|
||
assert_eq!(call.out_path, PathBuf::from("/rec/1-out.wav"));
|
||
}
|
||
|
||
#[test]
|
||
fn delete_call_cascades() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let id = store
|
||
.create_call(
|
||
"teams.exe",
|
||
Local::now(),
|
||
Path::new("/tmp/mic.wav"),
|
||
Path::new("/tmp/out.wav"),
|
||
)
|
||
.unwrap();
|
||
|
||
store
|
||
.save_segments(
|
||
id,
|
||
&[Segment {
|
||
speaker: Speaker::Me,
|
||
start: 0.0,
|
||
end: 1.0,
|
||
text: "Hi".to_string(),
|
||
}],
|
||
)
|
||
.unwrap();
|
||
store.set_speaker_name(id, "me", "Ich").unwrap();
|
||
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
|
||
store
|
||
.record_remote_job(id, "deepscript", "job-123")
|
||
.unwrap();
|
||
|
||
store.delete_call(id).unwrap();
|
||
|
||
assert_eq!(store.get_call(id).unwrap(), None);
|
||
assert_eq!(store.segments(id).unwrap(), Vec::new());
|
||
assert_eq!(store.speaker_names(id).unwrap(), BTreeMap::new());
|
||
assert_eq!(store.remote_jobs(id).unwrap(), Vec::new());
|
||
assert_eq!(store.speaker_merges(id).unwrap(), BTreeMap::new());
|
||
}
|
||
|
||
#[test]
|
||
fn calls_with_remote_jobs_lists_each_call_once() {
|
||
let store = Store::open_in_memory().unwrap();
|
||
let call = |app| {
|
||
store
|
||
.create_call(app, Local::now(), Path::new(""), Path::new(""))
|
||
.unwrap()
|
||
};
|
||
let (a, _without_jobs, c) = (call("a"), call("b"), call("c"));
|
||
assert_eq!(
|
||
store.calls_with_remote_jobs().unwrap(),
|
||
Vec::<CallId>::new()
|
||
);
|
||
|
||
store.record_remote_job(c, "mic", "job-c").unwrap();
|
||
store.record_remote_job(a, "mic", "job-a1").unwrap();
|
||
store.record_remote_job(a, "out", "job-a2").unwrap();
|
||
assert_eq!(store.calls_with_remote_jobs().unwrap(), vec![a, c]);
|
||
|
||
store.clear_remote_jobs(a).unwrap();
|
||
assert_eq!(store.calls_with_remote_jobs().unwrap(), vec![c]);
|
||
}
|
||
}
|