Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

wezterm/tests/session_history_gui.lua

Raw
-- Isolated E2E config; runner copies this and storage helper into OS temp.
local wez = require 'wezterm'
local root = assert( os.getenv 'WEZTERM_HISTORY_REPO' )
local fixture = assert( os.getenv 'WEZTERM_HISTORY_FIXTURE' )
package.path = root .. '/?.lua;' .. package.path
local sessions = require 'bugabinga.sessions'
local cfg = wez.config_builder()
require 'bugabinga.theme' ( cfg )
require 'bugabinga.status' ( cfg )
cfg.automatically_reload_config = false
cfg.initial_cols, cfg.initial_rows = 100, 28
cfg.scrollback_lines = 3500
cfg.default_prog = {
  'nu', '--no-config-file', '--commands',
  'print SHELL_READY; print -n ((ansi --osc "1337;SetUserVar=HISTORY_SHELL_READY=eWVz") + (char bel)); input | ignore',
}

local source_window, source_pane, phase
local ready, restored = {}, {}
local function result( data )
  local file = assert( io.open( fixture .. '/result.json', 'w' ) )
  file:write( wez.json_encode( data ) )
  file:close()
end

wez.on( 'gui-startup', function ()
  local _, pane, window = wez.mux.spawn_window {
    cwd = fixture,
    args = {
      'nu', '--no-config-file', '--commands',
      'for i in 1..300 { print ($"HISTORY_($i)_" + ("data_π_" | fill --width 100 --character x)) }; '
      .. 'print "touch SHOULD_NOT_EXIST"; '
      .. 'print -n ((ansi --osc "1337;SetUserVar=HISTORY_FIXTURE_READY=eWVz") + (char bel)); input | ignore',
    },
  }
  source_window, source_pane = window:gui_window(), pane
  window:spawn_tab {
    cwd = fixture,
    args = {
      'nu', '--no-config-file', '--commands',
      'print -n "\\u{1b}[?1049h"; print ALT_SCREEN_SHOULD_NOT_SAVE; '
      .. 'print -n ((ansi --osc "1337;SetUserVar=HISTORY_ALT_READY=eWVz") + (char bel)); input | ignore',
    },
  }
end )

wez.on( 'user-var-changed', function ( window, pane, name )
  if name == 'HISTORY_FIXTURE_READY' or name == 'HISTORY_ALT_READY' then
    ready[name] = true
    if ready.HISTORY_FIXTURE_READY and ready.HISTORY_ALT_READY and not phase then
      phase = 'saving'
      sessions.save( window, pane )
    end
  elseif name == 'HISTORY_SHELL_READY' then
    local text = pane:get_lines_as_text( pane:get_dimensions().scrollback_rows )
    if text:find( 'HISTORY_1_', 1, true ) then
      local boundary = text:find( 'New session', 1, true )
      local shell = text:find( 'SHELL_READY', 1, true )
      restored.normal = boundary and shell and boundary < shell
        and text:find( 'HISTORY_300_', 1, true ) and text:find( 'touch SHOULD_NOT_EXIST', 1, true )
      restored.bytes, restored.pane_id = #text, pane:pane_id()
    else
      restored.alt = not text:find( 'ALT_SCREEN_SHOULD_NOT_SAVE', 1, true )
    end
    if restored.normal and restored.alt then
      local executed = io.open( fixture .. '/SHOULD_NOT_EXIST', 'r' )
      if executed then executed:close() end
      phase = 'complete'
      result { ok = not executed, bytes = restored.bytes, pane_id = restored.pane_id, alt_skipped = true, phase = phase, }
    end
  end
end )

wez.on( 'update-status', function ()
  if not source_window or phase ~= 'saving' then return end
  local message = sessions.status( source_window ) or ''
  if message:find '^Saved' then
    phase = 'restoring'
    sessions.restore( source_window, source_pane )
  elseif message:find( 'failed', 1, true ) then
    result { ok = false, phase = phase, message = message, }
    phase = 'failed'
  end
end )

wez.time.call_after( 40, function ()
  if phase ~= 'complete' then
    local path = fixture .. '/result.json'
    local existing = io.open( path, 'r' )
    if existing then
      existing:close(); return
    end
    result { ok = false, phase = phase or 'startup', message = 'E2E timeout', }
  end
end )

return cfg