67 lines
2.2 KiB
TypeScript
67 lines
2.2 KiB
TypeScript
import { beforeEach, describe, expect, it, mock } from 'bun:test';
|
|
import {
|
|
__yahooCompanyProfileInternals,
|
|
getYahooCompanyDescription
|
|
} from './yahoo-company-profile';
|
|
|
|
describe('yahoo company profile', () => {
|
|
beforeEach(() => {
|
|
__yahooCompanyProfileInternals.resetCaches();
|
|
});
|
|
|
|
it('extracts a Yahoo session cookie from response headers', () => {
|
|
const headers = new Headers();
|
|
headers.append('set-cookie', 'A3=session-token; Domain=.yahoo.com; Path=/; Secure; HttpOnly');
|
|
|
|
expect(__yahooCompanyProfileInternals.pickYahooSessionCookie(headers)).toBe('A3=session-token');
|
|
});
|
|
|
|
it('normalizes and clips long descriptions', () => {
|
|
const value = ` Microsoft builds software.\n\n\n${'x'.repeat(1800)} `;
|
|
const normalized = __yahooCompanyProfileInternals.normalizeDescription(value);
|
|
|
|
expect(normalized).toContain('Microsoft builds software.');
|
|
expect((normalized?.length ?? 0) <= 1600).toBe(true);
|
|
});
|
|
|
|
it('fetches longBusinessSummary through Yahoo cookie and crumb endpoints', async () => {
|
|
const fetchMock = mock(async (input: string | URL | Request) => {
|
|
const url = String(input);
|
|
|
|
if (url === 'https://fc.yahoo.com') {
|
|
return new Response('', {
|
|
status: 404,
|
|
headers: {
|
|
'set-cookie': 'A3=session-token; Domain=.yahoo.com; Path=/; Secure; HttpOnly'
|
|
}
|
|
});
|
|
}
|
|
|
|
if (url === 'https://query1.finance.yahoo.com/v1/test/getcrumb') {
|
|
return new Response('crumb-token', { status: 200 });
|
|
}
|
|
|
|
if (url === 'https://query1.finance.yahoo.com/v10/finance/quoteSummary/MSFT?modules=assetProfile&crumb=crumb-token') {
|
|
return Response.json({
|
|
quoteSummary: {
|
|
result: [
|
|
{
|
|
assetProfile: {
|
|
longBusinessSummary: 'Microsoft builds cloud and software products worldwide.'
|
|
}
|
|
}
|
|
]
|
|
}
|
|
});
|
|
}
|
|
|
|
throw new Error(`Unexpected URL: ${url}`);
|
|
}) as unknown as typeof fetch;
|
|
|
|
const description = await getYahooCompanyDescription('msft', { fetchImpl: fetchMock });
|
|
|
|
expect(description).toBe('Microsoft builds cloud and software products worldwide.');
|
|
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
});
|
|
});
|