openclaw-endpoint.test.ts
1 import assert from 'node:assert/strict' 2 import { test } from 'node:test' 3 import { 4 deriveOpenClawWsUrl, 5 isLocalOpenClawEndpoint, 6 normalizeOpenClawEndpoint, 7 normalizeProviderEndpoint, 8 } from '@/lib/openclaw/openclaw-endpoint' 9 10 test('normalizeOpenClawEndpoint handles ws/http/path variants', () => { 11 assert.equal( 12 normalizeOpenClawEndpoint('ws://127.0.0.1:18789'), 13 'http://127.0.0.1:18789/v1', 14 ) 15 assert.equal( 16 normalizeOpenClawEndpoint('http://localhost:18789'), 17 'http://localhost:18789/v1', 18 ) 19 assert.equal( 20 normalizeOpenClawEndpoint('http://localhost:18789/v1/chat/completions'), 21 'http://localhost:18789/v1', 22 ) 23 }) 24 25 test('deriveOpenClawWsUrl strips v1 suffix and preserves host', () => { 26 assert.equal( 27 deriveOpenClawWsUrl('http://localhost:18789/v1'), 28 'ws://localhost:18789', 29 ) 30 assert.equal( 31 deriveOpenClawWsUrl('https://openclaw.example.com/v1'), 32 'wss://openclaw.example.com', 33 ) 34 }) 35 36 test('normalizeProviderEndpoint only rewrites openclaw provider', () => { 37 assert.equal( 38 normalizeProviderEndpoint('openclaw', 'ws://localhost:18789'), 39 'http://localhost:18789/v1', 40 ) 41 assert.equal( 42 normalizeProviderEndpoint('openai', 'https://api.openai.com/v1/'), 43 'https://api.openai.com/v1', 44 ) 45 assert.equal( 46 normalizeProviderEndpoint('openai', null), 47 null, 48 ) 49 }) 50 51 test('isLocalOpenClawEndpoint detects loopback hosts', () => { 52 assert.equal(isLocalOpenClawEndpoint('ws://localhost:18789'), true) 53 assert.equal(isLocalOpenClawEndpoint('http://127.0.0.1:18789/v1'), true) 54 assert.equal(isLocalOpenClawEndpoint('http://[::1]:18789/v1'), true) 55 assert.equal(isLocalOpenClawEndpoint('https://openclaw.example.com/v1'), false) 56 })