Skip to content

Visual Studio Code, rebuilt in Rust

Rust
6.7 KB
Raw Blame
use std::sync::{Arc, Mutex};

use gpui::{TestAppContext, VisualTestContext};
use serde_json::{Value, json};

use super::*;
use crate::menus::{Entry, MenuItem};
use crate::workbench::goto_symbol::tests::open;

/// A comment the extension switches to Editing opens its edit form with its text; Cmd+Enter runs the first
/// `comments/comment/context` action with the `CommentThreadNode`; switching back to Preview closes the form.
#[gpui::test]
fn a_comment_switched_to_editing_runs_the_edit_actions(cx: &mut TestAppContext) {
    let (workbench, cx, _, dir) = open(cx, "comment-node", "one\ntwo\nthree\n");
    workbench.update_in(cx, |workbench, _, _| workbench.load_keybindings(crate::keybindings::default_keybindings(&[]), dir.join("keybindings.json")));
    let resource = json!({ "scheme": "file", "path": dir.join("a.txt").to_string_lossy() });
    let request = |method: &str, args: Vec<Value>, cx: &mut VisualTestContext| {
        workbench.update_in(cx, |workbench, window, cx| drop(workbench.handle_comments_request(method, &args, window, cx)));
    };
    let comment = |mode: u64| json!({ "uniqueIdInThread": 3, "body": "Looks good", "userName": "Ann", "mode": mode });
    let update = |mode: u64, cx: &mut VisualTestContext| {
        request("$updateCommentThread", vec![json!(1), json!(7), json!("t"), resource.clone(), json!({ "comments": [comment(mode)], "collapseState": 1 })], cx);
    };
    request("$registerCommentController", vec![json!(1), json!("review"), json!("Review"), json!("test.ext")], cx);
    let range = json!({ "startLineNumber": 2, "startColumn": 1, "endLineNumber": 2, "endColumn": 4 });
    request("$createCommentThread", vec![json!(1), json!(7), json!("t"), resource.clone(), range, json!([comment(1)]), json!({ "value": "test.ext" }), json!(false)], cx);
    update(1, cx);
    let editing = |cx: &mut VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.comments.forms.first().map(|form| (form.comment, workbench.form_text(form))));
    assert_eq!(editing(cx), None);

    let ran = Arc::new(Mutex::new(Vec::new()));
    workbench.update_in(cx, |workbench, _, _| {
        let record = ran.clone();
        workbench.commands.register("test.save", move |_, args, _, _| {
            record.lock().unwrap().extend(args);
            crate::commands::Outcome::done()
        });
        let entry = Entry::Command { id: "test.save".into(), precondition: None, toggled: None, alt: None };
        workbench.menus.push(MenuItem { menu: MenuId::CommentActions, entry, title: "Save".into(), group: "inline".into(), order: 0., when: None });
    });
    update(EDITING, cx);
    assert_eq!(editing(cx), Some((Some(3), "Looks good".to_owned())));
    cx.run_until_parked();
    cx.simulate_input("!");
    cx.simulate_keystrokes(&crate::keybindings::primary_keys("secondary-enter"));
    cx.run_until_parked();
    let thread = json!({ "$mid": 7, "commentControlHandle": 1, "commentThreadHandle": 7 });
    assert_eq!(*ran.lock().unwrap(), vec![json!({ "thread": thread, "commentUniqueId": 3, "text": "Looks good!", "$mid": 11 })]);
    assert_eq!(editing(cx), None);

    update(1, cx);
    update(EDITING, cx);
    assert!(editing(cx).is_some());
    update(1, cx);
    assert_eq!(editing(cx), None);
}

#[test]
fn a_reaction_tooltip_names_its_count_or_reactors() {
    let reaction = |count: u64, reactors: Vec<&str>| json!({ "label": "+1", "count": count, "reactors": reactors });
    assert_eq!(reaction_tooltip(&reaction(1, vec![]), true), "Toggle reaction, 1 reaction with +1");
    assert_eq!(reaction_tooltip(&reaction(2, vec![]), false), "2 reactions with +1");
    assert_eq!(reaction_tooltip(&reaction(2, vec!["Ann", "Bob"]), false), "Ann, Bob reacted with +1");
    assert_eq!(reaction_tooltip(&reaction(5, vec!["Ann", "Bob"]), true), "Toggle reaction, Ann, Bob and 3 more reacted with +1");
}

/// review.css `.review-widget .action-item`: a reaction sits 4px into the comment's contents and the picker 4px after it.
#[gpui::test]
fn reactions_are_action_items_4px_apart(cx: &mut TestAppContext) {
    let (workbench, cx, _, dir) = open(cx, "comment-reaction-row", "one\ntwo\n");
    let resource = json!({ "scheme": "file", "path": dir.join("a.txt").to_string_lossy() });
    let request = |method: &str, args: Vec<Value>, cx: &mut VisualTestContext| {
        workbench.update_in(cx, |workbench, window, cx| drop(workbench.handle_comments_request(method, &args, window, cx)));
    };
    request("$registerCommentController", vec![json!(1), json!("review"), json!("Review"), json!("test.ext")], cx);
    request("$updateCommentControllerFeatures", vec![json!(1), json!({ "reactionHandler": true })], cx);
    let range = json!({ "startLineNumber": 1, "startColumn": 1, "endLineNumber": 1, "endColumn": 4 });
    request("$createCommentThread", vec![json!(1), json!(7), json!("t"), resource.clone(), range, json!([]), json!({ "value": "test.ext" }), json!(false)], cx);
    let comment = json!({ "uniqueIdInThread": 1, "body": "Looks good", "userName": "Ann", "commentReactions": [{ "label": "+1", "count": 2 }] });
    request("$updateCommentThread", vec![json!(1), json!(7), json!("t"), resource, json!({ "comments": [comment], "collapseState": 1 })], cx);
    cx.run_until_parked();
    let node = cx.debug_bounds("comment-node-0").expect("the zone shows its comment");
    let reaction = cx.debug_bounds("comment-reaction-0").expect("the reaction with a count shows");
    let picker = cx.debug_bounds("comment-reaction-picker").expect("the reaction handler adds the picker");
    // The node's 20px padding, then the contents' 20px.
    assert_eq!(f32::from(reaction.left() - node.left()), 20. + 20. + 4.);
    assert_eq!(f32::from(picker.left() - reaction.right()), 4.);
}

/// The picker registers one `reaction.command.<label>` per reaction; the next picker replaces them.
#[gpui::test]
fn the_reaction_picker_registers_a_command_per_reaction(cx: &mut TestAppContext) {
    let (workbench, cx, _, _) = open(cx, "comment-reactions", "one\n");
    let comment = |labels: &[&str]| json!({ "uniqueIdInThread": 1, "commentReactions": labels.iter().map(|label| json!({ "label": label, "count": 0 })).collect::<Vec<_>>() });
    let registered = |cx: &mut VisualTestContext| {
        workbench.read_with(cx, |workbench, _| workbench.commands.ids().filter(|id| id.starts_with("reaction.command.")).map(str::to_owned).collect::<Vec<_>>())
    };
    let anchor = gpui::Bounds::default();
    workbench.update_in(cx, |workbench, window, cx| workbench.show_reaction_picker(1, 7, &comment(&["+1", "heart"]), anchor, window, cx));
    let mut ids = registered(cx);
    ids.sort();
    assert_eq!(ids, ["reaction.command.+1", "reaction.command.heart"]);
    workbench.update_in(cx, |workbench, window, cx| workbench.show_reaction_picker(1, 7, &comment(&["laugh"]), anchor, window, cx));
    assert_eq!(registered(cx), ["reaction.command.laugh"]);
}