Home Information - Your home, your data, organized.
Python
/**
* Unit Tests for watchdog.js
*
* Tests the watchdog timer management functionality including:
* - Timer registration and monitoring
* - Inactive detection and restart logic
* - State management for multiple watchdog types
*/
(function() {
'use strict';
QUnit.module('Hi.watchdog', function(hooks) {
let mockTimers;
let originalSetInterval;
let callCounts;
hooks.beforeEach(function() {
// Mock setInterval to track timer creation
originalSetInterval = window.setInterval;
mockTimers = new Map();
callCounts = new Map();
window.setInterval = function(callback, delay) {
const timerId = Math.random().toString(36);
mockTimers.set(timerId, { callback, delay });
return timerId;
};
window.clearInterval = function(timerId) {
mockTimers.delete(timerId);
};
});
hooks.afterEach(function() {
// Restore original setInterval
window.setInterval = originalSetInterval;
// Clear any test state from the actual watchdog module
// Note: We can't easily reset the internal state of the watchdog module
// so these tests focus on testing the interface and expected behaviors
});
QUnit.test('watchdog module exists and has expected interface', function(assert) {
assert.ok(Hi.watchdog, 'Watchdog module exists');
assert.equal(typeof Hi.watchdog.add, 'function', 'add method exists');
assert.equal(typeof Hi.watchdog.ok, 'function', 'ok method exists');
});
QUnit.test('add method accepts parameters correctly', function(assert) {
let initCalled = false;
const mockInit = function() {
initCalled = true;
callCounts.set('test-type', (callCounts.get('test-type') || 0) + 1);
};
// This should not throw an error
Hi.watchdog.add('test-type', mockInit, 1000);
assert.ok(true, 'add method accepts valid parameters without error');
// Verify a timer was created
assert.ok(mockTimers.size > 0, 'Timer was created');
});
QUnit.test('ok method accepts type parameter correctly', function(assert) {
// This should not throw an error
Hi.watchdog.ok('test-type');
assert.ok(true, 'ok method accepts type parameter without error');
});
// Since the watchdog module maintains internal state that's hard to reset,
// we'll create a simplified version to test the core logic
QUnit.test('watchdog logic simulation - basic functionality', function(assert) {
// Simulate the watchdog's internal logic
const watchdogState = {
inactive: {},
functions: {},
timers: {}
};
const mockWatchdog = {
add: function(type, initFunction, normalRefreshMs) {
watchdogState.inactive[type] = false;
watchdogState.functions[type] = initFunction;
watchdogState.timers[type] = setInterval(() => {
this.check(type);
}, 2 * normalRefreshMs);
},
check: function(type) {
if (!(type in watchdogState.inactive)) {
return false; // Error case
}
if (watchdogState.inactive[type]) {
// Restart the function
if (watchdogState.functions[type]) {
watchdogState.functions[type]();
}
watchdogState.inactive[type] = false;
} else {
watchdogState.inactive[type] = true;
}
return true;
},
ok: function(type) {
watchdogState.inactive[type] = false;
}
};
let restartCount = 0;
const mockInitFunction = function() {
restartCount++;
};
// Add a watchdog
mockWatchdog.add('test', mockInitFunction, 1000);
assert.false(watchdogState.inactive['test'], 'Initially marked as active');
assert.equal(restartCount, 0, 'Init function not called yet');
// First check - should mark as inactive
mockWatchdog.check('test');
assert.true(watchdogState.inactive['test'], 'Marked as inactive after first check');
assert.equal(restartCount, 0, 'Init function not called on first check');
// Second check - should detect inactive and restart
mockWatchdog.check('test');
assert.false(watchdogState.inactive['test'], 'Marked as active after restart');
assert.equal(restartCount, 1, 'Init function called to restart');
// Call ok to reset state
mockWatchdog.ok('test');
assert.false(watchdogState.inactive['test'], 'Reset to active via ok call');
// Next check should mark inactive again
mockWatchdog.check('test');
assert.true(watchdogState.inactive['test'], 'Marked inactive again after ok reset');
});
QUnit.test('watchdog logic simulation - multiple types', function(assert) {
const watchdogState = {
inactive: {},
functions: {},
counters: {}
};
const mockWatchdog = {
add: function(type, initFunction, normalRefreshMs) {
watchdogState.inactive[type] = false;
watchdogState.functions[type] = initFunction;
watchdogState.counters[type] = 0;
},
check: function(type) {
if (watchdogState.inactive[type]) {
watchdogState.functions[type]();
watchdogState.inactive[type] = false;
} else {
watchdogState.inactive[type] = true;
}
},
ok: function(type) {
watchdogState.inactive[type] = false;
}
};
// Add multiple watchdog types
mockWatchdog.add('type1', () => { watchdogState.counters.type1++; }, 1000);
mockWatchdog.add('type2', () => { watchdogState.counters.type2++; }, 2000);
assert.equal(Object.keys(watchdogState.inactive).length, 2, 'Two types registered');
assert.false(watchdogState.inactive.type1, 'Type1 initially active');
assert.false(watchdogState.inactive.type2, 'Type2 initially active');
// Check type1 twice - should restart
mockWatchdog.check('type1');
mockWatchdog.check('type1');
assert.equal(watchdogState.counters.type1, 1, 'Type1 restarted once');
assert.equal(watchdogState.counters.type2, 0, 'Type2 not affected');
// Reset type1 and check type2
mockWatchdog.ok('type1');
mockWatchdog.check('type2');
mockWatchdog.check('type2');
assert.equal(watchdogState.counters.type1, 1, 'Type1 count unchanged');
assert.equal(watchdogState.counters.type2, 1, 'Type2 restarted once');
});
QUnit.test('watchdog logic simulation - error handling', function(assert) {
const watchdogState = {
inactive: {},
functions: {}
};
const mockWatchdog = {
check: function(type) {
if (!(type in watchdogState.inactive)) {
return false; // Simulate error for unknown type
}
if (watchdogState.inactive[type]) {
if (watchdogState.functions[type]) {
watchdogState.functions[type]();
}
watchdogState.inactive[type] = false;
} else {
watchdogState.inactive[type] = true;
}
return true;
}
};
// Check unknown type - should handle gracefully
const result = mockWatchdog.check('unknown-type');
assert.false(result, 'Returns false for unknown type');
});
QUnit.test('timer interval calculation', function(assert) {
let capturedDelay;
window.setInterval = function(callback, delay) {
capturedDelay = delay;
return 'mock-timer-id';
};
Hi.watchdog.add('interval-test', function() {}, 1000);
// Watchdog should set timer to 2x the normal refresh rate
assert.equal(capturedDelay, 2000, 'Timer interval is 2x the normal refresh rate');
});
});
})();