//! Tests for the Inkling chat-template layer: the custom tojson/raise_exception //! and generation-strip (deterministic, inline template), prepare_messages / //! finish_prompt (pure), and an #[ignore] render of the real chat_template.jinja. use serde_json::json; use strata_ctl::inkling::dialect::tag; use strata_ctl::inkling::template::{finish_prompt, prepare_messages, ChatTemplate}; #[test] fn tojson_compact_and_sorted() { let t = ChatTemplate::new("{{ {'b': 1, 'a': 2} | tojson(sort_keys=true, separators=(',', ':')) }}") .unwrap(); assert_eq!( t.render(&json!([]), None, None).unwrap(), "{\"a\":2,\"b\":1}" ); } #[test] fn generation_blocks_are_transparent() { let t = ChatTemplate::new("A{% generation %}B{% endgeneration %}C").unwrap(); assert_eq!(t.render(&json!([]), None, None).unwrap(), "ABC"); } #[test] fn namespace_and_dictsort_work() { let src = "{%- set s = namespace(x=[]) -%}\ {%- set s.x = s.x + [1, 2] -%}\ {{ s.x | tojson(separators=(',', ':')) }}|\ {%- for k, v in {'b': 1, 'a': 2} | dictsort(case_sensitive=true) -%}\ {{ k }}={{ v }};{%- endfor -%}"; let t = ChatTemplate::new(src).unwrap(); assert_eq!(t.render(&json!([]), None, None).unwrap(), "[1,2]|a=2;b=1;"); } #[test] fn raise_exception_fails_render() { let t = ChatTemplate::new("{{ raise_exception('boom') }}").unwrap(); let err = t.render(&json!([]), None, None).unwrap_err(); assert!(err.to_string().contains("boom"), "{err}"); } #[test] fn prepare_messages_parses_string_arguments() { let mut msgs = json!([ {"role": "assistant", "tool_calls": [ {"id": "c1", "type": "function", "function": {"name": "f", "arguments": "{\"a\": 1}"}} ]}, {"role": "assistant", "tool_calls": [ {"id": "c2", "function": {"name": "g", "arguments": null}} ]} ]); prepare_messages(&mut msgs); assert_eq!( msgs[0]["tool_calls"][0]["function"]["arguments"], json!({"a": 1}) ); assert_eq!(msgs[1]["tool_calls"][0]["function"]["arguments"], json!({})); } #[test] fn finish_prompt_only_for_effort_zero() { // positive effort: untouched (model opens its own thinking) let p = "<|message_system|><|content_text|>Thinking effort level: 0.2<|end_message|>"; assert_eq!(finish_prompt(p, false, false), p); // effort zero, no opener yet: append the text opener (no tools) let z = "<|message_system|><|content_text|>Thinking effort level: 0<|end_message|>"; let expect = format!("{z}{}{}", tag::MESSAGE_MODEL, tag::CONTENT_TEXT); assert_eq!(finish_prompt(z, false, false), expect); // with tools: the empty-thinking opener let expect_tools = format!( "{z}{}{}{}", tag::MESSAGE_MODEL, tag::CONTENT_THINKING, tag::END_MESSAGE ); assert_eq!(finish_prompt(z, true, false), expect_tools); // idempotent: already ends with the text opener assert_eq!(finish_prompt(&expect, false, false), expect); } /// Render the REAL template (host file). Run with: /// STRATA_TOK_DIR=~/.config/strata-inkling/inkling-tok cargo test -- --ignored #[test] #[ignore = "needs the real Inkling chat_template.jinja"] fn real_template_renders_a_turn() { let dir = std::env::var("STRATA_TOK_DIR") .unwrap_or_else(|_| "/home/federico/.config/strata-inkling/inkling-tok".to_string()); let src = std::fs::read_to_string(format!("{dir}/chat_template.jinja")).unwrap(); let t = ChatTemplate::new(&src).unwrap(); let msgs = json!([{"role": "user", "content": "ciao"}]); let out = t.render(&msgs, None, Some("low")).unwrap(); assert!(out.contains("Thinking effort level: 0.2"), "{out}"); assert!( out.contains("<|message_user|><|content_text|>ciao<|end_message|>"), "{out}" ); // effort none renders level 0 let out0 = t.render(&msgs, None, Some("none")).unwrap(); assert!( out0.contains("Thinking effort level: 0<|end_message|>"), "{out0}" ); } /// Ground truth: the composed context, rendered by the REAL template, must keep /// each section's closing tag followed by a blank line (what the model reads). #[test] #[ignore = "needs the real Inkling chat_template.jinja"] fn real_prompt_closes_sections_with_blank_line() { use strata_ctl::context; use strata_ctl::store::{ContextSection, Session, SessionSettings}; let dir = std::env::var("STRATA_TOK_DIR") .unwrap_or_else(|_| "/home/federico/.config/strata-inkling/inkling-tok".to_string()); let src = std::fs::read_to_string(format!("{dir}/chat_template.jinja")).unwrap(); let t = ChatTemplate::new(&src).unwrap(); let sess = Session { id: "s".into(), title: "Nebula".into(), model: "m".into(), settings: SessionSettings::default(), name_locked: true, folder: None, created_at: 0, updated_at: 0, }; let sections = vec![ ContextSection { name: "SYSTEM_PROMPT".into(), content: "{identity}{speaker_note}\n{session_prompt}".into(), position: 0, }, ContextSection { name: "TOOL_USE".into(), content: "manuale".into(), position: 1, }, ]; let msgs = context::compose(&sess, &[], "ciao", &[], "Federico", §ions); let val = serde_json::to_value(&msgs).unwrap(); let out = t.render(&val, None, Some("low")).unwrap(); assert!(out.contains("\n\n"), "{out}"); assert!(out.contains("\n\n"), "{out}"); assert!(out.contains(""), "{out}"); }