mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
314bc6f231
1. Remove root-level shim files (errors.ts, logger.ts, registry.ts, types.ts, utils.ts, launcher.ts) — update all ~840 adapter imports to reference src/ directly 2. Consolidate interceptor: reuse shared DISGUISE_FN in tap interceptor instead of reimplementing 3. Unify error classification: single ClassifiedError type with icon/exitCode/hint lookup table, eliminating duplicated pattern matching between resolveExitCode and renderError 4. Simplify cascade probe: replace repetitive switch cases with PROBE_OPTIONS lookup map 12. Clean up synthesize.ts: remove deprecated snake_case field aliases (recommended_args, recommended_columns, recommendedColumnsLegacy) and unnecessary constant aliases Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
115 lines
4.0 KiB
TypeScript
115 lines
4.0 KiB
TypeScript
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import type { CliCommand } from '../../src/registry.js';
|
|
import { getRegistry } from '../../src/registry.js';
|
|
import { AuthRequiredError, CommandExecutionError } from '../../src/errors.js';
|
|
import { createPageMock } from './test-utils.js';
|
|
|
|
// Mock download dependencies before importing the adapter
|
|
const { mockHttpDownload, mockMkdirSync } = vi.hoisted(() => ({
|
|
mockHttpDownload: vi.fn(),
|
|
mockMkdirSync: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('../../download/index.js', () => ({
|
|
formatCookieHeader: vi.fn().mockReturnValue('cookie=value'),
|
|
httpDownload: mockHttpDownload,
|
|
}));
|
|
|
|
vi.mock('node:fs', () => ({
|
|
mkdirSync: mockMkdirSync,
|
|
}));
|
|
|
|
// Now import the adapter (after mocks are set up)
|
|
await import('./download.js');
|
|
|
|
let cmd: CliCommand;
|
|
|
|
beforeAll(() => {
|
|
cmd = getRegistry().get('pixiv/download')!;
|
|
expect(cmd?.func).toBeTypeOf('function');
|
|
});
|
|
|
|
describe('pixiv download', () => {
|
|
beforeEach(() => {
|
|
mockHttpDownload.mockReset();
|
|
mockMkdirSync.mockReset();
|
|
});
|
|
|
|
it('throws CommandExecutionError on invalid illust ID', async () => {
|
|
const page = createPageMock([]);
|
|
|
|
await expect(cmd.func!(page, { 'illust-id': 'abc', output: '/tmp/test' })).rejects.toThrow(CommandExecutionError);
|
|
});
|
|
|
|
it('throws AuthRequiredError on 403', async () => {
|
|
const page = createPageMock([{ __httpError: 403 }]);
|
|
|
|
await expect(cmd.func!(page, { 'illust-id': '12345', output: '/tmp/test' })).rejects.toThrow(AuthRequiredError);
|
|
});
|
|
|
|
it('throws CommandExecutionError on 404', async () => {
|
|
const page = createPageMock([{ __httpError: 404 }]);
|
|
|
|
await expect(cmd.func!(page, { 'illust-id': '12345', output: '/tmp/test' })).rejects.toThrow(CommandExecutionError);
|
|
});
|
|
|
|
it('throws CommandExecutionError on non-auth HTTP failure', async () => {
|
|
const page = createPageMock([{ __httpError: 500 }]);
|
|
|
|
await expect(cmd.func!(page, { 'illust-id': '12345', output: '/tmp/test' })).rejects.toThrow(CommandExecutionError);
|
|
});
|
|
|
|
it('returns failure when no images found', async () => {
|
|
const page = createPageMock([{ body: [] }]);
|
|
|
|
const result = (await cmd.func!(page, { 'illust-id': '12345', output: '/tmp/test' })) as any[];
|
|
expect(result).toEqual([{ index: 0, type: '-', status: 'failed', size: 'No images found' }]);
|
|
});
|
|
|
|
it('downloads images with Referer header', async () => {
|
|
mockHttpDownload.mockResolvedValue({ success: true, size: 1024000 });
|
|
|
|
const page = createPageMock([
|
|
{
|
|
body: [
|
|
{ urls: { original: 'https://i.pximg.net/img-original/img/2025/01/01/00/00/00/12345_p0.png' } },
|
|
{ urls: { original: 'https://i.pximg.net/img-original/img/2025/01/01/00/00/00/12345_p1.jpg' } },
|
|
],
|
|
},
|
|
]);
|
|
|
|
const result = (await cmd.func!(page, { 'illust-id': '12345', output: '/tmp/test' })) as any[];
|
|
|
|
expect(result).toHaveLength(2);
|
|
expect(result[0]).toMatchObject({ index: 1, type: 'image', status: 'success' });
|
|
expect(result[1]).toMatchObject({ index: 2, type: 'image', status: 'success' });
|
|
|
|
// Verify Referer header was passed
|
|
expect(mockHttpDownload).toHaveBeenCalledTimes(2);
|
|
const firstCallOpts = mockHttpDownload.mock.calls[0][2];
|
|
expect(firstCallOpts.headers).toEqual({ Referer: 'https://www.pixiv.net/' });
|
|
});
|
|
|
|
it('handles individual download failures gracefully', async () => {
|
|
mockHttpDownload
|
|
.mockResolvedValueOnce({ success: true, size: 512000 })
|
|
.mockRejectedValueOnce(new Error('Connection timeout'));
|
|
|
|
const page = createPageMock([
|
|
{
|
|
body: [
|
|
{ urls: { original: 'https://i.pximg.net/img/12345_p0.png' } },
|
|
{ urls: { original: 'https://i.pximg.net/img/12345_p1.png' } },
|
|
],
|
|
},
|
|
]);
|
|
|
|
const result = (await cmd.func!(page, { 'illust-id': '12345', output: '/tmp/test' })) as any[];
|
|
|
|
expect(result).toHaveLength(2);
|
|
expect(result[0].status).toBe('success');
|
|
expect(result[1].status).toBe('failed');
|
|
expect(result[1].size).toBe('Connection timeout');
|
|
});
|
|
});
|