File size: 3,850 Bytes
27a07f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfc1f67
 
 
 
 
 
 
 
 
 
 
 
 
 
8ad82a9
 
 
 
 
 
f993108
8ad82a9
 
 
 
 
c09fbc9
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import test from 'node:test';
import assert from 'node:assert/strict';
import handler from './provider-test.js';

function response() {
  return { code:null,body:null,setHeader(){},status(code){this.code=code;return this;},json(body){this.body=body;return this;} };
}
const request = () => ({method:'POST',body:{provider:'openai',model:'test-model',apiKey:'test-secret',baseUrl:'https://openrouter.ai/api/v1'}});

test('probe sends selected model auth and records real JSON validation and latency', async () => {
  const original = globalThis.fetch;
  try {
    globalThis.fetch = async (url, options) => {
      assert.equal(url, 'https://openrouter.ai/api/v1/chat/completions');
      assert.equal(options.headers.Authorization, 'Bearer test-secret');
      assert.equal(JSON.parse(options.body).model, 'test-model');
      return {ok:true,json:async()=>({choices:[{message:{content:'{"findings":[]}'}}]})};
    };
    const res = response(); await handler(request(),res);
    assert.equal(res.code,200); assert.equal(res.body.json_object_valid,true); assert.ok(res.body.latency_ms>=0);
    globalThis.fetch = async () => ({ok:true,json:async()=>({choices:[{message:{content:'[]'}}]})});
    await handler(request(),res); assert.equal(res.body.json_object_valid,false);
  } finally { globalThis.fetch = original; }
});

test('probe rejects private destinations and redacts provider error credentials', async () => {
  const original = globalThis.fetch;
  try {
    const req=request(),res=response(); req.body.baseUrl='https://127.0.0.1/v1';
    globalThis.fetch=async()=>{throw new Error('must not fetch private address');};
    await handler(req,res); assert.equal(res.code,400);
    req.body.baseUrl='https://openrouter.ai/api/v1';
    globalThis.fetch=async()=>({ok:false,status:401,json:async()=>({error:{message:'bad test-secret'}})});
    await handler(req,res); assert.equal(res.code,400); assert.ok(!res.body.error.includes('test-secret'));
  } finally { globalThis.fetch=original; }
});

test('MiniMax probe reserves reasoning budget consistently with review calls', async () => {
  const original = globalThis.fetch;
  try {
    globalThis.fetch = async (url, options) => {
      const body = JSON.parse(options.body);
      assert.equal(body.max_tokens,8192);
      assert.deepEqual(body.reasoning,{effort:'low'});
      return {ok:true,json:async()=>({choices:[{message:{content:'{"findings":[]}'}}]})};
    };
    const req = request(), res = response(); req.body.model='minimax/minimax-m3.1-flash:free';
    await handler(req,res); assert.equal(res.code,200);
  } finally { globalThis.fetch=original; }
});

test('TokenHarbor OpenAI-compatible URL is allowed', async () => {
  const original = globalThis.fetch;
  try {
    globalThis.fetch = async (url) => {
      assert.equal(url, 'https://tokenharbor.ai/v1/chat/completions');
      return {ok:true,json:async()=>({choices:[{message:{content:'{"findings":[]}'}}]})};
    };
    const req = request(), res = response(); req.body.baseUrl='https://tokenharbor.ai/v1';
    await handler(req,res); assert.equal(res.code,200); assert.equal(res.body.json_object_valid,true);
  } finally { globalThis.fetch = original; }
});

test('Cloudflare temporary HTTPS tunnel is allowed for testing', async () => {
  const original = globalThis.fetch;
  try {
    globalThis.fetch = async (url) => {
      assert.equal(url, 'https://mapping-relates-mediterranean-cosmetic.trycloudflare.com/v1/chat/completions');
      return {ok:true,json:async()=>({choices:[{message:{content:'{"findings":[]}'}}]})};
    };
    const req = request(), res = response();
    req.body.baseUrl='https://mapping-relates-mediterranean-cosmetic.trycloudflare.com/v1';
    await handler(req,res); assert.equal(res.code,200); assert.equal(res.body.json_object_valid,true);
  } finally { globalThis.fetch=original; }
});