Skip to content
Open

Test #600

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ on:
branches: [master]

jobs:
publish-test:
uses: ./.github/workflows/test.yml

publish-npm:
needs: publish-test
environment: npm
runs-on: ubuntu-latest
permissions:
Expand Down
14 changes: 1 addition & 13 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,4 @@ on:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18, 20, 22, 24, 26]

steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v6
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build
uses: ./.github/workflows/test.yml
29 changes: 29 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Test Pull Request

on:
workflow_call:

jobs:
build:
runs-on: ubuntu-latest

services:
nexusd:
image: ghcr.io/gammazero/nexusd:latest
ports:
- 8080:8080
command: -realm test1 -ws 0.0.0.0:8080

strategy:
matrix:
node-version: [24, 26]

container: node:${{ matrix.node-version }}-bookworm-slim

steps:
- uses: actions/checkout@v7
- run: npm ci
- run: npm run check
- run: npm test
env:
WAMP_ROUTER_URL: ws://nexusd:8080
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ commitlint.config.cjs
docker-compose.yml
prettier.config.cjs
renovate.json
test
tsconfig.json
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
},
"scripts": {
"build": "tsc",
"check": "tsc --noEmit",
"clean": "rimraf src/*.{js,d.ts}",
"start": "vite-node src/program.ts",
"test": "node --test test/**/*.test.ts",
"prepare": "husky"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Connection } from 'autobahn'
import { program } from 'commander'
import * as repl from './repl.js'
import * as repl from './repl.ts'
import 'colors'
import { createRequire } from 'node:module'

Expand Down
223 changes: 96 additions & 127 deletions src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,149 +30,80 @@ export const start = (connection: Connection) => (session: Session) => {

replServer.defineCommand('SUB', {
help: 'Subscript to a topic.',
async action(topic) {
if (!topic) {
console.error('Usage: .SUB <topic>'.red)
this.displayPrompt()
return
}

try {
await session.subscribe(topic.trim(), (args, kwargs) => {
process.stdout.write('\r')
console.info(
'PUB>'.cyan,
`${topic}>`.yellow,
inspect({ args, kwargs }, false, 10),
)
this.displayPrompt()
})
console.info('Subscribed to', topic.green)
} catch (error) {
console.error(
`Error: ${error instanceof Error ? error.message : String(error)}`
.red,
action: command('Usage: .SUB <topic>', async function (topic) {
await session.subscribe(topic, (args, kwargs) => {
process.stdout.write('\r')
console.info(
'PUB>'.cyan,
`${topic}>`.yellow,
inspect({ args, kwargs }, false, 10),
)
}

this.displayPrompt()
},
this.displayPrompt()
})
console.info('Subscribed to', topic.green)
}),
})

replServer.defineCommand('PUB', {
help: 'Publish to a topic.',
action(topic) {
if (!topic) {
console.error('Usage: .PUB <topic>'.red)
this.displayPrompt()
return
}

this.question('Enter args (YAML array)> '.magenta, (argsInput) => {
this.question(
'Enter kwargs (YAML object)> '.magenta,
async (kwargsInput) => {
try {
const args = argsInput.trim() ? YAML.parse(argsInput) : []
const kwargs = kwargsInput.trim() ? YAML.parse(kwargsInput) : {}

await session.publish(topic.trim(), args, kwargs, {
exclude_me: false,
})
console.info(
'Published to',
topic.green,
inspect({ args, kwargs }, false, 10).yellow,
)
} catch (error) {
console.error(
`Error: ${
error instanceof Error ? error.message : String(error)
}`.red,
)
} finally {
this.displayPrompt()
}
},
)
action: command('Usage: .PUB <topic>', async function (topic) {
const args = await question(this, 'Enter args (YAML array)> '.magenta, [])

const kwargs = await question(
this,
'Enter kwargs (YAML object)> '.magenta,
{},
)

await session.publish(topic.trim(), args, kwargs, {
exclude_me: false,
})
},

console.info(
'Published to',
topic.green,
inspect({ args, kwargs }, false, 10).yellow,
)
}),
})

replServer.defineCommand('REG', {
help: 'Register a RPC endpoint.',
async action(procedure) {
if (!procedure) {
console.error('Usage: . <procedure>'.red)
this.displayPrompt()
return
}

try {
await session.register(procedure.trim(), (args, kwargs) => {
process.stdout.write('\r')
console.info(
'CALL>'.cyan,
`${procedure}>`.yellow,
inspect({ args, kwargs }, false, 10),
)
this.displayPrompt()
})
console.info('Register a', procedure.green, 'endpoint')
} catch (error) {
console.error(
`Error: ${error instanceof Error ? error.message : String(error)}`
.red,
action: command('Usage: .REG <procedure>', async function (procedure) {
await session.register(procedure.trim(), (args, kwargs) => {
process.stdout.write('\r')
console.info(
'CALL>'.cyan,
`${procedure}>`.yellow,
inspect({ args, kwargs }, false, 10),
)
} finally {
this.displayPrompt()
}
},
})
console.info('Register a', procedure.green, 'endpoint')
}),
})

replServer.defineCommand('CALL', {
help: 'Call a RPC endpoint',
async action(procedure) {
if (!procedure) {
console.error('Usage: .CALL <procedure>'.red)
this.displayPrompt()
return
}

this.question('Enter args (YAML array)> '.magenta, (argsInput) => {
this.question(
'Enter kwargs (YAML object)> '.magenta,
async (kwargsInput) => {
try {
const args = argsInput.trim() ? YAML.parse(argsInput) : []
const kwargs = kwargsInput.trim() ? YAML.parse(kwargsInput) : {}

const response = await session.call(
procedure.trim(),
args,
kwargs,
)

console.info(
'Called',
procedure.green,
inspect({ args, kwargs }, false, 10).yellow,
)

console.info('RES>'.cyan, response)
} catch (error) {
console.error(
`Error: ${
error instanceof Error ? error.message : String(error)
}`.red,
)
} finally {
this.displayPrompt()
}
},
)
})
},
action: command('Usage: .CALL <procedure>', async function (procedure) {
const args = await question(this, 'Enter args (YAML array)> '.magenta, [])

const kwargs = await question(
this,
'Enter kwargs (YAML object)> '.magenta,
{},
)

const response = await session.call(procedure, args, kwargs)

console.info(
'Called',
procedure.green,
inspect({ args, kwargs }, false, 10).yellow,
)

console.info('RES>'.cyan, response)
}),
})

replServer.on('reset', reset)
Expand All @@ -184,3 +115,41 @@ export const start = (connection: Connection) => (session: Session) => {
replServer.once('exit', () => connection.close())
}
}

function command(
usage: string,
cmd: (this: repl.REPLServer, input: string) => Promise<void>,
) {
return async function (this: repl.REPLServer, input: string) {
const $input = input.trim()

if (!$input) {
console.error(usage.red)
this.displayPrompt()
return
}

try {
await cmd.call(this, $input)
} catch (error) {
console.error(
`Error: ${error instanceof Error ? error.message : String(error)}`.red,
)
} finally {
this.displayPrompt()
}
}
}

function question<T>(
replServer: repl.REPLServer,
question: string,
dflt: T,
): Promise<string> {
return new Promise<string>((resolve) => {
replServer.question(question, (answer) => {
const trimmedAnswer = answer.trim()
resolve(trimmedAnswer ? YAML.parse(trimmedAnswer) : dflt)
})
})
}
Loading