File size: 2,027 Bytes
ef04721
 
 
 
 
 
 
 
 
 
3f516d0
 
ef04721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f516d0
ef04721
 
 
 
 
 
 
3f516d0
 
ef04721
 
 
 
 
 
 
 
3f516d0
ef04721
 
 
 
 
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
/**
 * 运行: cd client/src && npx tsx tests/api/resolveApiBase.test.ts
 */
import {
    normalizeApiBase,
    resolveApiBaseFromSources,
    apiUrl,
} from '../../shared/api/resolveApiBase';
import { resolveDemoFileUrlFromBase } from '../../shared/api/apiConfig';

const PORTAL = 'https://portal.example.test';

let passed = 0;
let failed = 0;

function assert(desc: string, actual: string, expected: string) {
    if (actual === expected) {
        console.log(`  ✓ ${desc}`);
        passed++;
    } else {
        console.error(`  ✗ ${desc} — expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
        failed++;
    }
}

console.log('normalizeApiBase');
assert('strip trailing slash', normalizeApiBase('https://example.com/'), 'https://example.com');
assert('empty', normalizeApiBase('  '), '');

console.log('resolveApiBaseFromSources');
assert('meta wins', resolveApiBaseFromSources('http://other', 'https://portal.hf.space'), 'https://portal.hf.space');
assert('meta wins over empty param', resolveApiBaseFromSources('', PORTAL), PORTAL);
assert('?api= when no meta', resolveApiBaseFromSources('http://localhost:5001', null), 'http://localhost:5001');
assert('same-origin default', resolveApiBaseFromSources(undefined, null), '');
assert('same-origin when meta empty', resolveApiBaseFromSources(undefined, '  '), '');

console.log('resolveDemoFileUrlFromBase');
assert(
    'portal + demo path',
    resolveDemoFileUrlFromBase(PORTAL, '/quick-start-1.json'),
    `${PORTAL}/demo/quick-start-1.json`,
);
assert(
    'same-origin demo',
    resolveDemoFileUrlFromBase('', '/InfoHighlight-intro.json'),
    '/demo/InfoHighlight-intro.json',
);

console.log('apiUrl');
assert('portal api path', apiUrl('/api/analyze', PORTAL), `${PORTAL}/api/analyze`);
assert('same-origin api', apiUrl('/api/tokenize', ''), '/api/tokenize');
assert('empty base explicit', apiUrl('/api/check_admin', ''), '/api/check_admin');

console.log(`\n${passed} passed, ${failed} failed`);
process.exit(failed > 0 ? 1 : 0);