Files
dashtab/tests/integration/components/openrouter-usage-test.gjs
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

146 lines
4.2 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 OpenRouterUsage from 'dashtab/components/openrouter-usage';
class FakeOpenRouterService {
@tracked enabled = false;
@tracked usage = null;
@tracked granularity = 'today';
@tracked loading = false;
@tracked error = null;
}
class FakeModalService {
opened = null;
open(descriptor) {
this.opened = descriptor;
}
}
module('Integration | Component | openrouter-usage', function (hooks) {
setupRenderingTest(hooks);
test('it renders nothing when disabled', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
await render(<template><OpenRouterUsage /></template>);
assert.dom('[data-test-openrouter-usage]').doesNotExist();
});
test('it opens the OpenRouter settings when enabled', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
const modal = new FakeModalService();
this.owner.register('service:modal', modal, { instantiate: false });
openrouter.enabled = true;
await render(<template><OpenRouterUsage /></template>);
await click('[data-test-openrouter-usage]');
assert.ok(modal.opened.component);
assert.strictEqual(modal.opened.props.mode, 'edit');
assert.strictEqual(modal.opened.size, 'md');
});
test('it shows a single usage number', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
openrouter.enabled = true;
openrouter.usage = 1.5;
await render(<template><OpenRouterUsage /></template>);
assert.dom().containsText('$1.50');
assert.dom().doesNotContainText('Today');
assert.dom().doesNotContainText('Yesterday');
});
test('it describes the selected window', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
openrouter.enabled = true;
openrouter.usage = 1.5;
openrouter.granularity = 'this-week';
await render(<template><OpenRouterUsage /></template>);
assert
.dom('[data-test-openrouter-usage]')
.hasAttribute('title', 'OpenRouter spend, this week (UTC)');
assert
.dom('[data-test-openrouter-usage]')
.hasAttribute('aria-label', 'Edit OpenRouter Spend settings');
});
test('it renders the icon', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
openrouter.enabled = true;
openrouter.usage = 1.5;
await render(<template><OpenRouterUsage /></template>);
assert.dom('[data-test-openrouter-usage] svg').exists();
});
test('it shows a placeholder when no value is available', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
openrouter.enabled = true;
await render(<template><OpenRouterUsage /></template>);
assert.dom().containsText('—');
});
test('it shows a loading state while loading and no data', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
openrouter.enabled = true;
openrouter.loading = true;
await render(<template><OpenRouterUsage /></template>);
assert.dom().containsText('Loading…');
});
test('it shows an error message', async function (assert) {
const openrouter = new FakeOpenRouterService();
this.owner.register('service:openrouter', openrouter, {
instantiate: false,
});
openrouter.enabled = true;
openrouter.error = 'Invalid or expired API key';
await render(<template><OpenRouterUsage /></template>);
assert.dom().containsText('Invalid or expired API key');
});
});