Skip to content

Commit

Permalink
Feat: adds testing fixtures for setup/teardown (#51)
Browse files Browse the repository at this point in the history
* feat: disable screenshots on failed tests

* feat: add worker unit tests
  • Loading branch information
alexlwn123 authored Sep 30, 2024
1 parent 3b343fa commit 6915c97
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 173 deletions.
1 change: 1 addition & 0 deletions docs/examples/vite-react.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ You might need to disable your adblocker or Brave Shields to see the preview.
:::

<iframe src="https://stackblitz.com/github/fedimint/fedimint-web-sdk/tree/main/examples/vite-core?embed=1&file=src%2FApp.tsx" style="width: 100%; height: 600px; border: 0;"></iframe>

[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/fedimint/fedimint-web-sdk/tree/main/examples/vite-core)

## Running the Example Locally
Expand Down
3 changes: 3 additions & 0 deletions examples/bare-js/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</head>

<body>
<!-- UI Code -->
<div class="container">
<h2>Fedimint Web SDK Bare html/js example</h2>
<div id="actions">
Expand All @@ -18,7 +19,9 @@ <h2>Fedimint Web SDK Bare html/js example</h2>
<div id="console"></div>
<p>Faucet Link: <a href="https://faucet.mutinynet.com/" target="_blank">https://faucet.mutinynet.com/</a></p>
</div>

<script type="module">
// Import the @fedimint/core-web library
import * as Default from './index.js';
const { FedimintWallet } = Default;

Expand Down
128 changes: 58 additions & 70 deletions packages/core-web/src/services/FederationService.test.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,58 @@
import { test, expect } from 'vitest'
import { TestFedimintWallet } from '../test/TestFedimintWallet'
import { beforeAll } from 'vitest'

let randomTestingId: string
let wallet: TestFedimintWallet

beforeAll(async () => {
randomTestingId = Math.random().toString(36).substring(2, 15)
wallet = new TestFedimintWallet()
expect(wallet).toBeDefined()
await expect(
wallet.joinFederation(wallet.testing.TESTING_INVITE, randomTestingId),
).resolves.toBeUndefined()
expect(wallet.isOpen()).toBe(true)

// Cleanup after all tests
return async () => {
// clear up browser resources
await wallet.cleanup()
// remove the wallet db
indexedDB.deleteDatabase(randomTestingId)
// swap out the randomTestingId for a new one, to avoid raciness
randomTestingId = Math.random().toString(36).substring(2, 15)
}
})

test('getConfig should return the federation config', async () => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)
const counterBefore = wallet.testing.getRequestCounter()
await expect(wallet.federation.getConfig()).resolves.toMatchObject({
api_endpoints: expect.any(Object),
broadcast_public_keys: expect.any(Object),
consensus_version: expect.any(Object),
meta: expect.any(Object),
modules: expect.any(Object),
})
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
})

test('getFederationId should return the federation id', async () => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
const federationId = await wallet.federation.getFederationId()
expect(federationId).toBeTypeOf('string')
expect(federationId).toHaveLength(64)
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
})

test('getInviteCode should return the invite code', async () => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
const inviteCode = await wallet.federation.getInviteCode(0)
expect(inviteCode).toBeTypeOf('string')
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
})

test('listOperations should return the list of operations', async () => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
await expect(wallet.federation.listOperations()).resolves.toMatchObject([])
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
})
import { expect } from 'vitest'
import { walletTest } from '../test/setupTests'

walletTest(
'getConfig should return the federation config',
async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)
const counterBefore = wallet.testing.getRequestCounter()
await expect(wallet.federation.getConfig()).resolves.toMatchObject({
api_endpoints: expect.any(Object),
broadcast_public_keys: expect.any(Object),
consensus_version: expect.any(Object),
meta: expect.any(Object),
modules: expect.any(Object),
})
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
},
)

walletTest(
'getFederationId should return the federation id',
async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
const federationId = await wallet.federation.getFederationId()
expect(federationId).toBeTypeOf('string')
expect(federationId).toHaveLength(64)
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
},
)

walletTest(
'getInviteCode should return the invite code',
async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
const inviteCode = await wallet.federation.getInviteCode(0)
expect(inviteCode).toBeTypeOf('string')
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
},
)

walletTest(
'listOperations should return the list of operations',
async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
await expect(wallet.federation.listOperations()).resolves.toMatchObject([])
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
},
)
190 changes: 89 additions & 101 deletions packages/core-web/src/services/LightningService.test.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,61 @@
import { test, expect } from 'vitest'
import { TestFedimintWallet } from '../test/TestFedimintWallet'
import { beforeAll } from 'vitest'

let randomTestingId: string
let wallet: TestFedimintWallet

beforeAll(async () => {
randomTestingId = Math.random().toString(36).substring(2, 15)
wallet = new TestFedimintWallet()
expect(wallet).toBeDefined()
await expect(
wallet.joinFederation(wallet.testing.TESTING_INVITE, randomTestingId),
).resolves.toBeUndefined()
expect(wallet.isOpen()).toBe(true)

// Cleanup after all tests
return async () => {
// clear up browser resources
await wallet.cleanup()
// remove the wallet db
indexedDB.deleteDatabase(randomTestingId)
// swap out the randomTestingId for a new one, to avoid raciness
randomTestingId = Math.random().toString(36).substring(2, 15)
}
})

test('createInvoice should create a bolt11 invoice', async () => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
const invoice = await wallet.lightning.createInvoice(100, 'test')
expect(invoice).toBeDefined()
expect(invoice).toMatchObject({
invoice: expect.any(String),
operation_id: expect.any(String),
})
// 3 requests were made, one for the invoice, one for refreshing the
// gateway cache, one for getting the gateway info
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 3)

// Test with expiry time
await expect(
wallet.lightning.createInvoice(100, 'test', 1000, {}),
).resolves.toBeDefined()
})

test('listGateways should return a list of gateways', async () => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
const gateways = await wallet.lightning.listGateways()
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
expect(gateways).toBeDefined()
expect(gateways).toMatchObject([
{
info: expect.any(Object),
},
])
})

test('updateGatewayCache should update the gateway cache', async () => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
await expect(wallet.lightning.updateGatewayCache()).resolves.toBeDefined()
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
})

test('getGateway should return a gateway', async () => {
import { expect } from 'vitest'
import { walletTest } from '../test/setupTests'

walletTest(
'createInvoice should create a bolt11 invoice',
async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
const invoice = await wallet.lightning.createInvoice(100, 'test')
expect(invoice).toBeDefined()
expect(invoice).toMatchObject({
invoice: expect.any(String),
operation_id: expect.any(String),
})
// 3 requests were made, one for the invoice, one for refreshing the
// gateway cache, one for getting the gateway info
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 3)

// Test with expiry time
await expect(
wallet.lightning.createInvoice(100, 'test', 1000, {}),
).resolves.toBeDefined()
},
)

walletTest(
'listGateways should return a list of gateways',
async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
const gateways = await wallet.lightning.listGateways()
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
expect(gateways).toBeDefined()
expect(gateways).toMatchObject([
{
info: expect.any(Object),
},
])
},
)

walletTest(
'updateGatewayCache should update the gateway cache',
async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const counterBefore = wallet.testing.getRequestCounter()
await expect(wallet.lightning.updateGatewayCache()).resolves.toBeDefined()
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
},
)

walletTest('getGateway should return a gateway', async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

Expand All @@ -89,40 +74,43 @@ test('getGateway should return a gateway', async () => {
})
})

test('createInvoiceWithGateway should create a bolt11 invoice with a gateway', async () => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)
walletTest(
'createInvoiceWithGateway should create a bolt11 invoice with a gateway',
async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

const gateways = await wallet.lightning.listGateways()
const gateway = gateways[0]
expect(gateway).toBeDefined()
const gateways = await wallet.lightning.listGateways()
const gateway = gateways[0]
expect(gateway).toBeDefined()

const counterBefore = wallet.testing.getRequestCounter()
const invoice = await wallet.lightning.createInvoiceWithGateway(
100,
'test',
null,
{},
gateway.info,
)
expect(invoice).toBeDefined()
expect(invoice).toMatchObject({
invoice: expect.any(String),
operation_id: expect.any(String),
})
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
await expect(
wallet.lightning.createInvoiceWithGateway(
const counterBefore = wallet.testing.getRequestCounter()
const invoice = await wallet.lightning.createInvoiceWithGateway(
100,
'test',
1000,
null,
{},
gateway.info,
),
).resolves.toBeDefined()
})

test('payInvoice should pay a bolt11 invoice', async () => {
)
expect(invoice).toBeDefined()
expect(invoice).toMatchObject({
invoice: expect.any(String),
operation_id: expect.any(String),
})
expect(wallet.testing.getRequestCounter()).toBe(counterBefore + 1)
await expect(
wallet.lightning.createInvoiceWithGateway(
100,
'test',
1000,
{},
gateway.info,
),
).resolves.toBeDefined()
},
)

walletTest('payInvoice should pay a bolt11 invoice', async ({ wallet }) => {
expect(wallet).toBeDefined()
expect(wallet.isOpen()).toBe(true)

Expand Down
6 changes: 4 additions & 2 deletions packages/core-web/src/test/TestingService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { WorkerClient } from '../worker'

export const TESTING_INVITE =
'fed11qgqrsdnhwden5te0v9cxjtt4dekxzamxw4kz6mmjvvkhydted9ukg6r9xfsnx7th0fhn26tf093juamwv4u8gtnpwpcz7qqpyz0e327ua8geceutfrcaezwt22mk6s2rdy09kg72jrcmncng2gn0kp2m5sk'

// This is a testing service that allows for inspecting the internals
// of the WorkerClient. It is not intended for use in production.
export class TestingService {
public TESTING_INVITE: string
constructor(private client: WorkerClient) {
// Solo Mint on mutinynet
this.TESTING_INVITE =
'fed11qgqrsdnhwden5te0v9cxjtt4dekxzamxw4kz6mmjvvkhydted9ukg6r9xfsnx7th0fhn26tf093juamwv4u8gtnpwpcz7qqpyz0e327ua8geceutfrcaezwt22mk6s2rdy09kg72jrcmncng2gn0kp2m5sk'
this.TESTING_INVITE = TESTING_INVITE
}

getRequestCounter() {
Expand Down
Loading

0 comments on commit 6915c97

Please sign in to comment.