mastoapi: add a test module

This commit is contained in:
Alex Gleason 2025-02-21 21:15:57 -06:00
parent d4fc10fe3e
commit 9c97cc387f
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 35 additions and 27 deletions

View file

@ -2,6 +2,7 @@
"name": "@ditto/mastoapi", "name": "@ditto/mastoapi",
"version": "1.1.0", "version": "1.1.0",
"exports": { "exports": {
"./middleware": "./middleware/mod.ts" "./middleware": "./middleware/mod.ts",
"./test": "./test.ts"
} }
} }

View file

@ -1,16 +1,9 @@
import { DittoConf } from '@ditto/conf'; import { setUser, testApp } from '@ditto/mastoapi/test';
import { DummyDB } from '@ditto/db';
import { DittoApp, type DittoMiddleware } from '@ditto/router';
import { type NostrSigner, NSecSigner } from '@nostrify/nostrify';
import { MockRelay } from '@nostrify/nostrify/test';
import { assertEquals } from '@std/assert'; import { assertEquals } from '@std/assert';
import { generateSecretKey, nip19 } from 'nostr-tools';
import { userMiddleware } from './userMiddleware.ts'; import { userMiddleware } from './userMiddleware.ts';
import { ReadOnlySigner } from '../signers/ReadOnlySigner.ts'; import { ReadOnlySigner } from '../signers/ReadOnlySigner.ts';
import type { User } from './User.ts';
Deno.test('no user 401', async () => { Deno.test('no user 401', async () => {
const { app } = testApp(); const { app } = testApp();
const response = await app.use(userMiddleware()).request('/'); const response = await app.use(userMiddleware()).request('/');
@ -79,21 +72,3 @@ Deno.test('admin role 200', async () => {
assertEquals(response.status, 200); assertEquals(response.status, 200);
}); });
function testApp() {
const relay = new MockRelay();
const signer = new NSecSigner(generateSecretKey());
const conf = new DittoConf(new Map([['DITTO_NSEC', nip19.nsecEncode(generateSecretKey())]]));
const db = new DummyDB();
const app = new DittoApp({ conf, relay, db });
const user = { signer, relay };
return { app, relay, conf, db, user };
}
function setUser<S extends NostrSigner>(user: User<S>): DittoMiddleware<{ user: User<S> }> {
return async (c, next) => {
c.set('user', user);
await next();
};
}

32
packages/mastoapi/test.ts Normal file
View file

@ -0,0 +1,32 @@
import { DittoConf } from '@ditto/conf';
import { DummyDB } from '@ditto/db';
import { DittoApp, type DittoMiddleware } from '@ditto/router';
import { type NostrSigner, NSecSigner } from '@nostrify/nostrify';
import { MockRelay } from '@nostrify/nostrify/test';
import { generateSecretKey, nip19 } from 'nostr-tools';
import type { User } from '@ditto/mastoapi/middleware';
export function testApp() {
const db = new DummyDB();
const nsec = nip19.nsecEncode(generateSecretKey());
const conf = new DittoConf(new Map([['DITTO_NSEC', nsec]]));
const relay = new MockRelay();
const app = new DittoApp({ conf, relay, db });
const user = {
signer: new NSecSigner(generateSecretKey()),
relay,
};
return { app, relay, conf, db, user };
}
export function setUser<S extends NostrSigner>(user: User<S>): DittoMiddleware<{ user: User<S> }> {
return async (c, next) => {
c.set('user', user);
await next();
};
}