repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

wezterm/tests/modules_test.lua

Raw
File outline
package.path = 'wezterm/?.lua;wezterm/?/init.lua;' .. package.path

local niri_socket = nil
local getenv = os.getenv
os.getenv = function ( name )
  if name == 'NIRI_SOCKET' then return niri_socket end
  return getenv( name )
end

local background_calls = {}
local child_calls = {}
local battery_rows = {}
local worktree_output = [[
worktree /home/test/Workspace/project
HEAD 1111111111111111111111111111111111111111
branch refs/heads/trunk

worktree /home/test/Workspace/project-fix
HEAD 2222222222222222222222222222222222222222
detached
]]
local instance_rows = {
  {
    gui_pid = 99,
    niri_window_id = 12,
    pane_id = 7,
    workspace = 'remote',
    title = 'π - remote task',
  },
}

local wezterm = {
  action = setmetatable( {}, {
    __index = function ( _, name )
      return function ( value ) return { kind = name, value = value, } end
    end,
  } ),
  mux = {
    all_windows = function () return {} end,
    get_workspace_names = function () return { 'local', } end,
  },
  procinfo = { pid = function () return 42 end, },
  config_dir = '/tmp/wezterm-test',
  home_dir = '/home/test',
  hostname = function () return 'test' end,
  target_triple = 'x86_64-unknown-linux-gnu',
  truncate_right = function ( value ) return value end,
  format = function ( items )
    local text = ''
    for _, item in ipairs( items ) do
      if item.Foreground then text = text .. '<' .. item.Foreground.Color .. '>' end
      text = text .. (item.Text or '')
    end
    return text
  end,
  battery_info = function () return battery_rows end,
  background_child_process = function ( args ) table.insert( background_calls, args ) end,
  action_callback = function ( fn ) return fn end,
  json_parse = function () return instance_rows end,
  run_child_process = function ( args )
    table.insert( child_calls, args )
    if args[1] == 'nu' then return true, '[]', '' end
    if args[1] == 'git' then return true, worktree_output, '' end
    return false, '', ''
  end,
  read_dir = function () return {} end,
  split_by_newlines = function ( value )
    local lines = {}
    for line in value:gmatch( '[^\r\n]+' ) do table.insert( lines, line ) end
    return lines
  end,
  log_error = function () end,
}

package.preload.wezterm = function () return wezterm end

local agent = require 'bugabinga.agent'
local battery = require 'bugabinga.battery'
local configure_options = require 'bugabinga.options'
local configure_window = require 'bugabinga.window'
local presentation_mode = require 'bugabinga.presentation_mode'
local workspace_switcher = require 'bugabinga.workspace_switcher'

local function equal( actual, expected, message )
  if actual ~= expected then
    error( string.format( '%s: expected %q, got %q', message, tostring( expected ), tostring( actual ) ) )
  end
end

local function contains( value, expected, message )
  if not value:find( expected, 1, true ) then
    error( string.format( '%s: expected %q in %q', message, expected, value ) )
  end
end

local function capture_selector( action, pane )
  local selector = nil
  local window = {
    perform_action = function ( _, performed ) selector = performed.value end,
    toast_notification = function () end,
  }
  action( window, pane or {} )
  return selector, window
end

local function public_api( module )
  local names = {}
  for name in pairs( module ) do table.insert( names, name ) end
  table.sort( names )
  return table.concat( names, ',' )
end

local glyph, progress_state = agent.badge 'Indeterminate'
equal( glyph, '◉', 'indeterminate progress badge' )
equal( progress_state, 'working', 'indeterminate progress state' )
glyph, progress_state = agent.badge { Error = 12, }
equal( glyph, '✗', 'error progress badge' )
equal( progress_state, 'error', 'error progress state' )
glyph, progress_state = agent.badge 'None'
equal( glyph, nil, 'cleared progress badge' )
equal( progress_state, 'idle', 'cleared progress state' )
local battery_fixtures = {
  { state = 'Full', state_of_charge = 1, glyph = '󰁹 ', role = 'accent', },
  { state = 'Empty', state_of_charge = 0, glyph = '󱃍 ', role = 'error', },
  { state = 'Unknown', state_of_charge = 0.75, glyph = '󱈑 ', role = 'accent', },
  { state = 'Unknown', state_of_charge = 0.25, glyph = '󰂑 ', role = 'minor', },
  { state = 'Charging', state_of_charge = 0, glyph = '󰢟 ', role = 'error', },
  { state = 'Charging', state_of_charge = 0.1, glyph = '󰢟 ', role = 'error', },
  { state = 'Charging', state_of_charge = 0.1001, glyph = '󰢜 ', role = 'warning', },
  { state = 'Charging', state_of_charge = 0.25, glyph = '󰂆 ', role = 'warning', },
  { state = 'Charging', state_of_charge = 0.2501, glyph = '󰂆 ', role = 'accent', },
  { state = 'Charging', state_of_charge = 0.5, glyph = '󰂈 ', role = 'accent', },
  { state = 'Charging', state_of_charge = 0.75, glyph = '󰢞 ', role = 'accent', },
  { state = 'Charging', state_of_charge = 1, glyph = '󰂋 ', role = 'accent', },
  { state = 'Discharging', state_of_charge = 0, glyph = '󰂎 ', role = 'error', },
  { state = 'Discharging', state_of_charge = 0.1, glyph = '󰂎 ', role = 'error', },
  { state = 'Discharging', state_of_charge = 0.1001, glyph = '󰁺 ', role = 'warning', },
  { state = 'Discharging', state_of_charge = 0.25, glyph = '󰁻 ', role = 'warning', },
  { state = 'Discharging', state_of_charge = 0.2501, glyph = '󰁻 ', role = 'accent', },
  { state = 'Discharging', state_of_charge = 0.5, glyph = '󰁽 ', role = 'accent', },
  { state = 'Discharging', state_of_charge = 0.75, glyph = '󰂀 ', role = 'accent', },
  { state = 'Discharging', state_of_charge = 1, glyph = '󰂂 ', role = 'accent', },
}
for _, theme in ipairs { 'dark', 'light', 'dark', } do
  local palette = require( 'bugabinga.nugu.palette_' .. theme ).palette
  local expected = {}
  for _, fixture in ipairs( battery_fixtures ) do
    battery_rows = { fixture, }
    local color = palette[fixture.role] or palette.ui[fixture.role]
    local icon = '<' .. color .. '>' .. fixture.glyph
    equal( battery.get_battery_icons( palette ), icon, theme .. ' ' .. fixture.state .. ' ' .. fixture.state_of_charge )
    table.insert( expected, icon )
  end
  battery_rows = battery_fixtures
  equal( battery.get_battery_icons( palette ), table.concat( expected, ' | ' ), theme .. ' multiple batteries' )
  battery_rows = {}
  equal( battery.get_battery_icons( palette ), '', theme .. ' no batteries' )
end

local function pane( id, title, process, progress )
  return {
    pane_id = function () return id end,
    get_title = function () return title end,
    get_foreground_process_name = function () return process end,
    get_progress = function () return progress end,
  }
end

local idle_pi = pane( 3, 'π - idle', '/usr/bin/node', 'None' )
local working_codex = pane( 2, 'codex', '/usr/bin/codex', 'Indeterminate' )
local failed_claude = pane( 1, 'Claude Code', '/usr/bin/claude', { Error = 1, } )
local plain_shell = pane( 4, 'grep claude history', '/usr/bin/nu', 'None' )
local fake_tab = {
  panes_with_info = function ()
    return {
      { pane = idle_pi, },
      { pane = working_codex, },
      { pane = failed_claude, },
      { pane = plain_shell, },
    }
  end,
}
local fake_window = {
  get_workspace = function () return 'dotfiles' end,
  tabs = function () return { fake_tab, } end,
}

niri_socket = nil
wezterm.mux.all_windows = function () return { fake_window, } end
local local_agents = capture_selector( agent.switcher() )
equal( #local_agents.choices, 3, 'agent count' )
contains( local_agents.choices[1].label, 'Claude', 'failed agent priority' )
contains( local_agents.choices[2].label, 'Codex', 'working agent priority' )
contains( local_agents.choices[3].label, 'Pi', 'idle agent priority' )

local cwd_pane = {
  get_current_working_dir = function () return { file_path = '/home/test/Workspace/project', } end,
}
local worktree_selector = capture_selector( workspace_switcher.switch_workspace(), cwd_pane )
equal( #worktree_selector.choices, 3, 'local workspace and worktree count' )
equal( worktree_selector.choices[2].id, '/home/test/Workspace/project', 'main worktree target' )
contains( worktree_selector.choices[2].label, 'trunk', 'main worktree branch' )
equal( worktree_selector.choices[3].id, '/home/test/Workspace/project-fix', 'linked worktree target' )
contains( worktree_selector.choices[3].label, 'detached', 'linked worktree state' )

niri_socket = '/test/niri.sock'
wezterm.mux.all_windows = function () error 'external inventory must not require local mux windows' end
local remote_agents = capture_selector( agent.switcher() )
equal( #remote_agents.choices, 1, 'remote agent count' )
equal( remote_agents.choices[1].id, 'niri:99:7', 'remote agent target' )
remote_agents.action( {}, {}, 'niri:99:7' )
local remote_activation = child_calls[#child_calls]
equal( remote_activation[4], 'activate', 'remote agent activation command' )

local selector = nil
local fake_gui = {
  perform_action = function ( _, action ) selector = action.value end,
}
local fake_pane = {}
workspace_switcher.switch_workspace()( fake_gui, fake_pane )
equal( #selector.choices, 2, 'local and external workspace count' )
equal( selector.choices[2].id, 'niri:99:7', 'external workspace target' )
selector.action( fake_gui, fake_pane, 'niri:99:7' )
local activation = child_calls[#child_calls]
equal( activation[4], 'activate', 'external workspace activation command' )
equal( activation[5], '99', 'external workspace GUI PID' )
equal( activation[6], '7', 'external workspace pane ID' )

local new_window_selector = capture_selector( workspace_switcher.switch_workspace_in_new_window(), cwd_pane )
new_window_selector.action( {}, {}, 'local' )
local background_spawn = background_calls[#background_calls]
equal( background_spawn[2], 'start', 'new workspace window executable action' )
equal( background_spawn[3], '--always-new-process', 'new workspace window process isolation' )
equal( background_spawn[5], 'local', 'new workspace window target' )

local options_config = {}
configure_options( options_config )
equal( options_config.default_cwd, nil, 'native default cwd' )
equal( options_config.enable_wayland, nil, 'native Wayland default' )
equal( options_config.front_end, nil, 'native OpenGL front end' )

local window_config = {}
configure_window( window_config )
equal( window_config.window_decorations, 'RESIZE', 'titlebar-free resizable window' )

local overrides = nil
local fullscreen_toggles = 0
local presentation_window = {
  window_id = function () return 11 end,
  effective_config = function ()
    return {
      font = { font = { { weight = 'Regular', }, }, },
      font_size = 13,
    }
  end,
  get_config_overrides = function () return overrides end,
  set_config_overrides = function ( _, value ) overrides = value end,
  toggle_fullscreen = function () fullscreen_toggles = fullscreen_toggles + 1 end,
}
presentation_mode.toggle( presentation_window )
equal( overrides.font.font[1].weight, 'DemiBold', 'presentation font weight' )
equal( overrides.font_size, 26, 'presentation font size' )
equal( fullscreen_toggles, 1, 'presentation fullscreen enable' )
presentation_mode.toggle( presentation_window )
equal( overrides.font.font[1].weight, 'Regular', 'restored font weight' )
equal( overrides.font_size, 13, 'restored font size' )
equal( fullscreen_toggles, 2, 'presentation fullscreen disable' )

equal( public_api( agent ), 'badge,switcher,write_snapshot', 'agent public API' )
equal( public_api( battery ), 'get_battery_icons', 'battery public API' )
equal(
  public_api( workspace_switcher ),
  'switch_workspace,switch_workspace_in_new_window',
  'workspace public API'
)

print 'wezterm module checks passed'