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

217 lines
6.4 KiB
Plaintext

import { module, test } from 'qunit';
import { setupRenderingTest } from 'dashtab/tests/helpers';
import { render, fillIn, triggerEvent } from '@ember/test-helpers';
import { tracked } from '@glimmer/tracking';
import OpenRouterForm from 'dashtab/components/settings/integrations/openrouter-form';
function changeSelect(selector, value) {
const select = document.querySelector(selector);
const event = new Event('change', { bubbles: true });
Object.defineProperty(event, 'target', { value: { value } });
select.dispatchEvent(event);
}
class FakeOpenRouterService {
@tracked apiKey = null;
@tracked interval = 300000;
@tracked granularity = 'today';
calls = [];
async enable(apiKey, interval, granularity) {
this.calls.push({ type: 'enable', apiKey, interval, granularity });
}
async update(apiKey, interval, granularity) {
this.calls.push({ type: 'update', apiKey, interval, granularity });
}
classifyError(err) {
return err?.message ?? 'OpenRouter returned an error';
}
}
module(
'Integration | Component | settings/integrations/openrouter-form',
function (hooks) {
setupRenderingTest(hooks);
test('it renders the API key fields and a fixed description', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
const noop = () => {};
await render(<template><OpenRouterForm @close={{noop}} /></template>);
assert.dom().containsText('Add OpenRouter Spend');
assert
.dom('[data-test-openrouter-description]')
.hasText('Show your OpenRouter spend on the dashboard.');
assert.dom('#openrouter-key-type').doesNotExist();
assert.dom('#openrouter-key').exists();
assert.dom('#openrouter-interval').exists();
assert.dom('#openrouter-granularity').exists();
assert.dom('[data-test-load-keys]').doesNotExist();
assert.dom().containsText('OpenRouter API keys');
});
test('offers the three supported granularity options', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
const noop = () => {};
await render(<template><OpenRouterForm @close={{noop}} /></template>);
const select = document.querySelector('#openrouter-granularity');
const labels = [...select.querySelectorAll('el-option')].map((option) =>
option.textContent.trim()
);
assert.deepEqual(labels, ['Today', 'This week', 'This month']);
});
test('submits the default settings in add mode', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
const noop = () => {};
await render(<template><OpenRouterForm @close={{noop}} /></template>);
await fillIn('#openrouter-key', 'sk-or-v1-test');
await triggerEvent('form', 'submit');
assert.deepEqual(openrouter.calls, [
{
type: 'enable',
apiKey: 'sk-or-v1-test',
interval: 300000,
granularity: 'today',
},
]);
});
test('edit mode prefills the stored settings and saves with update', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
this.props = {
mode: 'edit',
integration: {
apiKey: 'sk-or-v1-stored',
interval: 600000,
granularity: 'this-week',
},
};
let closed = false;
const closeDialog = () => {
closed = true;
};
await render(
<template>
<OpenRouterForm @props={{this.props}} @close={{closeDialog}} />
</template>
);
assert.dom().containsText('Edit OpenRouter Spend');
assert.dom('#openrouter-interval').containsText('Every 10 minutes');
assert.dom('#openrouter-granularity').containsText('This week');
await triggerEvent('form', 'submit');
assert.deepEqual(openrouter.calls, [
{
type: 'update',
apiKey: 'sk-or-v1-stored',
interval: 600000,
granularity: 'this-week',
},
]);
assert.true(closed);
});
test('a blank key in edit mode keeps the stored key', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
this.props = {
mode: 'edit',
integration: {
apiKey: 'sk-or-v1-stored',
interval: 600000,
granularity: 'today',
},
};
const noop = () => {};
await render(
<template>
<OpenRouterForm @props={{this.props}} @close={{noop}} />
</template>
);
await triggerEvent('form', 'submit');
assert.deepEqual(openrouter.calls, [
{
type: 'update',
apiKey: 'sk-or-v1-stored',
interval: 600000,
granularity: 'today',
},
]);
});
test('shows an error when the key is missing', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
const noop = () => {};
await render(<template><OpenRouterForm @close={{noop}} /></template>);
await triggerEvent('form', 'submit');
assert.dom().containsText('Please enter an API key');
assert.deepEqual(openrouter.calls, []);
});
test('uses the selected interval and granularity', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
const noop = () => {};
await render(<template><OpenRouterForm @close={{noop}} /></template>);
await fillIn('#openrouter-key', 'sk-or-v1-test');
changeSelect('#openrouter-interval', '600000');
changeSelect('#openrouter-granularity', 'this-month');
await triggerEvent('form', 'submit');
assert.deepEqual(openrouter.calls, [
{
type: 'enable',
apiKey: 'sk-or-v1-test',
interval: 600000,
granularity: 'this-month',
},
]);
});
}
);