Add a 'days with events' preference (default 3, range 1-7) that drives the upcoming events widget, and extend the fetch window to 8 days so the largest window can populate. Add a Calendar settings pane (moved under Integrations) that lists the connected CalDAV/ICS sources in a two-column grid with add/edit/remove, and remove calendars from Manage Integrations. Turn the modal service into a stack so add/edit forms open on top of the settings dialog, with one dialog per level, per-entry title ids, and a softened stacked backdrop. Move CALENDAR_ACTOR into its own config module to break the calendar/caldav/ics import cycle.
537 lines
15 KiB
JavaScript
537 lines
15 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import { caldavInstanceKey } from 'dashtab/config/caldav';
|
|
import { icsInstanceKey } from 'dashtab/config/ics';
|
|
import { CALENDAR_VISIBLE_DAYS_KEY } from 'dashtab/config/calendar';
|
|
|
|
class FakeSockethub {
|
|
constructor(overrides = {}) {
|
|
this.connected = false;
|
|
this.consumer = null;
|
|
this.calls = [];
|
|
this.releases = 0;
|
|
this.handler = overrides.handler ?? (() => ({ items: [] }));
|
|
}
|
|
|
|
async acquire(id) {
|
|
this.consumer = id;
|
|
this.connected = true;
|
|
}
|
|
|
|
release() {
|
|
this.connected = false;
|
|
this.releases += 1;
|
|
}
|
|
|
|
contextFor(platform) {
|
|
return ['ctx', platform];
|
|
}
|
|
|
|
async sendCredentials(message) {
|
|
this.calls.push({ event: 'credentials', message });
|
|
}
|
|
|
|
async send(message) {
|
|
this.calls.push({ event: 'message', message });
|
|
return this.handler(message);
|
|
}
|
|
}
|
|
|
|
const CALENDAR = {
|
|
id: 'c1',
|
|
type: 'calendar',
|
|
name: 'Work',
|
|
components: ['event'],
|
|
};
|
|
const EVENT = {
|
|
id: 'e1',
|
|
type: 'event',
|
|
name: 'Standup',
|
|
startTime: '2026-08-03T09:00:00Z',
|
|
};
|
|
|
|
module('Unit | Service | calendar', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
async function setup(context, overrides = {}) {
|
|
const storage = context.owner.lookup('service:storage');
|
|
await storage.clear('integrations');
|
|
await storage.remove('preferences', CALENDAR_VISIBLE_DAYS_KEY);
|
|
|
|
const cache = context.owner.lookup('service:calendar-cache');
|
|
await cache.ready;
|
|
await cache.clear();
|
|
|
|
const sockethub = new FakeSockethub(overrides);
|
|
context.owner.register('service:sockethub', sockethub, {
|
|
instantiate: false,
|
|
});
|
|
|
|
const service = context.owner.lookup('service:calendar');
|
|
await service.ready;
|
|
|
|
// Ignore the release from the initial (empty) load so tests measure only
|
|
// the calls they make.
|
|
sockethub.releases = 0;
|
|
|
|
return { service, sockethub, cache, storage };
|
|
}
|
|
|
|
test('it exists', function (assert) {
|
|
assert.ok(this.owner.lookup('service:calendar'));
|
|
});
|
|
|
|
test('stays disabled and unconnected without sources', async function (assert) {
|
|
const { service, sockethub } = await setup(this);
|
|
|
|
assert.false(service.enabled);
|
|
assert.strictEqual(sockethub.consumer, null);
|
|
assert.strictEqual(service.intervalId, null);
|
|
});
|
|
|
|
test('fetchCalendars sends credentials and a fetch, then releases', async function (assert) {
|
|
const { service, sockethub } = await setup(this, {
|
|
handler: () => ({ items: [CALENDAR, { type: 'event' }] }),
|
|
});
|
|
|
|
const calendars = await service.fetchCalendars({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
});
|
|
|
|
assert.deepEqual(calendars, [
|
|
{ id: 'c1', name: 'Work', components: ['event'] },
|
|
]);
|
|
assert.deepEqual(
|
|
sockethub.calls.map((call) => call.event),
|
|
['credentials', 'message']
|
|
);
|
|
assert.strictEqual(sockethub.calls[1].message.type, 'fetch');
|
|
assert.false(sockethub.connected);
|
|
assert.strictEqual(sockethub.releases, 1);
|
|
});
|
|
|
|
test('verifyFeed sends a read and returns the feed name', async function (assert) {
|
|
const { service, sockethub } = await setup(this, {
|
|
handler: () => ({ summary: 'Holidays', items: [EVENT] }),
|
|
});
|
|
|
|
const result = await service.verifyFeed('https://example.org/h.ics');
|
|
|
|
assert.strictEqual(result.name, 'Holidays');
|
|
assert.strictEqual(sockethub.calls[0].message.type, 'read');
|
|
assert.strictEqual(sockethub.calls[0].message.target.type, 'feed');
|
|
assert.false(sockethub.connected);
|
|
});
|
|
|
|
test('configureCaldav persists, connects and caches queried events', async function (assert) {
|
|
const { service, sockethub, cache, storage } = await setup(this, {
|
|
handler: (message) =>
|
|
message.type === 'query' ? { items: [EVENT] } : { items: [] },
|
|
});
|
|
|
|
await service.configureCaldav({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR],
|
|
selectedCalendarIds: ['c1'],
|
|
});
|
|
await service.refreshAll();
|
|
|
|
const key = caldavInstanceKey('https://cal.example/dav/', 'alice');
|
|
const record = await storage.get('integrations', key);
|
|
|
|
assert.strictEqual(record.type, 'calendar-caldav');
|
|
assert.strictEqual(record.status, 'active');
|
|
assert.true(service.enabled);
|
|
assert.strictEqual(sockethub.consumer, 'calendar');
|
|
assert.ok(service.intervalId);
|
|
|
|
const cached = cache.get(`calendar-caldav:${key}:c1`);
|
|
assert.strictEqual(cached.source.name, 'Work');
|
|
assert.strictEqual(cached.events.length, 1);
|
|
assert.strictEqual(cached.events[0].id, 'e1');
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('configureIcs reads the feed and caches its events', async function (assert) {
|
|
const { service, cache } = await setup(this, {
|
|
handler: () => ({ summary: 'Holidays', items: [EVENT] }),
|
|
});
|
|
|
|
await service.configureIcs({
|
|
url: 'https://example.org/h.ics',
|
|
name: 'Holidays',
|
|
});
|
|
await service.refreshAll();
|
|
|
|
const key = icsInstanceKey('https://example.org/h.ics');
|
|
const cached = cache.get(`calendar-ics:${key}:https://example.org/h.ics`);
|
|
|
|
assert.strictEqual(cached.source.name, 'Holidays');
|
|
assert.strictEqual(cached.events.length, 1);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('remove deletes the record and cache, then disconnects', async function (assert) {
|
|
const { service, sockethub, cache, storage } = await setup(this, {
|
|
handler: (message) =>
|
|
message.type === 'query' ? { items: [EVENT] } : { items: [] },
|
|
});
|
|
|
|
await service.configureCaldav({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR],
|
|
selectedCalendarIds: ['c1'],
|
|
});
|
|
|
|
const key = caldavInstanceKey('https://cal.example/dav/', 'alice');
|
|
|
|
await service.remove({ id: key, type: 'calendar-caldav' });
|
|
|
|
assert.strictEqual(await storage.get('integrations', key), null);
|
|
assert.strictEqual(cache.forSource('calendar-caldav', key).length, 0);
|
|
assert.false(service.enabled);
|
|
assert.false(sockethub.connected);
|
|
assert.strictEqual(service.intervalId, null);
|
|
});
|
|
|
|
test('refresh failures surface a classified error', async function (assert) {
|
|
const { service } = await setup(this, {
|
|
handler: () => {
|
|
throw Object.assign(new Error('caldav:connection-failed'), {
|
|
name: 'SockethubPlatformError',
|
|
});
|
|
},
|
|
});
|
|
|
|
await service.configureCaldav({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR],
|
|
selectedCalendarIds: ['c1'],
|
|
});
|
|
await service.refreshAll();
|
|
|
|
assert.strictEqual(service.error, 'Unable to reach the calendar server');
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('a successful refresh clears a previous error', async function (assert) {
|
|
const { service } = await setup(this, {
|
|
handler: (message) =>
|
|
message.type === 'query' ? { items: [EVENT] } : { items: [] },
|
|
});
|
|
|
|
await service.configureCaldav({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR],
|
|
selectedCalendarIds: ['c1'],
|
|
});
|
|
|
|
// Let the refresh started by polling settle before starting a new cycle.
|
|
await service.refreshAll();
|
|
|
|
service.error = 'stale error';
|
|
await service.refreshAll();
|
|
|
|
assert.strictEqual(service.error, null);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('a refresh cancelled by navigation does not surface an error', async function (assert) {
|
|
const { service } = await setup(this, {
|
|
handler: () => {
|
|
throw Object.assign(new Error('caldav:connection-failed'), {
|
|
name: 'SockethubPlatformError',
|
|
});
|
|
},
|
|
});
|
|
|
|
await service.configureCaldav({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR],
|
|
selectedCalendarIds: ['c1'],
|
|
});
|
|
await service.refreshAll();
|
|
|
|
assert.strictEqual(service.error, 'Unable to reach the calendar server');
|
|
|
|
service.error = null;
|
|
service.pageLifecycle.leaving = true;
|
|
await service.refreshAll();
|
|
|
|
assert.strictEqual(service.error, null);
|
|
|
|
service.pageLifecycle.leaving = false;
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('a refresh aborted mid-flight does not surface an error', async function (assert) {
|
|
const { service } = await setup(this, {
|
|
handler: () => {
|
|
throw Object.assign(new Error('The operation was aborted'), {
|
|
name: 'AbortError',
|
|
});
|
|
},
|
|
});
|
|
|
|
await service.configureCaldav({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR],
|
|
selectedCalendarIds: ['c1'],
|
|
});
|
|
await service.refreshAll();
|
|
|
|
assert.strictEqual(service.error, null);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('handleRestore reconnects and clears a stale error', async function (assert) {
|
|
const { service, sockethub } = await setup(this, {
|
|
handler: (message) =>
|
|
message.type === 'query' ? { items: [EVENT] } : { items: [] },
|
|
});
|
|
|
|
await service.configureCaldav({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR],
|
|
selectedCalendarIds: ['c1'],
|
|
});
|
|
|
|
// Simulate the socket being dropped and an error left behind when the tab
|
|
// was navigated away from.
|
|
sockethub.connected = false;
|
|
service.error = 'Unable to load calendar events';
|
|
|
|
await service.handleRestore();
|
|
|
|
assert.true(sockethub.connected);
|
|
assert.strictEqual(service.error, null);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('handleRestore retries an interrupted initial load', async function (assert) {
|
|
const { service, storage } = await setup(this);
|
|
|
|
await storage.set('integrations', 'caldav-key', {
|
|
type: 'calendar-caldav',
|
|
status: 'active',
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR],
|
|
selectedCalendarIds: ['c1'],
|
|
createdAt: Date.now(),
|
|
});
|
|
|
|
service.loadFailed = true;
|
|
service.error = 'Unable to load calendars';
|
|
service.instances = [];
|
|
|
|
await service.handleRestore();
|
|
|
|
assert.true(service.enabled);
|
|
assert.strictEqual(service.error, null);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('deselecting a calendar prunes its cached events', async function (assert) {
|
|
const { service, cache } = await setup(this, {
|
|
handler: (message) =>
|
|
message.type === 'query' ? { items: [EVENT] } : { items: [] },
|
|
});
|
|
|
|
const key = caldavInstanceKey('https://cal.example/dav/', 'alice');
|
|
const second = {
|
|
id: 'c2',
|
|
type: 'calendar',
|
|
name: 'Personal',
|
|
components: ['event'],
|
|
};
|
|
|
|
await service.configureCaldav({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR, second],
|
|
selectedCalendarIds: ['c1', 'c2'],
|
|
});
|
|
await service.refreshAll();
|
|
|
|
assert.ok(cache.get(`calendar-caldav:${key}:c1`));
|
|
assert.ok(cache.get(`calendar-caldav:${key}:c2`));
|
|
|
|
await service.configureCaldav({
|
|
url: 'https://cal.example/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [CALENDAR, second],
|
|
selectedCalendarIds: ['c1'],
|
|
});
|
|
|
|
assert.ok(cache.get(`calendar-caldav:${key}:c1`));
|
|
assert.strictEqual(cache.get(`calendar-caldav:${key}:c2`), null);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('events hide cached entries for calendars no longer selected', async function (assert) {
|
|
const { service, cache } = await setup(this);
|
|
|
|
const now = new Date();
|
|
const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
|
|
|
|
service.instances = [
|
|
{
|
|
id: 'calendar-caldav-x',
|
|
type: 'calendar-caldav',
|
|
selectedCalendarIds: ['c1'],
|
|
},
|
|
];
|
|
|
|
await cache.put({
|
|
key: 'calendar-caldav:calendar-caldav-x:c1',
|
|
source: {
|
|
type: 'calendar-caldav',
|
|
instanceKey: 'calendar-caldav-x',
|
|
calendarId: 'c1',
|
|
name: 'Work',
|
|
},
|
|
fetchedAt: Date.now(),
|
|
events: [{ id: 'kept', name: 'Kept', startTime: today, allDay: true }],
|
|
});
|
|
await cache.put({
|
|
key: 'calendar-caldav:calendar-caldav-x:c2',
|
|
source: {
|
|
type: 'calendar-caldav',
|
|
instanceKey: 'calendar-caldav-x',
|
|
calendarId: 'c2',
|
|
name: 'Old',
|
|
},
|
|
fetchedAt: Date.now(),
|
|
events: [
|
|
{ id: 'hidden', name: 'Hidden', startTime: today, allDay: true },
|
|
],
|
|
});
|
|
|
|
assert.deepEqual(
|
|
service.events.today.map((event) => event.id),
|
|
['kept']
|
|
);
|
|
});
|
|
|
|
test('events groups cached events from enabled sources', async function (assert) {
|
|
const { service, cache } = await setup(this);
|
|
|
|
const now = new Date();
|
|
const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
|
|
const tomorrow = new Date(now.getTime() + 24 * 60 * 60 * 1000);
|
|
|
|
service.instances = [{ id: 'x', type: 'calendar-ics' }];
|
|
|
|
await cache.put({
|
|
key: 'calendar-ics:x:feed',
|
|
source: {
|
|
type: 'calendar-ics',
|
|
instanceKey: 'x',
|
|
calendarId: 'feed',
|
|
name: 'Feed',
|
|
},
|
|
fetchedAt: Date.now(),
|
|
events: [
|
|
{
|
|
id: 'today',
|
|
name: 'Today',
|
|
startTime: today,
|
|
allDay: true,
|
|
},
|
|
{
|
|
id: 'future',
|
|
name: 'Future',
|
|
startTime: tomorrow.toISOString(),
|
|
allDay: false,
|
|
},
|
|
],
|
|
});
|
|
|
|
const grouped = service.events;
|
|
|
|
assert.deepEqual(
|
|
grouped.today.map((event) => event.id),
|
|
['today']
|
|
);
|
|
assert.strictEqual(grouped.days.length, 1);
|
|
assert.strictEqual(grouped.days[0].events[0].id, 'future');
|
|
});
|
|
|
|
test('setVisibleDays persists a normalized value', async function (assert) {
|
|
const { service, storage } = await setup(this);
|
|
|
|
await service.setVisibleDays('5');
|
|
|
|
assert.strictEqual(service.visibleDays, 5);
|
|
assert.strictEqual(
|
|
await storage.get('preferences', CALENDAR_VISIBLE_DAYS_KEY),
|
|
5
|
|
);
|
|
|
|
await service.setVisibleDays(99);
|
|
assert.strictEqual(service.visibleDays, 3);
|
|
});
|
|
|
|
test('events honors the configured visible day count', async function (assert) {
|
|
const { service, cache } = await setup(this);
|
|
|
|
const now = new Date();
|
|
const iso = (offset) => {
|
|
const day = new Date(now);
|
|
day.setDate(day.getDate() + offset);
|
|
return day.toISOString();
|
|
};
|
|
|
|
service.instances = [{ id: 'x', type: 'calendar-ics' }];
|
|
|
|
await cache.put({
|
|
key: 'calendar-ics:x:feed',
|
|
source: {
|
|
type: 'calendar-ics',
|
|
instanceKey: 'x',
|
|
calendarId: 'feed',
|
|
name: 'Feed',
|
|
},
|
|
fetchedAt: Date.now(),
|
|
events: [1, 2, 3].map((offset) => ({
|
|
id: `d${offset}`,
|
|
name: `Day ${offset}`,
|
|
startTime: iso(offset),
|
|
allDay: false,
|
|
})),
|
|
});
|
|
|
|
service.visibleDays = 2;
|
|
|
|
assert.strictEqual(service.events.days.length, 2);
|
|
});
|
|
});
|