tests — webhooks
tests — webhooks
This document describes the tests/webhooks/webhook-manager.test.ts module, which provides comprehensive unit tests for the WebhookManager class located in src/webhooks/webhook-manager.js.
Module Purpose
The primary purpose of this module is to ensure the WebhookManager class functions correctly and robustly. It validates all core functionalities, including webhook registration, retrieval, modification, removal, payload processing, signature verification, and data persistence.
By running these tests, developers can be confident that changes to WebhookManager or its dependencies do not introduce regressions and that the webhook system behaves as expected under various conditions.
Testing Environment and Setup
The tests utilize a temporary directory for each test run to ensure isolation and prevent side effects between tests.
- Temporary Directory Creation: Before each test (
beforeEach), a unique temporary directory is created usingmkdtempSync(join(tmpdir(), 'webhook-test-')). This directory serves as the storage location for webhook data managed by theWebhookManagerinstance. - WebhookManager Instantiation: A new
WebhookManagerinstance (mgr) is created for each test, initialized with the path to the temporary directory. This ensures a clean state for every test. - Cleanup: After each test (
afterEach), the temporary directory and all its contents are recursively removed usingrmSync(tempDir, { recursive: true, force: true }). This prevents accumulation of test data and ensures a clean environment for subsequent test runs.
Test Coverage
The test suite covers the following key aspects of the WebhookManager functionality:
1. Webhook Management Lifecycle
These tests validate the basic CRUD operations for webhooks.
- Registration:
should register a webhook with an ID: Verifies thatmgr.register(name, agentMessage, secret?)successfully creates a webhook, assigns a unique ID, and stores the provided details.- Listing:
should list all webhooks: Confirms thatmgr.list()returns all currently registered webhooks.- Retrieval:
should get webhook by ID: Ensuresmgr.get(id)retrieves the correct webhook object.should return undefined for unknown ID: Verifiesmgr.get()returnsundefinedfor non-existent IDs.- Removal:
should remove a webhook: Testsmgr.remove(id)successfully deletes a webhook and returnstrue.should return false when removing unknown webhook: Checksmgr.remove()returnsfalsefor non-existent IDs.- Enabling/Disabling:
should toggle enabled flag: Validatesmgr.setEnabled(id, enabled)correctly updates theenabledstatus of a webhook and returnstrue.should return false when setting enabled on unknown webhook: Checksmgr.setEnabled()returnsfalsefor non-existent IDs.
2. Payload Processing and Templating
These tests focus on how WebhookManager processes incoming payloads and resolves agent messages.
- Template Resolution:
should resolve template placeholders in processPayload: Verifies thatmgr.processPayload(id, payload, signature?)correctly substitutes{{body.placeholders in the}} agentMessagewith values from the providedpayload.should resolve nested template placeholders: Tests template resolution for deeply nested properties within the payload.should leave unresolved placeholders as-is: Ensures that placeholders for which no corresponding data exists in the payload are left unchanged in the resulting message.
- Error Handling during Processing:
should return error for unknown webhook ID: ConfirmsprocessPayloadreturns an error object if the webhook ID is not found.should return error for disabled webhook: VerifiesprocessPayloadreturns an error object if the target webhook is disabled.
3. Signature Verification
These tests validate the security mechanism for webhooks using HMAC signatures.
- Missing Signature:
should reject missing signature when secret is set: EnsuresprocessPayloadreturns an error if a webhook has a secret configured but no signature is provided.- Invalid Signature:
should reject invalid signature: ConfirmsprocessPayloadreturns an error if a signature is provided but does not match the expected HMAC hash.- Valid Signature:
should accept valid HMAC signature: Verifies thatprocessPayloadsuccessfully processes a payload when a correct HMAC-SHA256 signature is provided, generated usingcreateHmac('sha256', secret).update(payload).digest('hex').
4. Persistence
This test ensures that webhook data is correctly persisted to disk and can be reloaded.
should persist and reload webhooks: This critical test registers a webhook, then creates a newWebhookManagerinstance pointing to the same temporary directory. It then asserts that the newly instantiated manager can successfully retrieve the previously registered webhook, including its secret. This confirms that webhook data is saved and loaded correctly acrossWebhookManagerinstances.
Dependencies
This test module directly imports and uses:
WebhookManagerfrom../../src/webhooks/webhook-manager.js: The class under test.createHmacfromcrypto: Used to generate valid HMAC signatures for testing the signature verification logic.mkdtempSync,rmSyncfromfs: For creating and deleting temporary directories.joinfrompath: For constructing file paths within the temporary directory.tmpdirfromos: For getting the system's temporary directory path.
The tests rely on the Jest testing framework's global functions like describe, beforeEach, afterEach, it, and expect.