Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

wezterm/tests/wezterm_instances_test.nu

Raw
def check [condition: bool, message: string] {
  if not $condition { error make { msg: $message } }
}

let helper = ($env.FILE_PWD | path join '..' 'bugabinga' 'wezterm_instances.nu')
check (nu-check $helper) 'instance helper must parse'

let invalid = (^nu --no-config-file $helper unknown | complete)
check ($invalid.exit_code != 0) 'unknown helper commands must fail'

if $env.NIRI_SOCKET? == null {
  print 'wezterm instance checks skipped outside Niri'
  exit 0
}

let result = (^nu --no-config-file $helper list | complete)
check ($result.exit_code == 0) $"instance listing failed: ($result.stderr | str trim)"
let rows = (try { $result.stdout | from json } catch { null })
check ($rows != null) 'instance listing must emit JSON'
check (($rows | length) > 0) 'instance listing must find the current Niri WezTerm window'

let niri_pids = (^niri msg --json windows | from json | get pid | compact)
check ($rows | all {|row|
  (($row.gui_pid in $niri_pids)
    and (($row.pane_id | describe) =~ '^int')
    and (($row.niri_window_id | describe) =~ '^int'))
}) 'every listed pane must map to a Niri window'

let fixture = (^mktemp -d | str trim)
let failure = (try {
  source ../bugabinga/wezterm_instances.nu
  let runtime = ($fixture | path join runtime)
  let directory = ($runtime | path join wezterm)
  let snapshot = ($directory | path join instances-42.json)
  let window = { pid: 42, id: 7, workspace_id: 2, title: 'fixture', }

  with-env { XDG_RUNTIME_DIR: $runtime } {
    check (window-panes $window | is-empty) 'absent instance snapshot is valid'
    mkdir $directory
    '{"panes":[]}' | save $snapshot
    check (window-panes $window | is-empty) 'empty instance snapshot is valid'

    '{"panes":[{"pane_id":3,"workspace":"work","title":"agent"}]}' | save --force $snapshot
    let pane = (window-panes $window | first)
    check ($pane.gui_pid == 42 and $pane.niri_window_id == 7 and $pane.pane_id == 3) 'snapshot pane mapping'

    for payload in ['not-json' '{}' '{"panes":[{}]}'] {
      $payload | save --force $snapshot
      let rejected = (try { window-panes $window | ignore; false } catch { true })
      check $rejected 'malformed instance snapshot must fail'
    }
  }
  null
} catch {|error| $error })
rm --recursive $fixture
if $failure != null { error make $failure }

print 'wezterm Niri instance checks passed'