haematite-cli 0.21.1

The haematite command: serve a store, its door and Studio, answer MCP, query with jq, and host the store's assistant seat
Documentation
//! EMB-E2 (h): the window settings.

use std::path::PathBuf;
use std::time::Duration;

use super::{MAX_WINDOWS, OVERLAP_TOKENS, Options, Settings};
use crate::serve::query::Setting;
use crate::serve::storage::embedded::WINDOWS_MOST;

/// Every setting embedding needs, with `windows`, `max_windows` and
/// `overlap_tokens` as given.
fn options(windows: Setting<bool>, max_windows: Setting<u64>, overlap: Setting<u64>) -> Options {
    Options {
        worker: Setting::Present(PathBuf::from("/bin/sh")),
        model_dir: Setting::Present(PathBuf::from("/")),
        model_sha256: Setting::Present("1".repeat(64)),
        memory_bytes: Setting::Present(1 << 30),
        threads: Setting::Present(1),
        batch_items: Setting::Present(4),
        input_bytes: Setting::Present(16_384),
        tokens: Setting::Present(512),
        frame_bytes: Setting::Present(1 << 20),
        ready_ms: Setting::Present(2_000),
        call_ms: Setting::Present(30_000),
        waiting: Setting::Present(4),
        request_items: Setting::Present(64),
        page_records: Setting::Present(64),
        writer_wait_ms: Setting::Absent,
        windows,
        max_windows,
        overlap_tokens: overlap,
    }
}

fn resolved(options: Options) -> Result<Settings, String> {
    options
        .resolve()?
        .ok_or_else(|| "no embedding settings".to_owned())
}

/// The refusal `options` resolve to.
fn refusal(options: Options) -> Result<String, String> {
    match options.resolve() {
        Err(refused) => Ok(refused),
        Ok(_) => Err("the settings were taken".to_owned()),
    }
}

/// Fails while windows change anything when not asked for: no windows, and
/// a call is `embed_call_ms`.
#[test]
fn windows_off_is_todays_behaviour() -> Result<(), String> {
    let absent = options(Setting::Absent, Setting::Absent, Setting::Absent);
    let settings = resolved(absent)?;
    assert!(settings.windows.is_none());
    assert_eq!(settings.document_call(), Duration::from_secs(30));
    let off = options(Setting::Present(false), Setting::Absent, Setting::Absent);
    assert!(resolved(off)?.windows.is_none());
    Ok(())
}

/// Fails while windows on does not take the ruled values (64 tokens shared,
/// 32 windows at most), or a call is not given its time for each window.
#[test]
fn windows_on_takes_the_ruled_defaults() -> Result<(), String> {
    let on = options(Setting::Present(true), Setting::Absent, Setting::Absent);
    let settings = resolved(on)?;
    let windows = settings.windows.ok_or("windows are off")?;
    assert_eq!(
        (windows.overlap_tokens, windows.max_windows),
        (OVERLAP_TOKENS, MAX_WINDOWS)
    );
    assert_eq!(settings.document_call(), Duration::from_secs(30 * 32));
    let (four, sixteen) = (Setting::Present(4), Setting::Present(16));
    let given = options(Setting::Present(true), four, sixteen);
    let windows = resolved(given)?.windows.ok_or("windows are off")?;
    assert_eq!((windows.overlap_tokens, windows.max_windows), (16, 4));
    Ok(())
}

/// Fails while a cap or overlap is taken without windows on, a cap outside
/// 2 to `WINDOWS_MOST` is taken, or a window setting alone is ignored.
#[test]
fn window_settings_out_of_place_are_refused() -> Result<(), String> {
    let without = options(Setting::Absent, Setting::Present(8), Setting::Absent);
    let refused = refusal(without)?;
    assert!(refused.contains("need embed_windows true"), "{refused}");
    for cap in [1, u64::from(WINDOWS_MOST) + 1] {
        let cap = Setting::Present(cap);
        let refused = refusal(options(Setting::Present(true), cap, Setting::Absent))?;
        assert!(refused.contains("embed_max_windows"), "{refused}");
    }
    let alone = Options {
        windows: Setting::Present(true),
        ..Options::default()
    };
    let refused = refusal(alone)?;
    assert!(refused.contains("missing embed settings"), "{refused}");
    Ok(())
}