describe( 'std.dbg.init', function () local dbg local dbg_file local mod local TEST_FILE = vim.fn.stdpath 'state' .. '/test_init_spec.jsonl' local function read_debug_file(path) local file = io.open(path, 'r') if not file then return nil end local content = file:read('*a') file:close() if not content or content == '' then return nil end return content end local function require_debug_file(path) return assert(read_debug_file(path)) end before_each( function () package.loaded['std.dbg'] = nil package.loaded['std.dbg.init'] = nil package.loaded['std.dbg.file'] = nil vim.g.bugabinga_debug_file_override = TEST_FILE mod = require 'std.dbg.file' dbg_file = TEST_FILE vim.fn.delete(dbg_file) dbg = require 'std.dbg' vim.g.bugabinga_debug_mode = false end ) after_each( function () vim.g.bugabinga_debug_mode = false vim.g.bugabinga_debug_file_override = nil vim.fn.delete(dbg_file) end ) describe( 'dbg(...) __call', function () it( 'should be callable (no error when called)', function () vim.g.bugabinga_debug_mode = false local ok, err = pcall( function () dbg 'test' end ) assert.is_true( ok ) end ) it( 'should be no-op when debug mode is off', function () vim.g.bugabinga_debug_mode = false dbg 'test message' local content = read_debug_file(dbg_file) assert.is_nil( content ) end ) it( 'should write event when debug mode is on', function () vim.g.bugabinga_debug_mode = true dbg( 'test {1}', 'hello' ) local content = require_debug_file(dbg_file) assert.is_not_nil( content:match '"kind"' ) assert.is_not_nil( content:match '"message"' ) assert.is_not_nil( content:match '"data"' ) end ) it( 'should write event with date as os.time() integer', function () vim.g.bugabinga_debug_mode = true dbg 'test' local content = require_debug_file(dbg_file) local json = vim.json.decode( content ) assert.is_number( json.date ) end ) it( 'should have all required fields in written event', function () vim.g.bugabinga_debug_mode = true dbg( 'test {1}', 'y' ) local content = require_debug_file(dbg_file) local json = vim.json.decode( content ) assert.equal( 'DEBUG', json.kind ) assert.equal( 'test y', json.message ) assert.is_table( json.data ) assert.is_string( json.filename ) assert.is_string( json.fn ) assert.is_number( json.line ) end ) end ) describe( 'dbg.kind()', function () it( 'should be a function', function () assert.is_function( dbg.kind ) end ) it( 'should log with specified kind', function () vim.g.bugabinga_debug_mode = true dbg.kind( 'ERROR', 'error message' ) local content = require_debug_file(dbg_file) local json = vim.json.decode( content ) assert.equal( 'ERROR', json.kind ) end ) it( 'should log TRACE kind', function () vim.g.bugabinga_debug_mode = true dbg.kind( 'TRACE', 'trace message' ) local content = require_debug_file(dbg_file) local json = vim.json.decode( content ) assert.equal( 'TRACE', json.kind ) end ) end ) describe( 'dbg.toggle()', function () it( 'should toggle debug mode from off to on', function () vim.g.bugabinga_debug_mode = false assert.is_false( dbg.get() ) dbg.toggle() assert.is_true( dbg.get() ) end ) it( 'should toggle debug mode from on to off', function () vim.g.bugabinga_debug_mode = true assert.is_true( dbg.get() ) dbg.toggle() assert.is_false( dbg.get() ) end ) end ) describe( 'dbg.get()', function () it( 'should return current debug mode state', function () vim.g.bugabinga_debug_mode = false assert.is_false( dbg.get() ) vim.g.bugabinga_debug_mode = true assert.is_true( dbg.get() ) end ) end ) describe( '__newindex guard', function () it( 'should error on assignment attempt', function () local ok, err = pcall( function () dbg.newfield = 'value' end ) assert.is_false( ok ) assert.is_string( err ) end ) end ) describe( 'user commands', function () it( 'should register :Debug command', function () local ok = pcall( vim.api.nvim_get_command, 'Debug' ) assert.is_true( ok ~= nil ) end ) it( 'should register :DebugOpen command', function () local ok = pcall( vim.api.nvim_get_command, 'DebugOpen' ) assert.is_true( ok ~= nil ) end ) it( 'should register :DebugOpenRaw command', function () local ok = pcall( vim.api.nvim_get_command, 'DebugOpenRaw' ) assert.is_true( ok ~= nil ) end ) end ) end )