Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
Add 2 server, no-interfollow topology test (plus run test server in s…
Browse files Browse the repository at this point in the history
…ub process)
  • Loading branch information
pfrazee committed Jan 27, 2021
1 parent e15674c commit 0036392
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 23 deletions.
11 changes: 9 additions & 2 deletions api/accounts.js
@@ -1,6 +1,6 @@
import { createValidator } from '../lib/schemas.js'
import { v4 as uuidv4 } from 'uuid'
import { publicServerDb, privateServerDb } from '../db/index.js'
import { publicServerDb, privateServerDb, createUser } from '../db/index.js'
import { constructUserUrl, constructUserId } from '../lib/strings.js'

const registerParam = createValidator({
Expand All @@ -12,7 +12,14 @@ const registerParam = createValidator({
}
})

export function setup (wsServer) {
export function setup (wsServer, {debugMode} = {}) {
if (debugMode) {
wsServer.register('accounts.createDebugUser', async (params) => {
const {userId} = await createUser(params[0])
return {userId}
})
}

wsServer.register('accounts.whoami', async (params, client) => {
if (client.auth) {
return {
Expand Down
4 changes: 2 additions & 2 deletions api/index.js
Expand Up @@ -7,7 +7,7 @@ import * as notifications from './notifications.js'
import * as users from './users.js'
import * as votes from './votes.js'

export function setup (wsServer) {
export function setup (wsServer, opts) {
const origRegister = wsServer.register
wsServer.register = function (methodName, methodHandler) {
origRegister.call(this, methodName, async (params, socket_id) => {
Expand All @@ -17,7 +17,7 @@ export function setup (wsServer) {
})
}

accounts.setup(wsServer)
accounts.setup(wsServer, opts)
comments.setup(wsServer)
follows.setup(wsServer)
posts.setup(wsServer)
Expand Down
2 changes: 1 addition & 1 deletion api/posts.js
@@ -1,6 +1,6 @@
import { createValidator } from '../lib/schemas.js'
import { publicServerDb, publicUserDbs } from '../db/index.js'
import { constructEntryUrl, parseEntryUrl, constructUserUrl } from '../lib/strings.js'
import { constructEntryUrl, parseEntryUrl, constructUserUrl, getOrigin } from '../lib/strings.js'
import { fetchUserId } from '../lib/network.js'

const listParam = createValidator({
Expand Down
15 changes: 15 additions & 0 deletions bin.js
Expand Up @@ -13,6 +13,21 @@ const match = subcommand({
start({debugMode: true, port: 3000})
}
},
{
name: 'start-test',
command: args => {
if (!args.port) throw new Error('--port required')
if (!args.configDir) throw new Error('--configDir required')
if (!args.domain) throw new Error('--domain required')
start({
debugMode: true,
simulateHyperspace: true,
port: args.port,
configDir: args.configDir,
domain: args.domain
})
}
},
{
name: 'create-user',
command: async args => {
Expand Down
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -11,9 +11,9 @@ const DEFAULT_USER_THUMB_PATH = path.join(path.dirname(fileURLToPath(import.meta

let app

export async function start ({port, configDir, simulateHyperspace}) {
export async function start ({port, configDir, simulateHyperspace, domain, debugMode}) {
configDir = configDir || path.join(os.homedir(), '.ctzn')
setOrigin(`http://localhost:${port}`)
setOrigin(`http://${domain || 'localhost'}:${port}`)

app = express()
app.set('view engine', 'ejs')
Expand Down Expand Up @@ -90,7 +90,7 @@ export async function start ({port, configDir, simulateHyperspace}) {
})

const wsServer = new WebSocketServer({server})
api.setup(wsServer)
api.setup(wsServer, {debugMode})

// process.on('SIGINT', close)
// process.on('SIGTERM', close)
Expand Down
42 changes: 29 additions & 13 deletions tests/_util.js
@@ -1,37 +1,48 @@
import { start } from '../index.js'
import randomPort from 'random-port'
import { Client as WsClient } from 'rpc-websockets'
import tmp from 'tmp-promise'
import { parseEntryUrl } from '../lib/strings.js'
import { spawn } from 'child_process'
import * as path from 'path'
import { fileURLToPath } from 'url'

let nServer = 1
export async function createServer () {
const tmpdir = await tmp.dir({unsafeCleanup: true})
const port = await new Promise(r => randomPort(r))
const inst = await start({
debugMode: true,
port,
configDir: tmpdir.path,
simulateHyperspace: true
})
const domain = `dev${nServer++}.localhost`
console.log('Storing config in', tmpdir.path)

const binPath = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'bin.js')
const serverProcess = spawn(
'node',
[binPath, 'start-test', '--port', port, '--configDir', tmpdir.path, '--domain', domain],
{stdio: [process.stdin, process.stdout, process.stderr]}
)

const client = new WsClient(`ws://localhost:${port}/`)
const api = await createRpcApi(client)

return {
db: inst.db,
domain,
client,
api,
process: serverProcess,
close: async () => {
await inst.close()
const p = new Promise(r => {
if (serverProcess.exitCode !== null) r()
serverProcess.on('exit', r)
})
serverProcess.kill()
await p
await tmpdir.cleanup()
}
}
}

async function createRpcApi (ws) {
await new Promise(resolve => ws.on('open', resolve))
return new Proxy({}, {
return new Proxy({url: ws.address}, {
get (target, prop) {
// generate rpc calls as needed
if (!(prop in target)) {
Expand Down Expand Up @@ -158,6 +169,9 @@ export class TestFramework {

async getRandomParentFor (inst, post) {
if (randRange(0, 1) === 0) return undefined // 1/2 chance of no parent
if (Array.isArray(inst)) {
inst = inst[randRange(0, inst.length - 1)]
}
let comments = flattenThread(await inst.api.comments.getThread(post.url))
return comments[randRange(0, comments.length - 1)]
}
Expand All @@ -183,8 +197,10 @@ export class TestFramework {
return ids
}

getThread (post) {
const comments = this.allComments.filter(c => c.value.subjectUrl === post.url)
getThread (post, filterFn) {
const comments = this.allComments.filter(c => {
return (c.value.subjectUrl === post.url && (!filterFn || filterFn(c)))
})
return commentEntriesToThread(comments) || []
}

Expand Down Expand Up @@ -233,7 +249,7 @@ class TestUser {
}

async setup () {
const {userId} = await this.inst.db.createUser({
const {userId} = await this.inst.api.accounts.createDebugUser({
username: this.username,
email: `${this.username}@email.com`,
profile: {
Expand Down
2 changes: 1 addition & 1 deletion tests/profiles.js
Expand Up @@ -9,7 +9,7 @@ test.before(async () => {
close = inst.close
api = inst.api

await inst.db.createUser({
await api.accounts.createDebugUser({
username: 'bobo',
email: 'bobo@roberts.com',
profile: {
Expand Down

0 comments on commit 0036392

Please sign in to comment.