Track whether a Sockethub URL has been stored and start the ICS and CalDAV add flows past the server step for returning users, while keeping it for first-timers. Back navigation stops before the removed step, and the step's helper text now links Sockethub to its website.
277 lines
8.3 KiB
JavaScript
277 lines
8.3 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import {
|
|
SockethubConnectionError,
|
|
SockethubError,
|
|
SockethubPlatformError,
|
|
} from 'dashtab/services/sockethub';
|
|
import { DEFAULT_SOCKETHUB_URL } from 'dashtab/config/sockethub';
|
|
|
|
// Mirrors the public emitter the client exposes on `client.socket`.
|
|
class FakeSocket {
|
|
constructor() {
|
|
this.handlers = new Map();
|
|
this.sent = [];
|
|
this.connected = true;
|
|
this.disconnected = false;
|
|
}
|
|
|
|
on(event, handler) {
|
|
const list = this.handlers.get(event) ?? [];
|
|
list.push(handler);
|
|
this.handlers.set(event, list);
|
|
}
|
|
|
|
emit(event, message, callback) {
|
|
this.sent.push({ event, message, callback });
|
|
}
|
|
|
|
disconnect() {
|
|
this.connected = false;
|
|
this.disconnected = true;
|
|
}
|
|
|
|
fire(event, payload) {
|
|
for (const handler of this.handlers.get(event) ?? []) {
|
|
handler(payload);
|
|
}
|
|
}
|
|
|
|
last() {
|
|
return this.sent[this.sent.length - 1];
|
|
}
|
|
|
|
ack(response) {
|
|
this.last()?.callback?.(response);
|
|
}
|
|
}
|
|
|
|
class FakeClient {
|
|
constructor(descriptor = { platforms: [{ id: 'caldav' }] }) {
|
|
this.socket = new FakeSocket();
|
|
this.descriptor = descriptor;
|
|
this.readyCalls = 0;
|
|
}
|
|
|
|
async ready() {
|
|
this.readyCalls += 1;
|
|
}
|
|
|
|
contextFor(platform) {
|
|
return ['ctx', platform];
|
|
}
|
|
}
|
|
|
|
module('Unit | Service | sockethub', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
hooks.beforeEach(async function () {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.remove('settings', 'sockethub-url');
|
|
});
|
|
|
|
function setup(overrides = {}) {
|
|
const service = this.owner.lookup('service:sockethub');
|
|
const clients = [];
|
|
let createCalls = 0;
|
|
|
|
service.url = 'https://sockethub.example.org';
|
|
service.createClient = async () => {
|
|
createCalls += 1;
|
|
if (overrides.fail) {
|
|
throw overrides.fail;
|
|
}
|
|
const client = new FakeClient(overrides.descriptor);
|
|
clients.push(client);
|
|
return client;
|
|
};
|
|
|
|
return { service, clients, createCalls: () => createCalls };
|
|
}
|
|
|
|
test('it exists', function (assert) {
|
|
assert.ok(this.owner.lookup('service:sockethub'));
|
|
});
|
|
|
|
test('loadUrl defaults to the configured server', async function (assert) {
|
|
const { service } = setup.call(this);
|
|
|
|
assert.strictEqual(await service.loadUrl(), DEFAULT_SOCKETHUB_URL);
|
|
assert.strictEqual(service.url, DEFAULT_SOCKETHUB_URL);
|
|
assert.false(service.hasStoredUrl);
|
|
});
|
|
|
|
test('loadUrl reports a stored server', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('settings', 'sockethub-url', 'https://self.example.org');
|
|
|
|
const { service } = setup.call(this);
|
|
|
|
assert.strictEqual(await service.loadUrl(), 'https://self.example.org');
|
|
assert.strictEqual(service.url, 'https://self.example.org');
|
|
assert.true(service.hasStoredUrl);
|
|
});
|
|
|
|
test('acquire connects once and shares the client between consumers', async function (assert) {
|
|
const { service, clients, createCalls } = setup.call(this);
|
|
|
|
const first = await service.acquire('calendar');
|
|
|
|
assert.strictEqual(first, clients[0]);
|
|
assert.strictEqual(service.status, 'ready');
|
|
assert.strictEqual(service.descriptor, clients[0].descriptor);
|
|
assert.strictEqual(clients[0].readyCalls, 1);
|
|
|
|
const second = await service.acquire('other');
|
|
|
|
assert.strictEqual(second, clients[0]);
|
|
assert.strictEqual(clients[0].readyCalls, 1);
|
|
assert.strictEqual(createCalls(), 1);
|
|
});
|
|
|
|
test('release disconnects only after the last consumer leaves', async function (assert) {
|
|
const { service, clients } = setup.call(this);
|
|
|
|
await service.acquire('calendar');
|
|
await service.acquire('other');
|
|
|
|
service.release('calendar');
|
|
|
|
assert.strictEqual(service.status, 'ready');
|
|
assert.false(clients[0].socket.disconnected);
|
|
|
|
service.release('other');
|
|
|
|
assert.strictEqual(service.status, 'disconnected');
|
|
assert.true(clients[0].socket.disconnected);
|
|
assert.strictEqual(service.client, null);
|
|
assert.strictEqual(service.descriptor, null);
|
|
});
|
|
|
|
test('connect failure surfaces a connection error', async function (assert) {
|
|
const { service } = setup.call(this, {
|
|
fail: new Error('discovery failed'),
|
|
});
|
|
|
|
await assert.rejects(service.acquire('calendar'), SockethubConnectionError);
|
|
|
|
assert.strictEqual(service.status, 'error');
|
|
assert.strictEqual(service.error, 'discovery failed');
|
|
assert.strictEqual(service.client, null);
|
|
});
|
|
|
|
test('concurrent ensures share a single connect', async function (assert) {
|
|
const { service, clients, createCalls } = setup.call(this);
|
|
|
|
const [first, second] = await Promise.all([
|
|
service.ensureConnected(),
|
|
service.ensureConnected(),
|
|
]);
|
|
|
|
assert.strictEqual(first, clients[0]);
|
|
assert.strictEqual(second, clients[0]);
|
|
assert.strictEqual(createCalls(), 1);
|
|
});
|
|
|
|
test('send resolves with the acknowledgement', async function (assert) {
|
|
const { service, clients } = setup.call(this);
|
|
await service.acquire('calendar');
|
|
|
|
const promise = service.send({ type: 'fetch' });
|
|
|
|
assert.strictEqual(clients[0].socket.last().event, 'message');
|
|
clients[0].socket.ack({ items: [] });
|
|
|
|
assert.deepEqual(await promise, { items: [] });
|
|
});
|
|
|
|
test('send rejects when the acknowledgement carries an error', async function (assert) {
|
|
const { service, clients } = setup.call(this);
|
|
await service.acquire('calendar');
|
|
|
|
const promise = service.send({ type: 'fetch' });
|
|
|
|
clients[0].socket.ack({ error: 'caldav:authentication-failed' });
|
|
|
|
await assert.rejects(promise, SockethubPlatformError);
|
|
});
|
|
|
|
test('sendCredentials emits on the credentials event', async function (assert) {
|
|
const { service, clients } = setup.call(this);
|
|
await service.acquire('calendar');
|
|
|
|
const promise = service.sendCredentials({ type: 'credentials' });
|
|
|
|
assert.strictEqual(clients[0].socket.last().event, 'credentials');
|
|
clients[0].socket.ack({});
|
|
|
|
await promise;
|
|
});
|
|
|
|
test('send rejects before the client is connected', async function (assert) {
|
|
const { service } = setup.call(this);
|
|
|
|
await assert.rejects(service.send({ type: 'fetch' }), SockethubError);
|
|
});
|
|
|
|
test('contextFor throws before connecting and delegates afterwards', async function (assert) {
|
|
const { service } = setup.call(this);
|
|
|
|
assert.throws(() => service.contextFor('caldav'), SockethubError);
|
|
|
|
await service.acquire('calendar');
|
|
|
|
assert.deepEqual(service.contextFor('caldav'), ['ctx', 'caldav']);
|
|
});
|
|
|
|
test('setUrl persists the server and reconnects active consumers', async function (assert) {
|
|
const { service, clients } = setup.call(this);
|
|
await service.acquire('calendar');
|
|
|
|
await service.setUrl('https://other.example.org');
|
|
|
|
const storage = this.owner.lookup('service:storage');
|
|
const stored = await storage.get('settings', 'sockethub-url');
|
|
|
|
assert.strictEqual(stored, 'https://other.example.org');
|
|
assert.strictEqual(service.url, 'https://other.example.org');
|
|
assert.true(service.hasStoredUrl);
|
|
assert.true(clients[0].socket.disconnected);
|
|
assert.strictEqual(service.client, clients[1]);
|
|
assert.strictEqual(service.status, 'ready');
|
|
});
|
|
|
|
test('socket events update the tracked status', async function (assert) {
|
|
const { service, clients } = setup.call(this);
|
|
await service.acquire('calendar');
|
|
|
|
clients[0].socket.fire('disconnect');
|
|
assert.strictEqual(service.status, 'disconnected');
|
|
|
|
clients[0].socket.fire('connect');
|
|
assert.strictEqual(service.status, 'ready');
|
|
|
|
clients[0].socket.fire('connect_error', new Error('offline'));
|
|
assert.strictEqual(service.status, 'error');
|
|
assert.strictEqual(service.error, 'offline');
|
|
|
|
clients[0].socket.fire('init_error', { error: 'schemas failed' });
|
|
assert.strictEqual(service.error, 'schemas failed');
|
|
});
|
|
|
|
test('onMessage fans out incoming messages and unsubscribes', async function (assert) {
|
|
const { service, clients } = setup.call(this);
|
|
await service.acquire('calendar');
|
|
|
|
const received = [];
|
|
const unsubscribe = service.onMessage((message) => received.push(message));
|
|
|
|
clients[0].socket.fire('message', { type: 'event' });
|
|
assert.deepEqual(received, [{ type: 'event' }]);
|
|
|
|
unsubscribe();
|
|
clients[0].socket.fire('message', { type: 'ignored' });
|
|
assert.deepEqual(received, [{ type: 'event' }]);
|
|
});
|
|
});
|