-
Notifications
You must be signed in to change notification settings - Fork 87
feat: on-chain refunds, plan_changed event, webhook circuit breaker, … #717
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
Merged
Dev-AdeTutu
merged 3 commits into
Dev-AdeTutu:main
from
Olakunle567:feat/refunds-plan-events-webhook-circuit-breaker-tz-fix
Aug 26, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /** | ||
| * Covers the webhook circuit breaker: after CIRCUIT_FAILURE_THRESHOLD (10) | ||
| * consecutive delivery failures to a URL, further deliveries to that URL are | ||
| * skipped (no fetch attempt) until the cooldown window elapses. | ||
| */ | ||
|
|
||
| import { describe, it, expect, beforeEach } from "vitest"; | ||
|
|
||
| process.env.WEBHOOKS_DB_PATH = ":memory:"; | ||
|
|
||
| import { | ||
| fireWebhook, | ||
| getCircuitBreakerState, | ||
| resetCircuitBreakers, | ||
| } from "../src/lib/webhookRegistry.js"; | ||
|
|
||
| function installFetchSpy(status: number) { | ||
| const calls: string[] = []; | ||
| const original = globalThis.fetch; | ||
| globalThis.fetch = ((input: string | URL | Request) => { | ||
| calls.push(String(input)); | ||
| return Promise.resolve(new Response(status === 200 ? "{}" : "error", { status })); | ||
| }) as typeof fetch; | ||
| return { calls, restore: () => { globalThis.fetch = original; } }; | ||
| } | ||
|
|
||
| const URL = "https://circuit.example.com/hook"; | ||
|
|
||
| describe("webhook circuit breaker", () => { | ||
| beforeEach(() => { | ||
| resetCircuitBreakers(); | ||
| }); | ||
|
|
||
| it("opens after 10 consecutive failures and skips further delivery attempts", async () => { | ||
| const spy = installFetchSpy(500); | ||
| try { | ||
| for (let i = 0; i < 10; i++) { | ||
| await fireWebhook(URL, JSON.stringify({ i })); | ||
| } | ||
|
|
||
| const state = getCircuitBreakerState(URL); | ||
| expect(state.open).toBe(true); | ||
| expect(state.consecutiveFailures).toBeGreaterThanOrEqual(10); | ||
|
|
||
| const callsBeforeEleventh = spy.calls.length; | ||
| await fireWebhook(URL, JSON.stringify({ eleventh: true })); | ||
|
|
||
| // Circuit is open — no additional fetch attempt should have been made. | ||
| expect(spy.calls.length).toBe(callsBeforeEleventh); | ||
| } finally { | ||
| spy.restore(); | ||
| } | ||
| }); | ||
|
|
||
| it("does not open the circuit for a URL that stays under the failure threshold", async () => { | ||
| const spy = installFetchSpy(500); | ||
| try { | ||
| for (let i = 0; i < 9; i++) { | ||
| await fireWebhook(URL, JSON.stringify({ i })); | ||
| } | ||
| expect(getCircuitBreakerState(URL).open).toBe(false); | ||
| } finally { | ||
| spy.restore(); | ||
| } | ||
| }); | ||
|
|
||
| it("closes and resumes deliveries once the cooldown window elapses", async () => { | ||
| const failing = installFetchSpy(500); | ||
| try { | ||
| for (let i = 0; i < 10; i++) { | ||
| await fireWebhook(URL, JSON.stringify({ i })); | ||
| } | ||
| expect(getCircuitBreakerState(URL).open).toBe(true); | ||
| } finally { | ||
| failing.restore(); | ||
| } | ||
|
|
||
| const realDateNow = Date.now; | ||
| try { | ||
| // Fast-forward past the 5-minute cooldown. | ||
| Date.now = () => realDateNow() + 5 * 60 * 1000 + 1; | ||
|
|
||
| const succeeding = installFetchSpy(200); | ||
| try { | ||
| await fireWebhook(URL, JSON.stringify({ resumed: true })); | ||
| expect(succeeding.calls.length).toBe(1); | ||
| expect(getCircuitBreakerState(URL).open).toBe(false); | ||
| expect(getCircuitBreakerState(URL).consecutiveFailures).toBe(0); | ||
| } finally { | ||
| succeeding.restore(); | ||
| } | ||
| } finally { | ||
| Date.now = realDateNow; | ||
| } | ||
| }); | ||
|
|
||
| it("a success resets the consecutive-failure count", async () => { | ||
| const failing = installFetchSpy(500); | ||
| try { | ||
| for (let i = 0; i < 5; i++) { | ||
| await fireWebhook(URL, JSON.stringify({ i })); | ||
| } | ||
| expect(getCircuitBreakerState(URL).consecutiveFailures).toBe(5); | ||
| } finally { | ||
| failing.restore(); | ||
| } | ||
|
|
||
| const succeeding = installFetchSpy(200); | ||
| try { | ||
| await fireWebhook(URL, JSON.stringify({ ok: true })); | ||
| expect(getCircuitBreakerState(URL).consecutiveFailures).toBe(0); | ||
| } finally { | ||
| succeeding.restore(); | ||
| } | ||
| }); | ||
| }); |
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.