OpenRouter now rejects management keys from the browser on both /api/v1/keys and /api/v1/analytics/query, so the management-key flow can no longer work in the extension. Read the configured key's own usage from GET /api/v1/key instead, which returns UTC daily, weekly, and monthly buckets and works from the browser. Drop the key-type selection, key listing/selection, pagination, and the management/analytics endpoints. Validate the key against /key before persisting, and write a fresh record so legacy keyType/keyIds fields are dropped. Keep the browser-block hint for anyone still storing a management key.
554 lines
18 KiB
JavaScript
554 lines
18 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import { tracked } from '@glimmer/tracking';
|
|
import { DISCOURSE_ICON_URL } from 'dashtab/config/integrations';
|
|
|
|
class FakeSitesService {
|
|
@tracked all = [];
|
|
removed = [];
|
|
|
|
integrationFor(id) {
|
|
return this.records?.[id];
|
|
}
|
|
|
|
async removeSource(id) {
|
|
this.removed.push(id);
|
|
this.all = this.all.filter((site) => site.id !== id);
|
|
}
|
|
}
|
|
|
|
class FakeNotificationsService {
|
|
states = {};
|
|
|
|
stateFor(site) {
|
|
return this.states[site.id] ?? { error: null, errorType: null };
|
|
}
|
|
}
|
|
|
|
class FakeBitcoinService {
|
|
@tracked enabled = false;
|
|
@tracked interval = 30000;
|
|
@tracked provider = 'coingecko';
|
|
disabled = false;
|
|
|
|
async disable() {
|
|
this.disabled = true;
|
|
this.enabled = false;
|
|
}
|
|
}
|
|
|
|
class FakeOpenRouterService {
|
|
@tracked enabled = false;
|
|
@tracked apiKey = null;
|
|
@tracked interval = 300000;
|
|
@tracked granularity = 'today';
|
|
disabled = false;
|
|
|
|
async disable() {
|
|
this.disabled = true;
|
|
this.enabled = false;
|
|
}
|
|
}
|
|
|
|
class FakeCalendarService {
|
|
instancesByType = { 'calendar-caldav': [], 'calendar-ics': [] };
|
|
removed = [];
|
|
|
|
instancesFor(type) {
|
|
return this.instancesByType[type] ?? [];
|
|
}
|
|
|
|
async remove(instance) {
|
|
this.removed.push(instance.id);
|
|
}
|
|
}
|
|
|
|
function register(owner, overrides = {}) {
|
|
const sites = overrides.sites ?? new FakeSitesService();
|
|
const notifications =
|
|
overrides.notifications ?? new FakeNotificationsService();
|
|
const bitcoin = overrides.bitcoin ?? new FakeBitcoinService();
|
|
const openrouter = overrides.openrouter ?? new FakeOpenRouterService();
|
|
const calendar = overrides.calendar ?? new FakeCalendarService();
|
|
|
|
owner.register('service:sites', sites, { instantiate: false });
|
|
owner.register('service:notifications', notifications, {
|
|
instantiate: false,
|
|
});
|
|
owner.register('service:bitcoin', bitcoin, { instantiate: false });
|
|
owner.register('service:openrouter', openrouter, { instantiate: false });
|
|
owner.register('service:calendar', calendar, { instantiate: false });
|
|
|
|
return { sites, notifications, bitcoin, openrouter, calendar };
|
|
}
|
|
|
|
module('Unit | Service | integrations', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
test('it merges registry capabilities with live instances', function (assert) {
|
|
const sites = new FakeSitesService();
|
|
sites.all = [
|
|
{
|
|
id: 'discourse-abc',
|
|
type: 'discourse',
|
|
label: 'forum.example.com',
|
|
url: 'https://forum.example.com',
|
|
},
|
|
];
|
|
|
|
const bitcoin = new FakeBitcoinService();
|
|
bitcoin.enabled = true;
|
|
bitcoin.interval = 60000;
|
|
bitcoin.provider = 'binance';
|
|
|
|
register(this.owner, { sites, bitcoin });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const discourse = service.all.find((entry) => entry.id === 'discourse');
|
|
const bitcoinEntry = service.all.find((entry) => entry.id === 'bitcoin');
|
|
const github = service.all.find((entry) => entry.id === 'github');
|
|
const gitea = service.all.find((entry) => entry.id === 'gitea');
|
|
const homeassistant = service.all.find(
|
|
(entry) => entry.id === 'homeassistant'
|
|
);
|
|
|
|
assert.true(discourse.supportsMultiple);
|
|
assert.false(discourse.hasSettings);
|
|
assert.true(discourse.reconnectable);
|
|
assert.strictEqual(discourse.instances.length, 1);
|
|
assert.strictEqual(discourse.instances[0].id, 'discourse-abc');
|
|
|
|
assert.true(bitcoinEntry.hasSettings);
|
|
assert.strictEqual(bitcoinEntry.instances.length, 1);
|
|
assert.strictEqual(bitcoinEntry.instances[0].interval, 60000);
|
|
assert.strictEqual(bitcoinEntry.instances[0].provider, 'binance');
|
|
|
|
assert.true(github.available);
|
|
assert.false(github.supportsMultiple);
|
|
assert.true(github.hasSettings);
|
|
assert.false(github.reconnectable);
|
|
assert.strictEqual(github.instances.length, 0);
|
|
|
|
assert.true(gitea.available);
|
|
assert.true(gitea.supportsMultiple);
|
|
assert.true(gitea.hasSettings);
|
|
assert.false(gitea.reconnectable);
|
|
assert.strictEqual(gitea.themeColor, '#ffffff');
|
|
assert.strictEqual(gitea.instances.length, 0);
|
|
|
|
assert.true(homeassistant.available);
|
|
assert.true(homeassistant.supportsMultiple);
|
|
assert.true(homeassistant.hasSettings);
|
|
assert.false(homeassistant.reconnectable);
|
|
assert.strictEqual(homeassistant.themeColor, '#18bcf2');
|
|
assert.strictEqual(homeassistant.instances.length, 0);
|
|
|
|
const custom = service.all.find((entry) => entry.id === 'custom');
|
|
assert.true(custom.available);
|
|
assert.true(custom.supportsMultiple);
|
|
assert.true(custom.hasSettings);
|
|
assert.false(custom.reconnectable);
|
|
assert.strictEqual(custom.instances.length, 0);
|
|
});
|
|
|
|
test('rows is empty when nothing is connected', function (assert) {
|
|
register(this.owner);
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
|
|
assert.deepEqual(service.rows, []);
|
|
});
|
|
|
|
test('rows flattens every connected instance with a label, byline and avatar', function (assert) {
|
|
const sites = new FakeSitesService();
|
|
sites.all = [
|
|
{
|
|
id: 'discourse-a',
|
|
type: 'discourse',
|
|
label: 'a.example.com',
|
|
url: 'https://a.example.com',
|
|
icon: { dataUrl: 'data:image/png;base64,a', maskable: true },
|
|
},
|
|
{
|
|
id: 'discourse-b',
|
|
type: 'discourse',
|
|
label: 'b.example.com',
|
|
url: 'https://b.example.com',
|
|
},
|
|
];
|
|
|
|
const bitcoin = new FakeBitcoinService();
|
|
bitcoin.enabled = true;
|
|
|
|
register(this.owner, { sites, bitcoin });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const rows = service.rows;
|
|
|
|
assert.strictEqual(rows.length, 3);
|
|
// Rows with settings sort first; Discourse (no settings) comes last. The
|
|
// stable sort keeps registry order within each group.
|
|
assert.deepEqual(
|
|
rows.map((row) => row.id),
|
|
['bitcoin', 'discourse-a', 'discourse-b']
|
|
);
|
|
|
|
const discourseRow = rows.find((row) => row.id === 'discourse-a');
|
|
assert.strictEqual(discourseRow.label, 'Discourse');
|
|
assert.strictEqual(discourseRow.byline, 'a.example.com');
|
|
assert.strictEqual(discourseRow.iconUrl, DISCOURSE_ICON_URL);
|
|
// A resolved instance icon takes precedence over the bundled fallback.
|
|
assert.deepEqual(discourseRow.icon, {
|
|
dataUrl: 'data:image/png;base64,a',
|
|
maskable: true,
|
|
});
|
|
|
|
const bitcoinRow = rows.find((row) => row.type === 'bitcoin');
|
|
assert.strictEqual(bitcoinRow.label, 'Bitcoin Price Ticker');
|
|
assert.strictEqual(bitcoinRow.byline, 'Live BTC price');
|
|
});
|
|
|
|
test('remove delegates to each integration adapter', async function (assert) {
|
|
const sites = new FakeSitesService();
|
|
sites.all = [
|
|
{
|
|
id: 'discourse-abc',
|
|
type: 'discourse',
|
|
label: 'forum.example.com',
|
|
url: 'https://forum.example.com',
|
|
},
|
|
];
|
|
|
|
const bitcoin = new FakeBitcoinService();
|
|
bitcoin.enabled = true;
|
|
|
|
const { sites: registeredSites } = register(this.owner, {
|
|
sites,
|
|
bitcoin,
|
|
});
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
|
|
await service.remove({ type: 'discourse', id: 'discourse-abc' });
|
|
assert.deepEqual(registeredSites.removed, ['discourse-abc']);
|
|
|
|
await service.remove({ type: 'bitcoin', id: 'bitcoin' });
|
|
assert.true(bitcoin.disabled);
|
|
});
|
|
|
|
test('openrouter adapter exposes the configured integration', async function (assert) {
|
|
const openrouter = new FakeOpenRouterService();
|
|
openrouter.enabled = true;
|
|
openrouter.apiKey = 'sk-or-v1-test';
|
|
openrouter.interval = 600000;
|
|
openrouter.granularity = 'today';
|
|
|
|
register(this.owner, { openrouter });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const openrouterEntry = service.all.find(
|
|
(entry) => entry.id === 'openrouter'
|
|
);
|
|
|
|
assert.true(openrouterEntry.hasSettings);
|
|
assert.strictEqual(openrouterEntry.instances.length, 1);
|
|
assert.strictEqual(openrouterEntry.instances[0].apiKey, 'sk-or-v1-test');
|
|
assert.strictEqual(openrouterEntry.instances[0].interval, 600000);
|
|
assert.strictEqual(openrouterEntry.instances[0].granularity, 'today');
|
|
|
|
const row = service.rows.find((entry) => entry.type === 'openrouter');
|
|
assert.strictEqual(row.label, 'OpenRouter Spend');
|
|
assert.strictEqual(row.byline, 'Daily, weekly, or monthly');
|
|
assert.true(row.iconInset);
|
|
|
|
await service.remove({ type: 'openrouter', id: 'openrouter' });
|
|
assert.true(openrouter.disabled);
|
|
});
|
|
|
|
test('feedbin adapter exposes the connected account without reconnect', async function (assert) {
|
|
const sites = new FakeSitesService();
|
|
sites.all = [
|
|
{
|
|
id: 'feedbin',
|
|
type: 'feedbin',
|
|
label: 'Feedbin',
|
|
href: 'https://feedbin.com',
|
|
},
|
|
];
|
|
sites.records = {
|
|
feedbin: { email: 'me@example.com', token: 'dG9rZW4=' },
|
|
};
|
|
|
|
register(this.owner, { sites });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const feedbin = service.all.find((entry) => entry.id === 'feedbin');
|
|
|
|
assert.true(feedbin.supportsMultiple === false);
|
|
assert.true(feedbin.hasSettings);
|
|
assert.false(feedbin.reconnectable);
|
|
assert.strictEqual(feedbin.instances.length, 1);
|
|
assert.strictEqual(feedbin.instances[0].id, 'feedbin');
|
|
assert.strictEqual(feedbin.instances[0].label, 'Feedbin');
|
|
assert.notOk(feedbin.instances[0].url);
|
|
assert.strictEqual(feedbin.instances[0].email, 'me@example.com');
|
|
assert.strictEqual(feedbin.instances[0].token, 'dG9rZW4=');
|
|
assert.false(feedbin.instances[0].canReconnect);
|
|
});
|
|
|
|
test('github adapter exposes the connected account without reconnect', async function (assert) {
|
|
const sites = new FakeSitesService();
|
|
sites.all = [
|
|
{
|
|
id: 'github',
|
|
type: 'github',
|
|
label: 'GitHub',
|
|
href: 'https://github.com/notifications',
|
|
},
|
|
];
|
|
sites.records = {
|
|
github: { url: 'https://github.com', token: 'ghp_token' },
|
|
};
|
|
|
|
register(this.owner, { sites });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const github = service.all.find((entry) => entry.id === 'github');
|
|
|
|
assert.false(github.supportsMultiple);
|
|
assert.true(github.hasSettings);
|
|
assert.false(github.reconnectable);
|
|
assert.strictEqual(github.instances.length, 1);
|
|
assert.strictEqual(github.instances[0].id, 'github');
|
|
assert.strictEqual(github.instances[0].label, 'GitHub');
|
|
assert.strictEqual(github.instances[0].url, 'https://github.com');
|
|
assert.strictEqual(github.instances[0].token, 'ghp_token');
|
|
assert.false(github.instances[0].canReconnect);
|
|
});
|
|
|
|
test('gitea adapter exposes every connected instance without reconnect', async function (assert) {
|
|
const sites = new FakeSitesService();
|
|
sites.all = [
|
|
{
|
|
id: 'gitea-a.example.com',
|
|
type: 'gitea',
|
|
label: 'a.example.com',
|
|
href: 'https://a.example.com',
|
|
},
|
|
{
|
|
id: 'gitea-b.example.com',
|
|
type: 'gitea',
|
|
label: 'b.example.com',
|
|
href: 'https://b.example.com',
|
|
},
|
|
];
|
|
sites.records = {
|
|
'gitea-a.example.com': {
|
|
url: 'https://a.example.com',
|
|
token: 'token-a',
|
|
},
|
|
'gitea-b.example.com': {
|
|
url: 'https://b.example.com',
|
|
token: 'token-b',
|
|
},
|
|
};
|
|
|
|
register(this.owner, { sites });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const gitea = service.all.find((entry) => entry.id === 'gitea');
|
|
|
|
assert.true(gitea.supportsMultiple);
|
|
assert.true(gitea.hasSettings);
|
|
assert.false(gitea.reconnectable);
|
|
assert.strictEqual(gitea.instances.length, 2);
|
|
assert.deepEqual(
|
|
gitea.instances.map((instance) => instance.id),
|
|
['gitea-a.example.com', 'gitea-b.example.com']
|
|
);
|
|
assert.strictEqual(gitea.instances[0].token, 'token-a');
|
|
assert.false(gitea.instances[0].canReconnect);
|
|
});
|
|
|
|
test('homeassistant adapter exposes every connected instance without reconnect', async function (assert) {
|
|
const sites = new FakeSitesService();
|
|
sites.all = [
|
|
{
|
|
id: 'homeassistant-ha.example.com:8123',
|
|
type: 'homeassistant',
|
|
label: 'ha.example.com:8123',
|
|
href: 'https://ha.example.com:8123/',
|
|
},
|
|
];
|
|
sites.records = {
|
|
'homeassistant-ha.example.com:8123': {
|
|
url: 'https://ha.example.com:8123',
|
|
token: 'token-a',
|
|
name: 'Home',
|
|
},
|
|
};
|
|
|
|
register(this.owner, { sites });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const homeassistant = service.all.find(
|
|
(entry) => entry.id === 'homeassistant'
|
|
);
|
|
|
|
assert.true(homeassistant.supportsMultiple);
|
|
assert.true(homeassistant.hasSettings);
|
|
assert.false(homeassistant.reconnectable);
|
|
assert.strictEqual(homeassistant.instances.length, 1);
|
|
assert.strictEqual(
|
|
homeassistant.instances[0].id,
|
|
'homeassistant-ha.example.com:8123'
|
|
);
|
|
assert.strictEqual(
|
|
homeassistant.instances[0].url,
|
|
'https://ha.example.com:8123'
|
|
);
|
|
assert.strictEqual(homeassistant.instances[0].name, 'Home');
|
|
assert.strictEqual(homeassistant.instances[0].token, 'token-a');
|
|
assert.false(homeassistant.instances[0].canReconnect);
|
|
|
|
const row = service.rows.find(
|
|
(entry) => entry.id === 'homeassistant-ha.example.com:8123'
|
|
);
|
|
assert.strictEqual(row.label, 'Home Assistant');
|
|
assert.strictEqual(row.byline, 'Home');
|
|
});
|
|
|
|
test('homeassistant falls back to the host byline without a custom name', function (assert) {
|
|
const sites = new FakeSitesService();
|
|
sites.all = [
|
|
{
|
|
id: 'homeassistant-ha.example.com:8123',
|
|
type: 'homeassistant',
|
|
label: 'ha.example.com:8123',
|
|
href: 'https://ha.example.com:8123/',
|
|
},
|
|
];
|
|
sites.records = {
|
|
'homeassistant-ha.example.com:8123': {
|
|
url: 'https://ha.example.com:8123',
|
|
token: 'token-a',
|
|
},
|
|
};
|
|
|
|
register(this.owner, { sites });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const row = service.rows.find(
|
|
(entry) => entry.id === 'homeassistant-ha.example.com:8123'
|
|
);
|
|
|
|
assert.strictEqual(row.label, 'Home Assistant');
|
|
assert.strictEqual(row.byline, 'ha.example.com');
|
|
});
|
|
|
|
test('custom adapter exposes the stored url, name and custom icon', async function (assert) {
|
|
const customIcon = {
|
|
dataUrl: 'data:image/png;base64,up',
|
|
maskable: false,
|
|
};
|
|
const sites = new FakeSitesService();
|
|
sites.all = [
|
|
{
|
|
id: 'custom-1',
|
|
type: 'custom',
|
|
label: 'Example',
|
|
href: 'https://example.com',
|
|
icon: customIcon,
|
|
},
|
|
];
|
|
sites.records = {
|
|
'custom-1': {
|
|
url: 'https://example.com',
|
|
name: 'Example',
|
|
customIcon,
|
|
},
|
|
};
|
|
|
|
const { sites: registeredSites } = register(this.owner, { sites });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const custom = service.all.find((entry) => entry.id === 'custom');
|
|
|
|
assert.strictEqual(custom.instances.length, 1);
|
|
assert.strictEqual(custom.instances[0].id, 'custom-1');
|
|
assert.strictEqual(custom.instances[0].label, 'Example');
|
|
assert.strictEqual(custom.instances[0].url, 'https://example.com');
|
|
assert.strictEqual(custom.instances[0].name, 'Example');
|
|
assert.deepEqual(custom.instances[0].customIcon, customIcon);
|
|
assert.deepEqual(custom.instances[0].icon, customIcon);
|
|
assert.false(custom.instances[0].canReconnect);
|
|
|
|
const row = service.rows.find((entry) => entry.id === 'custom-1');
|
|
assert.strictEqual(row.label, 'Custom Link');
|
|
assert.strictEqual(row.byline, 'Example');
|
|
|
|
await service.remove({ type: 'custom', id: 'custom-1' });
|
|
assert.deepEqual(registeredSites.removed, ['custom-1']);
|
|
});
|
|
|
|
test('calendar adapters expose instances and remove', async function (assert) {
|
|
const calendar = new FakeCalendarService();
|
|
calendar.instancesByType['calendar-caldav'] = [
|
|
{
|
|
id: 'calendar-caldav-cal.example.com-alice',
|
|
type: 'calendar-caldav',
|
|
label: 'cal.example.com',
|
|
url: 'https://cal.example.com/dav/',
|
|
username: 'alice',
|
|
password: 'secret',
|
|
calendars: [{ id: 'c1', name: 'Work', components: ['event'] }],
|
|
selectedCalendarIds: ['c1'],
|
|
},
|
|
];
|
|
calendar.instancesByType['calendar-ics'] = [
|
|
{
|
|
id: 'calendar-ics-example.org',
|
|
type: 'calendar-ics',
|
|
label: 'Holidays',
|
|
url: 'https://example.org/h.ics',
|
|
},
|
|
];
|
|
|
|
const { calendar: registered } = register(this.owner, { calendar });
|
|
|
|
const service = this.owner.lookup('service:integrations');
|
|
const caldav = service.all.find((entry) => entry.id === 'calendar-caldav');
|
|
const ics = service.all.find((entry) => entry.id === 'calendar-ics');
|
|
|
|
assert.true(caldav.supportsMultiple);
|
|
assert.true(caldav.hasSettings);
|
|
assert.strictEqual(caldav.instances.length, 1);
|
|
assert.strictEqual(caldav.instances[0].username, 'alice');
|
|
assert.deepEqual(caldav.instances[0].selectedCalendarIds, ['c1']);
|
|
|
|
assert.true(ics.supportsMultiple);
|
|
assert.strictEqual(ics.instances.length, 1);
|
|
assert.strictEqual(ics.instances[0].label, 'Holidays');
|
|
|
|
const caldavRow = service.rows.find(
|
|
(entry) => entry.id === 'calendar-caldav-cal.example.com-alice'
|
|
);
|
|
assert.strictEqual(caldavRow.label, 'CalDAV');
|
|
assert.strictEqual(caldavRow.byline, 'cal.example.com');
|
|
|
|
const icsRow = service.rows.find(
|
|
(entry) => entry.id === 'calendar-ics-example.org'
|
|
);
|
|
assert.strictEqual(icsRow.label, 'ICS / WebCal');
|
|
assert.strictEqual(icsRow.byline, 'example.org');
|
|
|
|
await service.remove({
|
|
type: 'calendar-caldav',
|
|
id: 'calendar-caldav-cal.example.com-alice',
|
|
});
|
|
assert.deepEqual(registered.removed, [
|
|
'calendar-caldav-cal.example.com-alice',
|
|
]);
|
|
});
|
|
});
|