|
| 1 | +import { expect, test, describe } from "@jest/globals"; |
| 2 | +import * as tedge from "../../common/tedge"; |
| 3 | +import * as flow from "../src/main"; |
| 4 | + |
| 5 | +describe("map collectd messages", () => { |
| 6 | + test("simple message", () => { |
| 7 | + const output = flow.onMessage( |
| 8 | + { |
| 9 | + time: new Date("2026-01-01"), |
| 10 | + topic: "collectd/localhost/temperature/temp1", |
| 11 | + payload: "1776866602.745439166:23.7", |
| 12 | + }, |
| 13 | + tedge.createContext({}), |
| 14 | + ); |
| 15 | + expect(output).toHaveLength(1); |
| 16 | + expect(output[0].topic).toBe("te/device/main///m/collectd"); |
| 17 | + const payload = tedge.decodeJsonPayload(output[0].payload); |
| 18 | + expect(payload).toEqual({ |
| 19 | + time: 1776866602.745439166, |
| 20 | + temperature: { temp1: 23.7 }, |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + test("combined values", () => { |
| 25 | + const output = flow.onMessage( |
| 26 | + { |
| 27 | + time: new Date("2026-01-01"), |
| 28 | + topic: "collectd/localhost/temperature/temp", |
| 29 | + payload: "1776866602.745439166:23.7:24.0:25.8", |
| 30 | + }, |
| 31 | + tedge.createContext({}), |
| 32 | + ); |
| 33 | + expect(output).toHaveLength(3); |
| 34 | + |
| 35 | + expect(output[0].topic).toBe("te/device/main///m/collectd"); |
| 36 | + expect(tedge.decodeJsonPayload(output[0].payload)).toEqual({ |
| 37 | + time: 1776866602.745439166, |
| 38 | + temperature: { temp_val0: 23.7 }, |
| 39 | + }); |
| 40 | + |
| 41 | + expect(output[1].topic).toBe("te/device/main///m/collectd"); |
| 42 | + expect(tedge.decodeJsonPayload(output[1].payload)).toEqual({ |
| 43 | + time: 1776866602.745439166, |
| 44 | + temperature: { temp_val1: 24.0 }, |
| 45 | + }); |
| 46 | + |
| 47 | + expect(output[2].topic).toBe("te/device/main///m/collectd"); |
| 48 | + expect(tedge.decodeJsonPayload(output[2].payload)).toEqual({ |
| 49 | + time: 1776866602.745439166, |
| 50 | + temperature: { temp_val2: 25.8 }, |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
0 commit comments