fix: type errors

This commit is contained in:
Ricardo Arturo Cabral Mejía
2022-12-21 01:28:42 -05:00
parent f3c801e503
commit f3097983a0
14 changed files with 61 additions and 47 deletions

View File

@@ -4,6 +4,7 @@ services:
context: ../../
dockerfile: Dockerfile.test
environment:
RELAY_PORT: 18808
DB_HOST: db-test
DB_PORT: 5432
DB_NAME: nostr_ts_relay_test
@@ -17,10 +18,14 @@ services:
REDIS_PASSWORD: nostr_ts_relay_test
NOSTR_CONFIG_DIR: /code
volumes:
- ../../package.json:/code/package.json
- ../../settings.sample.json:/code/settings.sample.json
- ../../src:/code/src
- ../../test:/code/test
- ../../test/integration:/code/test/integration
- ../../cucumber.js:/code/cucumber.js
- ../../.coverage:/code/.coverage
- ../../.test-reports:/code/.test-reports
- ../../tsconfig.json:/code/tsconfig.json
working_dir: /code
depends_on:
cache-test:

View File

@@ -1,6 +1,6 @@
import * as secp256k1 from '@noble/secp256k1'
import { createHash, createHmac, Hash } from 'crypto'
import WebSocket, { RawData } from 'ws'
import { createHmac } from 'crypto'
import { Event } from '../../../src/@types/event'
import { MessageType } from '../../../src/@types/messages'
@@ -8,6 +8,9 @@ import { serializeEvent } from '../../../src/utils/event'
import { SubscriptionFilter } from '../../../src/@types/subscription'
secp256k1.utils.sha256Sync = (...messages: Uint8Array[]) =>
messages.reduce((hash: Hash, message: Uint8Array) => hash.update(message), createHash('sha256')).digest()
export async function connect(_name: string) {
const host = 'ws://localhost:18808'
const ws = new WebSocket(host)
@@ -35,17 +38,18 @@ export async function createEvent(input: Partial<Event>, privkey: any): Promise<
tags: input.tags ?? [],
} as any
const id = Buffer.from(
await secp256k1.utils.sha256(
Buffer.from(JSON.stringify(serializeEvent(event)))
)
).toString('hex')
const id = createHash('sha256').update(
Buffer.from(JSON.stringify(serializeEvent(event)))
).digest().toString('hex')
const sig = Buffer.from(
await secp256k1.schnorr.sign(id, privkey)
secp256k1.schnorr.signSync(id, privkey)
).toString('hex')
return { id, ...event, sig }
event.id = id
event.sig = sig
return event
}
export function createIdentity(name: string) {
@@ -73,7 +77,7 @@ export async function createSubscription(
...subscriptionFilters,
])
ws.send(message, (error: Error) => {
ws.send(message, (error?: Error) => {
if (error) {
reject(error)
return
@@ -157,7 +161,7 @@ export async function waitForEventCount(
count = 1,
eose = false,
): Promise<Event[]> {
const events = []
const events: Event[] = []
return new Promise((resolve, reject) => {
ws.on('message', onMessage)

View File

@@ -8,6 +8,7 @@ import {
When,
World,
} from '@cucumber/cucumber'
import { assocPath } from 'ramda'
import WebSocket from 'ws'
import { connect, createIdentity, createSubscription } from './helpers'
@@ -25,13 +26,14 @@ let dbClient: DatabaseClient
let cacheClient: CacheClient
BeforeAll({ timeout: 6000 }, async function () {
process.env.PORT = '18808'
process.env.RELAY_PORT = '18808'
cacheClient = getCacheClient()
dbClient = getDbClient()
await dbClient.raw('SELECT 1=1')
const limits = SettingsStatic.createSettings().limits
limits.event.createdAt.maxPositiveDelta = 0
const { limits } = SettingsStatic.createSettings()
assocPath(['event', 'createdAt', 'maxPositiveDelta'], 0)(limits)
worker = workerFactory()
worker.run()
@@ -56,18 +58,18 @@ Before(async function () {
After(async function () {
this.parameters.events = {}
this.parameters.subscriptions = {}
Object.values(this.parameters.clients).forEach((ws: WebSocket) => {
for (const ws of Object.values(this.parameters.clients as Record<string, WebSocket>)) {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.close()
}
})
}
this.parameters.clients = {}
const dbClient = getDbClient()
await Promise.all(
Object.values(this.parameters.identities)
.map(async (identity: { pubkey: string }) => dbClient('events').where({ event_pubkey: Buffer.from(identity.pubkey, 'hex') }).del())
)
for (const identity of Object.values(this.parameters.identities as Record<string, { pubkey: string }>)) {
await dbClient('events').where({ event_pubkey: Buffer.from(identity.pubkey, 'hex') }).del()
}
this.parameters.identities = {}
})

View File

@@ -68,7 +68,7 @@ describe('messageHandlerFactory', () => {
})
it('throws when given an invalid message', () => {
message = []
message = [] as any
expect(() => factory([message, adapter])).to.throw(Error, 'Unknown message type: undefined')
})