Visual Studio Code, rebuilt in Rust
Rust
use gpui::TestAppContext;
use serde_json::{Value, json};
use super::{InspectSubject, ResultsElement};
use crate::menus::{Entry, MenuId, MenuItem};
use crate::testing::{ResultState, TestResult};
use crate::workbench::Workbench;
use crate::workbench::testing_gutter::tests::{contributed, titles, workbench_with_tests};
const TESTS: &[(&str, &str, u32)] = &[("c\0f", "math", 0), ("c\0f\0a", "adds", 1), ("c\0f\0b", "subtracts", 2), ("c\0f\0c", "throws", 3)];
fn error(text: &str) -> Value {
json!({ "type": 0, "message": text, "expected": null, "actual": null, "contextValue": "diff", "location": null })
}
/// A result of one task in which `adds` passed, `subtracts` failed with one message and `throws` with two, and an
/// older result in which `adds` passed.
fn add_results(workbench: &mut Workbench) {
let chain = |workbench: &Workbench, id: &str| -> Vec<Value> {
let file = workbench.testing.collection.items["c\0f"].item.clone();
vec![workbench.testing.collection.items["c"].item.clone(), file, workbench.testing.collection.items[id].item.clone()]
};
let mut older = TestResult::new("r0".into(), json!({ "group": 2, "targets": [{ "testIds": ["c\0f\0a"] }] }));
older.add_task(&json!({ "id": "t0", "name": "Old Task", "ctrlId": "c", "running": false }));
older.add_chain(&chain(workbench, "c\0f\0a"));
older.update_state("c\0f\0a", "t0", ResultState::Passed, Some(1.));
older.mark_complete(1);
let mut newest = TestResult::new("r1".into(), json!({ "group": 2, "targets": [{ "testIds": ["c\0f"] }] }));
newest.add_task(&json!({ "id": "t1", "name": "Task A", "ctrlId": "c", "running": false }));
for id in ["c\0f\0a", "c\0f\0b", "c\0f\0c"] {
newest.add_chain(&chain(workbench, id));
}
newest.update_state("c\0f\0a", "t1", ResultState::Passed, Some(1.));
newest.update_state("c\0f\0b", "t1", ResultState::Failed, Some(1.));
newest.append_message("c\0f\0b", "t1", error("expected 1\nbut got 2"));
newest.update_state("c\0f\0c", "t1", ResultState::Failed, Some(1.));
newest.append_message("c\0f\0c", "t1", error("first"));
newest.append_message("c\0f\0c", "t1", error("second"));
newest.mark_complete(2);
workbench.testing.results = vec![newest, older];
}
/// testResultsOutput.ts `PlainTextMessagePeek`: an opened plain text message shows its `messageUri` model in a
/// read-only code editor without line numbers, in a group outside the grid; another message replaces it.
#[gpui::test]
fn an_opened_plain_text_message_shows_in_a_read_only_code_editor(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-plain-text", TESTS, cx);
workbench.update_in(cx, |workbench, _, _| {
add_results(workbench);
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 0 });
let editor = workbench.message_editor().expect("the message's code editor");
assert_eq!((editor.document().uri_string(), editor.document().lines.clone()), ("vscode-test-data://results/r1/message/0/0/TestFailureMessage?c%00f%00b".to_owned(), vec!["expected 1".to_owned(), "but got 2".to_owned()]));
assert_eq!(workbench.line_numbers(editor), crate::workbench::editor_view::LineNumbers::Off);
assert!(editor.highlights.is_empty(), "no word highlight before a cursor move");
assert!(workbench.readonly(crate::testing::TEST_DATA_SCHEME, &editor.document().path).is_some());
assert!(!workbench.editors.group_ids_mru().contains(&crate::editor::TEST_RESULTS_GROUP));
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0c".into(), message: 1 });
let (editors, _) = workbench.editors.group_tabs(crate::editor::TEST_RESULTS_GROUP);
assert_eq!(editors.iter().map(|editor| editor.document().lines.clone()).collect::<Vec<_>>(), vec![vec!["second".to_owned()]]);
});
}
/// testResultsViewContent.ts `fillBody`: the content and the tree split the view 2:1, by their split view minimums.
#[gpui::test]
fn the_content_and_the_tree_split_the_view_two_to_one(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-split", TESTS, cx);
workbench.update_in(cx, |workbench, _, cx| {
add_results(workbench);
workbench.open_panel_view(crate::workbench::testing_view::TEST_RESULTS_VIEW_ID, cx);
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 0 });
});
cx.run_until_parked();
let (content, tree) = (cx.debug_bounds("test-results-content").expect("drawn"), cx.debug_bounds("test-results-tree").expect("drawn"));
assert!(tree.size.width > gpui::px(100.));
// The tree's 1px left border is its flex basis, outside the 2:1 split of the rest.
assert!((content.size.width - (tree.size.width - gpui::px(1.)) * 2.).abs() <= gpui::px(1.), "{content:?} {tree:?}");
}
/// testMessageColorizer.ts `colorizeTestMessageInEditor`: a plain text message's ANSI codes leave its text and
/// color it through decorations, in the terminal's ANSI colors.
#[gpui::test]
fn an_opened_plain_text_message_shows_its_ansi_styles(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-ansi", TESTS, cx);
workbench.update_in(cx, |workbench, _, _| {
add_results(workbench);
workbench.testing.results[0].append_message("c\0f\0b", "t1", error("got \x1b[31mred\x1b[4m line\x1b[0m end"));
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 1 });
let editor = workbench.message_editor().expect("the message's code editor");
assert_eq!(editor.document().lines, vec!["got red line end".to_owned()]);
let styles: Vec<_> = workbench.text_decoration_styles(editor, 0, 4).into_iter().map(|(range, style)| (range, style.color, style.underline.is_some())).collect();
let red = workbench.theme.color("terminal.ansiRed").map(Into::into);
assert_eq!(styles, vec![(4..7, red, false), (7..12, red, true)]);
});
}
/// testResultsOutput.ts `TerminalMessagePeek`: a task shows the run's output in a detached terminal, a test its own
/// output, appended as it comes, and a test without output the notice once the run completed.
#[gpui::test]
fn task_and_test_output_show_in_a_detached_terminal(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-terminal", TESTS, cx);
workbench.update_in(cx, |workbench, _, _| {
add_results(workbench);
workbench.testing.results[0].append_output("t1", "run start\r\n", None, None);
workbench.testing.results[0].append_output("t1", "adds out\r\n", None, Some("c\0f\0a"));
let shown = |workbench: &mut Workbench, subject: InspectSubject| {
workbench.testing_view.results_subject = Some(subject);
let output = workbench.terminal_output().expect("a terminal subject");
let id = workbench.write_results_terminal(output);
let instance = workbench.terminal_view.instance_mut(id).expect("the detached terminal");
instance.select_all();
let text = instance.selection_text().unwrap_or_default();
assert!(workbench.terminal_view.instances.is_empty(), "no terminal in the terminal list");
text.trim_end().to_owned()
};
assert_eq!(shown(workbench, InspectSubject::Task { result: "r1".into(), task: 0 }), "run start\nadds out");
workbench.testing.results[0].append_output("t1", "more\r\n", None, None);
assert_eq!(shown(workbench, InspectSubject::Task { result: "r1".into(), task: 0 }), "run start\nadds out\nmore");
assert_eq!(shown(workbench, InspectSubject::Message { result: "r1".into(), task: 0, test: "c\0f\0a".into(), message: 0 }), "adds out");
assert_eq!(shown(workbench, InspectSubject::Output { result: "r1".into(), task: 0, test: "c\0f\0b".into() }), "\n * The test case did not report any output.");
let id = workbench.testing_view.results_terminal.as_ref().expect("the results terminal").0;
let (normal_weight, bold_weight) = (gpui::FontWeight::NORMAL, gpui::FontWeight::BOLD);
workbench.terminal_view.metrics = Some(super::super::terminal_view::grid::CellMetrics { family: "Menlo".into(), font_size: 12., bold_weight, normal_weight, line_height: 1., letter_spacing: 0., cell_width: 7., dom_cell_width: 7.2, cell_height: 16., descent: 3.3, scale: 2. });
assert_eq!(workbench.terminal_view.pane_metrics(id).map(|metrics| metrics.cell_width), Some(7.2), "`enableGpu: false` lays out on the DOM cell");
});
}
/// testResultsOutput.ts `MarkdownTestMessagePeek`: an opened markdown message renders its markdown, without an editor.
#[gpui::test]
fn an_opened_markdown_message_renders_its_markdown(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-markdown", TESTS, cx);
workbench.update_in(cx, |workbench, _, _| {
add_results(workbench);
workbench.testing.results[0].append_message("c\0f\0b", "t1", json!({ "type": 0, "message": { "value": "The **sum**", "isTrusted": false }, "expected": null, "actual": null, "contextValue": null, "location": null }));
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 1 });
assert_eq!(workbench.markdown_message().map(|markdown| markdown.value), Some("The **sum**"));
assert!(workbench.message_editor().is_none());
let label = workbench.results_label(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 1 }).0;
assert_eq!(label, "The sum", "the row shows the rendered text");
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 0 });
assert!(workbench.markdown_message().is_none());
});
}
/// testResultsOutput.ts `MarkdownTestMessagePeek` sets `userSelect: text`: a drag across a markdown message selects
/// its text, a double click a word, and Copy copies the selection with Chrome's blank line between paragraphs.
#[gpui::test]
fn a_markdown_messages_text_can_be_selected_and_copied(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-markdown-select", TESTS, cx);
workbench.update_in(cx, |workbench, _, cx| {
add_results(workbench);
workbench.testing.results[0].append_message("c\0f\0b", "t1", json!({ "type": 0, "message": { "value": "The **sum** failed\n\nsecond", "isTrusted": false }, "expected": null, "actual": null, "contextValue": null, "location": null }));
workbench.open_panel_view(crate::workbench::testing_view::TEST_RESULTS_VIEW_ID, cx);
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 1 });
});
cx.run_until_parked();
let text = cx.debug_bounds("test-results-markdown").expect("drawn");
// testing.css `.preview-text` pads the text 20px from the left and 8px from the top; its rows are 15px.
let first_row = text.origin + gpui::point(gpui::px(21.), gpui::px(15.));
cx.simulate_mouse_down(first_row, gpui::MouseButton::Left, gpui::Modifiers::none());
cx.simulate_mouse_move(text.origin + gpui::point(gpui::px(400.), gpui::px(45.)), gpui::MouseButton::Left, gpui::Modifiers::none());
cx.simulate_mouse_up(text.origin + gpui::point(gpui::px(400.), gpui::px(45.)), gpui::MouseButton::Left, gpui::Modifiers::none());
workbench.read_with(cx, |workbench, _| assert_eq!(workbench.text_selection.to_text(), "The sum failed\n\nsecond"));
workbench.update_in(cx, |workbench, window, cx| _ = workbench.execute_command("editor.action.clipboardCopyAction", Vec::new(), window, cx));
assert_eq!(cx.read_from_clipboard().and_then(|item| item.text()).as_deref(), Some("The sum failed\n\nsecond"));
cx.simulate_event(gpui::MouseDownEvent { button: gpui::MouseButton::Left, position: first_row, modifiers: gpui::Modifiers::none(), click_count: 2, first_mouse: false });
cx.simulate_mouse_up(first_row, gpui::MouseButton::Left, gpui::Modifiers::none());
workbench.read_with(cx, |workbench, _| assert_eq!(workbench.text_selection.to_text(), "The"));
}
/// testResultsViewContent.ts `setCallStackFrames` and callStackWidget.ts: a message's stack trace lists after it, a
/// frame with a source as a read-only editor of its line and 2 around it, the line highlighted and its column marked,
/// one without as its label and location; another message drops the frames' editors.
#[gpui::test]
fn an_opened_message_with_a_stack_trace_lists_its_frames(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-stack", TESTS, cx);
let source = std::env::temp_dir().join(format!("code-gpui-test-results-stack-{}", std::process::id())).join("stack.test.js");
std::fs::create_dir_all(source.parent().unwrap()).unwrap();
std::fs::write(&source, (1..=12).map(|line| format!("line {line}\n")).collect::<String>()).unwrap();
workbench.update_in(cx, |workbench, _, _| {
add_results(workbench);
let frames = json!([
{ "label": "sum", "uri": { "scheme": "file", "path": source.to_string_lossy() }, "position": { "lineNumber": 7, "column": 3 } },
{ "label": "processTicksAndRejections", "uri": null, "position": { "lineNumber": 96, "column": 6 } },
]);
workbench.testing.results[0].append_message("c\0f\0b", "t1", json!({ "type": 0, "message": "sum failed", "expected": null, "actual": null, "contextValue": null, "location": null, "stackTrace": frames }));
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 1 });
let rows: Vec<_> = workbench.results_call_stack_rows().into_iter().map(|row| (row.icon, row.label, row.suffix, row.description)).collect();
let folder = crate::quick_input::path_label(source.parent().unwrap(), None);
let expected = [(Some("beaker"), "subtracts", None, None), (None, "stack.test.js", None, Some(folder.as_str())), (Some("file-binary"), "processTicksAndRejections", Some(":96"), Some("Line 96 column 6"))];
assert_eq!(rows, expected.map(|(icon, label, suffix, description)| (icon, label.to_owned(), suffix.map(str::to_owned), description.map(str::to_owned))));
let frame = workbench.results_call_stack_rows()[1].editor.expect("the frame's code editor");
let editor = workbench.editors.editor_by_id(frame).expect("open");
let shown: Vec<_> = (0..editor.view_line_count()).map(|view_line| editor.model_line(view_line) + 1).collect();
assert_eq!(shown, vec![5, 6, 7, 8, 9]);
assert_eq!(workbench.line_numbers(editor), crate::workbench::editor_view::LineNumbers::On);
assert!(workbench.stack_frame_line(editor, 6).is_some() && workbench.stack_frame_line(editor, 5).is_none(), "the frame's line is highlighted");
workbench.sync_call_stack_frame_columns(7.2);
let editor = workbench.editors.editor_by_id(frame).expect("open");
let columns: Vec<_> = editor.injected_text.iter().map(|injection| (injection.position, injection.content.as_str())).collect();
assert_eq!(columns, vec![(crate::editor::Position::new(6, 2), "\u{EB8B}")]);
let readonly = workbench.with_group(crate::editor::TEST_RESULTS_GROUP, |workbench| workbench.with_code_editor(frame, |workbench| workbench.active_editor_readonly()));
assert_eq!(readonly, Some(crate::workbench::readonly::Readonly::Editor));
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 0 });
assert!(workbench.results_call_stack_rows().is_empty());
assert!(workbench.editors.editor_by_id(frame).is_none(), "the frame's editor went with its message");
});
}
/// callStackWidget.ts renders each frame in its own list row: a press that focuses a frame's code editor leaves the
/// message's row showing the message.
#[gpui::test]
fn a_press_in_a_frame_leaves_the_message_shown(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-stack-press", TESTS, cx);
let source = std::env::temp_dir().join(format!("code-gpui-test-results-stack-press-{}", std::process::id())).join("stack.test.js");
std::fs::create_dir_all(source.parent().unwrap()).unwrap();
std::fs::write(&source, (1..=12).map(|line| format!("line {line}\n")).collect::<String>()).unwrap();
let message = workbench.update_in(cx, |workbench, _, cx| {
add_results(workbench);
let frames = json!([{ "label": "sum", "uri": { "scheme": "file", "path": source.to_string_lossy() }, "position": { "lineNumber": 7, "column": 3 } }]);
workbench.testing.results[0].append_message("c\0f\0b", "t1", json!({ "type": 0, "message": "sum failed", "expected": null, "actual": null, "contextValue": null, "location": null, "stackTrace": frames }));
workbench.open_panel_view(crate::workbench::testing_view::TEST_RESULTS_VIEW_ID, cx);
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 1 });
workbench.message_editor().expect("the message's editor").model_id
});
cx.run_until_parked();
let frame = cx.debug_bounds("test-results-frame-1").expect("the frame's code editor is drawn");
cx.simulate_click(frame.center(), gpui::Modifiers::default());
cx.run_until_parked();
assert!(cx.debug_bounds(format!("text-area-{message}").leak()).is_some(), "the message's text area is drawn");
std::fs::remove_dir_all(source.parent().unwrap()).unwrap();
}
/// callStackWidget.ts `ClickToLocationContribution`: in a frame's code editor, the word under the mouse with Cmd held
/// is a link without a definition provider, and Cmd+click opens the frame's file at the word's start.
#[gpui::test]
fn cmd_click_on_a_word_in_a_frame_opens_its_file_there(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-stack-click", TESTS, cx);
let source = std::env::temp_dir().join(format!("code-gpui-test-results-stack-click-{}", std::process::id())).join("stack.test.js");
std::fs::create_dir_all(source.parent().unwrap()).unwrap();
std::fs::write(&source, (1..=12).map(|line| format!("line {line}\n")).collect::<String>()).unwrap();
let cmd = gpui::Modifiers { platform: true, ..Default::default() };
workbench.update_in(cx, |workbench, _, cx| {
add_results(workbench);
let frames = json!([{ "label": "sum", "uri": { "scheme": "file", "path": source.to_string_lossy() }, "position": { "lineNumber": 7, "column": 3 } }]);
workbench.testing.results[0].append_message("c\0f\0b", "t1", json!({ "type": 0, "message": "sum failed", "expected": null, "actual": null, "contextValue": null, "location": null, "stackTrace": frames }));
workbench.open_panel_view(crate::workbench::testing_view::TEST_RESULTS_VIEW_ID, cx);
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 1 });
});
cx.run_until_parked();
// The frame shows lines 5 to 9; "line 7", the third, starts past the 5-column line numbers.
let frame = cx.debug_bounds("test-results-frame-1").expect("the frame's code editor is drawn");
let word = frame.origin + gpui::point(gpui::px(80.), gpui::px(crate::editor::LINE_HEIGHT * 2.5));
cx.simulate_mouse_move(word, None, cmd);
cx.simulate_mouse_down(word, gpui::MouseButton::Left, cmd);
cx.simulate_mouse_up(word, gpui::MouseButton::Left, cmd);
cx.run_until_parked();
workbench.read_with(cx, |workbench, _| {
assert_ne!(workbench.editors.id, crate::editor::TEST_RESULTS_GROUP);
let editor = workbench.editors.active().expect("the frame's file");
assert_eq!(editor.document().path, source);
assert_eq!(editor.selection().head.line, 6);
});
std::fs::remove_dir_all(source.parent().unwrap()).unwrap();
}
/// testResultsOutput.ts `DiffContentProvider`: an opened diffable message shows its expected and actual models side by
/// side in an embedded diff editor, with line numbers when a side is multiline; a plain text message replaces it.
#[gpui::test]
fn an_opened_diffable_message_shows_expected_and_actual_in_a_diff_editor(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-diff", TESTS, cx);
workbench.update_in(cx, |workbench, window, _| {
add_results(workbench);
workbench.testing.results[0].append_message("c\0f\0b", "t1", json!({ "type": 0, "message": "not equal", "expected": "1\n2", "actual": "1\n3", "contextValue": null, "location": null }));
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 1 });
let tab = workbench.message_editor().expect("the message's diff editor").model_id;
workbench.with_group(crate::editor::TEST_RESULTS_GROUP, |workbench| {
let diff = workbench.editors.diffs.get(&tab).expect("a diff editor");
let side = |editor: &Option<crate::editor::OpenEditor>| editor.as_ref().map(|editor| (editor.document().uri_string(), editor.document().lines.clone()));
assert_eq!(side(&diff.original_editor), Some(("vscode-test-data://results/r1/message/0/1/ExpectedOutput?c%00f%00b".to_owned(), vec!["1".to_owned(), "2".to_owned()])));
assert_eq!(side(&diff.modified_editor), Some(("vscode-test-data://results/r1/message/0/1/ActualOutput?c%00f%00b".to_owned(), vec!["1".to_owned(), "3".to_owned()])));
assert!(diff.embedded && !diff.inline_now);
assert_eq!(diff.diff.as_ref().map(|computed| computed.diff.changes.len()), Some(1));
assert_eq!(workbench.line_numbers(diff.modified_editor.as_ref().expect("attached")), crate::workbench::editor_view::LineNumbers::On);
// lineNumbersMinChars 5 (36px) and '2ch' (14px) decorations; the glyph margin (18px) on the original only.
let mut content_left = |editor: &Option<crate::editor::OpenEditor>| workbench.editor_metrics(editor.as_ref().expect("attached"), window).content_left();
assert_eq!((content_left(&diff.original_editor), content_left(&diff.modified_editor)), (68., 50.));
for editor in [&diff.original_editor, &diff.modified_editor] {
let editor = editor.as_ref().expect("attached");
let zones: Vec<_> = editor.view_zones.iter().filter(|zone| zone.anchor.is_none()).map(|zone| (zone.height, workbench.testing_diff_lens(editor, zone.id, zone.height, 0.).is_some())).collect();
assert_eq!(zones, vec![(20., true)], "the side's title zone above line 1");
}
});
workbench.open_results_element(&ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 0 });
assert!(workbench.message_editor().is_some());
assert!(!workbench.editors.diffs.contains_key(&tab));
});
}
fn drawn_rows(workbench: &Workbench) -> Vec<String> {
workbench
.results_rows()
.iter()
.map(|row| {
let (label, description, _) = workbench.results_row_label(row);
format!("{}{label} | {}{}", " ".repeat(row.depth), description.unwrap_or_default(), if row.collapsed { " +" } else { "" })
})
.collect()
}
/// testResultsTree.ts `getRootChildren`: the newest result's tasks with the tests that ran, worst first, a test with
/// one error message drawn with it on one row, then the older results collapsed.
#[gpui::test]
fn the_results_tree_shows_tasks_tests_and_messages_as_testresultstree(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-tree", TESTS, cx);
let rows = workbench.update(cx, |workbench, _| {
add_results(workbench);
drawn_rows(workbench)
});
assert_eq!(rows, ["Task A | ", " subtracts | expected 1 @ math", " throws | math", " first | ", " second | ", " adds | math", "1 older result | +"]);
let rows = workbench.update(cx, |workbench, _| {
workbench.testing_view.results_toggled.insert("older-1".into());
drawn_rows(workbench)
});
assert_eq!(rows[6..], ["1 older result | ", " adds | +"]);
}
/// `TreeActionsProvider.provideActionBar` in `onContextMenu`: a test row's built-in actions, its
/// `testing/item/result` items with the test and the task as arguments, and a message row's
/// `testing/message/context` items.
#[gpui::test]
fn a_results_rows_menu_shows_its_actions_and_the_contributed_items(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-menu", TESTS, cx);
let (test_menu, message_menu, result_menu) = workbench.update_in(cx, |workbench, window, _| {
add_results(workbench);
workbench.menus.push(MenuItem { group: "inline".into(), ..contributed(MenuId::TestPeekElement, "ext.inline", "Ext Inline") });
workbench.menus.push(contributed(MenuId::TestPeekElement, "ext.result", "Ext Result"));
let when = crate::contextkey::Expr::deserialize("testMessage == diff");
workbench.menus.push(MenuItem { when, ..contributed(MenuId::TestMessageContext, "ext.message", "Ext Message") });
let test = ResultsElement::Test { result: "r1".into(), task: 0, test: "c\0f\0b".into() };
let message = ResultsElement::Message { result: "r1".into(), task: 0, test: "c\0f\0c".into(), message: 1 };
let result = ResultsElement::Result { result: "r0".into() };
(workbench.results_menu_actions(&test, window), workbench.results_menu_actions(&message, window), workbench.results_menu_actions(&result, window))
});
assert_eq!(titles(&test_menu), ["Go to Test", "Run Test", "Debug Test", "Ext Inline", "---", "Reveal in Test Explorer", "---", "Ext Result"]);
assert_eq!(titles(&message_menu), ["Go to Test", "Run Test", "Debug Test", "---", "Reveal in Test Explorer", "---", "Ext Message"]);
assert_eq!(titles(&result_menu), ["Show Result Output", "Rerun Test", "Ext Inline", "---", "Ext Result"]);
let received = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
let sink = received.clone();
workbench.update_in(cx, |workbench, window, cx| {
workbench.commands.register("ext.result", move |_, args, _, _| {
sink.borrow_mut().push(args);
crate::commands::Outcome::done()
});
let id = super::super::testing_gutter::tests::command_titled(&test_menu, "Ext Result");
drop(workbench.execute_command(&id, Vec::new(), window, cx));
});
let args = received.borrow()[0].clone();
assert_eq!((args[0]["$mid"].clone(), args[0]["tests"][0]["item"]["extId"].clone(), args[1]["taskId"].clone(), args[1]["resultId"].clone()), (json!(16), json!("c\0f\0b"), json!("t1"), json!("r1")));
}
/// mainThreadTesting.ts `$markTestRetired`: the tests under the ids, or all, are retired in every result and their
/// rows and explorer items take `testResultOutdated`.
#[gpui::test]
fn retired_tests_take_test_result_outdated(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-retired", TESTS, cx);
let retire = |test_ids: Value| exthost::session::ForwardedRequest { req: 1, proxy: "MainThreadTesting", method: "$markTestRetired".into(), args: vec![test_ids] };
let outdated = |workbench: &Workbench, window: &gpui::Window| -> Vec<bool> {
let row = |test: &str| titles(&workbench.results_actions(&ResultsElement::Test { result: "r1".into(), task: 0, test: test.into() }, window).2).contains(&"Ext Outdated".to_owned());
vec![row("c\0f\0b"), row("c\0f\0c"), workbench.test_item_context("c\0f\0b", window)["testResultOutdated"] == true, workbench.testing.results[1].items["c\0f\0a"].retired]
};
let (partial, all) = workbench.update_in(cx, |workbench, window, cx| {
add_results(workbench);
let when = crate::contextkey::Expr::deserialize("testResultOutdated");
workbench.menus.push(MenuItem { when, ..contributed(MenuId::TestPeekElement, "ext.outdated", "Ext Outdated") });
drop(workbench.handle_testing_request(retire(json!(["c\0f\0b"])), window, cx));
let partial = outdated(workbench, window);
drop(workbench.handle_testing_request(retire(Value::Null), window, cx));
(partial, outdated(workbench, window))
});
assert_eq!(partial, [true, false, true, false]);
assert_eq!(all, [true, true, true, true]);
let (failed, retired) = workbench.read_with(cx, |workbench, _| (workbench.color("testing.iconFailed"), workbench.color("testing.iconFailed.retired")));
assert_eq!((retired.r, retired.g, retired.b), (failed.r, failed.g, failed.b));
assert!((retired.a - failed.a * 0.7).abs() < 0.01, "{retired:?}");
}
/// testResultsTree.ts `TreeActionsProvider`: a contributed `inline` item draws with its command's icon.
#[gpui::test]
fn a_contributed_inline_result_action_draws_its_icon(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-inline-icon", TESTS, cx);
workbench.update(cx, |workbench, cx| {
add_results(workbench);
workbench.menus.push(MenuItem { group: "inline".into(), ..contributed(MenuId::TestPeekElement, "ext.inline", "Ext Inline") });
workbench.command_icons.insert("ext.inline".into(), crate::menus::CommandIcon::Codicon("beaker".into()));
workbench.open_panel_view(crate::workbench::testing_view::TEST_RESULTS_VIEW_ID, cx);
});
cx.run_until_parked();
cx.update(|window, _| window.refresh());
cx.run_until_parked();
let row = cx.debug_bounds("codicon-pass").expect("the passed test's row draws").center();
cx.simulate_mouse_move(row, None, gpui::Modifiers::none());
cx.run_until_parked();
assert!(cx.debug_bounds("codicon-go-to-file").is_some(), "the hovered row draws its actions");
assert!(cx.debug_bounds("codicon-beaker").is_some(), "the inline item draws its icon");
}
/// testResultsViewContent.ts `populateFloatingClick`: an opened message shows the first `testing/message/content`
/// item, run with the message's `getMessageArgs`; opening a test opens its first message.
#[gpui::test]
fn an_opened_message_shows_the_first_message_content_item(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-content", TESTS, cx);
let floating = workbench.update_in(cx, |workbench, window, _| {
add_results(workbench);
let entry = |id: &str| Entry::Command { id: id.into(), precondition: None, toggled: None, alt: None };
let when = crate::contextkey::Expr::deserialize("testMessage == diff");
workbench.menus.push(MenuItem { menu: MenuId::TestMessageContent, entry: entry("ext.accept"), title: "Accept".into(), group: "a".into(), order: 0., when });
workbench.menus.push(contributed(MenuId::TestMessageContent, "ext.other", "Other"));
workbench.open_results_element(&ResultsElement::Test { result: "r1".into(), task: 0, test: "c\0f\0c".into() });
workbench.results_floating_action(window)
});
let subject = workbench.read_with(cx, |workbench, _| workbench.testing_view.results_subject.clone());
assert_eq!(subject, Some(InspectSubject::Message { result: "r1".into(), task: 0, test: "c\0f\0c".into(), message: 0 }));
let (command, title, arg) = floating.expect("a floating button");
assert_eq!((command.as_str(), title.as_str(), arg["$mid"].clone(), arg["message"]["message"].clone(), arg["test"]["item"]["extId"].clone()), ("ext.accept", "Accept", json!(18), json!("first"), json!("c\0f\0c")));
}
/// listCommands.ts on the focused results tree: the arrows move the focus, Right on an expanded row goes to its
/// first child, Left collapses and then goes to the parent, Enter selects and opens the focused row.
#[gpui::test]
fn the_results_tree_moves_its_focus_with_the_list_commands(cx: &mut TestAppContext) {
let (workbench, cx, _host) = workbench_with_tests("test-results-keys", TESTS, cx);
workbench.update_in(cx, |workbench, window, cx| {
add_results(workbench);
window.focus(&workbench.testing_view.results_focus.clone(), cx);
assert_eq!(workbench.key_context(window).get("listFocus"), Some(&json!(true)));
let mut run = |workbench: &mut Workbench, id: &str| {
assert!(matches!(workbench.execute_command(id, vec![], window, cx), crate::commands::Outcome::Ready(Ok(_))), "{id}");
workbench.testing_view.results_focused.clone().unwrap_or_default()
};
assert_eq!(run(workbench, "list.focusDown"), "r1/0");
assert_eq!(run(workbench, "list.expand"), "r1/0/c\0f\0b/message/0");
assert_eq!(run(workbench, "list.focusDown"), "r1/c\0f\0c");
assert_eq!(run(workbench, "list.collapse"), "r1/c\0f\0c");
assert_eq!(drawn_rows(workbench)[2], " throws | math +");
assert_eq!(run(workbench, "list.collapse"), "r1/0");
assert_eq!(run(workbench, "list.focusDown"), "r1/0/c\0f\0b/message/0");
run(workbench, "list.select");
let message = InspectSubject::Message { result: "r1".into(), task: 0, test: "c\0f\0b".into(), message: 0 };
assert_eq!((workbench.testing_view.results_selected.as_deref(), workbench.testing_view.results_subject.clone()), (Some("r1/0/c\0f\0b/message/0"), Some(message)));
});
}