|
| 1 | +import { createTRPCProxyClient } from '@trpc/client'; |
| 2 | +import Aedes from 'aedes'; |
| 3 | +import { once } from 'events'; |
| 4 | +import mqtt from 'mqtt'; |
| 5 | +import { createServer } from 'net'; |
| 6 | + |
| 7 | +import { createMQTTHandler } from '../src/adapter'; |
| 8 | +import { mqttLink } from '../src/link'; |
| 9 | +import { AppRouter, appRouter } from './appRouter'; |
| 10 | + |
| 11 | +const requestTopic = 'rpc/request'; |
| 12 | + |
| 13 | +const aedes = new Aedes(); |
| 14 | +// aedes.on('publish', (packet, client) => console.log(packet.topic, packet.payload.toString())); |
| 15 | +const broker = createServer(aedes.handle); |
| 16 | +broker.listen(1883); |
| 17 | +const mqttClient = mqtt.connect('mqtt://localhost'); |
| 18 | + |
| 19 | +createMQTTHandler({ |
| 20 | + client: mqttClient, |
| 21 | + requestTopic, |
| 22 | + router: appRouter |
| 23 | +}); |
| 24 | + |
| 25 | +const client = createTRPCProxyClient<AppRouter>({ |
| 26 | + links: [ |
| 27 | + mqttLink({ |
| 28 | + client: mqttClient, |
| 29 | + requestTopic |
| 30 | + }) |
| 31 | + ] |
| 32 | +}); |
| 33 | + |
| 34 | +beforeAll(async () => { |
| 35 | + await once(broker, 'listening'); |
| 36 | + await once(mqttClient, 'connect'); |
| 37 | +}); |
| 38 | + |
| 39 | +test('broker is listening', () => { |
| 40 | + expect(broker.listening).toBe(true); |
| 41 | +}); |
| 42 | + |
| 43 | +test('mqtt client is connected', () => { |
| 44 | + expect(mqttClient.connected).toBe(true); |
| 45 | +}); |
| 46 | + |
| 47 | +test('greet query', async () => { |
| 48 | + const greeting = await client.greet.query('world'); |
| 49 | + expect(greeting).toEqual({ greeting: 'hello, world!' }); |
| 50 | +}); |
| 51 | + |
| 52 | +test('countUp mutation', async () => { |
| 53 | + const addOne = await client.countUp.mutate(1); |
| 54 | + expect(addOne).toBe(1); |
| 55 | + |
| 56 | + const addTwo = await client.countUp.mutate(2); |
| 57 | + expect(addTwo).toBe(3); |
| 58 | +}); |
| 59 | + |
| 60 | +afterAll(async () => { |
| 61 | + mqttClient.end(); |
| 62 | + broker.close(); |
| 63 | + aedes.close(); |
| 64 | +}); |
0 commit comments