Improve cashu test

This commit is contained in:
Alex Gleason 2025-02-21 15:05:54 -06:00
parent 438ab09216
commit d0c7cc7a45
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7

View file

@ -13,30 +13,15 @@ import { createTestDB } from '@/test.ts';
import cashuApp from '@/controllers/api/cashu.ts'; import cashuApp from '@/controllers/api/cashu.ts';
import { walletSchema } from '@/schema.ts'; import { walletSchema } from '@/schema.ts';
function testUserMiddleware(user: User<NSecSigner>): DittoMiddleware<{ user: User<NSecSigner> }> {
return async (c, next) => {
c.set('user', user);
await next();
};
}
Deno.test('PUT /wallet must be successful', { Deno.test('PUT /wallet must be successful', {
sanitizeOps: false, sanitizeOps: false,
sanitizeResources: false, sanitizeResources: false,
}, async () => { }, async () => {
using _mock = mockFetch(); await using test = await createTestApp();
await using db = await createTestDB();
const relay = db.store;
const sk = generateSecretKey(); const { app, signer, sk, relay } = test;
const signer = new NSecSigner(sk);
const nostrPrivateKey = bytesToString('hex', sk); const nostrPrivateKey = bytesToString('hex', sk);
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) });
app.use(testUserMiddleware({ signer, relay }));
app.route('/', cashuApp);
const response = await app.request('/wallet', { const response = await app.request('/wallet', {
method: 'PUT', method: 'PUT',
headers: { headers: {
@ -97,16 +82,8 @@ Deno.test('PUT /wallet must be successful', {
}); });
Deno.test('PUT /wallet must NOT be successful: wrong request body/schema', async () => { Deno.test('PUT /wallet must NOT be successful: wrong request body/schema', async () => {
using _mock = mockFetch(); await using test = await createTestApp();
await using db = await createTestDB(); const { app } = test;
const relay = db.store;
const sk = generateSecretKey();
const signer = new NSecSigner(sk);
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) });
app.use(testUserMiddleware({ signer, relay }));
app.route('/', cashuApp);
const response = await app.request('/wallet', { const response = await app.request('/wallet', {
method: 'PUT', method: 'PUT',
@ -128,18 +105,10 @@ Deno.test('PUT /wallet must NOT be successful: wallet already exists', {
sanitizeOps: false, sanitizeOps: false,
sanitizeResources: false, sanitizeResources: false,
}, async () => { }, async () => {
using _mock = mockFetch(); await using test = await createTestApp();
await using db = await createTestDB(); const { app, sk, relay } = test;
const relay = db.store;
const sk = generateSecretKey();
const signer = new NSecSigner(sk);
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) }); await relay.event(genEvent({ kind: 17375 }, sk));
app.use(testUserMiddleware({ signer, relay }));
app.route('/', cashuApp);
await db.store.event(genEvent({ kind: 17375 }, sk));
const response = await app.request('/wallet', { const response = await app.request('/wallet', {
method: 'PUT', method: 'PUT',
@ -162,23 +131,15 @@ Deno.test('GET /wallet must be successful', {
sanitizeOps: false, sanitizeOps: false,
sanitizeResources: false, sanitizeResources: false,
}, async () => { }, async () => {
using _mock = mockFetch(); await using test = await createTestApp();
await using db = await createTestDB(); const { app, sk, relay, signer } = test;
const relay = db.store;
const sk = generateSecretKey();
const signer = new NSecSigner(sk);
const pubkey = await signer.getPublicKey(); const pubkey = await signer.getPublicKey();
const privkey = bytesToString('hex', sk); const privkey = bytesToString('hex', sk);
const p2pk = getPublicKey(stringToBytes('hex', privkey)); const p2pk = getPublicKey(stringToBytes('hex', privkey));
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) });
app.use(testUserMiddleware({ signer, relay }));
app.route('/', cashuApp);
// Wallet // Wallet
await db.store.event(genEvent({ await relay.event(genEvent({
kind: 17375, kind: 17375,
content: await signer.nip44.encrypt( content: await signer.nip44.encrypt(
pubkey, pubkey,
@ -190,7 +151,7 @@ Deno.test('GET /wallet must be successful', {
}, sk)); }, sk));
// Nutzap information // Nutzap information
await db.store.event(genEvent({ await relay.event(genEvent({
kind: 10019, kind: 10019,
tags: [ tags: [
['pubkey', p2pk], ['pubkey', p2pk],
@ -199,7 +160,7 @@ Deno.test('GET /wallet must be successful', {
}, sk)); }, sk));
// Unspent proofs // Unspent proofs
await db.store.event(genEvent({ await relay.event(genEvent({
kind: 7375, kind: 7375,
content: await signer.nip44.encrypt( content: await signer.nip44.encrypt(
pubkey, pubkey,
@ -240,7 +201,7 @@ Deno.test('GET /wallet must be successful', {
// Nutzap // Nutzap
const senderSk = generateSecretKey(); const senderSk = generateSecretKey();
await db.store.event(genEvent({ await relay.event(genEvent({
kind: 9321, kind: 9321,
content: 'Nice post!', content: 'Nice post!',
tags: [ tags: [
@ -269,13 +230,8 @@ Deno.test('GET /wallet must be successful', {
}); });
Deno.test('GET /mints must be successful', async () => { Deno.test('GET /mints must be successful', async () => {
using _mock = mockFetch(); await using test = await createTestApp();
await using db = await createTestDB(); const { app } = test;
const relay = db.store;
const app = new DittoApp({ db, relay, conf: new DittoConf(new Map()) });
app.route('/', cashuApp);
const response = await app.request('/mints', { const response = await app.request('/mints', {
method: 'GET', method: 'GET',
@ -287,13 +243,42 @@ Deno.test('GET /mints must be successful', async () => {
assertEquals(body, { mints: [] }); assertEquals(body, { mints: [] });
}); });
function mockFetch() { async function createTestApp() {
const conf = new DittoConf(new Map());
const db = await createTestDB();
const relay = db.store;
const sk = generateSecretKey();
const signer = new NSecSigner(sk);
const app = new DittoApp({ db, relay, conf });
app.use(testUserMiddleware({ signer, relay }));
app.route('/', cashuApp);
const mock = stub(globalThis, 'fetch', () => { const mock = stub(globalThis, 'fetch', () => {
return Promise.resolve(new Response()); return Promise.resolve(new Response());
}); });
return { return {
[Symbol.dispose]: () => { app,
db,
conf,
sk,
signer,
relay,
[Symbol.asyncDispose]: async () => {
mock.restore(); mock.restore();
await db[Symbol.asyncDispose]();
await relay[Symbol.asyncDispose]();
}, },
}; };
} }
function testUserMiddleware(user: User<NSecSigner>): DittoMiddleware<{ user: User<NSecSigner> }> {
return async (c, next) => {
c.set('user', user);
await next();
};
}