Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/configStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function buildConfigStatus(bindings?: AppBindings): ConfigStatus {
},
zyteApiKey: {
configured: isConfigured(bindings?.ZYTE_API_KEY),
usedBy: ['oliveyoung', 'gs25', 'lottemart', 'cgv'],
usedBy: ['oliveyoung', 'gs25', 'cu', 'seveneleven', 'lottemart', 'cgv'],
},
naverLocalSearch: {
configured:
Expand Down
12 changes: 11 additions & 1 deletion src/utils/zyte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ function isRetryableError(error: unknown): boolean {
return isAbortError(error) || error instanceof RetryableZyteError;
}

function parseZyteResponseBody(text: string): ZyteExtractResponse {
try {
return JSON.parse(text) as ZyteExtractResponse;
} catch {
return {
detail: text.trim().replace(/\s+/g, ' ').slice(0, 300),
};
}
}

function wait(ms: number): Promise<void> {
if (ms <= 0) {
return Promise.resolve();
Expand Down Expand Up @@ -139,7 +149,7 @@ export async function requestByZyte(options: ZyteExtractOptions): Promise<ZyteEx
throw error;
}

const result = (await response.json()) as ZyteExtractResponse;
const result = parseZyteResponseBody(await response.text());

if (!response.ok) {
const message = `Zyte API 호출 실패: ${response.status} ${result.detail || result.title || ''}`.trim();
Expand Down
5 changes: 4 additions & 1 deletion tests/app/app-info-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ describe('기본 페이지', () => {
expect(data.status).toBe('ok');
expect(data.config).toEqual({
googleMapsApiKey: { configured: true, usedBy: expect.arrayContaining(['gs25', 'cgv']) },
zyteApiKey: { configured: false, usedBy: expect.arrayContaining(['oliveyoung', 'cgv']) },
zyteApiKey: {
configured: false,
usedBy: expect.arrayContaining(['oliveyoung', 'cgv', 'cu', 'seveneleven']),
},
naverLocalSearch: { configured: false, usedBy: ['places'] },
opinetApiKey: { configured: false, usedBy: ['opinet'] },
supabaseFeedback: { configured: false, usedBy: ['feedback'] },
Expand Down
20 changes: 20 additions & 0 deletions tests/services/cu/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,26 @@ describe('fetchCuStock', () => {
});
});

it('Zyte가 평문 HTTP 520을 반환해도 재고를 unavailable로 반환한다', async () => {
mockFetch
.mockResolvedValueOnce(new Response(JSON.stringify({ areaList: [] })))
.mockResolvedValueOnce(new Response('blocked', { status: 403 }))
.mockResolvedValueOnce(new Response('error code: 520', { status: 520 }));

const result = await fetchCuStock(
{ keyword: '과자', limit: 1, offset: 0, searchSort: 'recom' },
{ apiKey: 'test-key' },
);

expect(result).toEqual({
available: false,
unavailableReason: expect.stringContaining('Website Ban'),
totalCount: 0,
spellModifyYn: 'N',
items: [],
});
});

it('Zyte 키 없이 원본 재고가 차단되어도 unavailable로 반환한다', async () => {
mockFetch
.mockResolvedValueOnce(new Response(JSON.stringify({ areaList: [] })))
Expand Down
10 changes: 6 additions & 4 deletions tests/services/oliveyoung/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ beforeEach(() => {
});

afterEach(() => {
vi.unstubAllGlobals();
vi.restoreAllMocks();
delete process.env.ZYTE_API_KEY;
});
Expand Down Expand Up @@ -600,10 +601,11 @@ describe('fetchOliveyoungProducts', () => {
mockFetch.mockResolvedValue({
ok: true,
status: 200,
json: async () => ({
statusCode: 200,
httpResponseBody: encodedBody,
}),
text: async () =>
JSON.stringify({
statusCode: 200,
httpResponseBody: encodedBody,
}),
});

await expect(
Expand Down
14 changes: 14 additions & 0 deletions tests/utils/zyte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ describe('requestByZyte', () => {
expect(mockFetch).toHaveBeenCalledTimes(2);
});

it('Zyte API가 평문 520 오류를 반환해도 상태와 본문을 보존한다', async () => {
mockFetch.mockResolvedValueOnce(new Response('error code: 520', { status: 520 }));

await expect(
requestByZyte({
apiKey: 'test-key',
url: 'https://example.com/api',
retries: 0,
}),
).rejects.toThrow('Zyte API 호출 실패: 520 error code: 520');

expect(mockFetch).toHaveBeenCalledTimes(1);
});

it('대상 사이트 5xx 응답이면 한 번 재시도한다', async () => {
mockFetch
.mockResolvedValueOnce(new Response(JSON.stringify({ statusCode: 520, httpResponseBody: 'e30=' })))
Expand Down