local format = require 'bugabinga.cmdline.format' describe('format.detect', function() it('detects : as cmdline', function() local result = format.detect(':', 'hello', '') assert.equals('cmdline', result.type) assert.equals('', result.icon) end) it('detects / as search_down', function() local result = format.detect('/', 'hello', '') assert.equals('search_down', result.type) assert.equals('', result.icon) end) it('detects ? as search_up', function() local result = format.detect('?', 'hello', '') assert.equals('search_up', result.type) assert.equals('', result.icon) end) it('detects ! as shell', function() local result = format.detect('!', 'ls -la', '') assert.equals('shell', result.type) assert.equals('', result.icon) end) it('detects :help as help', function() local result = format.detect(':', 'help mytopic', '') assert.equals('help', result.type) assert.equals('󰋖', result.icon) end) it('detects :h as help', function() local result = format.detect(':', 'h', '') assert.equals('help', result.type) assert.equals('󰋖', result.icon) end) it('detects :he as help', function() local result = format.detect(':', 'he', '') assert.equals('help', result.type) assert.equals('󰋖', result.icon) end) it('detects :hel as help', function() local result = format.detect(':', 'hel', '') assert.equals('help', result.type) assert.equals('󰋖', result.icon) end) it('detects :help alone as help', function() local result = format.detect(':', 'help', '') assert.equals('help', result.type) assert.equals('󰋖', result.icon) end) it('detects :h with topic as help', function() local result = format.detect(':', 'h usr_01', '') assert.equals('help', result.type) assert.equals('󰋖', result.icon) end) it('detects :!cmd as shell', function() local result = format.detect(':', '!ls', '') assert.equals('shell', result.type) assert.equals('', result.icon) end) it('detects :lua as lua', function() local result = format.detect(':', 'lua print("hi")', '') assert.equals('lua', result.type) assert.equals('', result.icon) end) it('detects :lua alone as lua', function() local result = format.detect(':', 'lua', '') assert.equals('lua', result.type) assert.equals('', result.icon) end) it('detects := as lua', function() local result = format.detect(':', '= { foo = 1 }', '') assert.equals('lua', result.type) assert.equals('', result.icon) end) it('does not detect :luafile as lua', function() local result = format.detect(':', 'luafile', '') assert.equals('cmdline', result.type) end) it('does not detect :luado as lua', function() local result = format.detect(':', 'luado', '') assert.equals('cmdline', result.type) end) it('detects input when prompt is non-empty', function() local result = format.detect(':', 'some input', 'Enter name: ') assert.equals('input', result.type) assert.equals('', result.icon) end) it('returns cmdline for unknown firstc', function() local result = format.detect('-', 'some text', '') assert.equals('cmdline', result.type) end) it('returns input for empty firstc and prompt', function() local result = format.detect('', 'some text', '') assert.equals('input', result.type) end) it('handles nil content gracefully', function() local result = format.detect(':', nil, '') assert.equals('cmdline', result.type) end) end) describe('format.prompt_lines', function() it('returns empty table for nil prompt', function() local result = format.prompt_lines(nil, 80) assert.equals(0, #result) end) it('returns empty table for empty prompt', function() local result = format.prompt_lines('', 80) assert.equals(0, #result) end) it('returns single entry for short prompt', function() local result = format.prompt_lines('Enter name: ', 80) assert.equals(1, #result) assert.equals('Enter name: ', result[1]) end) it('returns single entry when prompt fits exactly at max_width', function() local prompt = 'abc' local max_w = 3 local result = format.prompt_lines(prompt, max_w) assert.equals(1, #result) assert.equals('abc', result[1]) end) it('splits on newline', function() local result = format.prompt_lines('line 1\nline 2', 80) assert.equals(2, #result) assert.equals('line 1', result[1]) assert.equals('line 2', result[2]) end) it('double newline produces blank line', function() local result = format.prompt_lines('a\n\nb', 80) assert.equals(3, #result) assert.equals('a', result[1]) assert.equals('', result[2]) assert.equals('b', result[3]) end) it('trailing newline is stripped', function() local result = format.prompt_lines('hello\n', 80) assert.equals(1, #result) assert.equals('hello', result[1]) end) it('wraps long line at word boundary', function() local long = 'the quick brown fox jumps over the lazy dog' local max_w = 20 local result = format.prompt_lines(long, max_w) for _, line in ipairs(result) do assert.is_true(vim.fn.strdisplaywidth(line) <= max_w, 'line exceeds max_width: ' .. line) end local concatenated = table.concat(result, ' ') assert.equals(long, concatenated) end) it('hard-breaks single word exceeding max_width', function() local long = 'supercalifragilisticexpialidocious' local max_w = 10 local result = format.prompt_lines(long, max_w) assert.is_true(#result > 1, 'single long word should be broken into multiple pieces') for _, line in ipairs(result) do assert.is_true(vim.fn.strdisplaywidth(line) <= max_w, 'line exceeds max_width: ' .. line) end end) it('no wrapping when prompt shorter than max_width', function() local result = format.prompt_lines('short', 80) assert.equals(1, #result) assert.equals('short', result[1]) end) end) describe('format.PROMPT_MAX_RATIO', function() it('is 0.85', function() assert.equals(0.85, format.PROMPT_MAX_RATIO) end) end)