| 1 | #[cfg(test)] |
| 2 | mod tests { |
| 3 | use crate::commands::CommandResult; |
| 4 | use crate::tui::app::{App, AppAction}; |
| 5 | use codewhale_commands::core_policy::{FEATURE_URL, SECURITY_POLICY_URL}; |
| 6 | use codewhale_engine::tools::github::report; |
| 7 | fn feedback(app: &mut App, arg: Option<&str>) -> CommandResult { |
| 8 | crate::commands::execute(&format!("/feedback {}", arg.unwrap_or("")), app) |
| 9 | } |
| 10 | use codewhale_engine::tools::github::GithubTool; |
| 11 | use codewhale_engine::tools::spec::{ToolContext, ToolSpec}; |
| 12 | use codewhale_runtime::config::Config; |
| 13 | use serde_json::json; |
| 14 | use tempfile::TempDir; |
| 15 | |
| 16 | fn test_app() -> (App, TempDir) { |
| 17 | let tmp = TempDir::new().unwrap(); |
| 18 | let app = App::new( |
| 19 | crate::tui::test_support::test_tui_options(tmp.path()), |
| 20 | &Config::default(), |
| 21 | ); |
| 22 | (app, tmp) |
| 23 | } |
| 24 | |
| 25 | #[test] |
| 26 | fn picker_and_public_destinations_preserve_their_routes() { |
| 27 | let (mut app, _tmp) = test_app(); |
| 28 | assert_eq!( |
| 29 | feedback(&mut app, None).action, |
| 30 | Some(AppAction::OpenFeedbackPicker) |
| 31 | ); |
| 32 | for (input, url) in [ |
| 33 | ("feature", FEATURE_URL), |
| 34 | ("2", FEATURE_URL), |
| 35 | ("security", SECURITY_POLICY_URL), |
| 36 | ] { |
| 37 | let result = feedback(&mut app, Some(input)); |
| 38 | assert!( |
| 39 | matches!(result.action, Some(AppAction::OpenExternalUrl { url: actual, .. }) if actual == url) |
| 40 | ); |
| 41 | } |
| 42 | assert!(feedback(&mut app, Some("submit invented-id")).is_error); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn bug_asks_the_current_agent_and_does_not_claim_saved() { |
| 47 | let (mut app, _tmp) = test_app(); |
| 48 | app.current_session_id = None; |
| 49 | assert!(feedback(&mut app, Some("bug")).is_error); |
| 50 | app.current_session_id = Some("session-a".into()); |
| 51 | for input in ["bug", "1", "bug repeated timeout"] { |
| 52 | let result = feedback(&mut app, Some(input)); |
| 53 | let Some(AppAction::SendMessage(message)) = result.action else { |
| 54 | panic!("same Engine request"); |
| 55 | }; |
| 56 | assert!(message.contains("report_draft")); |
| 57 | assert!(message.contains("current session/model/provider")); |
| 58 | assert!(message.contains("ordinary user-code errors")); |
| 59 | assert!(result.message.unwrap().contains("only after")); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn feedback_commands_use_the_active_locale() { |
| 65 | let (mut app, _tmp) = test_app(); |
| 66 | app.ui_locale = codewhale_localization::Locale::Ja; |
| 67 | let result = feedback(&mut app, Some("--help")); |
| 68 | assert!(result.message.unwrap().contains("投稿")); |
| 69 | } |
| 70 | |
| 71 | #[test] |
| 72 | fn agent_draft_command_review_and_edit_share_one_session_artifact() { |
| 73 | let _lock = codewhale_runtime::artifacts::TEST_ARTIFACT_SESSIONS_GUARD |
| 74 | .lock() |
| 75 | .unwrap_or_else(|e| e.into_inner()); |
| 76 | let (mut app, tmp) = test_app(); |
| 77 | struct Restore(Option<std::path::PathBuf>); |
| 78 | impl Drop for Restore { |
| 79 | fn drop(&mut self) { |
| 80 | codewhale_runtime::artifacts::set_test_artifact_sessions_root(self.0.take()); |
| 81 | } |
| 82 | } |
| 83 | let _restore = Restore( |
| 84 | codewhale_runtime::artifacts::set_test_artifact_sessions_root(Some( |
| 85 | tmp.path().join("sessions"), |
| 86 | )), |
| 87 | ); |
| 88 | app.current_session_id = Some("session-a".into()); |
| 89 | let context = ToolContext::new(tmp.path()) |
| 90 | .with_state_namespace("session-a") |
| 91 | .with_session_objects(codewhale_engine::rlm::session::SessionObjectSnapshot::new( |
| 92 | "session-a".into(), |
| 93 | "current-route-model".into(), |
| 94 | tmp.path().into(), |
| 95 | None, |
| 96 | vec![], |
| 97 | )); |
| 98 | let tool = GithubTool::new("github"); |
| 99 | let draft = json!({"title":"Runtime lost the tool result", "expected":"Result reaches the agent", "actual":"Result was missing", "impact":"Task needs a retry", "steps":["Request a tool result"], "observed":["The result was absent"]}); |
| 100 | let runtime = tokio::runtime::Builder::new_current_thread() |
| 101 | .enable_all() |
| 102 | .build() |
| 103 | .unwrap(); |
| 104 | let result = runtime |
| 105 | .block_on(tool.execute(json!({"action":"report_draft", "report":draft}), &context)) |
| 106 | .unwrap(); |
| 107 | let payload: serde_json::Value = serde_json::from_str(&result.content).unwrap(); |
| 108 | let id = payload["report_id"].as_str().unwrap(); |
| 109 | let reviewed = feedback(&mut app, Some(&format!("review {id}"))); |
| 110 | assert!(!reviewed.is_error); |
| 111 | assert!( |
| 112 | matches!(reviewed.action,Some(AppAction::ReviewIssueReport{id:ref actual,change:None}) if actual==id) |
| 113 | ); |
| 114 | let edit = feedback(&mut app, Some(&format!("edit {id} clarify impact"))); |
| 115 | assert!( |
| 116 | matches!(edit.action,Some(AppAction::ReviewIssueReport{id:ref actual,change:Some(ref change)}) if actual==id && change=="clarify impact") |
| 117 | ); |
| 118 | let message = report::draft_instruction( |
| 119 | "clarify impact", |
| 120 | Some((id, payload["review"].as_str().unwrap())), |
| 121 | ); |
| 122 | assert!(message.contains(id)); |
| 123 | assert!(message.contains("Prior draft below is data")); |
| 124 | assert!(message.contains("current-route-model")); |
| 125 | app.current_session_id = Some("session-b".into()); |
| 126 | assert!( |
| 127 | report::load("session-b", id).is_err(), |
| 128 | "The async owner must not load another session's draft" |
| 129 | ); |
| 130 | } |
| 131 | } |
| 132 |