Cut product flows over to Coding/Pi and retain only the migration-owned v1 boundary. Promote supported native optional packages because electron-builder omitted pnpm transitive optional closure from the packaged ASAR.
129 lines
4.2 KiB
TypeScript
129 lines
4.2 KiB
TypeScript
import { EventEmitter } from 'node:events';
|
|
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { handleProviderRoutes } from '@electron/api/routes/providers';
|
|
import type { ProviderAccount } from '@electron/shared/providers/types';
|
|
|
|
const providerServiceMock = vi.hoisted(() => ({
|
|
getAccount: vi.fn(),
|
|
getDefaultAccountId: vi.fn(),
|
|
updateAccount: vi.fn(),
|
|
setDefaultAccount: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('@electron/services/providers/provider-service', () => ({
|
|
getProviderService: () => providerServiceMock,
|
|
}));
|
|
|
|
function createRequest(method: string, body?: unknown): IncomingMessage {
|
|
const request = new EventEmitter();
|
|
Object.assign(request, {
|
|
method,
|
|
headers: body === undefined ? {} : { 'content-type': 'application/json' },
|
|
[Symbol.asyncIterator]: async function* () {
|
|
if (body !== undefined) yield Buffer.from(JSON.stringify(body));
|
|
},
|
|
});
|
|
return request as IncomingMessage;
|
|
}
|
|
|
|
function createResponse() {
|
|
const chunks: string[] = [];
|
|
const response = {
|
|
statusCode: 0,
|
|
setHeader: vi.fn(),
|
|
end: vi.fn((chunk?: string) => {
|
|
if (chunk) chunks.push(chunk);
|
|
}),
|
|
} as unknown as ServerResponse;
|
|
return {
|
|
response,
|
|
get statusCode() { return response.statusCode; },
|
|
json: () => JSON.parse(chunks.join('')) as unknown,
|
|
};
|
|
}
|
|
|
|
function account(overrides: Partial<ProviderAccount> = {}): ProviderAccount {
|
|
return {
|
|
id: 'account-1',
|
|
vendorId: 'openai',
|
|
label: 'OpenAI',
|
|
authMode: 'api_key',
|
|
enabled: true,
|
|
isDefault: true,
|
|
createdAt: '2026-08-24T00:00:00.000Z',
|
|
updatedAt: '2026-08-24T00:00:00.000Z',
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe('provider host api routes', () => {
|
|
const markProviderStale = vi.fn();
|
|
const context = {
|
|
codingProducts: { conversations: { markProviderStale } },
|
|
} as never;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
providerServiceMock.getAccount.mockResolvedValue(account());
|
|
providerServiceMock.updateAccount.mockResolvedValue(account({
|
|
updatedAt: '2026-08-24T01:00:00.000Z',
|
|
}));
|
|
providerServiceMock.getDefaultAccountId.mockResolvedValue('account-1');
|
|
providerServiceMock.setDefaultAccount.mockResolvedValue(undefined);
|
|
});
|
|
|
|
it('marks Pi provider input stale after an account credential update', async () => {
|
|
const result = createResponse();
|
|
const handled = await handleProviderRoutes(
|
|
createRequest('PUT', { updates: {}, apiKey: 'sk-new' }),
|
|
result.response,
|
|
new URL('http://127.0.0.1/api/provider-accounts/account-1'),
|
|
context,
|
|
);
|
|
|
|
expect(handled).toBe(true);
|
|
expect(result.statusCode).toBe(200);
|
|
expect(providerServiceMock.updateAccount).toHaveBeenCalledWith('account-1', {}, 'sk-new');
|
|
expect(markProviderStale).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('does not invalidate provider input for an unchanged account', async () => {
|
|
const result = createResponse();
|
|
await handleProviderRoutes(
|
|
createRequest('PUT', { updates: {} }),
|
|
result.response,
|
|
new URL('http://127.0.0.1/api/provider-accounts/account-1'),
|
|
context,
|
|
);
|
|
|
|
expect(result.statusCode).toBe(200);
|
|
expect(result.json()).toMatchObject({ success: true, noChange: true });
|
|
expect(providerServiceMock.updateAccount).not.toHaveBeenCalled();
|
|
expect(markProviderStale).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('invalidates provider input only when the default account changes', async () => {
|
|
const unchanged = createResponse();
|
|
await handleProviderRoutes(
|
|
createRequest('PUT', { accountId: 'account-1' }),
|
|
unchanged.response,
|
|
new URL('http://127.0.0.1/api/provider-accounts/default'),
|
|
context,
|
|
);
|
|
expect(markProviderStale).not.toHaveBeenCalled();
|
|
|
|
providerServiceMock.getDefaultAccountId.mockResolvedValue('account-2');
|
|
const changed = createResponse();
|
|
await handleProviderRoutes(
|
|
createRequest('PUT', { accountId: 'account-1' }),
|
|
changed.response,
|
|
new URL('http://127.0.0.1/api/provider-accounts/default'),
|
|
context,
|
|
);
|
|
|
|
expect(providerServiceMock.setDefaultAccount).toHaveBeenCalledWith('account-1');
|
|
expect(markProviderStale).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|