More aggressive using of cache on engine settings changes (#4691)
* move around the files for cache to better localtions Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * udpates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * cleanup Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * ensure we can change the grid setting via the command bar Signed-off-by: Jess Frazelle <github@jessfraz.com> * pass thru all setttings Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * updates Signed-off-by: Jess Frazelle <github@jessfraz.com> * fix playwright test Signed-off-by: Jess Frazelle <github@jessfraz.com> * A snapshot a day keeps the bugs away! 📷🐛 (OS: ubuntu-latest-8-cores) * A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest-8-cores) * emoty --------- Signed-off-by: Jess Frazelle <github@jessfraz.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
216
src/wasm-lib/tests/executor/cache.rs
Normal file
@ -0,0 +1,216 @@
|
||||
//! Cache testing framework.
|
||||
|
||||
use anyhow::Result;
|
||||
use kcl_lib::ExecError;
|
||||
|
||||
struct Variation<'a> {
|
||||
code: &'a str,
|
||||
settings: &'a kcl_lib::ExecutorSettings,
|
||||
}
|
||||
|
||||
async fn cache_test(test_name: &str, variations: Vec<Variation<'_>>) -> Result<Vec<(String, image::DynamicImage)>> {
|
||||
let first = variations
|
||||
.first()
|
||||
.ok_or_else(|| anyhow::anyhow!("No variations provided for test '{}'", test_name))?;
|
||||
|
||||
let mut ctx = kcl_lib::ExecutorContext::new_with_client(first.settings.clone(), None, None).await?;
|
||||
let mut exec_state = kcl_lib::ExecState::default();
|
||||
|
||||
let mut old_ast_state = None;
|
||||
let mut img_results = Vec::new();
|
||||
for (index, variation) in variations.iter().enumerate() {
|
||||
let program = kcl_lib::Program::parse_no_errs(variation.code)?;
|
||||
|
||||
// set the new settings.
|
||||
ctx.settings = variation.settings.clone();
|
||||
|
||||
ctx.run(
|
||||
kcl_lib::CacheInformation {
|
||||
old: old_ast_state,
|
||||
new_ast: program.ast.clone(),
|
||||
},
|
||||
&mut exec_state,
|
||||
)
|
||||
.await?;
|
||||
let snapshot_png_bytes = ctx.prepare_snapshot().await?.contents.0;
|
||||
|
||||
// Decode the snapshot, return it.
|
||||
let img = image::ImageReader::new(std::io::Cursor::new(snapshot_png_bytes))
|
||||
.with_guessed_format()
|
||||
.map_err(|e| ExecError::BadPng(e.to_string()))
|
||||
.and_then(|x| x.decode().map_err(|e| ExecError::BadPng(e.to_string())))?;
|
||||
// Save the snapshot.
|
||||
let path = crate::assert_out(&format!("cache_{}_{}", test_name, index), &img);
|
||||
|
||||
img_results.push((path, img));
|
||||
|
||||
// Prepare the last state.
|
||||
old_ast_state = Some(kcl_lib::OldAstState {
|
||||
ast: program.ast,
|
||||
exec_state: exec_state.clone(),
|
||||
settings: variation.settings.clone(),
|
||||
});
|
||||
}
|
||||
|
||||
Ok(img_results)
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn kcl_test_cache_change_units_changes_output() {
|
||||
let code = r#"part001 = startSketchOn('XY')
|
||||
|> startProfileAt([5.5229, 5.25217], %)
|
||||
|> line([10.50433, -1.19122], %)
|
||||
|> line([8.01362, -5.48731], %)
|
||||
|> line([-1.02877, -6.76825], %)
|
||||
|> line([-11.53311, 2.81559], %)
|
||||
|> close(%)
|
||||
|> extrude(4, %)
|
||||
"#;
|
||||
|
||||
let result = cache_test(
|
||||
"change_units_changes_output",
|
||||
vec![
|
||||
Variation {
|
||||
code,
|
||||
settings: &kcl_lib::ExecutorSettings {
|
||||
units: kcl_lib::UnitLength::In,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
Variation {
|
||||
code,
|
||||
settings: &kcl_lib::ExecutorSettings {
|
||||
units: kcl_lib::UnitLength::Mm,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let first = result.first().unwrap();
|
||||
let second = result.last().unwrap();
|
||||
|
||||
assert!(first.1 != second.1);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn kcl_test_cache_change_grid_visualizes_grid_off_to_on() {
|
||||
let code = r#"part001 = startSketchOn('XY')
|
||||
|> startProfileAt([5.5229, 5.25217], %)
|
||||
|> line([10.50433, -1.19122], %)
|
||||
|> line([8.01362, -5.48731], %)
|
||||
|> line([-1.02877, -6.76825], %)
|
||||
|> line([-11.53311, 2.81559], %)
|
||||
|> close(%)
|
||||
|> extrude(4, %)
|
||||
"#;
|
||||
|
||||
let result = cache_test(
|
||||
"change_grid_visualizes_grid_off_to_on",
|
||||
vec![
|
||||
Variation {
|
||||
code,
|
||||
settings: &kcl_lib::ExecutorSettings {
|
||||
show_grid: false,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
Variation {
|
||||
code,
|
||||
settings: &kcl_lib::ExecutorSettings {
|
||||
show_grid: true,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let first = result.first().unwrap();
|
||||
let second = result.last().unwrap();
|
||||
|
||||
assert!(first.1 != second.1);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn kcl_test_cache_change_grid_visualizes_grid_on_to_off() {
|
||||
let code = r#"part001 = startSketchOn('XY')
|
||||
|> startProfileAt([5.5229, 5.25217], %)
|
||||
|> line([10.50433, -1.19122], %)
|
||||
|> line([8.01362, -5.48731], %)
|
||||
|> line([-1.02877, -6.76825], %)
|
||||
|> line([-11.53311, 2.81559], %)
|
||||
|> close(%)
|
||||
|> extrude(4, %)
|
||||
"#;
|
||||
|
||||
let result = cache_test(
|
||||
"change_grid_visualizes_grid_on_to_off",
|
||||
vec![
|
||||
Variation {
|
||||
code,
|
||||
settings: &kcl_lib::ExecutorSettings {
|
||||
show_grid: true,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
Variation {
|
||||
code,
|
||||
settings: &kcl_lib::ExecutorSettings {
|
||||
show_grid: false,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let first = result.first().unwrap();
|
||||
let second = result.last().unwrap();
|
||||
|
||||
assert!(first.1 != second.1);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn kcl_test_cache_change_highlight_edges_changes_visual() {
|
||||
let code = r#"part001 = startSketchOn('XY')
|
||||
|> startProfileAt([5.5229, 5.25217], %)
|
||||
|> line([10.50433, -1.19122], %)
|
||||
|> line([8.01362, -5.48731], %)
|
||||
|> line([-1.02877, -6.76825], %)
|
||||
|> line([-11.53311, 2.81559], %)
|
||||
|> close(%)
|
||||
|> extrude(4, %)
|
||||
"#;
|
||||
|
||||
let result = cache_test(
|
||||
"change_highlight_edges_changes_visual",
|
||||
vec![
|
||||
Variation {
|
||||
code,
|
||||
settings: &kcl_lib::ExecutorSettings {
|
||||
highlight_edges: true,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
Variation {
|
||||
code,
|
||||
settings: &kcl_lib::ExecutorSettings {
|
||||
highlight_edges: false,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
],
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let first = result.first().unwrap();
|
||||
let second = result.last().unwrap();
|
||||
|
||||
assert!(first.1 != second.1);
|
||||
}
|
@ -1,3 +1,5 @@
|
||||
mod cache;
|
||||
|
||||
use kcl_lib::{
|
||||
test_server::{execute_and_snapshot, execute_and_snapshot_no_auth},
|
||||
UnitLength,
|
||||
@ -5,7 +7,7 @@ use kcl_lib::{
|
||||
|
||||
/// The minimum permissible difference between asserted twenty-twenty images.
|
||||
/// i.e. how different the current model snapshot can be from the previous saved one.
|
||||
const MIN_DIFF: f64 = 0.99;
|
||||
pub(crate) const MIN_DIFF: f64 = 0.99;
|
||||
|
||||
macro_rules! kcl_input {
|
||||
($file:literal) => {
|
||||
@ -13,8 +15,11 @@ macro_rules! kcl_input {
|
||||
};
|
||||
}
|
||||
|
||||
fn assert_out(test_name: &str, result: &image::DynamicImage) {
|
||||
twenty_twenty::assert_image(format!("tests/executor/outputs/{test_name}.png"), result, MIN_DIFF);
|
||||
pub(crate) fn assert_out(test_name: &str, result: &image::DynamicImage) -> String {
|
||||
let path = format!("tests/executor/outputs/{test_name}.png");
|
||||
twenty_twenty::assert_image(&path, result, MIN_DIFF);
|
||||
|
||||
path
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
|
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 114 KiB |
After Width: | Height: | Size: 114 KiB |
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 47 KiB |
After Width: | Height: | Size: 35 KiB |
After Width: | Height: | Size: 53 KiB |
After Width: | Height: | Size: 47 KiB |