run test server from within test suite

This commit is contained in:
dholms 2022-09-12 12:35:24 -05:00
parent d79c637223
commit efe3f622b7
11 changed files with 87 additions and 69 deletions

@ -45,7 +45,9 @@
],
"nohoist": [
"**/ipld-hashmap",
"**/ipld-hashmap/**"
"**/ipld-hashmap/**",
"**/sqlite3",
"**/sqlite3/**"
]
}
}

@ -6,7 +6,7 @@ require('esbuild')
outdir: 'dist',
platform: 'node',
external: [
'../../node_modules/knex/*',
'./node_modules/sqlite3/*',
'../../node_modules/sqlite3/*',
'../../node_modules/level/*',
'../../node_modules/classic-level/*',

@ -5,7 +5,7 @@
"scripts": {
"build": "node ./build.js",
"start": "node dist/index.js",
"test": "jest tests/views.test.ts",
"test": "jest",
"prettier": "prettier --check src/",
"prettier:fix": "prettier --write src/",
"lint": "eslint . --ext .ts,.tsx",
@ -34,6 +34,7 @@
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"axios": "^0.26.1",
"get-port": "^6.1.2",
"nodemon": "^2.0.15",
"rimraf": "^3.0.2"
}

@ -43,7 +43,6 @@ export class Database {
for (const view of views) {
this.views[view.id] = view.fn(db)
}
this.db.synchronize()
}
static async sqlite(location: string): Promise<Database> {
@ -63,6 +62,7 @@ export class Database {
RepostIndex,
UserDid,
],
synchronize: true,
})
await db.initialize()
return new Database(db)
@ -72,6 +72,10 @@ export class Database {
return Database.sqlite(':memory:')
}
async close(): Promise<void> {
await this.db.destroy()
}
async getRepoRoot(did: string): Promise<CID | null> {
const table = this.db.getRepository(RepoRoot)
const found = await table.findOneBy({ did })
@ -90,16 +94,13 @@ export class Database {
await table.save(newRoot)
}
async getDidForUsername(username: string): Promise<string | null> {
async getUser(user: string): Promise<UserDid | null> {
const table = this.db.getRepository(UserDid)
const found = await table.findOneBy({ username })
return found ? found.did : null
}
async getUsernameForDid(did: string): Promise<string | null> {
const table = this.db.getRepository(UserDid)
const found = await table.findOneBy({ did })
return found ? found.username : null
if (user.startsWith('did:')) {
return table.findOneBy({ did: user })
} else {
return table.findOneBy({ username: user })
}
}
async registerUser(username: string, did: string) {

@ -42,8 +42,6 @@ export const viewFn =
thread.replies = await getReplies(db, thread, depth - 1, requester)
}
thread = setParents(thread)
return { thread }
}
@ -149,6 +147,7 @@ const rowToPost = (
}
}
// @TODO use or delete?
// parents were set without replies set yet, so we recurse back through updating the parent
const setParents = (
root: PostThreadView.Post,

@ -189,29 +189,18 @@ router.get('/:nameOrDid', async (req, res) => {
// }
const db = util.getDB(res)
if (nameOrDid.startsWith('did:')) {
did = nameOrDid
const found = await db.getUsernameForDid(did)
if (found === null) {
throw new ServerError(404, 'Could not find username for did')
}
name = found
} else {
name = nameOrDid
const found = await db.getDidForUsername(name)
if (found === null) {
throw new ServerError(404, 'Could not find did for username')
}
did = found
const user = await db.getUser(nameOrDid)
if (user === null) {
throw new ServerError(404, `Could not find user: ${nameOrDid}`)
}
didDoc = {} as any
nameIsCorrect = true
const collections = await db.listCollectionsForDid(did)
const collections = await db.listCollectionsForDid(user.did)
const resBody: DescribeRepoResponse = {
name,
did,
name: user.username,
did: user.did,
didDoc,
collections,
nameIsCorrect,
@ -235,7 +224,7 @@ router.get('/:nameOrDid/c/:namespace/:dataset', async (req, res) => {
const db = util.getDB(res)
const did = nameOrDid.startsWith('did:')
? nameOrDid
: await db.getDidForUsername(nameOrDid)
: (await db.getUser(nameOrDid))?.did
if (!did) {
throw new ServerError(404, `Could not find did for ${nameOrDid}`)
}

@ -7,6 +7,6 @@ const router = express.Router()
router.use('/root', Root)
router.use('/repo', Repo)
router.use('/subscribe', Subscribe)
// router.use('/subscribe', Subscribe)
export default router

@ -1,6 +1,8 @@
import { Repo, service } from '@adxp/common'
import Database from './db'
// @TODO revamp all subscriber logic
export const attemptNotify = async (
host: string,
repo: Repo,
@ -24,8 +26,8 @@ export const notifySubscribers = async (
db: Database,
repo: Repo,
): Promise<void> => {
const hosts = await db.getSubscriptionsForUser(repo.did)
await notifyHosts(hosts, repo)
// const hosts = await db.getSubscriptionsForUser(repo.did)
// await notifyHosts(hosts, repo)
}
export const isSubscriber = async (
@ -33,8 +35,9 @@ export const isSubscriber = async (
host: string,
user: string,
): Promise<boolean> => {
const hosts = await db.getSubscriptionsForUser(user)
return hosts.indexOf(host) > -1
// const hosts = await db.getSubscriptionsForUser(user)
// return hosts.indexOf(host) > -1
return true
}
export const notifyOneOff = async (
@ -53,7 +56,7 @@ export const notifyOneOff = async (
const baseUrl = `http://${host}`
// if it's a subscriber, we'll be notifying anyway
if (!(await isSubscriber(db, host, repo.did))) {
await attemptNotify(baseUrl, repo)
}
// if (!(await isSubscriber(db, host, repo.did))) {
// await attemptNotify(baseUrl, repo)
// }
}

@ -1,25 +1,15 @@
import { IpldStore, MicroblogDelegator } from '@adxp/common'
import * as auth from '@adxp/auth'
import { MemoryBlockstore } from '@adxp/common'
import * as crypto from '@adxp/crypto'
import server from '../src/server'
import Database from '../src/db/index'
export const newClient = async (url: string): Promise<MicroblogDelegator> => {
const keypair = await crypto.EcdsaKeypair.create()
const authStore = await auth.AuthStore.fromTokens(keypair, [])
await authStore.claimFull()
return new MicroblogDelegator(url, keypair.did(), authStore)
}
import Database from '../src/db'
export type CloseFn = () => Promise<void>
export const runTestServer = async (port: number): Promise<CloseFn> => {
const db = Database.memory()
const serverBlockstore = IpldStore.createInMemory()
const db = await Database.memory()
const serverBlockstore = new MemoryBlockstore()
const keypair = await crypto.EcdsaKeypair.create()
await db.dropTables()
await db.createTables()
const s = server(serverBlockstore, db, keypair, port)
return async () => {
await db.close()

@ -1,15 +1,32 @@
import { AdxClient, AdxUri } from '@adxp/api'
import { schemas } from '@adxp/microblog'
import * as util from './_util'
import getPort from 'get-port'
const USE_TEST_SERVER = true
const url = 'http://localhost:2583'
const adx = new AdxClient({
pds: url,
schemas: schemas,
})
const alice = { username: 'alice', did: 'did:example:alice' }
const bob = { username: 'bob', did: 'did:example:bob' }
describe('crud operations', () => {
let adx: AdxClient
let closeFn: util.CloseFn
beforeAll(async () => {
const port = USE_TEST_SERVER ? await getPort() : 2583
closeFn = await util.runTestServer(port)
const url = `http://localhost:${port}`
adx = new AdxClient({
pds: url,
schemas: schemas,
})
})
afterAll(async () => {
await closeFn()
})
it('registers users', async () => {
await adx.mainPds.registerRepo(alice)
await adx.mainPds.registerRepo(bob)
@ -151,9 +168,10 @@ describe('crud operations', () => {
await feed.del(uri3.recordKey)
await feed.del(uri4.recordKey)
})
})
describe('validation', () => {
// Validation
// --------------
it('requires a $type on records', async () => {
const feed = adx.mainPds.repo(alice.did).collection('bsky/posts')
await expect(feed.create('Post', {})).rejects.toThrow(

@ -1,13 +1,10 @@
import { AdxUri } from '@adxp/common'
import { MicroblogClient, Post } from '@adxp/microblog'
import { users, posts, replies } from './test-data'
import util from 'util'
import getPort from 'get-port'
import * as util from './_util'
const url = 'http://localhost:2583'
const alice = new MicroblogClient(url, users.alice.did)
const bob = new MicroblogClient(url, users.bob.did)
const carol = new MicroblogClient(url, users.carol.did)
const dan = new MicroblogClient(url, users.dan.did)
const USE_TEST_SERVER = true
let alicePosts: AdxUri[] = []
let bobPosts: AdxUri[] = []
@ -17,7 +14,25 @@ let bobFollows: Record<string, AdxUri> = {}
let bobLikes: Record<string, AdxUri> = {}
let badges: AdxUri[] = []
describe('server', () => {
describe('pds views', () => {
let alice, bob, carol, dan: MicroblogClient
let closeFn: util.CloseFn
beforeAll(async () => {
const port = USE_TEST_SERVER ? await getPort() : 2583
closeFn = await util.runTestServer(port)
const url = `http://localhost:${port}`
alice = new MicroblogClient(url, users.alice.did)
bob = new MicroblogClient(url, users.bob.did)
carol = new MicroblogClient(url, users.carol.did)
dan = new MicroblogClient(url, users.dan.did)
})
afterAll(async () => {
await closeFn()
})
it('register users', async () => {
await alice.register(users.alice.name)
await bob.register(users.bob.name)