Skip to content

Commit f5b94a2

Browse files
authored
Merge pull request #1 from FlowFuse/first-tests
First tests
2 parents 79910a0 + 21e8cd9 commit f5b94a2

File tree

9 files changed

+545
-13
lines changed

9 files changed

+545
-13
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"extends": [
1010
"standard"
1111
],
12-
"plugins": [ "eslint-plugin-html", "no-only-tests" ],
12+
"plugins": [ "no-only-tests" ],
1313
"parserOptions": {
1414
"ecmaVersion": 13
1515
},

.github/workflows/tests.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
jobs:
10+
unit:
11+
name: Unit tests
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
with:
17+
fetch-depth: 0
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v4
20+
- name: Install Dependencies
21+
run: npm ci
22+
- name: Lint
23+
run: npm run lint
24+
- name: Unit tests
25+
run: npm run test
26+
27+

index.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
11
const express = require('express')
2-
const mqtt = require('mqtt')
2+
const bodyParser = require('body-parser')
3+
const { API } = require('./lib/api.js')
4+
const { healthz } = require('./lib/health.js')
35

6+
const port = 3500
7+
8+
const app = express()
9+
app.use(bodyParser.json({}))
10+
11+
app.get('/healthz', healthz)
12+
13+
const options = {
14+
forgeURL: process.env.FORGE_URL,
15+
team: process.env.FORGE_TEAM_ID,
16+
broker: process.env.FORGE_BROKER_ID,
17+
token: process.env.FORGE_TEAM_TOKEN
18+
}
19+
20+
const api = new API(app, options)
21+
22+
process.on('SIGTERM', async () => {
23+
await api.stop()
24+
process.exit(0)
25+
})
26+
27+
app.listen(port, () => {
28+
console.log(`listening on port ${port}`)
29+
})

lib/agent.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const got = require('got')
2+
const mqtt = require('mqtt')
3+
4+
class Agent {
5+
constructor (options) {
6+
this.options = options
7+
this.topics = {}
8+
}
9+
10+
async connect () {
11+
const agent = this
12+
this.connected = false
13+
14+
try {
15+
this.creds = await got.get(`${this.options.forgeURL}/api/v1/team/${this.options.team}/broker/${this.options.broker}/creds`, {
16+
headers: {
17+
Authorization: `Bearer ${this.options.token}`
18+
}
19+
}).json()
20+
21+
this.client = mqtt.connect(`${this.creds.protocol}//${this.creds.hostname}:${this.creds.port}`, this.creds)
22+
this.client.on('connect', function () {
23+
agent.connected = true
24+
// console.log('connected')
25+
agent.client.subscribe('#')
26+
})
27+
this.client.on('reconnect', function () {
28+
console.log('reconnecting')
29+
})
30+
this.client.on('close', function () {
31+
// console.log('closed')
32+
})
33+
this.client.on('disconnect', function () {})
34+
this.client.on('error', function (error) {
35+
console.log('error', error)
36+
})
37+
this.client.on('message', function (topic) {
38+
// console.log(topic)
39+
agent.topics[topic] = Date.now()
40+
})
41+
} catch (err) {
42+
console.log(err)
43+
throw err
44+
}
45+
}
46+
47+
async start () {
48+
if (this.client) {
49+
await this.stop()
50+
}
51+
await this.connect()
52+
}
53+
54+
async stop () {
55+
if (this.client) {
56+
this.client.end()
57+
}
58+
this.connected = false
59+
}
60+
61+
state () {
62+
return {
63+
connected: this.connected,
64+
topics: this.topics
65+
}
66+
}
67+
}
68+
69+
module.exports = {
70+
Agent
71+
}

lib/api.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class API {
2+
constructor (app, options) {
3+
app.get('/', (request, reply) => {
4+
})
5+
}
6+
7+
async stop () {
8+
9+
}
10+
}
11+
12+
module.exports = {
13+
API
14+
}

lib/health.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const healthz = async function (request, response) {
2+
response.send({})
3+
}
4+
5+
module.exports = {
6+
healthz
7+
}

0 commit comments

Comments
 (0)