Files
dashtab/tests/unit/services/notifications-test.js

907 lines
26 KiB
JavaScript

import { module, test } from 'qunit';
import { setupTest } from 'dashtab/tests/helpers';
import {
HomeAssistantAuthError,
HomeAssistantConnectionError,
} from 'dashtab/services/home-assistant';
module('Unit | Service | notifications', function (hooks) {
setupTest(hooks);
let originalFetch;
let originalConsoleError;
hooks.beforeEach(async function () {
originalFetch = globalThis.fetch;
originalConsoleError = console.error;
console.error = () => {};
const storage = this.owner.lookup('service:storage');
await storage.clear('integrations');
const siteIcons = this.owner.lookup('service:site-icons');
siteIcons.iconFor = async () => undefined;
});
hooks.afterEach(function () {
globalThis.fetch = originalFetch;
console.error = originalConsoleError;
});
test('it exists', function (assert) {
let service = this.owner.lookup('service:notifications');
assert.ok(service);
});
test('stateFor defaults to loading for pollable sites and ready for plain links', function (assert) {
const service = this.owner.lookup('service:notifications');
const pollable = service.stateFor({ id: 'a', type: 'discourse' });
assert.strictEqual(pollable.status, 'loading');
assert.strictEqual(pollable.unreadCount, 0);
const plain = service.stateFor({ id: 'b', type: 'unknown' });
assert.strictEqual(plain.status, 'ready');
assert.strictEqual(plain.unreadCount, 0);
service.stopPolling();
});
test('setup starts polling when a pollable site is connected', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
assert.ok(service.intervalId);
service.stopPolling();
});
test('isPollable is true only for integrations with an adapter', function (assert) {
const service = this.owner.lookup('service:notifications');
assert.true(service.isPollable({ type: 'discourse' }));
assert.true(service.isPollable({ type: 'feedbin' }));
assert.true(service.isPollable({ type: 'github' }));
assert.true(service.isPollable({ type: 'gitea' }));
assert.true(service.isPollable({ type: 'homeassistant' }));
assert.false(service.isPollable({ type: 'unknown' }));
service.stopPolling();
});
test('fetchSite normalizes a discourse response', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
clientId: 'abc',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
let requestedUrl;
let requestedHeaders;
globalThis.fetch = async (url, options) => {
requestedUrl = url;
requestedHeaders = options.headers;
return {
ok: true,
json: async () => ({
notifications: [
{
id: 1,
read: false,
created_at: '2024-01-02T00:00:00.000Z',
topic_id: 5,
data: { topic_title: 'Hello there' },
},
{
id: 2,
read: true,
created_at: '2024-01-01T00:00:00.000Z',
topic_id: 4,
data: { badge_name: 'Nice' },
},
],
}),
};
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(
requestedUrl,
'https://forum.example.com/notifications.json'
);
assert.strictEqual(requestedHeaders['User-Api-Key'], 'key');
assert.strictEqual(requestedHeaders['User-Api-Client-Id'], 'abc');
const state = service.stateFor(site);
assert.strictEqual(state.status, 'ready');
assert.strictEqual(state.unreadCount, 1);
assert.strictEqual(state.items.length, 2);
assert.strictEqual(state.items[0].title, 'Hello there');
assert.strictEqual(state.items[1].title, 'Nice');
service.stopPolling();
});
test('fetchSite reads the Feedbin unread count from the record count header', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'feedbin', {
type: 'feedbin',
status: 'active',
email: 'me@example.com',
token: 'dG9rZW4=',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
let requestedUrl;
let requestedHeaders;
globalThis.fetch = async (url, options) => {
requestedUrl = url;
requestedHeaders = options.headers;
return {
ok: true,
headers: {
get: (name) => (name === 'X-Feedbin-Record-Count' ? '42' : null),
},
};
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(
requestedUrl,
'https://api.feedbin.com/v2/entries.json?read=false&per_page=1'
);
assert.strictEqual(requestedHeaders['Authorization'], 'Basic dG9rZW4=');
assert.strictEqual(requestedHeaders['Accept'], 'application/json');
const state = service.stateFor(site);
assert.strictEqual(state.status, 'ready');
assert.strictEqual(state.unreadCount, 42);
assert.deepEqual(state.items, []);
service.stopPolling();
});
test('fetchSite derives the Feedbin API host from a self-hosted URL', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'feedbin', {
type: 'feedbin',
status: 'active',
url: 'https://feedbin.example.com',
email: 'me@example.com',
token: 'dG9rZW4=',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
let requestedUrl;
globalThis.fetch = async (url) => {
requestedUrl = url;
return { ok: true, headers: { get: () => '7' } };
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(
requestedUrl,
'https://api.feedbin.example.com/v2/entries.json?read=false&per_page=1'
);
assert.strictEqual(service.stateFor(site).unreadCount, 7);
service.stopPolling();
});
test('fetchSite treats a missing Feedbin count header as zero', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'feedbin', {
type: 'feedbin',
status: 'active',
email: 'me@example.com',
token: 'dG9rZW4=',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({
ok: true,
headers: { get: () => null },
});
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(service.stateFor(site).unreadCount, 0);
service.stopPolling();
});
test('fetchSite flags invalid Feedbin credentials', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'feedbin', {
type: 'feedbin',
status: 'active',
email: 'me@example.com',
token: 'stale',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({
ok: false,
status: 401,
json: async () => ({}),
});
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(state.error, 'Update Feedbin credentials');
service.stopPolling();
});
test('fetchSite counts the GitHub unread threads', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'github', {
type: 'github',
status: 'active',
url: 'https://github.com',
token: 'ghp_token',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
let requestedUrl;
let requestedHeaders;
globalThis.fetch = async (url, options) => {
requestedUrl = url;
requestedHeaders = options.headers;
return {
ok: true,
headers: { get: () => null },
json: async () => [
{ id: '1', unread: true },
{ id: '2', unread: true },
{ id: '3', unread: false },
],
};
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(
requestedUrl,
'https://api.github.com/notifications?per_page=50'
);
assert.strictEqual(requestedHeaders['Authorization'], 'Bearer ghp_token');
assert.strictEqual(
requestedHeaders['Accept'],
'application/vnd.github+json'
);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'ready');
assert.strictEqual(state.unreadCount, 2);
assert.deepEqual(state.items, []);
service.stopPolling();
});
test('fetchSite follows the GitHub next link across pages', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'github', {
type: 'github',
status: 'active',
url: 'https://github.com',
token: 'ghp_token',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
const requested = [];
globalThis.fetch = async (url) => {
requested.push(url);
if (requested.length === 1) {
return {
ok: true,
headers: {
get: (name) =>
name === 'Link'
? '<https://api.github.com/notifications?per_page=50&page=2>; rel="next"'
: null,
},
json: async () => [
{ id: '1', unread: true },
{ id: '2', unread: true },
],
};
}
return {
ok: true,
headers: { get: () => null },
json: async () => [{ id: '3', unread: true }],
};
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.deepEqual(requested, [
'https://api.github.com/notifications?per_page=50',
'https://api.github.com/notifications?per_page=50&page=2',
]);
assert.strictEqual(service.stateFor(site).unreadCount, 3);
service.stopPolling();
});
test('fetchSite derives the GitHub Enterprise API base from the host', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'github', {
type: 'github',
status: 'active',
url: 'https://git.example.com',
token: 'ghp_token',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
let requestedUrl;
globalThis.fetch = async (url) => {
requestedUrl = url;
return { ok: true, headers: { get: () => null }, json: async () => [] };
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(
requestedUrl,
'https://git.example.com/api/v3/notifications?per_page=50'
);
service.stopPolling();
});
test('fetchSite flags an invalid GitHub token', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'github', {
type: 'github',
status: 'active',
url: 'https://github.com',
token: 'stale',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({
ok: false,
status: 401,
json: async () => ({}),
});
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(state.error, 'Update GitHub token');
service.stopPolling();
});
test('fetchSite flags a GitHub token missing the notifications scope', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'github', {
type: 'github',
status: 'active',
url: 'https://github.com',
token: 'no-scope',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({
ok: false,
status: 403,
json: async () => ({}),
});
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(state.error, 'Update GitHub token');
service.stopPolling();
});
test('fetchSite reads the Gitea unread count from the count endpoint', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'gitea-git.example.com', {
type: 'gitea',
status: 'active',
url: 'https://git.example.com',
token: 'gta_token',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
let requestedUrl;
let requestedHeaders;
globalThis.fetch = async (url, options) => {
requestedUrl = url;
requestedHeaders = options.headers;
return { ok: true, json: async () => ({ new: 5 }) };
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(
requestedUrl,
'https://git.example.com/api/v1/notifications/new'
);
assert.strictEqual(requestedHeaders['Authorization'], 'token gta_token');
assert.strictEqual(requestedHeaders['Accept'], 'application/json');
const state = service.stateFor(site);
assert.strictEqual(state.status, 'ready');
assert.strictEqual(state.unreadCount, 5);
assert.deepEqual(state.items, []);
service.stopPolling();
});
test('fetchSite treats a missing Gitea count as zero', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'gitea-git.example.com', {
type: 'gitea',
status: 'active',
url: 'https://git.example.com',
token: 'gta_token',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({ ok: true, json: async () => ({}) });
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(service.stateFor(site).unreadCount, 0);
service.stopPolling();
});
test('fetchSite flags an invalid Gitea token', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'gitea-git.example.com', {
type: 'gitea',
status: 'active',
url: 'https://git.example.com',
token: 'stale',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({
ok: false,
status: 401,
json: async () => ({}),
});
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(state.error, 'Update Gitea token');
service.stopPolling();
});
test('fetchSite flags a Gitea token missing the notification scope', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'gitea-git.example.com', {
type: 'gitea',
status: 'active',
url: 'https://git.example.com',
token: 'no-scope',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({
ok: false,
status: 403,
json: async () => ({}),
});
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(state.error, 'Update Gitea token');
service.stopPolling();
});
test('fetchSite marks an unreachable site when the request fails', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => {
throw new TypeError('Failed to fetch');
};
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'unreachable');
assert.strictEqual(state.unreadCount, 0);
service.stopPolling();
});
test('fetchSite ignores a request cancelled by navigation', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => {
const err = new Error('The operation was aborted');
err.name = 'AbortError';
throw err;
};
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'loading');
assert.strictEqual(state.error, undefined);
service.stopPolling();
});
test('fetchSite ignores failures while the page is leaving', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
service.pageLifecycle.leaving = true;
globalThis.fetch = async () => {
throw new TypeError('Failed to fetch');
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(service.stateFor(site).status, 'loading');
service.pageLifecycle.leaving = false;
service.stopPolling();
});
test('handleRestore clears an error state and refetches', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => {
throw new TypeError('Failed to fetch');
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(service.stateFor(site).status, 'error');
let calls = 0;
service.fetchAll = async () => {
calls++;
};
await service.handleRestore();
assert.strictEqual(service.stateFor(site).status, 'loading');
assert.strictEqual(calls, 1);
service.stopPolling();
});
test('fetchSite flags an unauthorized response', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'expired',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({
ok: false,
status: 401,
json: async () => ({}),
});
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(state.error, 'Reconnect this forum');
service.stopPolling();
});
test('fetchSite surfaces a rejected access token', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'stale',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({
ok: false,
status: 403,
json: async () => ({ error_type: 'invalid_access' }),
});
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(
state.error,
'Access was rejected — reconnect this forum'
);
service.stopPolling();
});
test('fetchSite flags other HTTP failures', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
apiKey: 'key',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
globalThis.fetch = async () => ({
ok: false,
status: 500,
json: async () => ({}),
});
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'http');
assert.strictEqual(state.error, 'Forum returned 500');
service.stopPolling();
});
test('stopPolling clears the interval', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'discourse-abc', {
type: 'discourse',
status: 'active',
url: 'https://forum.example.com',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
service.startPolling();
assert.ok(service.intervalId);
service.stopPolling();
assert.strictEqual(service.intervalId, null);
});
test('fetchSite reads the Home Assistant summed count', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'homeassistant-ha.example.com', {
type: 'homeassistant',
status: 'active',
url: 'https://ha.example.com:8123',
token: 'token',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
let requested;
service.homeAssistant.fetchSummary = async (integration) => {
requested = integration;
return { unreadCount: 4 };
};
const [site] = service.sites.all;
await service.fetchSite(site);
assert.strictEqual(requested.url, 'https://ha.example.com:8123');
assert.strictEqual(requested.token, 'token');
const state = service.stateFor(site);
assert.strictEqual(state.status, 'ready');
assert.strictEqual(state.unreadCount, 4);
assert.deepEqual(state.items, []);
service.stopPolling();
});
test('fetchSite flags an invalid Home Assistant token', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'homeassistant-ha.example.com', {
type: 'homeassistant',
status: 'active',
url: 'https://ha.example.com:8123',
token: 'stale',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
service.homeAssistant.fetchSummary = async () => {
throw new HomeAssistantAuthError();
};
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'unauthorized');
assert.strictEqual(state.error, 'Update Home Assistant token');
service.stopPolling();
});
test('fetchSite flags an unreachable Home Assistant instance', async function (assert) {
const storage = this.owner.lookup('service:storage');
await storage.set('integrations', 'homeassistant-ha.example.com', {
type: 'homeassistant',
status: 'active',
url: 'https://ha.example.com:8123',
token: 'token',
});
const service = this.owner.lookup('service:notifications');
service.fetchAll = async () => {};
await service.ready;
service.homeAssistant.fetchSummary = async () => {
throw new HomeAssistantConnectionError();
};
const [site] = service.sites.all;
await service.fetchSite(site);
const state = service.stateFor(site);
assert.strictEqual(state.status, 'error');
assert.strictEqual(state.errorType, 'unreachable');
assert.strictEqual(
state.error,
'Home Assistant blocked — check the URL or open it'
);
service.stopPolling();
});
});