Skip to content

Commit 5cfbfc3

Browse files
committed
test: add integration
1 parent aa51d4e commit 5cfbfc3

File tree

5 files changed

+73
-17
lines changed

5 files changed

+73
-17
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

syndterm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,4 @@ serial_test = { version = "3.0.0", default_features = false, features = [
5252
"async",
5353
"file_locks",
5454
] }
55+
tokio-stream = "0.1.14"

syndterm/src/application/mod.rs

+39-11
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,52 @@ enum Screen {
3030
Browse,
3131
}
3232

33+
#[derive(PartialEq, Eq)]
34+
pub enum EventLoopControlFlow {
35+
Quit,
36+
}
37+
38+
pub struct Config {
39+
pub idle_timer_interval: Duration,
40+
}
41+
42+
impl Default for Config {
43+
fn default() -> Self {
44+
Self {
45+
idle_timer_interval: Duration::from_secs(250),
46+
}
47+
}
48+
}
49+
3350
pub struct Application {
3451
terminal: Terminal,
3552
client: Client,
3653
jobs: Jobs,
3754
components: Components,
3855
theme: Theme,
3956
idle_timer: Pin<Box<Sleep>>,
57+
config: Config,
4058

4159
screen: Screen,
4260
should_render: bool,
4361
should_quit: bool,
4462
}
4563

46-
#[derive(PartialEq, Eq)]
47-
pub enum EventLoopControlFlow {
48-
Quit,
49-
}
50-
5164
impl Application {
5265
pub fn new(terminal: Terminal, client: Client) -> Self {
66+
Application::with(terminal, client, Config::default())
67+
}
68+
69+
pub fn with(terminal: Terminal, client: Client, config: Config) -> Self {
5370
Self {
5471
terminal,
5572
client,
5673
components: Components::new(),
5774
jobs: Jobs::new(),
5875
theme: Theme::new(),
59-
idle_timer: Box::pin(tokio::time::sleep(Duration::from_millis(250))),
76+
idle_timer: Box::pin(tokio::time::sleep(config.idle_timer_interval)),
6077
screen: Screen::Login,
78+
config,
6179
should_quit: false,
6280
should_render: false,
6381
}
@@ -138,15 +156,14 @@ impl Application {
138156
}
139157

140158
if self.should_quit {
159+
self.should_quit = false; // for testing
141160
break EventLoopControlFlow::Quit;
142161
}
143162
}
144163
}
145164

146165
#[tracing::instrument(skip_all,fields(%command))]
147166
fn apply(&mut self, command: Command) {
148-
tracing::debug!("Apply {command:?}");
149-
150167
let mut next = Some(command);
151168

152169
// how to handle command => command pattern ?
@@ -259,7 +276,9 @@ impl Application {
259276
..
260277
}) => None,
261278
CrosstermEvent::Key(key) => {
262-
tracing::debug!("{key:?}");
279+
self.reset_idle_timer();
280+
281+
tracing::debug!("Handle key event: {key:?}");
263282
match self.screen {
264283
Screen::Login => match key.code {
265284
KeyCode::Enter => {
@@ -411,7 +430,9 @@ impl Application {
411430
}
412431

413432
impl Application {
433+
#[tracing::instrument(skip(self))]
414434
fn authenticate(&mut self, provider: AuthenticationProvider) {
435+
tracing::info!("Start authenticate");
415436
match provider {
416437
AuthenticationProvider::Github => {
417438
let fut = async move {
@@ -469,21 +490,28 @@ impl Application {
469490

470491
impl Application {
471492
fn handle_idle(&mut self) {
472-
self.reset_idle_timer();
493+
self.clear_idle_timer();
473494

474495
#[cfg(feature = "integration")]
475496
{
497+
tracing::debug!("Quit for idle");
476498
self.should_render = true;
477499
self.should_quit = true;
478500
}
479501
}
480502

481-
fn reset_idle_timer(&mut self) {
503+
fn clear_idle_timer(&mut self) {
482504
// https://github.com/tokio-rs/tokio/blob/e53b92a9939565edb33575fff296804279e5e419/tokio/src/time/instant.rs#L62
483505
self.idle_timer
484506
.as_mut()
485507
.reset(Instant::now() + Duration::from_secs(86400 * 365 * 30));
486508
}
509+
510+
fn reset_idle_timer(&mut self) {
511+
self.idle_timer
512+
.as_mut()
513+
.reset(Instant::now() + self.config.idle_timer_interval);
514+
}
487515
}
488516

489517
#[cfg(feature = "integration")]

syndterm/src/auth/github.rs

+5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ impl DeviceFlow {
3535
}
3636
}
3737

38+
#[tracing::instrument(skip(self))]
3839
pub async fn device_authorize_request(&self) -> anyhow::Result<DeviceAuthorizationResponse> {
40+
tracing::debug!("Sending request");
41+
3942
// https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/scopes-for-oauth-apps
4043
let scope = "user:email";
4144

@@ -53,6 +56,8 @@ impl DeviceFlow {
5356
.json::<DeviceAuthorizationResponse>()
5457
.await?;
5558

59+
tracing::debug!("Got response");
60+
5661
Ok(response)
5762
}
5863

syndterm/tests/integration.rs

+27-6
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,47 @@
22
mod test {
33
mod helper;
44

5+
use std::time::Duration;
6+
57
use crossterm::event::{Event, KeyCode, KeyEvent};
6-
use futures_util::stream;
78
use ratatui::{
89
prelude::Buffer,
910
style::{Modifier, Style},
1011
};
1112
use serial_test::file_serial;
12-
use syndterm::{application::Application, client::Client, ui::theme::Theme};
13+
use syndterm::{
14+
application::{Application, Config},
15+
client::Client,
16+
ui::theme::Theme,
17+
};
18+
use tokio_stream::wrappers::UnboundedReceiverStream;
19+
use tracing::Level;
1320

1421
#[tokio::test(flavor = "multi_thread")]
1522
#[file_serial(a)]
1623
async fn hello_world() -> anyhow::Result<()> {
1724
// TODO: wrap once
18-
tracing_subscriber::fmt::init();
25+
tracing_subscriber::fmt()
26+
.with_max_level(Level::DEBUG)
27+
.with_line_number(true)
28+
.with_file(true)
29+
.init();
1930

2031
tracing::info!("TEST hello_world run");
2132

2233
let endpoint = "http://localhost:5960".parse().unwrap();
2334
let terminal = helper::new_test_terminal();
2435
let client = Client::new(endpoint).unwrap();
25-
let mut application = Application::new(terminal, client);
26-
let _event = Event::Key(KeyEvent::from(KeyCode::Char('j')));
36+
let config = Config {
37+
idle_timer_interval: Duration::from_millis(2000),
38+
};
2739
// or mpsc and tokio_stream ReceiverStream
28-
let mut event_stream = stream::iter(vec![]);
40+
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
41+
let mut event_stream = UnboundedReceiverStream::new(rx);
2942
let theme = Theme::new();
3043
let bg = theme.background.bg.unwrap_or_default();
3144

45+
let mut application = Application::with(terminal, client, config);
3246
application.event_loop_until_idle(&mut event_stream).await;
3347

3448
// login
@@ -74,6 +88,13 @@ mod test {
7488

7589
application.assert_buffer(&expected);
7690

91+
tracing::info!("Login assertion OK");
92+
93+
// push enter => start auth flow
94+
let event = Event::Key(KeyEvent::from(KeyCode::Enter));
95+
tx.send(Ok(event)).unwrap();
96+
application.event_loop_until_idle(&mut event_stream).await;
97+
7798
Ok(())
7899
}
79100
}

0 commit comments

Comments
 (0)