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.
104 lines
3.0 KiB
TypeScript
104 lines
3.0 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const { mockApiGet } = vi.hoisted(() => ({
|
|
mockApiGet: vi.fn(),
|
|
}));
|
|
|
|
vi.mock('./utils.js', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('./utils.js')>()),
|
|
apiGet: mockApiGet,
|
|
}));
|
|
|
|
import { getRegistry } from '@jackwener/opencli/registry';
|
|
import './comments.js';
|
|
|
|
describe('bilibili comments', () => {
|
|
const command = getRegistry().get('bilibili/comments');
|
|
|
|
beforeEach(() => {
|
|
mockApiGet.mockReset();
|
|
});
|
|
|
|
it('resolves bvid to aid and fetches replies', async () => {
|
|
mockApiGet
|
|
.mockResolvedValueOnce({ data: { aid: 12345 } }) // view endpoint
|
|
.mockResolvedValueOnce({
|
|
data: {
|
|
replies: [
|
|
{
|
|
member: { uname: 'Alice' },
|
|
content: { message: 'Great video!' },
|
|
like: 42,
|
|
rcount: 3,
|
|
ctime: 1700000000,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
const result = await command!.func!({} as any, { bvid: 'BV1WtAGzYEBm', limit: 5 });
|
|
|
|
expect(mockApiGet).toHaveBeenNthCalledWith(1, {}, '/x/web-interface/view', { params: { bvid: 'BV1WtAGzYEBm' } });
|
|
expect(mockApiGet).toHaveBeenNthCalledWith(2, {}, '/x/v2/reply/main', {
|
|
params: { oid: 12345, type: 1, mode: 3, ps: 5 },
|
|
signed: true,
|
|
});
|
|
|
|
expect(result).toEqual([
|
|
{
|
|
rank: 1,
|
|
author: 'Alice',
|
|
text: 'Great video!',
|
|
likes: 42,
|
|
replies: 3,
|
|
time: new Date(1700000000 * 1000).toISOString().slice(0, 16).replace('T', ' '),
|
|
},
|
|
]);
|
|
});
|
|
|
|
it('throws when aid cannot be resolved', async () => {
|
|
mockApiGet.mockResolvedValueOnce({ data: {} }); // no aid
|
|
|
|
await expect(command!.func!({} as any, { bvid: 'BVinvalid123', limit: 5 })).rejects.toThrow(
|
|
'Cannot resolve aid for bvid: BVinvalid123',
|
|
);
|
|
});
|
|
|
|
it('returns empty array when replies is missing', async () => {
|
|
mockApiGet
|
|
.mockResolvedValueOnce({ data: { aid: 99 } })
|
|
.mockResolvedValueOnce({ data: {} }); // no replies key
|
|
|
|
const result = await command!.func!({} as any, { bvid: 'BV1xxx', limit: 5 });
|
|
expect(result).toEqual([]);
|
|
});
|
|
|
|
it('caps limit at 50', async () => {
|
|
mockApiGet
|
|
.mockResolvedValueOnce({ data: { aid: 1 } })
|
|
.mockResolvedValueOnce({ data: { replies: [] } });
|
|
|
|
await command!.func!({} as any, { bvid: 'BV1xxx', limit: 999 });
|
|
|
|
expect(mockApiGet).toHaveBeenNthCalledWith(2, {}, '/x/v2/reply/main', {
|
|
params: { oid: 1, type: 1, mode: 3, ps: 50 },
|
|
signed: true,
|
|
});
|
|
});
|
|
|
|
it('collapses newlines in comment text', async () => {
|
|
mockApiGet
|
|
.mockResolvedValueOnce({ data: { aid: 1 } })
|
|
.mockResolvedValueOnce({
|
|
data: {
|
|
replies: [
|
|
{ member: { uname: 'Bob' }, content: { message: 'line1\nline2\nline3' }, like: 0, rcount: 0, ctime: 0 },
|
|
],
|
|
},
|
|
});
|
|
|
|
const result = (await command!.func!({} as any, { bvid: 'BV1xxx', limit: 5 })) as any[];
|
|
expect(result[0].text).toBe('line1 line2 line3');
|
|
});
|
|
});
|