anurag008w commited on
Commit
ceff200
Β·
1 Parent(s): e6ee6f1

update rotator and startup files

Browse files
Files changed (1) hide show
  1. test-rotator.mjs +0 -124
test-rotator.mjs DELETED
@@ -1,124 +0,0 @@
1
- import { createRequire } from 'module';
2
- import path from 'path';
3
- import { fileURLToPath } from 'url';
4
- const require = createRequire(import.meta.url);
5
- const G = s => `\x1b[32m${s}\x1b[0m`;
6
- const R = s => `\x1b[31m${s}\x1b[0m`;
7
- const Y = s => `\x1b[33m${s}\x1b[0m`;
8
- const B = s => `\x1b[1m${s}\x1b[0m`;
9
-
10
- Object.assign(process.env, {
11
- ANTHROPIC_API_KEYS:'ant-k1,ant-k2,ant-k3', OPENAI_API_KEYS:'oai-k1,oai-k2,oai-k3',
12
- GEMINI_API_KEYS:'gem-k1,gem-k2,gem-k3', DEEPSEEK_API_KEYS:'dsk-k1,dsk-k2,dsk-k3',
13
- OPENROUTER_API_KEYS:'ort-k1,ort-k2,ort-k3', KILOCODE_API_KEYS:'kil-k1,kil-k2,kil-k3',
14
- OPENCODE_API_KEYS:'ocd-k1,ocd-k2,ocd-k3', ZAI_API_KEYS:'zai-k1,zai-k2,zai-k3',
15
- MOONSHOT_API_KEYS:'msn-k1,msn-k2,msn-k3', MINIMAX_API_KEYS:'mmx-k1,mmx-k2,mmx-k3',
16
- XIAOMI_API_KEYS:'xmi-k1,xmi-k2,xmi-k3', VOLCANO_ENGINE_API_KEYS:'vlc-k1,vlc-k2,vlc-k3',
17
- BYTEPLUS_API_KEYS:'btp-k1,btp-k2,btp-k3', MISTRAL_API_KEYS:'mst-k1,mst-k2,mst-k3',
18
- XAI_API_KEYS:'xai-k1,xai-k2,xai-k3', NVIDIA_API_KEYS:'nv-k1,nv-k2,nv-k3',
19
- GROQ_API_KEYS:'grq-k1,grq-k2,grq-k3', COHERE_API_KEYS:'coh-k1,coh-k2,coh-k3',
20
- TOGETHER_API_KEYS:'tgt-k1,tgt-k2,tgt-k3', CEREBRAS_API_KEYS:'crb-k1,crb-k2,crb-k3',
21
- HUGGINGFACE_HUB_TOKENS:'hf-k1,hf-k2,hf-k3',
22
- });
23
-
24
- // Mock fetch BEFORE loading rotator (rotator captures this as "originalFetch")
25
- const fetchLog = [];
26
- globalThis.fetch = async function mockFetch(input, init = {}) {
27
- const url = typeof input === 'string' ? input : input?.url ?? '?';
28
- const h = init?.headers ?? {};
29
- const auth = typeof h.get === 'function' ? h.get('authorization') : (h.authorization ?? null);
30
- fetchLog.push({ url, auth });
31
- return new Response('{}', { status: 200 });
32
- };
33
-
34
- // Mock http/https BEFORE loading rotator
35
- const http = require('http');
36
- const https = require('https');
37
- const httpLog = [];
38
- for (const mod of [http, https]) {
39
- mod.request = function mockReq(...args) {
40
- const opts = args[0];
41
- const hostname = typeof opts === 'string' ? new URL(opts).hostname : opts?.hostname ?? '?';
42
- const auth = opts?.headers?.authorization ?? null;
43
- httpLog.push({ hostname, auth });
44
- return { on() { return this; }, end() {}, write() {} };
45
- };
46
- }
47
-
48
- // Load rotator
49
- console.log(B('\n── Loading rotator ─────────────────────────────────────\n'));
50
- const origLog = console.log; const ll = [];
51
- console.log = (...a) => ll.push(a.join(' '));
52
- require('./multi-provider-key-rotator.cjs');
53
- console.log = origLog;
54
- ll.forEach(l => console.log(' ' + l));
55
-
56
- const TESTS = [
57
- ['api.anthropic.com','ant-','Anthropic'],
58
- ['api.openai.com','oai-','OpenAI'],
59
- ['generativelanguage.googleapis.com','gem-','Gemini'],
60
- ['api.deepseek.com','dsk-','DeepSeek'],
61
- ['openrouter.ai','ort-','OpenRouter'],
62
- ['kilocode.ai','kil-','KiloCode'],
63
- ['opencode.ai','ocd-','OpenCode'],
64
- ['open.bigmodel.cn','zai-','Z.ai / GLM'],
65
- ['api.moonshot.cn','msn-','Moonshot'],
66
- ['api.minimax.chat','mmx-','MiniMax'],
67
- ['api.xiaomi.com','xmi-','Xiaomi'],
68
- ['ark.cn-beijing.volces.com','vlc-','Volcengine'],
69
- ['maas-api.ml-platform-cn-beijing.byteplus.com','btp-','BytePlus'],
70
- ['api.mistral.ai','mst-','Mistral'],
71
- ['api.x.ai','xai-','xAI / Grok'],
72
- ['integrate.api.nvidia.com','nv-','NVIDIA (integrate)'],
73
- ['api.nvidia.com','nv-','NVIDIA (api)'],
74
- ['api.groq.com','grq-','Groq'],
75
- ['api.cohere.com','coh-','Cohere'],
76
- ['api.cohere.ai','coh-','Cohere (alt)'],
77
- ['api.together.xyz','tgt-','Together'],
78
- ['api.cerebras.ai','crb-','Cerebras'],
79
- ['api-inference.huggingface.co','hf-','HuggingFace'],
80
- ];
81
-
82
- console.log(B('\n── fetch() β€” 3 requests each (rotation check) ─────────\n'));
83
- let passed = 0, failed = 0;
84
-
85
- for (const [hostname, prefix, label] of TESTS) {
86
- fetchLog.length = 0;
87
- for (let i = 0; i < 3; i++) await globalThis.fetch(`https://${hostname}/v1/messages`, {});
88
- const keys = fetchLog.map(f => f.auth?.replace('Bearer ', '') ?? null);
89
- const ok = keys.every(k => k?.startsWith(prefix)) && keys[0] !== keys[1] && keys[1] !== keys[2];
90
- if (ok) { passed++; console.log(` ${G('βœ“')} ${label.padEnd(24)} ${keys.join(' β†’ ')}`); }
91
- else { failed++; console.log(` ${R('βœ—')} ${label.padEnd(24)} got: ${JSON.stringify(keys)}`); }
92
- }
93
-
94
- // Unknown host β€” no header
95
- fetchLog.length = 0;
96
- await globalThis.fetch('https://example.com/test', {});
97
- const unk = fetchLog[0]?.auth ?? null;
98
- if (!unk) { passed++; console.log(`\n ${G('βœ“')} ${'Unknown host'.padEnd(24)} correctly skipped`); }
99
- else { failed++; console.log(`\n ${R('βœ—')} ${'Unknown host'.padEnd(24)} wrongly injected: ${unk}`); }
100
-
101
- console.log(B('\n── http.request() β€” direct options object ──────────────\n'));
102
- const HTTP_TESTS = [
103
- ['api.anthropic.com','ant-','Anthropic'],
104
- ['api.openai.com','oai-','OpenAI'],
105
- ['api.mistral.ai','mst-','Mistral'],
106
- ['integrate.api.nvidia.com','nv-','NVIDIA'],
107
- ['api.groq.com','grq-','Groq'],
108
- ['api.x.ai','xai-','xAI'],
109
- ];
110
- for (const [hostname, prefix, label] of HTTP_TESTS) {
111
- httpLog.length = 0;
112
- http.request({ hostname, path: '/v1', headers: {} });
113
- http.request({ hostname, path: '/v1', headers: {} });
114
- const keys = httpLog.map(e => e.auth?.replace('Bearer ', '') ?? null);
115
- const ok = keys.every(k => k?.startsWith(prefix)) && keys[0] !== keys[1];
116
- if (ok) { passed++; console.log(` ${G('βœ“')} ${label.padEnd(24)} ${keys.join(' β†’ ')}`); }
117
- else { failed++; console.log(` ${R('βœ—')} ${label.padEnd(24)} got: ${JSON.stringify(keys)}`); }
118
- }
119
-
120
- console.log(B('\n────────────────────────────────────────────────────────'));
121
- const total = passed + failed;
122
- console.log(` ${G(passed+'/'+total+' passed')} ${failed ? R(failed+' failed') : ''}`);
123
- if (failed === 0) console.log(` ${G('βœ“ Rotator sahi kaam kar raha hai β€” deploy karo!')}\n`);
124
- else console.log(` ${R('βœ— Issues hain β€” upar dekho.')}\n`);