Skip to content

Commit

Permalink
test(term): setup application in helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed May 31, 2024
1 parent 96b7a27 commit 489bd75
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 172 deletions.
14 changes: 13 additions & 1 deletion crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,6 @@ impl Application {
#[cfg(feature = "integration")]
{
tracing::debug!("Quit for idle");
self.should_render();
self.flags.insert(Should::Quit);
}
}
Expand All @@ -1026,4 +1025,17 @@ impl Application {
pub fn buffer(&self) -> &ratatui::buffer::Buffer {
self.terminal.buffer()
}

pub async fn wait_until_jobs_completed<S>(&mut self, input: &mut S)
where
S: Stream<Item = std::io::Result<CrosstermEvent>> + Unpin,
{
loop {
self.event_loop_until_idle(input).await;
if self.jobs.futures.is_empty() {
break;
}
self.reset_idle_timer();
}
}
}
9 changes: 9 additions & 0 deletions crates/synd_term/src/keymap/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ macro_rules! keymap {
}

pub(crate) use keymap;

#[macro_export]
macro_rules! key {
( enter ) => {
crossterm::event::Event::Key(crossterm::event::KeyEvent::from(
crossterm::event::KeyCode::Enter,
))
};
}
138 changes: 55 additions & 83 deletions crates/synd_term/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,97 +1,68 @@
#[cfg(feature = "integration")]
mod test {
mod helper;

use std::time::Duration;

use crossterm::event::{Event, KeyCode, KeyEvent};
use serial_test::file_serial;

use synd_auth::device_flow::{provider, DeviceFlow};

use synd_term::{
application::{Application, Authenticator, Config, DeviceFlows},
client::Client,
config::Categories,
ui::theme::Theme,
};
use tokio::net::TcpListener;
use synd_term::key;
use tokio_stream::wrappers::UnboundedReceiverStream;
use tracing_subscriber::EnvFilter;

mod helper;
use crate::test::helper::TestCase;

#[tokio::test(flavor = "multi_thread")]
#[file_serial(a)]
async fn happy() -> anyhow::Result<()> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.with_line_number(true)
.with_file(true)
.with_target(false)
.init();

let mock_port = 6000;
let api_port = 6001;
let oauth_addr = ("127.0.0.1", mock_port);
let oauth_listener = TcpListener::bind(oauth_addr).await?;
tokio::spawn(synd_test::mock::serve(oauth_listener));
helper::serve_api(mock_port, api_port).await?;

check_command_test(api_port);

let endpoint = format!("https://localhost:{api_port}/graphql")
.parse()
.unwrap();
let terminal = helper::new_test_terminal(120, 30);
let client = Client::new(endpoint, Duration::from_secs(30)).unwrap();
let device_flows = DeviceFlows {
github: DeviceFlow::new(
provider::Github::new("dummy")
.with_device_authorization_endpoint(format!(
"http://localhost:{mock_port}/case1/github/login/device/code",
))
.with_token_endpoint(
"http://localhost:6000/case1/github/login/oauth/access_token",
),
),
google: DeviceFlow::new(provider::Google::new("dummy", "dummy")),
};
let authenticator = Authenticator::new().with_device_flows(device_flows);
let config = Config {
idle_timer_interval: Duration::from_millis(2000),
throbber_timer_interval: Duration::from_secs(3600), // disable throbber
..Default::default()
helper::init_tracing();

let test_case = TestCase {
oauth_provider_port: 6000,
synd_api_port: 6001,
kvsd_port: 47379,
terminal_col_row: (120, 30),
device_flow_case: "case1",
};
// or mpsc and tokio_stream ReceiverStream
let mut application = test_case.init_app().await?;

let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
let mut event_stream = UnboundedReceiverStream::new(rx);
let theme = Theme::default();

let mut application =
Application::with(terminal, client, Categories::default_toml(), config)
.with_theme(theme.clone())
.with_authenticator(authenticator);
application.event_loop_until_idle(&mut event_stream).await;

insta::assert_debug_snapshot!(application.buffer());

tracing::info!("Login assertion OK");

// push enter => start auth flow
let event = Event::Key(KeyEvent::from(KeyCode::Enter));
tx.send(Ok(event)).unwrap();
application.event_loop_until_idle(&mut event_stream).await;

insta::assert_debug_snapshot!(application.buffer());

tracing::info!("Login prompt assertion OK");

// for quit event loop after polling job complete
application.reset_idle_timer();
// polling device access token complete
application.event_loop_until_idle(&mut event_stream).await;

export_command_test(api_port);
clean_command_test();
{
application
.wait_until_jobs_completed(&mut event_stream)
.await;
insta::with_settings!({
description => "initial login prompt",
}, {
insta::assert_debug_snapshot!("initial_login", application.buffer());
});
}

{
// push enter => start auth flow
tx.send(Ok(key!(enter))).unwrap();
application.event_loop_until_idle(&mut event_stream).await;
insta::with_settings!({
description => "show device flow code",
},{
insta::assert_debug_snapshot!("device_flow_prompt", application.buffer());
});
}

{
// polling device access token complete
application
.wait_until_jobs_completed(&mut event_stream)
.await;
insta::with_settings!({
description => "initial landing entries",
},{
insta::assert_debug_snapshot!("landing_entries", application.buffer());
});
}

{
check_command_test(test_case.synd_api_port);
export_command_test(test_case.synd_api_port);
clean_command_test();
}

Ok(())
}
Expand Down Expand Up @@ -127,6 +98,7 @@ mod test {
fn clean_command_test() {
let mut cmd = assert_cmd::Command::cargo_bin("synd").unwrap();

cmd.args(["clean"]).assert().success();
// TODO: Currently clear real cache :(
cmd.args(["clean", "--help"]).assert().success();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/synd_term/tests/integration.rs
description: show device flow code
expression: application.buffer()
---
Buffer {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/synd_term/tests/integration.rs
description: initial login prompt
expression: application.buffer()
---
Buffer {
Expand All @@ -12,19 +13,7 @@ Buffer {
" ",
" ",
" ",
" β–β–ˆβ–Œ β–β–ˆ β–ˆ ▐ β–ˆ β–β–ˆ ",
" β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ ",
" β–ˆβ–Œ β–ˆ β–ˆ β–ˆβ–ˆβ–Œ β–ˆ β–β–ˆ β–β–ˆβ–Œ β–β–ˆβ–Œ β–β–ˆβ–ˆ β–β–ˆ β–β–ˆβ–Œ β–ˆβ–ˆβ–Œ β–ˆ ",
" β–β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–β–ˆβ–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–β–ˆβ–ˆ ",
" β–β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–β–ˆβ–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ ",
" β–ˆ β–ˆ β–β–ˆβ–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆβ– β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ β–ˆ ",
" β–β–ˆβ–Œ β–ˆ β–ˆ β–ˆ β–β–ˆβ–β–Œβ–β–ˆβ–Œ β–β–ˆβ–Œ β–β–ˆβ–β–Œ β–β–Œ β–β–ˆβ–Œ β–β–ˆβ–Œ β–ˆ β–ˆ β–β–ˆβ–β–Œ ",
" β–ˆβ–ˆβ–Œ ",
" ",
" Login ",
" ──────────────────────────────────────────────── ",
"  󰊀 GitHub ",
" 󰊭 Google ",
" ",
" ",
" ",
Expand All @@ -34,15 +23,21 @@ Buffer {
" ",
" ",
" ",
" j/k:σ°ΉΉ Ent:󰏌 q: ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
" ",
],
styles: [
x: 0, y: 0, fg: Rgb(254, 205, 178), bg: Rgb(43, 41, 45), underline: Reset, modifier: NONE,
x: 58, y: 16, fg: Rgb(254, 205, 178), bg: Rgb(43, 41, 45), underline: Reset, modifier: BOLD,
x: 63, y: 16, fg: Rgb(254, 205, 178), bg: Rgb(43, 41, 45), underline: Reset, modifier: NONE,
x: 36, y: 18, fg: Rgb(254, 205, 178), bg: Rgb(43, 41, 45), underline: Reset, modifier: BOLD,
x: 84, y: 18, fg: Rgb(254, 205, 178), bg: Rgb(43, 41, 45), underline: Reset, modifier: NONE,
x: 51, y: 29, fg: Rgb(111, 93, 99), bg: Rgb(43, 41, 45), underline: Reset, modifier: NONE,
x: 70, y: 29, fg: Rgb(254, 205, 178), bg: Rgb(43, 41, 45), underline: Reset, modifier: NONE,
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/synd_term/tests/integration.rs
description: initial entries
expression: application.buffer()
---
Buffer {
Expand Down
Loading

0 comments on commit 489bd75

Please sign in to comment.