60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
import { describe, test } from 'node:test';
|
|||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import type { BridgeContext } from './bridge';
|
||
|
|
import { handleProxyBridgeMessage } from './bridge-proxy-runtime';
|
||
|
|
|
||
|
|
const deps = {
|
||
|
|
tryHandleLocalFsProxy: async () => null,
|
||
|
|
buildUnavailableApiResponse: () => ({ status: 503, headers: {}, bodyText: '' }),
|
||
|
|
sanitizeForwardHeaders: (input: Record<string, string> | undefined) => input ?? {},
|
||
|
|
collectHeaders: (headers: Headers) => {
|
||
|
|
const result: Record<string, string> = {};
|
||
|
|
headers.forEach((value, key) => {
|
||
|
|
result[key] = value;
|
||
|
|
});
|
||
|
|
return result;
|
||
|
|
},
|
||
|
|
base64EncodeUtf8: (text: string) => Buffer.from(text, 'utf8').toString('base64'),
|
||
|
|
};
|
||
|
|
|
||
|
|
const ctx = {
|
||
|
|
manager: {
|
||
|
|
getApiUrl: () => 'http://127.0.0.1:3902',
|
||
|
|
getOpenCodeAuthHeaders: () => ({}),
|
||
|
|
},
|
||
|
|
} as unknown as BridgeContext;
|
||
|
|
|
||
|
|
describe('VS Code API proxy aborts', () => {
|
||
|
|
test('aborts non-SSE api:proxy fetches by bridge request id', async () => {
|
||
|
|
const originalFetch = globalThis.fetch;
|
||
|
|
let capturedSignal: AbortSignal | undefined;
|
||
|
|
|
||
|
|
try {
|
||
|
|
globalThis.fetch = (async (_input: Parameters<typeof fetch>[0], init?: RequestInit) => {
|
||
|
|
capturedSignal = init?.signal ?? undefined;
|
||
|
|
return new Promise<Response>((_resolve, reject) => {
|
||
|
|
capturedSignal?.addEventListener('abort', () => reject(new DOMException('Aborted', 'AbortError')), { once: true });
|
||
|
|
});
|
||
|
|
}) as typeof fetch;
|
||
|
|
|
||
|
|
const pending = handleProxyBridgeMessage(
|
||
|
|
{ id: 'req_1', type: 'api:proxy', payload: { method: 'POST', path: '/session/abc/prompt_async', bodyBase64: Buffer.from('{}').toString('base64') } },
|
||
|
|
ctx,
|
||
|
|
deps,
|
||
|
|
);
|
||
|
|
|
||
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
||
|
|
assert.equal(capturedSignal?.aborted, false);
|
||
|
|
|
||
|
|
await handleProxyBridgeMessage({ id: 'abort_req_1', type: 'api:proxy:abort', payload: { requestID: 'req_1' } }, ctx, deps);
|
||
|
|
assert.equal(capturedSignal?.aborted, true);
|
||
|
|
|
||
|
|
const response = await pending;
|
||
|
|
assert.equal(response?.success, true);
|
||
|
|
assert.equal((response?.data as { status?: number }).status, 502);
|
||
|
|
} finally {
|
||
|
|
globalThis.fetch = originalFetch;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
});
|