Repository: GraphiteEditor/Graphite
File: editor/src/messages/tool/tool_messages/spline_tool.rs (Rust)

Code before:

		// Initiate drawing a spline
		editor.left_mousedown(50.0, 50.0, ModifierKeys::empty()).await;
		editor
			.mouseup(
				EditorMouseState {
					editor_position: DVec2::new(50.0, 50.0),
					mouse_keys: MouseKeys::empty(),
					scroll_delta: ScrollDelta::default(),
				},
				ModifierKeys::empty(),
			)
			.await;

		// Adding the second point
		editor.left_mousedown(100.0, 50.0, ModifierKeys::empty()).await;
		editor
			.mouseup(
				EditorMouseState {
					editor_position: DVec2::new(100.0, 50.0),
					mouse_keys: MouseKeys::empty(),
					scroll_delta: ScrollDelta::default(),
				},
				ModifierKeys::empty(),
			)
			.await;

		// Adding the third point
		editor.left_mousedown(150.0, 100.0, ModifierKeys::empty()).await;
		editor
			.mouseup(
				EditorMouseState {
					editor_position: DVec2::new(150.0, 100.0),
					mouse_keys: MouseKeys::empty(),
					scroll_delta: ScrollDelta::default(),
				},
				ModifierKeys::empty(),
			)
			.await;

		// Finish the spline
		editor.handle_message(SplineToolMessage::Confirm).await;

		// Evaluate the graph to ensure everything is processed
		editor.eval_graph().await;

		if let Some(points) = get_spline_points(&mut editor).await {
			assert!(points.len() >= 3, "Spline should have at least 3 points");

			// Calculating expected points in document coordinates
			let document = editor.active_document();

Review comment (refactor):
Consider using `editor.click_tool(…)` to simplify the logic. You are also free to write your own helper functions e.g. on `EditorTestUtils`.

Code after:
				i,
				expected_point,
				actual_point,
				distance
			);
		}
	}

	#[tokio::test]
	async fn test_continue_drawing_from_existing_spline() {
		let mut editor = EditorTestUtils::create();
		editor.new_document().await;

		let initial_points = [DVec2::new(100., 100.), DVec2::new(200., 150.), DVec2::new(300., 100.)];

		editor.select_tool(ToolType::Spline).await;

		for &point in &initial_points {
			editor.click_tool(ToolType::Spline, MouseKeys::LEFT, point, ModifierKeys::empty()).await;
		}

		editor.press(Key::Enter, ModifierKeys::empty()).await;

		let document = editor.active_document();
		let spline_layer = document
			.metadata()
			.all_layers()
			.find(|layer| find_spline(document, *layer).is_some())
			.expect("Failed to find a layer with a spline node");

		let first_spline_node = find_spline(document, spline_layer).expect("Spline node not found in the layer");

		let first_vector_data = document.network_interface.compute_modified_vector(spline_layer).expect("Vector data not found for the spline layer");

		// Verify initial spline has correct number of points and segments
		let initial_point_count = first_vector_data.point_domain.ids().len();
		let initial_segment_count = first_vector_data.segment_domain.ids().len();
		assert_eq!(initial_point_count, 3, "Expected 3 points in initial spline, found {}", initial_point_count);
		assert_eq!(initial_segment_count, 2, "Expected 2 segments in initial spline, found {}", initial_segment_count);

		let layer_to_viewport = document.metadata().transform_to_viewport(spline_layer);

		let endpoints: Vec<(PointId, DVec2)> = first_vector_data
			.extendable_points(false)
			.filter_map(|point_id| first_vector_data.point_domain.position_from_id(point_id).map(|pos| (point_id, layer_to_viewport.transform_point2(pos))))
			.collect();

		assert_eq!(endpoints.len(), 2, "Expected 2 endpoints in the initial spline");

		let (_, endpoint_position) = endpoints.first().expect("No endpoints found in spline");