Skip to content

Visual Studio Code, rebuilt in Rust

Rust
3.5 KB
Raw Blame
use gpui::TestAppContext;
use serde_json::{Value, json};

use super::InspectSubject;
use crate::commands::Outcome;
use crate::testing::{ResultState, TestResult};
use crate::workbench::Workbench;
use crate::workbench::testing_gutter::tests::{titles, workbench_with_tests};

const TESTS: &[(&str, &str, u32)] = &[("c\0f", "math", 0), ("c\0f\0a", "adds", 1), ("c\0f\0b", "$(beaker) subtracts", 3)];

/// `adds` passed and `subtracts` failed with a message without a location.
fn add_result(workbench: &mut Workbench) {
    let item = |id: &str| workbench.testing.collection.items[id].item.clone();
    let mut result = TestResult::new("r1".into(), json!({ "group": 2, "targets": [{ "testIds": ["c\0f"] }] }));
    result.add_task(&json!({ "id": "t1", "name": "Task A", "ctrlId": "c", "running": false }));
    for id in ["c\0f\0a", "c\0f\0b"] {
        result.add_chain(&[item("c"), item("c\0f"), item(id)]);
    }
    result.update_state("c\0f\0a", "t1", ResultState::Passed, Some(1.));
    result.update_state("c\0f\0b", "t1", ResultState::Failed, Some(1.));
    result.append_message("c\0f\0b", "t1", json!({ "type": 1, "message": "log line", "location": null }));
    result.append_message("c\0f\0b", "t1", json!({ "type": 0, "message": "expected 1\nbut got 2", "location": null }));
    result.mark_complete(2);
    workbench.testing.results = vec![result];
}

fn peek(workbench: &gpui::Entity<Workbench>, id: &str, cx: &mut gpui::VisualTestContext) -> Value {
    let outcome = workbench.update_in(cx, |workbench, window, cx| workbench.execute_command("vscode.peekTestError", vec![json!(id)], window, cx));
    let Outcome::Ready(Ok(peeked)) = outcome else { panic!("a ready value") };
    peeked
}

/// testingDecorations.ts `testing.gutter.peekFailure` on a failed test only; `vscode.peekTestError` on the parent
/// peeks the first failed child's error message at the test's range, titled by the message's first line and the
/// test's label; Escape's `editor.closeTestPeek` closes it.
#[gpui::test]
fn peek_error_opens_the_failed_childs_message_at_the_test(cx: &mut TestAppContext) {
    let (workbench, cx, _host) = workbench_with_tests("testing-peek", TESTS, cx);
    let (failed, passed) = workbench.update_in(cx, |workbench, window, _| {
        add_result(workbench);
        (titles(&workbench.test_gutter_actions(3, window)), titles(&workbench.test_gutter_actions(1, window)))
    });
    assert!(failed.contains(&"Peek Error".to_owned()), "{failed:?}");
    assert!(!passed.contains(&"Peek Error".to_owned()), "{passed:?}");

    assert_eq!(peek(&workbench, "c\0f\0a", cx), json!(false));
    assert_eq!(peek(&workbench, "c\0f", cx), json!(true));
    cx.run_until_parked();
    workbench.update_in(cx, |workbench, window, _| {
        let peek = workbench.testing_peek.as_ref().expect("a peek");
        assert_eq!(peek.subject, InspectSubject::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 1 });
        let (label, description, _) = workbench.testing_peek_message().unwrap();
        assert_eq!((label.as_str(), description.as_str()), ("expected 1", "subtracts"));
        assert_eq!(workbench.editors.active().unwrap().selection().head.line, 2);
        assert_eq!(workbench.key_context(window).get("testing.isPeekVisible"), Some(&json!(true)));
    });

    workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("editor.closeTestPeek", Vec::new(), window, cx)));
    cx.run_until_parked();
    assert!(workbench.read_with(cx, |workbench, _| workbench.testing_peek.is_none()));
}