mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
4fe9a73ebc
* refactor: migrate adapter imports to package exports Replace all relative imports (../../src/registry.js, ../../browser/cdp.js, etc.) with package exports (@jackwener/opencli/registry, @jackwener/opencli/errors, etc.) across all 484 adapter files. This decouples adapter import resolution from directory structure: - User CLIs in ~/.opencli/clis/ resolve via node_modules symlink - Internal adapters resolve via Node.js self-referencing - No more shim files needed for import resolution Changes: - package.json: add sub-path exports for all public modules - clis/**: replace relative imports with @jackwener/opencli/... - discovery.ts: simplify ensureUserCliCompatShims to symlink-only - registry-api.ts: export CommandArgs type - Remove root-level shim directories (browser/, download/, pipeline/) - Remove shim entries from tsconfig.json include and package.json files * test: add regression tests for package exports Prevents regressions like #788/#791 by: 1. Scanning all adapter files for forbidden relative imports (../../src/, ../../browser/, etc.) — fails if any remain 2. Verifying every package.json export maps to an existing source file 18 new test cases. * fix: use junction on Windows + broaden test patterns - discovery.ts: use 'junction' symlink type on Windows (no admin required) - package-exports.test.ts: generalize forbidden patterns to catch any depth of ../ traversal (not just ../../ and ../../../) * fix: update stale vi.mock/importActual paths in adapter tests Test files still used old relative paths for vi.mock() and vi.importActual() calls. Updated 5 test files to use package exports. Also broadened regression test patterns to catch mock/importActual paths. * fix: use rm instead of unlink for symlink cleanup, add warn on failure Addresses review feedback from Astro-Han: - rm() handles both symlinks and stale directories (unlink fails on dirs) - Log a warning when symlink creation fails instead of silent catch * docs: update import examples to use package exports Update all documentation, contributing guides, and skills to use @jackwener/opencli/registry instead of ../../src/registry.js. Without this, users following the docs would write adapters with broken imports since the old shim files are no longer created.
116 lines
4.1 KiB
TypeScript
116 lines
4.1 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import type { IPage } from '@jackwener/opencli/types';
|
|
import { TimeoutError } from '@jackwener/opencli/errors';
|
|
import { pollTranscodeWithFetch } from './transcode.js';
|
|
import type { TranscodeResult } from './types.js';
|
|
|
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
|
|
|
function makePage(): IPage {
|
|
return {
|
|
goto: vi.fn(),
|
|
evaluate: vi.fn(),
|
|
getCookies: vi.fn(),
|
|
snapshot: vi.fn(),
|
|
click: vi.fn(),
|
|
typeText: vi.fn(),
|
|
pressKey: vi.fn(),
|
|
scrollTo: vi.fn(),
|
|
getFormState: vi.fn(),
|
|
wait: vi.fn(),
|
|
tabs: vi.fn(),
|
|
selectTab: vi.fn(),
|
|
networkRequests: vi.fn(),
|
|
consoleMessages: vi.fn(),
|
|
scroll: vi.fn(),
|
|
autoScroll: vi.fn(),
|
|
installInterceptor: vi.fn(),
|
|
getInterceptedRequests: vi.fn(),
|
|
screenshot: vi.fn(),
|
|
} as unknown as IPage;
|
|
}
|
|
|
|
const COMPLETE_RESULT: TranscodeResult & { encode: number } = {
|
|
encode: 2,
|
|
duration: 30,
|
|
fps: 30,
|
|
height: 1920,
|
|
width: 1080,
|
|
poster_uri: 'tos-cn-i-alisg.volces.com/poster/abc',
|
|
poster_url: 'https://p3-creator.douyinpic.com/poster/abc',
|
|
};
|
|
|
|
// ── Tests ─────────────────────────────────────────────────────────────────────
|
|
|
|
describe('pollTranscodeWithFetch', () => {
|
|
it('returns TranscodeResult immediately when first response has encode=2', async () => {
|
|
const fetchFn = vi.fn().mockResolvedValue(COMPLETE_RESULT);
|
|
const page = makePage();
|
|
|
|
const result = await pollTranscodeWithFetch(fetchFn, page, 'vid_123');
|
|
|
|
expect(result).toEqual(COMPLETE_RESULT);
|
|
expect(fetchFn).toHaveBeenCalledOnce();
|
|
const [, method, url] = fetchFn.mock.calls[0] as [IPage, string, string];
|
|
expect(method).toBe('GET');
|
|
expect(url).toContain('video_id=vid_123');
|
|
expect(url).toContain('aid=1128');
|
|
});
|
|
|
|
it('polls multiple times until encode=2 is received', async () => {
|
|
const pending = { encode: 1, duration: 0, fps: 0, height: 0, width: 0, poster_uri: '', poster_url: '' };
|
|
const fetchFn = vi
|
|
.fn()
|
|
.mockResolvedValueOnce(pending)
|
|
.mockResolvedValueOnce(pending)
|
|
.mockResolvedValueOnce(COMPLETE_RESULT);
|
|
const page = makePage();
|
|
|
|
// Use a large timeoutMs so it doesn't expire, but override POLL_INTERVAL via
|
|
// a short timeout knowing we'll get 3 calls quickly with mocked promises.
|
|
// Since pollTranscodeWithFetch uses setTimeout for 3s between polls, we need
|
|
// to mock timers to keep tests fast.
|
|
vi.useFakeTimers();
|
|
|
|
const resultPromise = pollTranscodeWithFetch(fetchFn, page, 'vid_456', 60_000);
|
|
|
|
// Advance timers for each pending poll cycle
|
|
await vi.runAllTimersAsync();
|
|
|
|
const result = await resultPromise;
|
|
expect(result).toEqual(COMPLETE_RESULT);
|
|
expect(fetchFn).toHaveBeenCalledTimes(3);
|
|
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('throws TimeoutError when encode never becomes 2 within timeoutMs', async () => {
|
|
const pending = { encode: 1, duration: 0, fps: 0, height: 0, width: 0, poster_uri: '', poster_url: '' };
|
|
const fetchFn = vi.fn().mockResolvedValue(pending);
|
|
const page = makePage();
|
|
|
|
vi.useFakeTimers();
|
|
|
|
const resultPromise = pollTranscodeWithFetch(fetchFn, page, 'vid_789', 5_000);
|
|
// Suppress unhandled-rejection so vitest doesn't flag it
|
|
resultPromise.catch(() => undefined);
|
|
|
|
// Advance time past the 5s timeout
|
|
await vi.runAllTimersAsync();
|
|
|
|
await expect(resultPromise).rejects.toBeInstanceOf(TimeoutError);
|
|
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('URL encodes video_id in the request URL', async () => {
|
|
const fetchFn = vi.fn().mockResolvedValue(COMPLETE_RESULT);
|
|
const page = makePage();
|
|
|
|
await pollTranscodeWithFetch(fetchFn, page, 'vid with spaces');
|
|
|
|
const [, , url] = fetchFn.mock.calls[0] as [IPage, string, string];
|
|
expect(url).toContain('video_id=vid%20with%20spaces');
|
|
});
|
|
});
|