Repository navigation
fix(webhook): inline reply posts through the CAP post path (TASK-015) #2797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lilyshen0722
wants to merge
4
commits into
main
Choose a base branch
from
sprint-review/task-015-webhook-inline-post
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d9e000
fix(webhook): inline reply posts through the CAP post path (TASK-015)
lilyshen0722 fc7ce84
docs(review): rule against tests that re-implement the function under…
lilyshen0722 d1e0469
fix(webhook): the delivered trigger is seen, so an inline reply is no…
lilyshen0722 a7a06a1
test(freshness): pin the PostgreSQL trigger-id bound on unseenMessage…
lilyshen0722 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
169 changes: 169 additions & 0 deletions
169
backend/__tests__/unit/services/agentEventService.webhookInlinePost.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| /** | ||
| * TASK-015 — an inline webhook reply must actually post, and a failed post | ||
| * must not be recorded as posted. | ||
| * | ||
| * The old path called agentMessageService.postAgentMessage, which does not | ||
| * exist, so the TypeError was swallowed and the event still settled as | ||
| * `posted`. These tests drive the real deliverEventViaWebhook (through the | ||
| * static seam), with only the network and persistence edges mocked. | ||
| */ | ||
|
|
||
| jest.mock('../../../models/AgentEvent', () => ({ | ||
| findByIdAndUpdate: jest.fn().mockResolvedValue({}), | ||
| })); | ||
|
|
||
| jest.mock('../../../services/outboundUrlGuard', () => ({ | ||
| postToPublicAddress: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('../../../services/agentMessageService', () => ({ | ||
| postMessage: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('../../../services/agentMentionService', () => ({ | ||
| enqueueMentions: jest.fn().mockResolvedValue(undefined), | ||
| enqueueDmEvent: jest.fn().mockResolvedValue(undefined), | ||
| })); | ||
|
|
||
| jest.mock('../../../services/agentSendFreshness', () => ({ | ||
| unseenMessages: jest.fn().mockResolvedValue(null), | ||
| })); | ||
|
|
||
| jest.mock('../../../models/Pod', () => ({ | ||
| findById: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('../../../services/agentIdentityService', () => ({ | ||
| getOrCreateAgentUser: jest.fn(), | ||
| })); | ||
|
|
||
| jest.mock('../../../models/AgentRegistry', () => ({ | ||
| AgentInstallation: { find: jest.fn(), findOne: jest.fn() }, | ||
| })); | ||
|
|
||
| jest.mock('../../../models/Integration', () => ({})); | ||
| jest.mock('../../../models/Gateway', () => ({ findById: jest.fn() })); | ||
| jest.mock('../../../services/agentDigestFoldService', () => ({ | ||
| foldExpiredDigests: jest.fn().mockResolvedValue(0), | ||
| readFoldedDigest: jest.fn().mockResolvedValue(null), | ||
| settleFoldedDigest: jest.fn().mockResolvedValue(undefined), | ||
| })); | ||
| jest.mock('../../../services/agentProvisionerService', () => ({ | ||
| clearAgentRuntimeSessions: jest.fn(), | ||
| restartAgentRuntime: jest.fn(), | ||
| resolveOpenClawAccountId: jest.fn(), | ||
| })); | ||
|
|
||
| const AgentEvent = require('../../../models/AgentEvent'); | ||
| const { postToPublicAddress } = require('../../../services/outboundUrlGuard'); | ||
| const AgentMessageService = require('../../../services/agentMessageService'); | ||
| const AgentMentionService = require('../../../services/agentMentionService'); | ||
| const AgentSendFreshness = require('../../../services/agentSendFreshness'); | ||
| const PodModel = require('../../../models/Pod'); | ||
| const AgentIdentityService = require('../../../services/agentIdentityService'); | ||
| const AgentEventService = require('../../../services/agentEventService'); | ||
|
|
||
| const installation = { | ||
| displayName: 'Echo', | ||
| config: { runtime: { webhookUrl: 'https://agent.example.com/hook' } }, | ||
| }; | ||
|
|
||
| const event = { | ||
| _id: 'evt_inline_001', | ||
| type: 'chat.mention', | ||
| podId: 'pod_inline', | ||
| agentName: 'echo-agent', | ||
| instanceId: 'default', | ||
| createdAt: new Date().toISOString(), | ||
| payload: {}, | ||
| }; | ||
|
|
||
| const agentUser = { _id: 'user_echo', username: 'echo-agent' }; | ||
|
|
||
| const respondWith = (body) => postToPublicAddress.mockResolvedValue({ | ||
| ok: true, | ||
| status: 200, | ||
| text: JSON.stringify(body), | ||
| }); | ||
|
|
||
| const recordedDelivery = () => { | ||
| const call = AgentEvent.findByIdAndUpdate.mock.calls.at(-1); | ||
| return call[1].delivery; | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| AgentEventService.attachLegacyDigest = jest.fn().mockResolvedValue(undefined); | ||
| AgentEventService.settleLegacyDigest = jest.fn().mockResolvedValue(undefined); | ||
| AgentIdentityService.getOrCreateAgentUser.mockResolvedValue(agentUser); | ||
| AgentSendFreshness.unseenMessages.mockResolvedValue(null); | ||
| PodModel.findById.mockReturnValue({ | ||
| select: () => ({ lean: () => Promise.resolve({ type: 'team' }) }), | ||
| }); | ||
| AgentMessageService.postMessage.mockResolvedValue({ | ||
| success: true, | ||
| message: { _id: 'msg_1', content: 'hi' }, | ||
| }); | ||
| }); | ||
|
|
||
| describe('webhook inline reply (TASK-015)', () => { | ||
| test('posted reply goes through postMessage and is recorded as posted', async () => { | ||
| respondWith({ outcome: 'posted', content: 'hi there' }); | ||
|
|
||
| await AgentEventService.deliverEventViaWebhook(installation, event); | ||
|
|
||
| expect(AgentMessageService.postMessage).toHaveBeenCalledWith(expect.objectContaining({ | ||
| agentName: 'echo-agent', | ||
| podId: 'pod_inline', | ||
| content: 'hi there', | ||
| installationConfig: installation.config, | ||
| })); | ||
| expect(recordedDelivery()).toEqual(expect.objectContaining({ outcome: 'posted' })); | ||
| }); | ||
|
|
||
| test('a mention-enqueue runs for the posted reply, as on the CAP route', async () => { | ||
| respondWith({ outcome: 'posted', content: 'hi there' }); | ||
|
|
||
| await AgentEventService.deliverEventViaWebhook(installation, event); | ||
| await new Promise((resolve) => { setImmediate(resolve); }); | ||
|
|
||
| expect(AgentMentionService.enqueueMentions).toHaveBeenCalledWith(expect.objectContaining({ | ||
| podId: 'pod_inline', | ||
| userId: 'user_echo', | ||
| })); | ||
| }); | ||
|
|
||
| test('a post that throws is recorded as error, never as posted', async () => { | ||
| AgentMessageService.postMessage.mockRejectedValue(new Error('db down')); | ||
| respondWith({ outcome: 'posted', content: 'hi there' }); | ||
|
|
||
| await AgentEventService.deliverEventViaWebhook(installation, event); | ||
|
|
||
| expect(recordedDelivery()).toEqual(expect.objectContaining({ | ||
| outcome: 'error', | ||
| reason: 'inline_post_failed', | ||
| })); | ||
| }); | ||
|
|
||
| test('a reply held by the freshness gate is recorded as skipped, not posted', async () => { | ||
| AgentSendFreshness.unseenMessages.mockResolvedValue({ code: 'UNSEEN_MESSAGES' }); | ||
| respondWith({ outcome: 'posted', content: 'hi there' }); | ||
|
|
||
| await AgentEventService.deliverEventViaWebhook(installation, event); | ||
|
|
||
| expect(AgentMessageService.postMessage).not.toHaveBeenCalled(); | ||
| expect(recordedDelivery()).toEqual(expect.objectContaining({ | ||
| outcome: 'skipped', | ||
| reason: 'unseen_messages', | ||
| })); | ||
| }); | ||
|
|
||
| test('a non-posted outcome never calls postMessage', async () => { | ||
| respondWith({ outcome: 'no_action', reason: 'nothing_to_add' }); | ||
|
|
||
| await AgentEventService.deliverEventViaWebhook(installation, event); | ||
|
|
||
| expect(AgentMessageService.postMessage).not.toHaveBeenCalled(); | ||
| expect(recordedDelivery()).toEqual(expect.objectContaining({ outcome: 'no_action' })); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.