Files
raucao 4f3c4334bd Use only normal API keys for OpenRouter spend
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.
2026-11-16 22:16:35 +04:00

197 lines
6.4 KiB
Plaintext

import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render, click } from '@ember/test-helpers';
import { tracked } from '@glimmer/tracking';
import TickersStats from 'dashtab/components/settings/global-settings/sections/tickers-stats';
import AddList from 'dashtab/components/settings/integrations/add-list';
import BitcoinForm from 'dashtab/components/settings/integrations/bitcoin-form';
import OpenRouterForm from 'dashtab/components/settings/integrations/openrouter-form';
class FakeSitesService {
@tracked all = [];
integrationFor() {
return undefined;
}
async removeSource() {}
}
class FakeNotificationsService {
stateFor() {
return { 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 {
instancesFor() {
return [];
}
}
class FakeModalService {
opened = null;
open(descriptor) {
this.opened = descriptor;
}
replace(descriptor) {
this.opened = descriptor;
}
}
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();
const modal = overrides.modal ?? new FakeModalService();
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 });
owner.register('service:modal', modal, { instantiate: false });
return { sites, notifications, bitcoin, openrouter, calendar, modal };
}
module(
'Integration | Component | settings/global-settings/sections/tickers-stats',
function (hooks) {
setupRenderingTest(hooks);
test('offers a single Add integration button when nothing is enabled', async function (assert) {
register(this.owner);
await render(<template><TickersStats /></template>);
assert.dom('[data-test-add]').exists();
assert.dom('[data-test-add]').hasText('Add integration');
assert.dom('[data-test-add="bitcoin"]').doesNotExist();
assert.dom('[data-test-add="openrouter"]').doesNotExist();
assert.dom('[data-instance="bitcoin"]').doesNotExist();
assert.dom('[data-instance="openrouter"]').doesNotExist();
});
test('adding opens the integration list filtered to tickers and stats', async function (assert) {
const { modal } = register(this.owner);
await render(<template><TickersStats /></template>);
await click('[data-test-add]');
assert.strictEqual(modal.opened.component, AddList);
assert.deepEqual(modal.opened.props.types, ['bitcoin', 'openrouter']);
});
test('hides the Add integration button when both integrations are connected', async function (assert) {
const bitcoin = new FakeBitcoinService();
bitcoin.enabled = true;
const openrouter = new FakeOpenRouterService();
openrouter.enabled = true;
register(this.owner, { bitcoin, openrouter });
await render(<template><TickersStats /></template>);
assert.dom('[data-test-add]').doesNotExist();
assert.dom('[data-instance="bitcoin"]').exists();
assert.dom('[data-instance="openrouter"]').exists();
});
test('shows active tickers and stats with settings and remove buttons', async function (assert) {
const bitcoin = new FakeBitcoinService();
bitcoin.enabled = true;
const openrouter = new FakeOpenRouterService();
openrouter.enabled = true;
register(this.owner, { bitcoin, openrouter });
await render(<template><TickersStats /></template>);
assert.dom('[data-instance="bitcoin"]').exists();
assert.dom('[data-instance="bitcoin"] [data-test-settings]').exists();
assert.dom('[data-instance="bitcoin"] [data-test-remove]').exists();
assert.dom('[data-instance="openrouter"]').exists();
assert.dom('[data-instance="openrouter"] [data-test-settings]').exists();
assert.dom('[data-instance="openrouter"] [data-test-remove]').exists();
});
test('editing a ticker opens its form', async function (assert) {
const bitcoin = new FakeBitcoinService();
bitcoin.enabled = true;
const { modal } = register(this.owner, { bitcoin });
await render(<template><TickersStats /></template>);
await click('[data-instance="bitcoin"] [data-test-settings]');
assert.strictEqual(modal.opened.component, BitcoinForm);
assert.strictEqual(modal.opened.props.mode, 'edit');
assert.strictEqual(modal.opened.props.integration.type, 'bitcoin');
});
test('editing a stat opens its form', async function (assert) {
const openrouter = new FakeOpenRouterService();
openrouter.enabled = true;
const { modal } = register(this.owner, { openrouter });
await render(<template><TickersStats /></template>);
await click('[data-instance="openrouter"] [data-test-settings]');
assert.strictEqual(modal.opened.component, OpenRouterForm);
assert.strictEqual(modal.opened.props.mode, 'edit');
assert.strictEqual(modal.opened.props.integration.type, 'openrouter');
});
test('removing a ticker or stat disables it', async function (assert) {
const bitcoin = new FakeBitcoinService();
bitcoin.enabled = true;
const openrouter = new FakeOpenRouterService();
openrouter.enabled = true;
register(this.owner, { bitcoin, openrouter });
await render(<template><TickersStats /></template>);
await click('[data-instance="bitcoin"] [data-test-remove]');
assert.true(bitcoin.disabled);
await click('[data-instance="openrouter"] [data-test-remove]');
assert.true(openrouter.disabled);
});
}
);