web-actions.ts was a 330-line monolith with 14 exported functions
covering 4 distinct themes: cookie/credential primitives (the 8
functions every web call builds on), group honor leaderboards, group
essence (highlighted messages), and group notices. Same shape problem
the OIDB bridge-actions.ts split fixed three commits back, just smaller.
New layout under packages/core/src/bridge/web-actions/ :
cookies.ts (161 lines, 8 fns)
forceFetchClientKey / getPSkey / getCookies / getSKey /
getBknFromSKey / getCookiesStr / getCsrfToken / getCredentials
— the ptlogin2-jump + bkn-derivation primitives.
group-honor.ts (45 lines, 1 fn)
getGroupHonorInfo (talkative / performer / legend / emotion / ALL)
group-essence.ts (27 lines, 2 fns)
getGroupEssence (paged) + getGroupEssenceAll
group-notice.ts (118 lines, 3 fns)
sendGroupNotice (with optional image-from-URL-or-file upload),
getGroupNotice (OneBot shape flatten), deleteGroupNoticeByFid
index.ts (27 lines)
Barrel re-export. `bridge.ts` keeps its existing
`from './web-actions'` import — TS resolves the directory's
index.ts so no caller changes.
The lower-level web/* helpers (request-util, group-essence,
group-honor, group-notice) stay where they were — they're the
direct HTTP/cookie wrappers, distinct from these Bridge-level
orchestrators.
Behavior preserved end-to-end. Same ptlogin2 URL construction,
same fall-back-to-getPSkey-when-p_skey-missing path in getCookies,
same default keyIndex='19' / expireTime='1800', same djb2 bkn hash,
same NapCat-derived default 540x300 notice image dimensions, same
chinese error messages ('设置群公告失败', '获取公告失败').
Adds 35 happy-path tests across 4 new files in tests/web-actions/ :
cookies (13) forceFetchClientKey response shape + defaults,
getPSkey pskeyItems folding, getCookies ptlogin2
URL params, p_skey fallback path (both success
and throwing), getSKey success / no-clientKey /
no-skey throws, getBknFromSKey determinism +
range, getCookiesStr formatting, getCsrfToken
composition, getCredentials full + empty paths.
group-honor (7) TALKATIVE list vs empty list behaviour,
PERFORMER / LEGEND / EMOTION per-type dispatch,
ALL fetches all four types, groupId stringification.
group-essence (4) pageStart/pageLimit forwarding, defaults,
null-result fallback envelope, getAll delegation.
group-notice (11) text-only dispatch + defaults, options override,
http image fetch + upload + thread, fetch failure,
ec≠0 surfaces em, generic error fallback,
getGroupNotice feed flattening + pics mapping,
null-result throw, empty-feeds case,
deleteGroupNoticeByFid argument forwarding.
Tests use vi.mock on bridge-oidb / web/request-util / web/group-*
to isolate each theme from real HTTP, mirroring the pattern from
the bridge-actions split.
369 tests pass (was 334); 3 stability runs, same pre-existing
macOS-arm64 websocket addon failure, unrelated.
87 lines
3.8 KiB
TypeScript
87 lines
3.8 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
vi.mock('../../src/bridge/web-actions/cookies', () => ({
|
|
getCookies: vi.fn(async () => ({ p_skey: 'psk' })),
|
|
}));
|
|
|
|
vi.mock('../../src/bridge/web/group-honor', async () => {
|
|
// Keep the real WebHonorType enum so the function-under-test still
|
|
// sees the same constants as production code; only the network call
|
|
// (`getHonorListWebAPI`) is faked.
|
|
const actual = await vi.importActual<typeof import('../../src/bridge/web/group-honor')>('../../src/bridge/web/group-honor');
|
|
return {
|
|
...actual,
|
|
getHonorListWebAPI: vi.fn(async () => [] as Array<unknown>),
|
|
};
|
|
});
|
|
|
|
import * as cookies from '../../src/bridge/web-actions/cookies';
|
|
import * as honorWeb from '../../src/bridge/web/group-honor';
|
|
import { WebHonorType } from '../../src/bridge/web/group-honor';
|
|
import { getGroupHonorInfo } from '../../src/bridge/web-actions/group-honor';
|
|
|
|
const bridge = {} as any;
|
|
|
|
describe('group-honor — getGroupHonorInfo', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(cookies.getCookies).mockClear();
|
|
vi.mocked(honorWeb.getHonorListWebAPI).mockReset();
|
|
});
|
|
|
|
it('TALKATIVE: calls API only for type=1, populates current_talkative + talkative_list', async () => {
|
|
vi.mocked(honorWeb.getHonorListWebAPI).mockResolvedValueOnce([
|
|
{ uin: 10001, nick: 'alice', avatar: '', desc: '' },
|
|
{ uin: 10002, nick: 'bob', avatar: '', desc: '' },
|
|
]);
|
|
|
|
const out = await getGroupHonorInfo(bridge, 12345, WebHonorType.TALKATIVE);
|
|
expect(honorWeb.getHonorListWebAPI).toHaveBeenCalledTimes(1);
|
|
expect(vi.mocked(honorWeb.getHonorListWebAPI).mock.calls[0]![2]).toBe(1);
|
|
expect(out.current_talkative).toEqual({ uin: 10001, nick: 'alice', avatar: '', desc: '' });
|
|
expect(out.talkative_list).toHaveLength(2);
|
|
expect(out.performer_list).toEqual([]);
|
|
expect(out.legend_list).toEqual([]);
|
|
expect(out.emotion_list).toEqual([]);
|
|
});
|
|
|
|
it('TALKATIVE with empty list: current_talkative stays null', async () => {
|
|
vi.mocked(honorWeb.getHonorListWebAPI).mockResolvedValueOnce([]);
|
|
const out = await getGroupHonorInfo(bridge, 12345, WebHonorType.TALKATIVE);
|
|
expect(out.current_talkative).toBeNull();
|
|
expect(out.talkative_list).toEqual([]);
|
|
});
|
|
|
|
it('PERFORMER: dispatches API with type=2 only', async () => {
|
|
vi.mocked(honorWeb.getHonorListWebAPI).mockResolvedValueOnce([{ uin: 1, nick: 'p', avatar: '', desc: '' }]);
|
|
const out = await getGroupHonorInfo(bridge, 12345, WebHonorType.PERFORMER);
|
|
expect(vi.mocked(honorWeb.getHonorListWebAPI).mock.calls[0]![2]).toBe(2);
|
|
expect(out.performer_list).toHaveLength(1);
|
|
expect(out.talkative_list).toEqual([]);
|
|
});
|
|
|
|
it('LEGEND: type=3', async () => {
|
|
vi.mocked(honorWeb.getHonorListWebAPI).mockResolvedValueOnce([]);
|
|
await getGroupHonorInfo(bridge, 12345, WebHonorType.LEGEND);
|
|
expect(vi.mocked(honorWeb.getHonorListWebAPI).mock.calls[0]![2]).toBe(3);
|
|
});
|
|
|
|
it('EMOTION: type=6', async () => {
|
|
vi.mocked(honorWeb.getHonorListWebAPI).mockResolvedValueOnce([]);
|
|
await getGroupHonorInfo(bridge, 12345, WebHonorType.EMOTION);
|
|
expect(vi.mocked(honorWeb.getHonorListWebAPI).mock.calls[0]![2]).toBe(6);
|
|
});
|
|
|
|
it('ALL: fetches talkative + performer + legend + emotion (4 calls)', async () => {
|
|
vi.mocked(honorWeb.getHonorListWebAPI).mockResolvedValue([]);
|
|
await getGroupHonorInfo(bridge, 12345, WebHonorType.ALL);
|
|
expect(honorWeb.getHonorListWebAPI).toHaveBeenCalledTimes(4);
|
|
expect(vi.mocked(honorWeb.getHonorListWebAPI).mock.calls.map(c => c[2])).toEqual([1, 2, 3, 6]);
|
|
});
|
|
|
|
it('groupId is stringified to groupCode', async () => {
|
|
vi.mocked(honorWeb.getHonorListWebAPI).mockResolvedValueOnce([]);
|
|
await getGroupHonorInfo(bridge, 99999, WebHonorType.TALKATIVE);
|
|
expect(vi.mocked(honorWeb.getHonorListWebAPI).mock.calls[0]![1]).toBe('99999');
|
|
});
|
|
});
|