feat(desktop): support remote runtime headers

This commit is contained in:
Bohdan Triapitsyn
2026-06-30 00:30:48 +03:00
parent 9c1eb755f9
commit 359c73fcf3
23 changed files with 458 additions and 45 deletions
+84 -1
View File
@@ -1,5 +1,26 @@
import { describe, expect, test } from 'bun:test';
import { redactSensitiveUrl, resolveDesktopHostUrl } from './desktopHosts';
import { desktopHostProbe, desktopHostsGet, desktopHostsSet, redactSensitiveUrl, resolveDesktopHostUrl } from './desktopHosts';
const withDesktopBridge = async <T>(handler: (cmd: string, args: Record<string, unknown>) => unknown | Promise<unknown>, run: () => Promise<T>): Promise<T> => {
const previousWindow = Object.getOwnPropertyDescriptor(globalThis, 'window');
Object.defineProperty(globalThis, 'window', {
configurable: true,
value: {
__OPENCHAMBER_DESKTOP__: {
invoke: handler,
},
},
});
try {
return await run();
} finally {
if (previousWindow) {
Object.defineProperty(globalThis, 'window', previousWindow);
} else {
Reflect.deleteProperty(globalThis, 'window');
}
}
};
describe('resolveDesktopHostUrl', () => {
test('keeps regular host URLs unchanged', () => {
@@ -32,3 +53,65 @@ describe('resolveDesktopHostUrl', () => {
);
});
});
describe('desktop host runtime headers', () => {
test('parses persisted request headers from desktop config', async () => {
await withDesktopBridge(async (cmd) => {
expect(cmd).toBe('desktop_hosts_get');
return {
hosts: [{
id: 'remote-1',
label: 'Remote',
url: 'https://remote.example',
requestHeaders: {
' CF-Access-Client-Id ': ' client-id ',
Authorization: 'Bearer should-not-be-read',
'Bad:Name': 'bad',
},
}],
defaultHostId: 'remote-1',
initialHostChoiceCompleted: true,
};
}, async () => {
const config = await desktopHostsGet();
expect(config.hosts[0]?.requestHeaders).toEqual({
'CF-Access-Client-Id': 'client-id',
});
});
});
test('passes request headers through host save and probe IPC calls', async () => {
const calls: Array<{ cmd: string; args: Record<string, unknown> }> = [];
await withDesktopBridge(async (cmd, args) => {
calls.push({ cmd, args });
if (cmd === 'desktop_host_probe') return { status: 'ok', latencyMs: 7 };
return null;
}, async () => {
const requestHeaders = { 'CF-Access-Client-Id': 'client-id' };
await desktopHostsSet({
hosts: [{ id: 'remote-1', label: 'Remote', url: 'https://remote.example', requestHeaders }],
defaultHostId: 'remote-1',
});
const probe = await desktopHostProbe('https://remote.example', { requestHeaders });
expect(probe).toEqual({ status: 'ok', latencyMs: 7 });
});
expect(calls[0]).toEqual({
cmd: 'desktop_hosts_set',
args: {
input: {
hosts: [{ id: 'remote-1', label: 'Remote', url: 'https://remote.example', requestHeaders: { 'CF-Access-Client-Id': 'client-id' } }],
defaultHostId: 'remote-1',
initialHostChoiceCompleted: undefined,
},
},
});
expect(calls[1]).toEqual({
cmd: 'desktop_host_probe',
args: {
url: 'https://remote.example',
requestHeaders: { 'CF-Access-Client-Id': 'client-id' },
},
});
});
});