Cotton code
129 lines · 5.7 KB · RustRaw
//! The pure half of the birth — parsing and refusing.//!//! Everything that needs a `Ctx` (the row itself, `ensure_conversation`) is//! driven end to end from `tests/mcp_records_2_sessions.rs`, where there is a//! real file to read back.use super::*;#[test]fn a_session_is_refused_over_a_model_a_tool_a_mode_or_a_branch_nobody_takes() {    let model = validate_create(&json!({ "model": "gpt-9", "prompt": "" })).expect_err("a refusal");    assert!(model.contains("fable"), "{model}");    let branch =        validate_create(&json!({ "branch": "-rf ../x", "prompt": "" })).expect_err("a refusal");    assert!(branch.contains("`branch`"), "{branch}");    let tool = validate_create(&json!({ "tool": "kimi", "prompt": "" })).expect_err("a refusal");    assert!(tool.contains("claude-code, codex"), "{tool}");    let mode = validate_create(&json!({ "permissionMode": "whatever", "prompt": "" }))        .expect_err("a refusal");    assert!(mode.contains("bypass, ask"), "{mode}");}/// Everything the owner or a conversation asks for is a THREAD. The one row/// that is not is written by `ensure_conversation`, and there is no argument/// that makes one.#[test]fn nothing_a_caller_can_send_asks_for_a_conversation() {    let args = validate_create(&json!({ "prompt": "do it", "kind": "conversation" }))        .expect("the arguments");    assert_eq!(args.kind, SessionKind::Thread);    assert_eq!(SessionKind::default(), SessionKind::Thread);}#[test]fn the_parent_is_carried_when_it_is_given_and_absent_when_it_is_blank() {    let named = validate_create(&json!({ "prompt": "do it", "parentSessionId": "  row-1  " }))        .expect("the arguments");    assert_eq!(named.parent_session_id.as_deref(), Some("row-1"));    let blank =        validate_create(&json!({ "prompt": "do it", "parentSessionId": "   " })).expect("arguments");    assert!(blank.parent_session_id.is_none(), "whitespace is not an id");    let none = validate_create(&json!({ "prompt": "do it" })).expect("the arguments");    assert!(none.parent_session_id.is_none());}#[test]fn the_two_words_the_file_spells_and_no_third() {    assert_eq!(SessionKind::Conversation.as_str(), "conversation");    assert_eq!(SessionKind::Thread.as_str(), "thread");    assert_eq!(SessionKind::parse("thread"), Some(SessionKind::Thread));    assert_eq!(        SessionKind::parse("conversation"),        Some(SessionKind::Conversation)    );    assert_eq!(SessionKind::parse("chat"), None);}#[test]fn a_title_is_the_prompts_first_line_and_never_a_paragraph() {    assert_eq!(title_from_prompt("   \n  "), None);    assert_eq!(        title_from_prompt("Fix the flaky test.\nAnd then some more."),        Some("Fix the flaky test.".to_string())    );    let long = "x".repeat(200);    assert_eq!(        title_from_prompt(&long).expect("a title").chars().count(),        TITLE_FROM_PROMPT_MAX    );}// ── resume, restart, and the one verb over both ─────────────────────────────/// RULE: a session that failed before its agent ran is restarted, never refused.// The claim closes a row `failed` and hands the agent id back when it could// not spawn (no folder, no clone, no login), so `can_resume` is false for it —// and the phone's Resume button used to vanish while the wake pass logged a// refusal every minute. Nothing was lost on such a row, so it goes again.#[test]fn a_failed_row_with_no_agent_restarts_and_every_other_row_does_not() {    assert!(can_restart("failed", true, None));    assert!(can_restart("failed", true, Some("")));    // An agent DID run: that is a resume, not a restart.    assert!(!can_restart("failed", true, Some("agent-1")));    // An ended row with no agent id is an older build's closing, not a    // session that never began; nothing here knows what it was.    assert!(!can_restart("ended", true, None));    // Not Cotton's to start again.    assert!(!can_restart("failed", false, None));    for state in ["queued", "starting", "running", "idle", "waiting", "withdrawn"] {        assert!(!can_restart(state, true, None), "{state}");    }}#[test]fn carry_on_is_resume_or_restart_and_nothing_else() {    // A resume: over, hosted, an agent to continue, a Mac to do it on.    assert!(can_carry_on("ended", true, Some("agent-1"), Some("mac-1")));    assert!(can_carry_on("failed", true, Some("agent-1"), Some("mac-1")));    // A restart: failed before any agent ran, whatever Mac it was placed on.    assert!(can_carry_on("failed", true, None, Some("mac-1")));    assert!(can_carry_on("failed", true, None, None));    // Neither.    assert!(!can_carry_on("ended", true, None, Some("mac-1")));    assert!(!can_carry_on("ended", true, Some("agent-1"), None));    assert!(!can_carry_on("failed", false, None, None));    assert!(!can_carry_on("running", true, Some("agent-1"), Some("mac-1")));}/// The words a handed todo starts its session with: its title alone, its/// notes under it, and never blank lines for notes that are only space.#[test]fn a_handed_todos_prompt_is_its_title_and_its_notes_under_it() {    assert_eq!(todo_prompt("Fix the importer", None), "Fix the importer");    assert_eq!(        todo_prompt("Fix the importer", Some("It drops the last row.")),        "Fix the importer\n\nIt drops the last row."    );    assert_eq!(todo_prompt("Fix the importer", Some("   ")), "Fix the importer");}#[test]fn a_project_is_named_by_its_id_as_a_records_link_holds_it() {    let args = validate_create(&json!({ "projectId": "  example-project  ", "todoId": "example-todo" }))        .expect("the arguments");    assert_eq!(args.project_id.as_deref(), Some("example-project"));    assert_eq!(args.project_slug, None);    assert_eq!(args.prompt, "", "the todo's words are filled in where the todo is read");}