/ tests / spec / wezterm_backdrops_spec.lua
wezterm_backdrops_spec.lua
 1  -- Busted spec for WezTerm Backdrops (implements wezterm_backdrops.feature scenarios).
 2  -- Run with: busted tests/spec/wezterm_backdrops_spec.lua
 3  local repo_root = os.getenv('DOTFILES_HOME') or (os.getenv('HOME') and (os.getenv('HOME') .. '/.dotfiles')) or '.'
 4  local wezterm_path = repo_root .. '/wezterm/?.lua'
 5  
 6  -- Pure helper: cycle index forward (1-based, wraps)
 7  local function cycle_forward(current, n)
 8    if current == n then return 1 end
 9    return current + 1
10  end
11  
12  local function cycle_back(current, n)
13    if current == 1 then return n end
14    return current - 1
15  end
16  
17  describe('WezTerm Backdrops (pure index logic)', function()
18    it('Scenario: Cycle forward wraps from last to first', function()
19      local n = 3
20      local idx = cycle_forward(3, n)
21      assert.is_equal(1, idx)
22    end)
23  
24    it('Scenario: Cycle back wraps from first to last', function()
25      local n = 3
26      local idx = cycle_back(1, n)
27      assert.is_equal(3, idx)
28    end)
29  
30    it('Scenario: Set image at valid index', function()
31      local files = { 'a.png', 'b.png' }
32      local idx = 2
33      assert.is_equal('b.png', files[idx])
34    end)
35  
36    it('Scenario: Index out of range is invalid', function()
37      local files = { 'a.png' }
38      local idx = 5
39      assert.is_nil(files[idx])
40      assert.is_true(idx > #files)
41    end)
42  end)
43  
44  -- Optional: load real BackDrops with mocks (requires colors.custom mock)
45  describe('WezTerm Backdrops (module)', function()
46    local BackDrops
47  
48    before_each(function()
49      package.loaded['wezterm'] = {
50        GLOBAL = {},
51        config_dir = repo_root .. '/wezterm',
52        read_dir = function() return { 'a.png', 'b.png', 'c.png' } end,
53        log_error = function() end,
54      }
55      package.loaded['colors.custom'] = { background = '#1e1e2e' }
56      package.path = package.path .. ';' .. wezterm_path
57      package.loaded['extensions.backdrops'] = nil
58    end)
59  
60    after_each(function()
61      package.loaded['wezterm'] = nil
62      package.loaded['colors.custom'] = nil
63      package.loaded['extensions.backdrops'] = nil
64    end)
65  
66    it('loads and set_files populates files', function()
67      local ok, mod = pcall(require, 'extensions.backdrops')
68      if not ok then
69        pending('extensions.backdrops not loadable from test path: ' .. tostring(mod))
70        return
71      end
72      mod:set_files()
73      assert.is_equal(3, #mod.files)
74      assert.is_equal(1, mod.current_idx)
75    end)
76  end)