Files
jackwener__opencli/clis/weibo/utils.test.js
Benjamin Liu dadf01b56f fix(weibo): unwrap page.evaluate envelope in read adapters (#1568)
* fix(weibo): unwrap page.evaluate envelope in read adapters (#1567)

`page.evaluate(...)` returns a `{ session, data }` envelope rather than
the raw IIFE return value, so all weibo cookie-strategy read adapters
silently dropped their results on v1.7.19:

- `getSelfUid` returned the envelope object instead of the uid string,
  so `'10001' + uid` produced `'10001[object Object]'` and every
  feed/me/favorites request hit a broken list_id.
- `feed`, `hot`, `comments`, `search`, `favorites` did `Array.isArray`
  on the envelope (always false) and returned `[]`.
- `me`, `user`, `post` returned the envelope wrapper itself instead of
  the inner profile/post object.

Same pattern as #1561 for xiaohongshu/rednote. Adds an
`unwrapEvaluateResult` helper to `clis/weibo/utils.js` (kept local
rather than cross-importing from `xiaohongshu/search.js` since weibo
is an unrelated site) and wraps every `await page.evaluate(...)` in
the 8 read adapters plus the two helper calls in `getSelfUid`.

Skipped `publish.js` (write command, out of scope for this read fix).

Verified live:
- `opencli weibo hot --limit 3` returns 3 real trending items
- `opencli weibo feed --limit 3` returns 3 timeline posts with
  correct `https://weibo.com/<uid>/<mblogid>` URLs (proves
  `getSelfUid` unwrap works)
- `opencli weibo me` returns the logged-in profile object
- All 20 weibo unit tests pass (6 new for `unwrapEvaluateResult`)

* fix(weibo): fail typed on malformed evaluate payloads

---------

Co-authored-by: jackwener <jakevingoo@gmail.com>
2026-05-14 22:34:43 +08:00

37 lines
2.1 KiB
JavaScript

import { describe, expect, it } from 'vitest';
import { CommandExecutionError } from '@jackwener/opencli/errors';
import { requireArrayEvaluateResult, requireObjectEvaluateResult, unwrapEvaluateResult } from './utils.js';
describe('unwrapEvaluateResult (browser-bridge envelope normalization)', () => {
it('returns the raw array unchanged when payload is already an array', () => {
const arr = [{ id: '1' }, { id: '2' }];
expect(unwrapEvaluateResult(arr)).toBe(arr);
});
it('unwraps { session, data: [...] } envelope to the inner array', () => {
const arr = [{ id: '1' }];
const env = { session: 'site:weibo:abc', data: arr };
expect(unwrapEvaluateResult(env)).toBe(arr);
});
it('unwraps primitive data (e.g. uid string) from Browser Bridge envelopes', () => {
expect(unwrapEvaluateResult({ session: 'site:weibo:abc', data: '1234567890' })).toBe('1234567890');
});
it('unwraps null payload data so getSelfUid fallback can trigger', () => {
expect(unwrapEvaluateResult({ session: 'site:weibo:abc', data: null })).toBe(null);
});
it('passes non-envelope objects through unchanged (e.g. profile result)', () => {
const obj = { screen_name: 'alice', uid: '42' };
expect(unwrapEvaluateResult(obj)).toBe(obj);
});
it('handles null and undefined safely', () => {
expect(unwrapEvaluateResult(null)).toBe(null);
expect(unwrapEvaluateResult(undefined)).toBe(undefined);
});
it('keeps malformed array/object payloads as typed command failures after unwrap', () => {
expect(requireArrayEvaluateResult([{ id: '1' }], 'weibo feed')).toEqual([{ id: '1' }]);
expect(() => requireArrayEvaluateResult({ error: 'API error' }, 'weibo feed')).toThrow(CommandExecutionError);
expect(() => requireArrayEvaluateResult({ error: 'API error' }, 'weibo feed')).toThrow('weibo feed: API error');
expect(requireObjectEvaluateResult({ uid: '42' }, 'weibo me')).toEqual({ uid: '42' });
expect(() => requireObjectEvaluateResult([{ uid: '42' }], 'weibo me')).toThrow(CommandExecutionError);
});
});