-
Notifications
You must be signed in to change notification settings - Fork 3
feat: fetch --dry-run previews the price without paying #56
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1750150
feat: fetch --dry-run previews the price without paying
reneaaron e63960e
Merge branch 'master' into feat/fetch-dry-run
rolznz 46cd43b
fix: silently ignore payment flags with --dry-run instead of erroring
rolznz d67ccf8
feat: suggest l402.space bridge when a 402 offers no lightning invoice
rolznz ff7e19c
fix: fall back to bridge suggestion when a matched invoice doesn't de…
rolznz 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,102 @@ | ||
| import { describe, test, expect, vi, afterEach } from "vitest"; | ||
| import { dryRun402 } from "../tools/lightning/fetch.js"; | ||
|
|
||
| // A real 100-sat BOLT-11 invoice (shared with lightning-tools.test.ts) so the | ||
| // price extraction exercises actual invoice decoding, not a mock. | ||
| const exampleInvoice = | ||
| "lnbc1u1p5hlrr8dqqnp4qwmtpr4p72ms7gnq3pkfk2876y2msvl33s3840dlp6xsv2w59dpscpp55utq6s8u5407namwt4jvhgsaf9fyszppjfwyxp7qsw6cyc8vxukqsp583usez9yhmkcavvvjz8cq56v3nglh2q37xkf4ufrgwxfrfjkm54s9qyysgqcqzp2xqyz5vqgtyysw64zt9sj6kfpqnekzwc37y2uyg0xdapgxqqth4uahff0x89sjfsvukjlllasg5dn05u2uha6qcvxz2y3ye5k7958qtes4pv4ggqtnjyky"; | ||
|
|
||
| function stub402(headers: Record<string, string>, body = "payment required") { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn().mockResolvedValue(new Response(body, { status: 402, headers })), | ||
| ); | ||
| } | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe("fetch --dry-run price preview", () => { | ||
| test("reports the sats price from an L402 challenge without paying", async () => { | ||
| stub402({ | ||
| "www-authenticate": `L402 token="abc", invoice="${exampleInvoice}"`, | ||
| }); | ||
|
|
||
| const result = await dryRun402({ url: "https://l402.example/api" }); | ||
|
|
||
| expect(result.payment_required).toBe(true); | ||
| expect(result.amount_in_sats).toBe(100); | ||
| }); | ||
|
|
||
| test("reports the sats price from an MPP Payment challenge", async () => { | ||
| stub402({ | ||
| "www-authenticate": `Payment realm="svc", method="lightning", intent="charge", invoice="${exampleInvoice}", amount="100", currency="sat"`, | ||
| }); | ||
|
|
||
| const result = await dryRun402({ url: "https://mpp.example/api" }); | ||
|
|
||
| expect(result.payment_required).toBe(true); | ||
| expect(result.amount_in_sats).toBe(100); | ||
| }); | ||
|
|
||
| test("finds the invoice inside a base64 x402 Payment-Required header", async () => { | ||
| const x402Challenge = Buffer.from( | ||
| JSON.stringify({ | ||
| x402Version: 2, | ||
| accepts: [ | ||
| { | ||
| network: "bip122:000000000019d6689c085ae165831e93", | ||
| asset: "BTC", | ||
| extra: { paymentMethod: "lightning", invoice: exampleInvoice }, | ||
| }, | ||
| ], | ||
| }), | ||
| ).toString("base64"); | ||
| stub402({ "payment-required": x402Challenge }); | ||
|
|
||
| const result = await dryRun402({ url: "https://x402ln.example/api" }); | ||
|
|
||
| expect(result.payment_required).toBe(true); | ||
| expect(result.amount_in_sats).toBe(100); | ||
| }); | ||
|
|
||
| test("suggests the l402.space bridge when no lightning invoice is offered", async () => { | ||
| stub402({ | ||
| "www-authenticate": 'Payment realm="svc", method="tempo", intent="charge"', | ||
| }); | ||
|
|
||
| const result = await dryRun402({ url: "https://tempo.example/api" }); | ||
|
|
||
| expect(result.payment_required).toBe(true); | ||
| expect(result.amount_in_sats).toBeUndefined(); | ||
| expect(result.message).toContain("no lightning invoice found"); | ||
| expect(result.message).toContain( | ||
| "https://l402.space/https%3A%2F%2Ftempo.example%2Fapi", | ||
| ); | ||
| }); | ||
|
|
||
| test("falls back to the bridge suggestion when the invoice doesn't decode", async () => { | ||
| stub402({ | ||
| "www-authenticate": 'L402 token="abc", invoice="lnbc1notarealinvoice00"', | ||
| }); | ||
|
|
||
| const result = await dryRun402({ url: "https://broken.example/api" }); | ||
|
|
||
| expect(result.payment_required).toBe(true); | ||
| expect(result.amount_in_sats).toBeUndefined(); | ||
| expect(result.message).toContain("no lightning invoice found"); | ||
| }); | ||
|
|
||
| test("reports payment_required false for a non-402 response", async () => { | ||
| vi.stubGlobal( | ||
| "fetch", | ||
| vi.fn().mockResolvedValue(new Response("free content", { status: 200 })), | ||
| ); | ||
|
|
||
| const result = await dryRun402({ url: "https://free.example/api" }); | ||
|
|
||
| expect(result.payment_required).toBe(false); | ||
| expect(result.status).toBe(200); | ||
| }); | ||
| }); |
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.
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.