Skip to content

Commit 259c172

Browse files
committed
more tests
Signed-off-by: Nick Mitchell <[email protected]>
1 parent ebd6511 commit 259c172

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

pdl-live-react/src-tauri/src/pdl/interpreter.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ pub fn run_file(source_file_path: &str, debug: bool) -> Interpretation {
327327
run(&from_reader(File::open(source_file_path)?)?, debug)
328328
}
329329

330+
pub fn run_string(source: &str, debug: bool) -> Interpretation {
331+
run(&from_str(source)?, debug)
332+
}
333+
334+
pub fn run_json(source: Value, debug: bool) -> Interpretation {
335+
run_string(&to_string(&source)?, debug)
336+
}
337+
330338
fn pretty_print(messages: &Vec<ChatMessage>) {
331339
messages
332340
.into_iter()

pdl-live-react/src-tauri/src/pdl/interpreter_tests.rs

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
mod tests {
33
// use super::*;
44
use ::std::error::Error;
5+
use serde_json::json;
56

67
use crate::pdl::{
78
ast::{PdlBlock, PdlModelBlock, PdlParser, PdlTextBlock},
8-
interpreter::run,
9+
interpreter::{run, run_json},
910
};
1011

1112
use ollama_rs::generation::chat::MessageRole;
@@ -35,42 +36,67 @@ mod tests {
3536

3637
#[test]
3738
fn single_model_via_text_chain() -> Result<(), Box<dyn Error>> {
38-
let (messages, _) = run(
39-
&PdlBlock::Text(
40-
vec![
41-
"hello".into(),
42-
PdlBlock::Model(PdlModelBlock::new(DEFAULT_MODEL).build()),
39+
let (messages, _) = run_json(
40+
json!({
41+
"text": [
42+
"hello",
43+
{ "model": DEFAULT_MODEL }
4344
]
44-
.into(),
45-
),
45+
}),
4646
false,
4747
)?;
4848
assert_eq!(messages.len(), 2);
4949
assert_eq!(messages[0].role, MessageRole::User);
50-
assert!(messages[0].content.contains("hello"));
50+
assert_eq!(messages[0].content, "hello");
5151
assert_eq!(messages[1].role, MessageRole::Assistant);
5252
assert!(messages[1].content.contains("Hello!"));
5353
Ok(())
5454
}
5555

56+
#[test]
57+
fn two_models_via_text_chain() -> Result<(), Box<dyn Error>> {
58+
let (messages, _) = run_json(
59+
json!({
60+
"text": [
61+
"what is the fastest animal?",
62+
{ "model": DEFAULT_MODEL },
63+
"in europe?",
64+
{ "model": DEFAULT_MODEL },
65+
]
66+
}),
67+
false,
68+
)?;
69+
assert_eq!(messages.len(), 4);
70+
assert_eq!(messages[0].role, MessageRole::User);
71+
assert_eq!(messages[0].content, "what is the fastest animal?");
72+
assert_eq!(messages[1].role, MessageRole::Assistant);
73+
assert!(messages[1].content.contains("cheetah"));
74+
assert_eq!(messages[2].role, MessageRole::User);
75+
assert_eq!(messages[2].content, "in europe?");
76+
assert_eq!(messages[3].role, MessageRole::Assistant);
77+
assert!(
78+
messages[3].content.contains("peregrine")
79+
|| messages[3].content.contains("bison")
80+
|| messages[3].content.contains("hare")
81+
|| messages[3].content.contains("Eagle")
82+
|| messages[3].content.contains("Greyhound")
83+
|| messages[3].content.contains("gazelle")
84+
|| messages[3].content.contains("lynx")
85+
);
86+
Ok(())
87+
}
88+
5689
#[test]
5790
fn text_parser_json() -> Result<(), Box<dyn Error>> {
5891
let json = "{\"key\": \"value\"}";
59-
let program = PdlBlock::Text(
60-
vec![
61-
PdlBlock::Text(
62-
PdlTextBlock::new(vec![json.into()])
63-
.def(&"foo")
64-
.parser(PdlParser::Json)
65-
.build(),
66-
),
67-
"${ foo.key }".into(),
92+
let program = json!({
93+
"text": [
94+
{ "def": "foo", "parser": "json", "text": [json] },
95+
"${ foo.key }"
6896
]
69-
.into(),
70-
);
71-
println!("{}", serde_json::to_string(&program)?);
97+
});
7298

73-
let (messages, _) = run(&program, false)?;
99+
let (messages, _) = run_json(program, false)?;
74100
assert_eq!(messages.len(), 2);
75101
assert_eq!(messages[0].role, MessageRole::User);
76102
assert_eq!(messages[0].content, json);

0 commit comments

Comments
 (0)