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.
348 lines
10 KiB
JavaScript
348 lines
10 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import { OPENROUTER_KEY_URL } from 'dashtab/config/openrouter';
|
|
|
|
module('Unit | Service | openrouter', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
let originalFetch;
|
|
let originalConsoleError;
|
|
|
|
hooks.beforeEach(async function () {
|
|
originalFetch = globalThis.fetch;
|
|
originalConsoleError = console.error;
|
|
console.error = () => {};
|
|
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.remove('integrations', 'openrouter');
|
|
});
|
|
|
|
hooks.afterEach(function () {
|
|
globalThis.fetch = originalFetch;
|
|
console.error = originalConsoleError;
|
|
});
|
|
|
|
test('it exists', function (assert) {
|
|
const service = this.owner.lookup('service:openrouter');
|
|
assert.ok(service);
|
|
});
|
|
|
|
test('load enables a stored active integration', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
// A record written by the old management-key flow must still load.
|
|
await storage.set('integrations', 'openrouter', {
|
|
type: 'openrouter',
|
|
status: 'active',
|
|
apiKey: 'sk-or-v1-test',
|
|
keyType: 'management',
|
|
keyIds: ['abc'],
|
|
interval: 600000,
|
|
granularity: 'this-week',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
service.fetchUsage = async () => {};
|
|
await service.ready;
|
|
|
|
assert.true(service.enabled);
|
|
assert.strictEqual(service.apiKey, 'sk-or-v1-test');
|
|
assert.strictEqual(service.interval, 600000);
|
|
assert.strictEqual(service.granularity, 'this-week');
|
|
assert.ok(service.intervalId);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('load stays disabled for a pending integration', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'openrouter', {
|
|
type: 'openrouter',
|
|
status: 'pending',
|
|
});
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
await service.ready;
|
|
|
|
assert.false(service.enabled);
|
|
assert.strictEqual(service.intervalId, null);
|
|
});
|
|
|
|
test('enable validates the key and persists the integration', async function (assert) {
|
|
let requestedUrl;
|
|
let requestedHeaders;
|
|
globalThis.fetch = async (url, options) => {
|
|
requestedUrl = url;
|
|
requestedHeaders = options.headers;
|
|
return {
|
|
ok: true,
|
|
json: async () => ({ data: { usage_daily: 1.25 } }),
|
|
};
|
|
};
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
service.fetchUsage = async () => {};
|
|
|
|
await service.enable('sk-or-v1-test', 600000, 'this-month');
|
|
|
|
assert.strictEqual(requestedUrl, OPENROUTER_KEY_URL);
|
|
assert.strictEqual(requestedHeaders.Authorization, 'Bearer sk-or-v1-test');
|
|
assert.true(service.enabled);
|
|
assert.strictEqual(service.apiKey, 'sk-or-v1-test');
|
|
assert.strictEqual(service.interval, 600000);
|
|
assert.strictEqual(service.granularity, 'this-month');
|
|
|
|
const storage = this.owner.lookup('service:storage');
|
|
const integration = await storage.get('integrations', 'openrouter');
|
|
assert.strictEqual(integration.status, 'active');
|
|
assert.strictEqual(integration.type, 'openrouter');
|
|
assert.strictEqual(integration.apiKey, 'sk-or-v1-test');
|
|
assert.strictEqual(integration.interval, 600000);
|
|
assert.strictEqual(integration.granularity, 'this-month');
|
|
assert.false('keyType' in integration);
|
|
assert.false('keyIds' in integration);
|
|
|
|
service.stopPolling();
|
|
});
|
|
|
|
test('enable throws when the key is invalid', async function (assert) {
|
|
globalThis.fetch = async () => ({
|
|
ok: false,
|
|
status: 401,
|
|
json: async () => ({ error: { code: 401 } }),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
await service.ready;
|
|
|
|
await assert.rejects(service.enable('bad', 600000, 'today'), (err) => {
|
|
assert.strictEqual(err.status, 401);
|
|
return true;
|
|
});
|
|
|
|
assert.false(service.enabled);
|
|
});
|
|
|
|
test('fetchUsage reads the usage bucket for the granularity', async function (assert) {
|
|
const requested = [];
|
|
globalThis.fetch = async (url, options) => {
|
|
requested.push({ url, options });
|
|
return {
|
|
ok: true,
|
|
json: async () => ({
|
|
data: {
|
|
usage: 9,
|
|
usage_daily: 1,
|
|
usage_weekly: 2,
|
|
usage_monthly: 3,
|
|
},
|
|
}),
|
|
};
|
|
};
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
service.apiKey = 'sk-or-v1-test';
|
|
service.enabled = true;
|
|
|
|
service.granularity = 'today';
|
|
await service.fetchUsage();
|
|
assert.strictEqual(service.usage, 1);
|
|
|
|
service.granularity = 'this-week';
|
|
await service.fetchUsage();
|
|
assert.strictEqual(service.usage, 2);
|
|
|
|
service.granularity = 'this-month';
|
|
await service.fetchUsage();
|
|
assert.strictEqual(service.usage, 3);
|
|
|
|
assert.strictEqual(requested.length, 3);
|
|
assert.strictEqual(requested[0].url, OPENROUTER_KEY_URL);
|
|
assert.strictEqual(
|
|
requested[0].options.headers.Authorization,
|
|
'Bearer sk-or-v1-test'
|
|
);
|
|
assert.strictEqual(service.error, null);
|
|
});
|
|
|
|
test('fetchUsage surfaces the browser management key error', async function (assert) {
|
|
globalThis.fetch = async () => ({
|
|
ok: false,
|
|
status: 403,
|
|
json: async () => ({
|
|
error: {
|
|
message:
|
|
'Management keys cannot be used from a browser. Call the management API from your server and keep the key out of client-side code.',
|
|
},
|
|
}),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
service.apiKey = 'sk-or-v1-test';
|
|
service.enabled = true;
|
|
|
|
await service.fetchUsage();
|
|
|
|
assert.strictEqual(
|
|
service.error,
|
|
'OpenRouter does not allow management keys from the browser. Use a normal API key instead.'
|
|
);
|
|
});
|
|
|
|
test('fetchUsage surfaces the API error message on failure', async function (assert) {
|
|
globalThis.fetch = async () => ({
|
|
ok: false,
|
|
status: 403,
|
|
json: async () => ({
|
|
error: {
|
|
code: 403,
|
|
message: 'Only management keys can perform this operation',
|
|
},
|
|
}),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
service.apiKey = 'sk-or-v1-test';
|
|
service.enabled = true;
|
|
|
|
await service.fetchUsage();
|
|
|
|
assert.strictEqual(
|
|
service.error,
|
|
'Only management keys can perform this operation'
|
|
);
|
|
});
|
|
|
|
test('classifyError falls back to status-based messages', async function (assert) {
|
|
const service = this.owner.lookup('service:openrouter');
|
|
|
|
assert.strictEqual(
|
|
service.classifyError({ status: 401 }),
|
|
'Invalid or expired API key'
|
|
);
|
|
assert.strictEqual(
|
|
service.classifyError({ status: 403 }),
|
|
'Access denied by OpenRouter'
|
|
);
|
|
assert.strictEqual(
|
|
service.classifyError({ status: 408 }),
|
|
'OpenRouter timed out'
|
|
);
|
|
assert.strictEqual(
|
|
service.classifyError({ status: 429 }),
|
|
'Rate limited by OpenRouter'
|
|
);
|
|
assert.strictEqual(
|
|
service.classifyError({ status: 500 }),
|
|
'OpenRouter returned an error'
|
|
);
|
|
assert.strictEqual(
|
|
service.classifyError({ name: 'NetworkError' }),
|
|
'Unable to reach OpenRouter'
|
|
);
|
|
});
|
|
|
|
test('fetchUsage ignores cancellation and page leaving', async function (assert) {
|
|
const service = this.owner.lookup('service:openrouter');
|
|
service.apiKey = 'sk-or-v1-test';
|
|
service.enabled = true;
|
|
|
|
globalThis.fetch = async () => {
|
|
throw new DOMException('Aborted', 'AbortError');
|
|
};
|
|
|
|
await service.fetchUsage();
|
|
assert.strictEqual(service.error, null);
|
|
|
|
service.pageLifecycle.leaving = true;
|
|
globalThis.fetch = async () => {
|
|
throw new Error('boom');
|
|
};
|
|
|
|
await service.fetchUsage();
|
|
assert.strictEqual(service.error, null);
|
|
|
|
service.pageLifecycle.leaving = false;
|
|
});
|
|
|
|
test('fetchUsage skips overlapping requests', async function (assert) {
|
|
let calls = 0;
|
|
globalThis.fetch = async () => {
|
|
calls += 1;
|
|
return { ok: true, json: async () => ({ data: {} }) };
|
|
};
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
service.apiKey = 'sk-or-v1-test';
|
|
service.enabled = true;
|
|
service.fetching = true;
|
|
|
|
await service.fetchUsage();
|
|
|
|
assert.strictEqual(calls, 0);
|
|
service.fetching = false;
|
|
});
|
|
|
|
test('disable clears storage and resets state', async function (assert) {
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
json: async () => ({ data: { usage_daily: 1 } }),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
service.fetchUsage = async () => {};
|
|
|
|
await service.enable('sk-or-v1-test', 600000, 'this-month');
|
|
service.usage = 1.5;
|
|
service.error = 'nope';
|
|
|
|
await service.disable();
|
|
|
|
const storage = this.owner.lookup('service:storage');
|
|
const integration = await storage.get('integrations', 'openrouter');
|
|
|
|
assert.strictEqual(integration, null);
|
|
assert.false(service.enabled);
|
|
assert.strictEqual(service.intervalId, null);
|
|
assert.strictEqual(service.apiKey, null);
|
|
assert.strictEqual(service.interval, 300000);
|
|
assert.strictEqual(service.granularity, 'today');
|
|
assert.strictEqual(service.usage, null);
|
|
assert.strictEqual(service.error, null);
|
|
});
|
|
|
|
test('update validates the key, keeps createdAt, and drops old fields', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('integrations', 'openrouter', {
|
|
type: 'openrouter',
|
|
status: 'active',
|
|
apiKey: 'sk-or-v1-old',
|
|
keyType: 'management',
|
|
keyIds: ['old'],
|
|
interval: 300000,
|
|
granularity: 'today',
|
|
createdAt: 12345,
|
|
});
|
|
|
|
globalThis.fetch = async () => ({
|
|
ok: true,
|
|
json: async () => ({ data: { usage_weekly: 2 } }),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:openrouter');
|
|
service.fetchUsage = async () => {};
|
|
await service.ready;
|
|
|
|
await service.update('sk-or-v1-new', 600000, 'this-week');
|
|
|
|
const integration = await storage.get('integrations', 'openrouter');
|
|
assert.strictEqual(integration.apiKey, 'sk-or-v1-new');
|
|
assert.strictEqual(integration.interval, 600000);
|
|
assert.strictEqual(integration.granularity, 'this-week');
|
|
assert.strictEqual(integration.createdAt, 12345);
|
|
assert.false('keyType' in integration);
|
|
assert.false('keyIds' in integration);
|
|
|
|
service.stopPolling();
|
|
});
|
|
});
|