fix: resolve runtime URLs from injected desktop API base
Uses the injected desktop API base URL at call time Fixes packaged desktop WebSocket URL generation Adds regression coverage for injected runtime URLs
This commit is contained in:
@@ -8,6 +8,19 @@ import {
|
||||
import { setRuntimeBearerToken, setRuntimeUrlAuthToken } from './runtime-auth';
|
||||
|
||||
describe('createRuntimeUrlResolver', () => {
|
||||
const withWindow = <T>(value: unknown, callback: () => T): T => {
|
||||
const originalWindow = globalThis.window;
|
||||
try {
|
||||
Object.defineProperty(globalThis, 'window', {
|
||||
configurable: true,
|
||||
value,
|
||||
});
|
||||
return callback();
|
||||
} finally {
|
||||
Object.defineProperty(globalThis, 'window', { configurable: true, value: originalWindow });
|
||||
}
|
||||
};
|
||||
|
||||
test('preserves relative same-origin URLs by default', () => {
|
||||
const urls = createRuntimeUrlResolver({ currentHref: () => 'http://127.0.0.1:3000/app' });
|
||||
|
||||
@@ -52,6 +65,29 @@ describe('createRuntimeUrlResolver', () => {
|
||||
expect(urls.websocket('/api/terminal/ws')).toBe('ws://localhost:5173/api/terminal/ws');
|
||||
});
|
||||
|
||||
test('uses injected desktop API base URL for packaged WebSocket URLs', () => {
|
||||
withWindow({
|
||||
location: { origin: 'openchamber-ui://app', href: 'openchamber-ui://app/index.html' },
|
||||
__OPENCHAMBER_API_BASE_URL__: 'http://127.0.0.1:57123',
|
||||
}, () => {
|
||||
const urls = createRuntimeUrlResolver({});
|
||||
|
||||
expect(urls.websocket('/api/global/event/ws')).toBe('ws://127.0.0.1:57123/api/global/event/ws');
|
||||
});
|
||||
});
|
||||
|
||||
test('reads injected desktop API base URL at call time', () => {
|
||||
withWindow({
|
||||
location: { origin: 'openchamber-ui://app', href: 'openchamber-ui://app/index.html' },
|
||||
}, () => {
|
||||
const urls = createRuntimeUrlResolver({});
|
||||
(window as typeof window & { __OPENCHAMBER_API_BASE_URL__?: string }).__OPENCHAMBER_API_BASE_URL__ = 'http://127.0.0.1:57123';
|
||||
|
||||
expect(urls.api('/api/config/settings')).toBe('http://127.0.0.1:57123/api/config/settings');
|
||||
expect(urls.websocket('/api/global/event/ws')).toBe('ws://127.0.0.1:57123/api/global/event/ws');
|
||||
});
|
||||
});
|
||||
|
||||
test('allows runtime-wide resolver configuration', () => {
|
||||
const previous = getRuntimeUrlResolver();
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user