Files
jackwener__opencli/clis/linkedin/timeline.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

100 lines
2.7 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { getRegistry } from '@jackwener/opencli/registry';
import './timeline.js';
const { parseMetric, buildPostId, mergeTimelinePosts } = await import('./timeline.js').then(
(m) => (m as any).__test__,
);
describe('linkedin timeline adapter', () => {
const command = getRegistry().get('linkedin/timeline');
it('registers the command with correct shape', () => {
expect(command).toBeDefined();
expect(command!.site).toBe('linkedin');
expect(command!.name).toBe('timeline');
expect(command!.domain).toBe('www.linkedin.com');
expect(command!.strategy).toBe('cookie');
expect(command!.browser).toBe(true);
expect(typeof command!.func).toBe('function');
});
it('has limit arg with default 20', () => {
const limitArg = command!.args.find((a) => a.name === 'limit');
expect(limitArg).toBeDefined();
expect(limitArg!.default).toBe(20);
});
it('includes expected columns', () => {
expect(command!.columns).toEqual(
expect.arrayContaining(['author', 'text', 'reactions', 'comments', 'url']),
);
});
});
describe('parseMetric', () => {
it('parses plain numbers', () => {
expect(parseMetric('42')).toBe(42);
expect(parseMetric('1,234')).toBe(1234);
});
it('handles k/m suffixes', () => {
expect(parseMetric('2.5k')).toBe(2500);
expect(parseMetric('1.2M')).toBe(1200000);
});
it('returns 0 for empty/undefined', () => {
expect(parseMetric('')).toBe(0);
expect(parseMetric(undefined)).toBe(0);
expect(parseMetric(null)).toBe(0);
});
});
describe('buildPostId', () => {
it('uses url when present', () => {
expect(buildPostId({ url: 'https://linkedin.com/post/123' })).toBe(
'https://linkedin.com/post/123',
);
});
it('falls back to composite key', () => {
const id = buildPostId({ author: 'Alice', posted_at: '2h', text: 'Hello world' });
expect(id).toBe('Alice::2h::Hello world');
});
});
describe('mergeTimelinePosts', () => {
it('deduplicates by url', () => {
const url = 'https://linkedin.com/post/1';
const a = {
id: url,
author: 'Alice',
author_url: '',
headline: '',
text: 'Hello',
posted_at: '1h',
reactions: 5,
comments: 1,
url,
};
const result = mergeTimelinePosts([a], [a]);
expect(result).toHaveLength(1);
});
it('skips posts without author or text', () => {
const empty = {
id: '2',
author: '',
author_url: '',
headline: '',
text: 'some text',
posted_at: '',
reactions: 0,
comments: 0,
url: '',
};
const result = mergeTimelinePosts([], [empty]);
expect(result).toHaveLength(0);
});
});