u
This commit is contained in:
89
test/openrouter.test.js
Normal file
89
test/openrouter.test.js
Normal file
@@ -0,0 +1,89 @@
|
||||
'use strict';
|
||||
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
import { OpenRouterProvider } from '../src/providers/OpenRouterProvider.js';
|
||||
|
||||
test('OpenRouterProvider shares process-lifetime session ID by default', () => {
|
||||
assert.ok(OpenRouterProvider.processSessionId, 'processSessionId should exist');
|
||||
assert.match(
|
||||
OpenRouterProvider.processSessionId,
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
||||
'processSessionId should be a valid UUID',
|
||||
);
|
||||
|
||||
const provider1 = new OpenRouterProvider({ apiKey: 'test-key-1' });
|
||||
const provider2 = new OpenRouterProvider({ apiKey: 'test-key-2' });
|
||||
|
||||
assert.equal(provider1.sessionId, OpenRouterProvider.processSessionId);
|
||||
assert.equal(provider2.sessionId, OpenRouterProvider.processSessionId);
|
||||
assert.equal(provider1.sessionId, provider2.sessionId);
|
||||
});
|
||||
|
||||
test('OpenRouterProvider allows custom sessionId override in constructor', () => {
|
||||
const provider = new OpenRouterProvider({ apiKey: 'test-key', sessionId: 'custom-session-123' });
|
||||
assert.equal(provider.sessionId, 'custom-session-123');
|
||||
});
|
||||
|
||||
test('chatCompletions sends session_id in JSON body and x-session-id in HTTP header', async () => {
|
||||
let capturedUrl = null;
|
||||
let capturedOptions = null;
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
globalThis.fetch = async (url, options) => {
|
||||
capturedUrl = url;
|
||||
capturedOptions = options;
|
||||
return {
|
||||
ok: true,
|
||||
status: 200,
|
||||
body: {
|
||||
getReader() {
|
||||
let done = false;
|
||||
return {
|
||||
async read() {
|
||||
if (done) return { done: true, value: undefined };
|
||||
done = true;
|
||||
const encoder = new TextEncoder();
|
||||
return {
|
||||
done: false,
|
||||
value: encoder.encode('data: {"choices":[{"delta":{"content":"ok"}}]}\n\ndata: [DONE]\n\n'),
|
||||
};
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
try {
|
||||
const provider = new OpenRouterProvider({ apiKey: 'sk-test' });
|
||||
const res = await provider.chatCompletions({
|
||||
model: 'test/model',
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
});
|
||||
|
||||
assert.equal(res.content, 'ok');
|
||||
assert.ok(capturedOptions, 'fetch should have been called');
|
||||
|
||||
// Check header
|
||||
assert.equal(capturedOptions.headers['x-session-id'], OpenRouterProvider.processSessionId);
|
||||
|
||||
// Check body
|
||||
const parsedBody = JSON.parse(capturedOptions.body);
|
||||
assert.equal(parsedBody.session_id, OpenRouterProvider.processSessionId);
|
||||
assert.equal(parsedBody.model, 'test/model');
|
||||
|
||||
// Test custom sessionId override per-call
|
||||
await provider.chatCompletions({
|
||||
model: 'test/model',
|
||||
messages: [{ role: 'user', content: 'hello again' }],
|
||||
sessionId: 'call-specific-session',
|
||||
});
|
||||
const parsedBody2 = JSON.parse(capturedOptions.body);
|
||||
assert.equal(capturedOptions.headers['x-session-id'], 'call-specific-session');
|
||||
assert.equal(parsedBody2.session_id, 'call-specific-session');
|
||||
} finally {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user