wip: test: improve tmux tests

- Fix bugs found during testing.

Co-authored-by: mahmmoud.hassanein <mahmmoud.hassanein@gmail.com>
This commit is contained in:
2025-01-27 18:27:50 +02:00
parent 0ae8e227fc
commit 9f6e49963e
7 changed files with 96 additions and 95 deletions

View File

@@ -1,67 +0,0 @@
module tmux
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.installers.tmux
import freeflowuniverse.herolib.ui.console
// uses single tmux instance for all tests
__global (
tmux Tmux
)
fn init() {
tmux = get_remote('185.69.166.152')!
// reset tmux for tests
if tmux.is_running() {
tmux.stop() or { panic('Cannot stop tmux') }
}
}
fn testsuite_end() {
if tmux.is_running() {
tmux.stop()!
}
}
fn test_window_new() {
tmux.start() or { panic("can't start tmux: ${err}") }
// test window new with only name arg
window_args := WindowArgs{
name: 'TestWindow'
}
assert !tmux.sessions.keys().contains('main')
mut window := tmux.window_new(window_args) or { panic("Can't create new window: ${err}") }
assert tmux.sessions.keys().contains('main')
window.delete() or { panic('Cant delete window') }
}
// // tests creating duplicate windows
// fn test_window_new0() {
//
// installer := tmux.get_install(
// mut tmux := Tmux {
// node: node_ssh
// }
// window_args := WindowArgs {
// name: 'TestWindow0'
// }
// // console.print_debug(tmux)
// mut window := tmux.window_new(window_args) or {
// panic("Can't create new window: $err")
// }
// assert tmux.sessions.keys().contains('main')
// mut window_dup := tmux.window_new(window_args) or {
// panic("Can't create new window: $err")
// }
// console.print_debug(node_ssh.exec('tmux ls') or { panic("fail:$err")})
// window.delete() or { panic("Cant delete window") }
// // console.print_debug(tmux)
// }

View File

@@ -23,20 +23,20 @@ pub fn new(args TmuxNewArgs) !Tmux {
mut t := Tmux{
sessionid: args.sessionid
}
t.load()!
// t.load()!
t.scan()!
return t
}
// loads tmux session, populate the object
pub fn (mut tmux Tmux) load() ! {
isrunning := tmux.is_running()!
if !isrunning {
tmux.start()!
}
// console.print_debug("SCAN")
tmux.scan()!
}
// // loads tmux session, populate the object
// pub fn (mut tmux Tmux) load() ! {
// // isrunning := tmux.is_running()!
// // if !isrunning {
// // tmux.start()!
// // }
// // console.print_debug("SCAN")
// tmux.scan()!
// }
pub fn (mut t Tmux) stop() ! {
$if debug {
@@ -91,19 +91,18 @@ pub fn (mut t Tmux) windows_get() []&Window {
// checks whether tmux server is running
pub fn (mut t Tmux) is_running() !bool {
res := osal.exec(cmd: 'tmux info', stdout: false, name: 'tmux_info', raise_error: false) or {
panic('bug')
}
if res.error.contains('no server running') {
res := os.execute('tmux info')
if res.exit_code != 0 {
if res.output.contains('no server running') {
// console.print_debug(" TMUX NOT RUNNING")
return false
}
if res.error.contains('no current client') {
if res.output.contains('no current client') {
return true
}
if res.exit_code > 0 {
return error('could not execute tmux info.\n${res}')
return error('could not execute tmux info.\n${res.output}')
}
return true
}

View File

@@ -58,6 +58,9 @@ pub fn (mut t Tmux) scan() ! {
cmd_list_session := "tmux list-sessions -F '#{session_name}'"
exec_list := osal.exec(cmd: cmd_list_session, stdout: false, name: 'tmux_list') or {
if err.msg().contains('no server running') {
return
}
return error('could not execute list sessions.\n${err}')
}
@@ -80,7 +83,7 @@ pub fn (mut t Tmux) scan() ! {
}
console.print_debug(t)
println('t: ${t}')
// mut done := map[string]bool{}
cmd := "tmux list-panes -a -F '#{session_name}|#{window_name}|#{window_id}|#{pane_active}|#{pane_id}|#{pane_pid}|#{pane_start_command}'"
out := osal.execute_silent(cmd) or { return error("Can't execute ${cmd} \n${err}") }

View File

@@ -1,7 +1,7 @@
module tmux
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.installers.tmux
// import freeflowuniverse.herolib.installers.tmux
// fn testsuite_end() {
@@ -26,13 +26,13 @@ fn test_session_create() {
mut s := Session{
tmux: &tmux
windows: map[string]&Window{}
windows: []&Window{}
name: 'testsession'
}
mut s2 := Session{
tmux: &tmux
windows: map[string]&Window{}
windows: []&Window{}
name: 'testsession2'
}

View File

@@ -191,7 +191,7 @@ pub fn (mut w Window) stop() ! {
stdout: false
name: 'tmux_kill-window'
// die: false
) or { return error("Can't kill window with id:${w.id}") }
) or { return error("Can't kill window with id:${w.id}: ${err}") }
w.pid = 0
w.active = false
}

View File

@@ -0,0 +1,67 @@
module tmux
import freeflowuniverse.herolib.osal
import freeflowuniverse.herolib.ui.console
import time
// uses single tmux instance for all tests
fn testsuite_begin() {
muttmux := new() or { panic('Cannot create tmux: ${err}') }
// reset tmux for tests
is_running := tmux.is_running() or { panic('cannot check if tmux is running: ${err}') }
if is_running {
tmux.stop() or { panic('Cannot stop tmux: ${err}') }
}
}
fn testsuite_end() {
is_running := is_running() or { panic('cannot check if tmux is running: ${err}') }
if is_running {
stop() or { panic('Cannot stop tmux: ${err}') }
}
}
fn test_window_new() ! {
mut tmux_ := new()!
// test window new with only name arg
window_args := WindowArgs{
name: 'TestWindow'
}
assert tmux_.sessions.filter(it.name == 'main').len == 0
mut window := tmux_.window_new(window_args)!
assert tmux_.sessions.filter(it.name == 'main').len > 0
// time.sleep(1000 * time.millisecond)
// window.stop()!
}
// tests creating duplicate windows
fn test_window_new0() {
installer := tmux.get_install(
mut tmux := Tmux {
node: node_ssh
}
window_args := WindowArgs {
name: 'TestWindow0'
}
// console.print_debug(tmux)
mut window := tmux.window_new(window_args) or {
panic("Can't create new window: $err")
}
assert tmux.sessions.keys().contains('main')
mut window_dup := tmux.window_new(window_args) or {
panic("Can't create new window: $err")
}
console.print_debug(node_ssh.exec('tmux ls') or { panic("fail:$err")})
window.delete() or { panic("Cant delete window") }
// console.print_debug(tmux)
}

View File

@@ -185,7 +185,6 @@ clients/livekit
'
tests_error := '
tmux_session_test.v
tmux_window_test.v
tmux_test.v
startupmanager_test.v