forked from coder/claudecode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger_spec.lua
More file actions
175 lines (140 loc) · 5.11 KB
/
logger_spec.lua
File metadata and controls
175 lines (140 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
-- luacheck: globals expect
require("tests.busted_setup")
describe("Logger", function()
local logger
local original_vim_schedule
local original_vim_notify
local original_nvim_echo
local scheduled_calls = {}
local notify_calls = {}
local echo_calls = {}
local function setup()
package.loaded["claudecode.logger"] = nil
-- Mock vim.schedule to track calls
original_vim_schedule = vim.schedule
vim.schedule = function(fn)
table.insert(scheduled_calls, fn)
-- Immediately execute the function for testing
fn()
end
-- Mock vim.notify to track calls
original_vim_notify = vim.notify
vim.notify = function(msg, level, opts)
table.insert(notify_calls, { msg = msg, level = level, opts = opts })
end
-- Mock nvim_echo to track calls
original_nvim_echo = vim.api.nvim_echo
vim.api.nvim_echo = function(chunks, history, opts)
table.insert(echo_calls, { chunks = chunks, history = history, opts = opts })
end
logger = require("claudecode.logger")
-- Set log level to TRACE to enable all logging levels for testing
logger.setup({ log_level = "trace" })
end
local function teardown()
vim.schedule = original_vim_schedule
vim.notify = original_vim_notify
vim.api.nvim_echo = original_nvim_echo
scheduled_calls = {}
notify_calls = {}
echo_calls = {}
end
before_each(function()
setup()
end)
after_each(function()
teardown()
end)
describe("error logging", function()
it("should wrap error calls in vim.schedule", function()
logger.error("test", "error message")
-- Should have made one scheduled call
expect(#scheduled_calls).to_be(1)
-- Should have called vim.notify with error level
expect(#notify_calls).to_be(1)
expect(notify_calls[1].level).to_be(vim.log.levels.ERROR)
assert_contains(notify_calls[1].msg, "error message")
end)
it("should handle error calls without component", function()
logger.error("error message")
expect(#scheduled_calls).to_be(1)
expect(#notify_calls).to_be(1)
assert_contains(notify_calls[1].msg, "error message")
end)
end)
describe("warn logging", function()
it("should wrap warn calls in vim.schedule", function()
logger.warn("test", "warning message")
-- Should have made one scheduled call
expect(#scheduled_calls).to_be(1)
-- Should have called vim.notify with warn level
expect(#notify_calls).to_be(1)
expect(notify_calls[1].level).to_be(vim.log.levels.WARN)
assert_contains(notify_calls[1].msg, "warning message")
end)
it("should handle warn calls without component", function()
logger.warn("warning message")
expect(#scheduled_calls).to_be(1)
expect(#notify_calls).to_be(1)
assert_contains(notify_calls[1].msg, "warning message")
end)
end)
describe("info logging", function()
it("should wrap info calls in vim.schedule", function()
logger.info("test", "info message")
-- Should have made one scheduled call
expect(#scheduled_calls).to_be(1)
-- Should have called nvim_echo instead of notify
expect(#echo_calls).to_be(1)
expect(#notify_calls).to_be(0)
assert_contains(echo_calls[1].chunks[1][1], "info message")
end)
end)
describe("debug logging", function()
it("should wrap debug calls in vim.schedule", function()
logger.debug("test", "debug message")
-- Should have made one scheduled call
expect(#scheduled_calls).to_be(1)
-- Should have called nvim_echo instead of notify
expect(#echo_calls).to_be(1)
expect(#notify_calls).to_be(0)
assert_contains(echo_calls[1].chunks[1][1], "debug message")
end)
end)
describe("trace logging", function()
it("should wrap trace calls in vim.schedule", function()
logger.trace("test", "trace message")
-- Should have made one scheduled call
expect(#scheduled_calls).to_be(1)
-- Should have called nvim_echo instead of notify
expect(#echo_calls).to_be(1)
expect(#notify_calls).to_be(0)
assert_contains(echo_calls[1].chunks[1][1], "trace message")
end)
end)
describe("fast event context safety", function()
it("should not call vim API functions directly", function()
-- Simulate a fast event context by removing the mocked functions
-- and ensuring no direct calls are made
local direct_notify_called = false
local direct_echo_called = false
vim.notify = function()
direct_notify_called = true
end
vim.api.nvim_echo = function()
direct_echo_called = true
end
vim.schedule = function(fn)
-- Don't execute the function, just verify it was scheduled
table.insert(scheduled_calls, fn)
end
logger.error("test", "error in fast context")
logger.warn("test", "warn in fast context")
logger.info("test", "info in fast context")
-- All should be scheduled, none should be called directly
expect(#scheduled_calls).to_be(3)
expect(direct_notify_called).to_be_false()
expect(direct_echo_called).to_be_false()
end)
end)
end)