Skip to content

Commit

Permalink
Fix tests for discovery and availability using QOS1
Browse files Browse the repository at this point in the history
  • Loading branch information
ruifung committed Aug 27, 2023
1 parent 1ea2734 commit e6ee457
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 74 deletions.
34 changes: 17 additions & 17 deletions test/availability.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ describe('Availability', () => {

it('Should publish availabilty on startup for device where it is enabled for', async () => {
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
'online', {retain: true, qos: 0}, expect.any(Function));
'online', {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability',
'online', {retain: true, qos: 0}, expect.any(Function));
'online', {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).not.toHaveBeenCalledWith('zigbee2mqtt/bulb_color_2/availability',
'online', {retain: true, qos: 0}, expect.any(Function));
'online', {retain: true, qos: 1}, expect.any(Function));
});

it('Should publish offline for active device when not seen for 10 minutes', async () => {
Expand All @@ -77,7 +77,7 @@ describe('Availability', () => {
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(1);
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(1, true);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
'offline', {retain: true, qos: 1}, expect.any(Function));
});

it('Shouldnt do anything for a device when availability: false is set for device', async () => {
Expand All @@ -93,7 +93,7 @@ describe('Availability', () => {
await advancedTime(utils.hours(26));
expect(devices.remote.ping).toHaveBeenCalledTimes(0);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
'offline', {retain: true, qos: 1}, expect.any(Function));
});

it('Should reset ping timer when device last seen changes for active device', async () => {
Expand All @@ -111,7 +111,7 @@ describe('Availability', () => {
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(1);
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(1, true);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
'offline', {retain: true, qos: 1}, expect.any(Function));
});

it('Should ping again when first ping fails', async () => {
Expand All @@ -132,7 +132,7 @@ describe('Availability', () => {
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(1, true);
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(2, false);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
'offline', {retain: true, qos: 1}, expect.any(Function));
});

it('Should reset ping timer when device last seen changes for passive device', async () => {
Expand All @@ -150,21 +150,21 @@ describe('Availability', () => {
await advancedTime(utils.hours(3));
expect(devices.remote.ping).toHaveBeenCalledTimes(0);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
'offline', {retain: true, qos: 1}, expect.any(Function));
});

it('Should immediately mark device as online when it lastSeen changes', async () => {
MQTT.publish.mockClear();

await advancedTime(utils.minutes(15));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
'offline', {retain: true, qos: 1}, expect.any(Function));

devices.bulb_color.lastSeen = Date.now();
await zigbeeHerdsman.events.lastSeenChanged({device: devices.bulb_color});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
'online', {retain: true, qos: 0}, expect.any(Function));
'online', {retain: true, qos: 1}, expect.any(Function));
});

it('Should allow to change availability timeout via device options', async () => {
Expand Down Expand Up @@ -283,12 +283,12 @@ describe('Availability', () => {
await flushPromises();

expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability',
null, {retain: true, qos: 0}, expect.any(Function));
null, {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_new_name/availability',
'online', {retain: true, qos: 0}, expect.any(Function));
'online', {retain: true, qos: 1}, expect.any(Function));
await advancedTime(utils.hours(12));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_new_name/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
'offline', {retain: true, qos: 1}, expect.any(Function));
});

it('Should publish availabiltiy payload in JSON format', async () => {
Expand All @@ -298,7 +298,7 @@ describe('Availability', () => {
await advancedTime(utils.hours(26));
expect(devices.remote.ping).toHaveBeenCalledTimes(0);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability',
stringify({state: 'offline'}), {retain: true, qos: 0}, expect.any(Function));
stringify({state: 'offline'}), {retain: true, qos: 1}, expect.any(Function));
});

it('Deprecated - should allow to block via advanced.availability_blocklist', async () => {
Expand Down Expand Up @@ -332,16 +332,16 @@ describe('Availability', () => {
settings.set(['devices', devices.bulb_color_2.ieeeAddr, 'availability'], true);
await resetExtension();
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/group_tradfri_remote/availability',
'online', {retain: true, qos: 0}, expect.any(Function));
'online', {retain: true, qos: 1}, expect.any(Function));
MQTT.publish.mockClear();
await advancedTime(utils.minutes(12));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/group_tradfri_remote/availability',
'offline', {retain: true, qos: 0}, expect.any(Function));
'offline', {retain: true, qos: 1}, expect.any(Function));
MQTT.publish.mockClear();
devices.bulb_color_2.lastSeen = Date.now();
await zigbeeHerdsman.events.lastSeenChanged({device: devices.bulb_color_2});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/group_tradfri_remote/availability',
'online', {retain: true, qos: 0}, expect.any(Function));
'online', {retain: true, qos: 1}, expect.any(Function));
});
});
Loading

0 comments on commit e6ee457

Please sign in to comment.