Files
jackwener__opencli/clis/douban/utils.test.ts
jakevin 4fe9a73ebc refactor: migrate adapter imports to package exports (#795)
* 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.
2026-04-05 16:02:17 +08:00

98 lines
3.5 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import type { IPage } from '@jackwener/opencli/types';
import {
getDoubanPhotoExtension,
loadDoubanSubjectPhotos,
normalizeDoubanSubjectId,
promoteDoubanPhotoUrl,
resolveDoubanPhotoAssetUrl,
} from './utils.js';
describe('douban utils', () => {
it('normalizes valid subject ids', () => {
expect(normalizeDoubanSubjectId(' 30382501 ')).toBe('30382501');
});
it('rejects invalid subject ids', () => {
expect(() => normalizeDoubanSubjectId('tt30382501')).toThrow('Invalid Douban subject ID');
});
it('promotes thumbnail urls to large photo urls', () => {
expect(
promoteDoubanPhotoUrl('https://img1.doubanio.com/view/photo/m/public/p2913450214.webp'),
).toBe('https://img1.doubanio.com/view/photo/l/public/p2913450214.webp');
expect(
promoteDoubanPhotoUrl('https://img9.doubanio.com/view/photo/s_ratio_poster/public/p2578474613.jpg'),
).toBe('https://img9.doubanio.com/view/photo/l/public/p2578474613.jpg');
});
it('rejects non-http photo urls during promotion', () => {
expect(promoteDoubanPhotoUrl('data:image/gif;base64,abc')).toBe('');
});
it('prefers lazy-loaded photo urls over data placeholders', () => {
expect(
resolveDoubanPhotoAssetUrl([
'',
'https://img1.doubanio.com/view/photo/m/public/p2913450214.webp',
'data:image/gif;base64,abc',
], 'https://movie.douban.com/subject/30382501/photos?type=Rb'),
).toBe('https://img1.doubanio.com/view/photo/m/public/p2913450214.webp');
});
it('drops unsupported non-http photo urls when no real image url exists', () => {
expect(
resolveDoubanPhotoAssetUrl(
['data:image/gif;base64,abc', 'blob:https://movie.douban.com/example'],
'https://movie.douban.com/subject/30382501/photos?type=Rb',
),
).toBe('');
});
it('removes the default photo cap when scanning for an exact photo id', async () => {
const evaluate = vi.fn()
.mockResolvedValueOnce({ blocked: false, title: 'Some Movie', href: 'https://movie.douban.com/subject/30382501/photos?type=Rb' })
.mockResolvedValueOnce({
subjectId: '30382501',
subjectTitle: 'The Wandering Earth 2',
type: 'Rb',
photos: [
{
index: 731,
photoId: '2913450215',
title: 'Character poster',
imageUrl: 'https://img1.doubanio.com/view/photo/l/public/p2913450215.jpg',
thumbUrl: 'https://img1.doubanio.com/view/photo/m/public/p2913450215.jpg',
detailUrl: 'https://movie.douban.com/photos/photo/2913450215/',
page: 25,
},
],
});
const page = {
goto: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue(undefined),
evaluate,
} as unknown as IPage;
await loadDoubanSubjectPhotos(page, '30382501', {
type: 'Rb',
targetPhotoId: '2913450215',
});
const scanScript = evaluate.mock.calls[1]?.[0];
expect(scanScript).toContain('const targetPhotoId = "2913450215";');
expect(scanScript).toContain(`const limit = ${Number.MAX_SAFE_INTEGER};`);
expect(scanScript).toContain('for (let pageIndex = 0; photos.length < limit; pageIndex += 1)');
});
it('keeps image extensions when download urls contain query params', () => {
expect(
getDoubanPhotoExtension('https://img1.doubanio.com/view/photo/l/public/p2913450214.webp?foo=1'),
).toBe('.webp');
expect(
getDoubanPhotoExtension('https://img1.doubanio.com/view/photo/l/public/p2913450214.jpeg'),
).toBe('.jpeg');
});
});