Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
test(oauth): isolate cloud bridge callback port allocation
DavidShen30ce4ba1c57e
diff --git a/apps/desktop/src/main/plugin-oauth/__tests__/bridge.test.ts b/apps/desktop/src/main/plugin-oauth/__tests__/bridge.test.tsindex a77ccec12d..5317557452 100644--- a/apps/desktop/src/main/plugin-oauth/__tests__/bridge.test.ts+++ b/apps/desktop/src/main/plugin-oauth/__tests__/bridge.test.ts@@ -1,5 +1,5 @@import { createHash } from 'node:crypto';-import { afterEach, describe, expect, it, vi } from 'vitest';+import { afterEach, describe, expect, it, onTestFinished, vi } from 'vitest';import {parsePluginOauthRequest,type PluginOauthOffer,@@ -310,6 +310,20 @@ describe('shared authorization transaction lifecycle', () => {describe('real local loopback callback and cloud-owned account exchange', () => {it('stores only on the cloud Host; callback state/PKCE and redirect remain the original transaction', async () => {+ let sockets: ReturnType<typeof ephemeralCallbackPorts> | undefined;+ let redirectUri = '';+ onTestFinished(async () => {+ try {+ await Promise.all([...(sockets?.servers.values() ?? [])].map(server =>+ new Promise<void>((resolve) => {+ server.close(() => resolve());+ server.closeAllConnections();+ }),+ ));+ } finally {+ sockets?.listen.mockRestore();+ }+ });const vault = new Map<string, string>();const wire: unknown[] = [];let challenge = '';@@ -332,6 +346,7 @@ describe('real local loopback callback and cloud-owned account exchange', () =>const form = new URLSearchParams(String(init?.body));exchangeCount++;expect(form.get('code')).toBe('synthetic-code');+ expect(form.get('redirect_uri')).toBe(redirectUri);expect(createHash('sha256').update(form.get('code_verifier')!).digest('base64url')).toBe(challenge,);@@ -351,6 +366,14 @@ describe('real local loopback callback and cloud-owned account exchange', () =>bind: async () => ({ ghostId: 'test-plugin', current: () => true }),run: () => {const remote = getRemoteOauthContext()!;+ const authorize = remote.authorize.bind(remote);+ vi.spyOn(remote, 'authorize').mockImplementation((offer, signal) => {+ redirectUri = offer.callbackUrl;+ // Cloud and controller represent different machines. Keep the offered+ // authority intact while giving the controller an independently owned socket.+ sockets = ephemeralCallbackPorts(Number(new URL(offer.callbackUrl).port));+ return authorize(offer, signal);+ });accountRun = manager.connectAccount('test-plugin',@@ -390,10 +413,11 @@ describe('real local loopback callback and cloud-owned account exchange', () =>openExternal: async (raw) => {const authorize = new URL(raw);challenge = authorize.searchParams.get('code_challenge')!;+ expect(authorize.searchParams.get('redirect_uri')).toBe(redirectUri);const url = new URL(authorize.searchParams.get('redirect_uri')!);url.searchParams.set('state', authorize.searchParams.get('state')!);url.searchParams.set('code', 'synthetic-code');- const response = await fetch(url);+ const response = await sockets!.fetch(url.toString());expect(response.status).toBe(200);expect(await response.text()).toContain('callback received');},diff --git a/apps/desktop/src/main/plugin-oauth/__tests__/ephemeralCallbackPorts.ts b/apps/desktop/src/main/plugin-oauth/__tests__/ephemeralCallbackPorts.tsindex c192199494..2a3ea4f90f 100644--- a/apps/desktop/src/main/plugin-oauth/__tests__/ephemeralCallbackPorts.ts+++ b/apps/desktop/src/main/plugin-oauth/__tests__/ephemeralCallbackPorts.ts@@ -3,7 +3,7 @@ import type { AddressInfo, ListenOptions } from 'node:net';import { expect, vi } from 'vitest';/** Keep real HTTP handlers/sockets, but allocate ports at bind time, not with a racy probe. */-export function ephemeralCallbackPorts() {+export function ephemeralCallbackPorts(expectedPort = 12345) {const servers = new Map<string, http.Server>();const ports = new Map<string, number>();const original = http.Server.prototype.listen;@@ -12,9 +12,9 @@ export function ephemeralCallbackPorts() {...args: unknown[]) {const options = args[0] as ListenOptions;- // 12345 is only the logical callback authority, never an OS port reservation.+ // The offered port is only the logical callback authority, never an OS reservation.// Conflict tests restore this spy and retain an actual listen(0) owner.- expect(options).toMatchObject({ port: 12345, ipv6Only: options.host === '::1' });+ expect(options).toMatchObject({ port: expectedPort, ipv6Only: options.host === '::1' });expect(['127.0.0.1', '::1']).toContain(options.host);servers.set(options.host!, this);this.once('listening', () => ports.set(options.host!, (this.address() as AddressInfo).port));