Skip to content

Commit b561883

Browse files
committed
fix: await send_message chain to eliminate flaky user_message_count test
send_message fired off the .and_then/.catch chain without awaiting it, causing the user_message_count decrement to be deferred via vim.schedule_wrap. The test relied on vim.wait(50ms) polling which was insufficient on macOS CI runners. Await the chain inside send_message (already Promise.async) so the promise reflects the full operation lifecycle, and replace the polling loops in tests with deterministic :wait() calls.
1 parent 1e1fd08 commit b561883

2 files changed

Lines changed: 6 additions & 13 deletions

File tree

lua/opencode/services/messaging.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ M.send_message = Promise.async(function(prompt, opts)
8383
update_sent_message_count(-1)
8484
session_runtime.cancel():await()
8585
end)
86+
:await()
8687
end)
8788

8889
---@param prompt string

tests/unit/services_messaging_spec.lua

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ describe('opencode.services.messaging', function()
7878

7979
local count_before = state.user_message_count['sess1'] or 0
8080
local count_during = nil
81-
local count_after = nil
8281

8382
local orig = state.api_client.create_message
8483
state.api_client.create_message = function(_, sid, params)
@@ -90,12 +89,9 @@ describe('opencode.services.messaging', function()
9089
})
9190
end
9291

93-
messaging.send_message('hello world')
92+
messaging.send_message('hello world'):wait()
9493

95-
vim.wait(50, function()
96-
count_after = state.user_message_count['sess1'] or 0
97-
return count_after == 0
98-
end)
94+
local count_after = state.user_message_count['sess1'] or 0
9995

10096
assert.equal(0, count_before)
10197
assert.equal(1, count_during)
@@ -111,7 +107,6 @@ describe('opencode.services.messaging', function()
111107

112108
local count_before = state.user_message_count['sess1'] or 0
113109
local count_during = nil
114-
local count_after = nil
115110

116111
local orig = state.api_client.create_message
117112
state.api_client.create_message = function(_, sid, params)
@@ -120,14 +115,11 @@ describe('opencode.services.messaging', function()
120115
end
121116

122117
local orig_cancel = session_runtime.cancel
123-
stub(session_runtime, 'cancel')
118+
stub(session_runtime, 'cancel').returns(Promise.new():resolve(nil))
124119

125-
messaging.send_message('hello world')
120+
messaging.send_message('hello world'):wait()
126121

127-
vim.wait(50, function()
128-
count_after = state.user_message_count['sess1'] or 0
129-
return count_after == 0
130-
end)
122+
local count_after = state.user_message_count['sess1'] or 0
131123

132124
assert.equal(0, count_before)
133125
assert.equal(1, count_during)

0 commit comments

Comments
 (0)