215 lines
5.6 KiB
JavaScript
215 lines
5.6 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import {
|
|
HomeAssistantAuthError,
|
|
HomeAssistantConnectionError,
|
|
updateCountFromEntities,
|
|
} from 'dashtab/services/home-assistant';
|
|
|
|
class FakeSocket {
|
|
constructor() {
|
|
this.sent = [];
|
|
this.listeners = { message: [], error: [], close: [], open: [] };
|
|
this.closed = false;
|
|
}
|
|
|
|
addEventListener(type, handler) {
|
|
this.listeners[type].push(handler);
|
|
}
|
|
|
|
removeEventListener(type, handler) {
|
|
this.listeners[type] = this.listeners[type].filter(
|
|
(candidate) => candidate !== handler
|
|
);
|
|
}
|
|
|
|
send(data) {
|
|
this.sent.push(JSON.parse(data));
|
|
}
|
|
|
|
close() {
|
|
this.closed = true;
|
|
}
|
|
|
|
emit(type, event = {}) {
|
|
for (const handler of [...this.listeners[type]]) {
|
|
handler(event);
|
|
}
|
|
}
|
|
|
|
emitMessage(message) {
|
|
this.emit('message', { data: JSON.stringify(message) });
|
|
}
|
|
|
|
find(type) {
|
|
return this.sent.find((message) => message.type === type);
|
|
}
|
|
}
|
|
|
|
module('Unit | Service | home-assistant', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
function setup(overrides = {}) {
|
|
const service = this.owner.lookup('service:home-assistant');
|
|
const socket = new FakeSocket();
|
|
service.createSocket = () => socket;
|
|
return { service, socket, ...overrides };
|
|
}
|
|
|
|
test('it exists', function (assert) {
|
|
assert.ok(this.owner.lookup('service:home-assistant'));
|
|
});
|
|
|
|
test('updateCountFromEntities counts installable pending updates', function (assert) {
|
|
const event = {
|
|
a: {
|
|
'update.a': { s: 'on', a: { supported_features: 1 } },
|
|
'update.b': { s: 'off', a: { supported_features: 1 } },
|
|
'update.c': { s: 'on', a: { supported_features: 0 } },
|
|
'update.d': { s: 'on', a: {} },
|
|
},
|
|
};
|
|
|
|
assert.strictEqual(updateCountFromEntities(event), 2);
|
|
assert.strictEqual(updateCountFromEntities(null), 0);
|
|
});
|
|
|
|
test('fetchSummary sums persistent, repair and update counts', async function (assert) {
|
|
const { service, socket } = setup.call(this);
|
|
|
|
const promise = service.fetchSummary({
|
|
url: 'https://ha.example.com:8123',
|
|
token: 'tok',
|
|
});
|
|
|
|
socket.emit('open');
|
|
const auth = socket.find('auth');
|
|
assert.deepEqual(auth, { type: 'auth', access_token: 'tok' });
|
|
|
|
socket.emitMessage({ type: 'auth_ok' });
|
|
|
|
const persistent = socket.find('persistent_notification/get');
|
|
const repairs = socket.find('repairs/list_issues');
|
|
const subscription = socket.find('subscribe_entities');
|
|
assert.ok(persistent);
|
|
assert.ok(repairs);
|
|
assert.deepEqual(subscription.include, { domains: ['update'] });
|
|
|
|
socket.emitMessage({
|
|
id: persistent.id,
|
|
type: 'result',
|
|
success: true,
|
|
result: [{ notification_id: 'a' }, { notification_id: 'b' }],
|
|
});
|
|
socket.emitMessage({
|
|
id: repairs.id,
|
|
type: 'result',
|
|
success: true,
|
|
result: { issues: [{ ignored: false }, { ignored: true }] },
|
|
});
|
|
socket.emitMessage({
|
|
id: subscription.id,
|
|
type: 'result',
|
|
success: true,
|
|
result: null,
|
|
});
|
|
socket.emitMessage({
|
|
id: subscription.id,
|
|
type: 'event',
|
|
event: {
|
|
a: {
|
|
'update.a': { s: 'on', a: { supported_features: 1 } },
|
|
'update.b': { s: 'off', a: { supported_features: 1 } },
|
|
},
|
|
},
|
|
});
|
|
|
|
assert.deepEqual(await promise, {
|
|
persistentCount: 2,
|
|
repairsCount: 1,
|
|
updatesCount: 1,
|
|
unreadCount: 4,
|
|
});
|
|
assert.true(socket.closed);
|
|
});
|
|
|
|
test('fetchSummary tolerates a missing repair list', async function (assert) {
|
|
const { service, socket } = setup.call(this);
|
|
|
|
const promise = service.fetchSummary({
|
|
url: 'https://ha.example.com:8123',
|
|
token: 'tok',
|
|
});
|
|
|
|
socket.emitMessage({ type: 'auth_required' });
|
|
socket.emitMessage({ type: 'auth_ok' });
|
|
|
|
const persistent = socket.find('persistent_notification/get');
|
|
const repairs = socket.find('repairs/list_issues');
|
|
const subscription = socket.find('subscribe_entities');
|
|
|
|
socket.emitMessage({
|
|
id: persistent.id,
|
|
type: 'result',
|
|
success: true,
|
|
result: [],
|
|
});
|
|
socket.emitMessage({
|
|
id: repairs.id,
|
|
type: 'result',
|
|
success: false,
|
|
error: { message: 'Unauthorized' },
|
|
});
|
|
socket.emitMessage({
|
|
id: subscription.id,
|
|
type: 'result',
|
|
success: true,
|
|
result: null,
|
|
});
|
|
socket.emitMessage({
|
|
id: subscription.id,
|
|
type: 'event',
|
|
event: { a: {} },
|
|
});
|
|
|
|
const summary = await promise;
|
|
assert.strictEqual(summary.repairsCount, 0);
|
|
assert.strictEqual(summary.unreadCount, 0);
|
|
});
|
|
|
|
test('fetchSummary rejects an invalid token', async function (assert) {
|
|
const { service, socket } = setup.call(this);
|
|
|
|
const promise = service.fetchSummary({
|
|
url: 'https://ha.example.com:8123',
|
|
token: 'bad',
|
|
});
|
|
|
|
socket.emitMessage({ type: 'auth_required' });
|
|
socket.emitMessage({ type: 'auth_invalid' });
|
|
|
|
await assert.rejects(promise, HomeAssistantAuthError);
|
|
});
|
|
|
|
test('fetchSummary reports a configuration error without a token', async function (assert) {
|
|
const { service } = setup.call(this);
|
|
|
|
await assert.rejects(
|
|
service.fetchSummary({ url: 'https://ha.example.com:8123' }),
|
|
HomeAssistantConnectionError
|
|
);
|
|
});
|
|
|
|
test('fetchSummary times out when the socket never responds', async function (assert) {
|
|
const { service } = setup.call(this);
|
|
|
|
await assert.rejects(
|
|
service.fetchSummary(
|
|
{ url: 'https://ha.example.com:8123', token: 'tok' },
|
|
{ timeout: 5 }
|
|
),
|
|
HomeAssistantConnectionError
|
|
);
|
|
});
|
|
});
|