-
Notifications
You must be signed in to change notification settings - Fork 40
test: add NWC faucet integration test #535
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
10 commits
Select commit
Hold shift + click to select a range
908c16b
feat: add NWC faucet integration test
DSanich b2ac59d
refactor(test): move NWC faucet test to e2e directory
DSanich 68e5fcc
fix: improved error catching
DSanich dbb933b
fix(e2e): don't retry parse errors, adjust test timeout
DSanich 7e7f034
fix(e2e): ensure NWCClient is closed in finally block
DSanich 7070630
fix(e2e): exclude browser folder from Jest e2e config
DSanich f4450d8
feat(e2e): added e2e test step to ci workflow
DSanich eeff61f
Update nwc-faucet.test.ts
DSanich c59a603
Updated node version on CI
DSanich 67ad635
fix(e2e): CI fix
DSanich 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
Some comments aren't visible on the classic Files Changed page.
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,57 @@ | ||
| /** | ||
| * Test helpers for E2E integration tests using the NWC faucet. | ||
| * @see https://github.com/getAlby/cli/tree/master/src/test | ||
| */ | ||
|
|
||
| export interface TestWallet { | ||
| nwcUrl: string; | ||
| lightningAddress: string; | ||
| } | ||
|
|
||
| const FAUCET_URL = "https://faucet.nwc.dev"; | ||
|
|
||
| /** | ||
| * Creates a test wallet via the NWC faucet with the given balance in sats. | ||
| * Retries on failure (faucet can be rate-limited or temporarily unavailable). | ||
| */ | ||
| export async function createTestWallet( | ||
| balanceSats = 10_000, | ||
| retries = 5, | ||
| ): Promise<TestWallet> { | ||
| for (let i = 0; i < retries; i++) { | ||
| try { | ||
| const response = await fetch(`${FAUCET_URL}?balance=${balanceSats}`, { | ||
| method: "POST", | ||
| }); | ||
| if (!response.ok) { | ||
| if (i < retries - 1) { | ||
| await new Promise((r) => setTimeout(r, 2000 * (i + 1))); | ||
| continue; | ||
| } | ||
| throw new Error(`Faucet request failed: ${response.status}`); | ||
| } | ||
| const nwcUrl = (await response.text()).trim(); | ||
| const lud16Match = nwcUrl.match(/lud16=([^&\s]+)/); | ||
| if (!lud16Match) { | ||
| throw new Error("No lud16 in NWC URL"); | ||
| } | ||
| return { | ||
| nwcUrl, | ||
| lightningAddress: decodeURIComponent(lud16Match[1]), | ||
| }; | ||
| } catch (error) { | ||
| if ( | ||
| error instanceof Error && | ||
| error.message === "No lud16 in NWC URL" | ||
| ) { | ||
| throw error; | ||
| } | ||
| if (i < retries - 1) { | ||
| await new Promise((r) => setTimeout(r, 2000 * (i + 1))); | ||
| continue; | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
| throw new Error("Failed to create test wallet"); | ||
| } |
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,28 @@ | ||
| import { NWCClient } from "../src/nwc/NWCClient"; | ||
| import { createTestWallet } from "./helpers"; | ||
|
|
||
| /** | ||
| * E2E integration test using the NWC faucet. | ||
| * Creates a wallet with 10_000 sats and verifies the balance via get_balance. | ||
| * Requires network access. | ||
| */ | ||
| describe("NWC faucet integration", () => { | ||
| const EXPECTED_BALANCE_SATS = 10_000; | ||
| const EXPECTED_BALANCE_MSATS = EXPECTED_BALANCE_SATS * 1000; | ||
|
|
||
| test( | ||
| "creates wallet via faucet and balance is 10_000 sats", | ||
| async () => { | ||
| const { nwcUrl } = await createTestWallet(EXPECTED_BALANCE_SATS, 3); | ||
|
|
||
| const nwc = new NWCClient({ nostrWalletConnectUrl: nwcUrl }); | ||
| try { | ||
| const result = await nwc.getBalance(); | ||
| expect(result.balance).toBe(EXPECTED_BALANCE_MSATS); | ||
| } finally { | ||
| nwc.close(); | ||
| } | ||
| }, | ||
| 60_000, | ||
| ); | ||
| }); |
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| /** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
| module.exports = { | ||
| export default { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| testPathIgnorePatterns: ['/node_modules/', '/e2e/browser/'], | ||
| testPathIgnorePatterns: ['/node_modules/', '/e2e/'], | ||
| }; | ||
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,7 @@ | ||
| /** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
| export default { | ||
| preset: 'ts-jest', | ||
| testEnvironment: 'node', | ||
| testMatch: ['<rootDir>/e2e/**/*.test.ts'], | ||
DSanich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| testPathIgnorePatterns: ['/node_modules/', '/e2e/browser'], | ||
| }; | ||
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.