refactor(core): split web-actions.ts into themed modules + barrel
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.
This commit is contained in:
10 files changed
+873
-331
No files matched your search
@@ -0,0 +1,186 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
// Mock the OIDB shim so we can control what `forceFetchClientKey` and
|
||||
// `getPSkey` see without standing up a Bridge.
|
||||
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
||||
sendOidbAndDecode: vi.fn(async () => ({})),
|
||||
sendOidbAndCheck: vi.fn(async () => undefined),
|
||||
resolveUserUid: vi.fn(async () => 'uid'),
|
||||
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
||||
}));
|
||||
|
||||
// Mock the actual HTTP cookie fetch.
|
||||
vi.mock('../../src/bridge/web/request-util', () => ({
|
||||
RequestUtil: {
|
||||
HttpsGetCookies: vi.fn(async () => ({}) as Record<string, string>),
|
||||
},
|
||||
}));
|
||||
|
||||
import * as oidb from '../../src/bridge/bridge-oidb';
|
||||
import { RequestUtil } from '../../src/bridge/web/request-util';
|
||||
import {
|
||||
forceFetchClientKey,
|
||||
getPSkey,
|
||||
getCookies,
|
||||
getSKey,
|
||||
getBknFromSKey,
|
||||
getCookiesStr,
|
||||
getCsrfToken,
|
||||
getCredentials,
|
||||
} from '../../src/bridge/web-actions/cookies';
|
||||
|
||||
const bridge = { qqInfo: { uin: '10001' } } as any;
|
||||
|
||||
describe('cookies — forceFetchClientKey', () => {
|
||||
beforeEach(() => { vi.mocked(oidb.sendOidbAndDecode).mockReset(); });
|
||||
|
||||
it('returns clientKey + keyIndex + expireTime from the OIDB response', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
||||
clientKey: 'ck-xyz', keyIndex: 42, expireTime: 3600,
|
||||
});
|
||||
const out = await forceFetchClientKey(bridge);
|
||||
expect(out).toEqual({ clientKey: 'ck-xyz', keyIndex: '42', expireTime: '3600' });
|
||||
});
|
||||
|
||||
it('falls back to default keyIndex/expireTime when the response omits them', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({ clientKey: 'ck' });
|
||||
const out = await forceFetchClientKey(bridge);
|
||||
expect(out).toEqual({ clientKey: 'ck', keyIndex: '19', expireTime: '1800' });
|
||||
});
|
||||
});
|
||||
|
||||
describe('cookies — getPSkey', () => {
|
||||
beforeEach(() => { vi.mocked(oidb.sendOidbAndDecode).mockReset(); });
|
||||
|
||||
it('folds pskeyItems into a domain → pskey Map', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
||||
pskeyItems: [
|
||||
{ domain: 'qun.qq.com', pskey: 'psk-a' },
|
||||
{ domain: 'qzone.qq.com', pskey: 'psk-b' },
|
||||
{ domain: 'corrupt', pskey: '' },
|
||||
],
|
||||
});
|
||||
const { domainPskeyMap } = await getPSkey(bridge, ['qun.qq.com', 'qzone.qq.com']);
|
||||
expect(domainPskeyMap.get('qun.qq.com')).toBe('psk-a');
|
||||
expect(domainPskeyMap.get('qzone.qq.com')).toBe('psk-b');
|
||||
expect(domainPskeyMap.has('corrupt')).toBe(false); // empty pskey skipped
|
||||
});
|
||||
});
|
||||
|
||||
describe('cookies — getCookies', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockReset();
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockReset();
|
||||
});
|
||||
|
||||
it('builds the ptlogin2 jump URL with the right query params', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({
|
||||
p_skey: 'psk',
|
||||
skey: 'sk',
|
||||
});
|
||||
|
||||
const out = await getCookies(bridge, 'qun.qq.com');
|
||||
expect(out).toEqual({ p_skey: 'psk', skey: 'sk' });
|
||||
|
||||
const url = vi.mocked(RequestUtil.HttpsGetCookies).mock.calls[0]![0];
|
||||
expect(url).toContain('ssl.ptlogin2.qq.com/jump');
|
||||
expect(url).toContain('clientuin=10001');
|
||||
expect(url).toContain('clientkey=ck');
|
||||
expect(url).toContain('keyindex=19');
|
||||
});
|
||||
|
||||
it('falls back to getPSkey when p_skey is missing in the cookie response', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode)
|
||||
.mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 }) // forceFetchClientKey
|
||||
.mockResolvedValueOnce({ // getPSkey
|
||||
pskeyItems: [{ domain: 'qun.qq.com', pskey: 'psk-from-oidb' }],
|
||||
});
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({});
|
||||
|
||||
const out = await getCookies(bridge, 'qun.qq.com');
|
||||
expect(out['p_skey']).toBe('psk-from-oidb');
|
||||
});
|
||||
|
||||
it('returns whatever cookies it got if getPSkey fallback throws', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode)
|
||||
.mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 })
|
||||
.mockRejectedValueOnce(new Error('pskey unavailable'));
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ skey: 'sk' });
|
||||
|
||||
const out = await getCookies(bridge, 'qun.qq.com');
|
||||
expect(out).toEqual({ skey: 'sk' }); // no p_skey, but no throw
|
||||
});
|
||||
});
|
||||
|
||||
describe('cookies — getSKey', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockReset();
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockReset();
|
||||
});
|
||||
|
||||
it('returns skey from the qzone-targeted ptlogin2 jump', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ skey: 'the-skey' });
|
||||
expect(await getSKey(bridge)).toBe('the-skey');
|
||||
});
|
||||
|
||||
it('throws when clientKey comes back empty', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({ clientKey: '' });
|
||||
await expect(getSKey(bridge)).rejects.toThrow(/clientKey is empty/);
|
||||
});
|
||||
|
||||
it('throws when the jump response omits skey', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({});
|
||||
await expect(getSKey(bridge)).rejects.toThrow(/SKey is Empty/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('cookies — pure helpers', () => {
|
||||
it('getBknFromSKey: deterministic djb2 truncated to 31 bits', () => {
|
||||
// Two calls on the same input → same number; different input → different.
|
||||
const a = getBknFromSKey('hello');
|
||||
const b = getBknFromSKey('hello');
|
||||
const c = getBknFromSKey('world');
|
||||
expect(a).toBe(b);
|
||||
expect(a).not.toBe(c);
|
||||
expect(a).toBeGreaterThanOrEqual(0);
|
||||
expect(a).toBeLessThanOrEqual(2147483647);
|
||||
});
|
||||
|
||||
it('getCookiesStr: joins cookies with "; "', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ p_skey: 'p', skey: 's' });
|
||||
const out = await getCookiesStr(bridge, 'qun.qq.com');
|
||||
expect(out).toBe('p_skey=p; skey=s');
|
||||
});
|
||||
|
||||
it('getCsrfToken: combines getSKey + getBknFromSKey', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ skey: 'abc' });
|
||||
const csrf = await getCsrfToken(bridge);
|
||||
expect(csrf).toBe(getBknFromSKey('abc'));
|
||||
});
|
||||
|
||||
it('getCredentials: returns { cookies, token, csrf_token } with bkn(p_skey)', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 });
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({ p_skey: 'p', skey: 's' });
|
||||
|
||||
const out = await getCredentials(bridge, 'qun.qq.com');
|
||||
expect(out.cookies).toBe('p_skey=p; skey=s');
|
||||
expect(out.token).toBe(getBknFromSKey('p'));
|
||||
expect(out.csrf_token).toBe(out.token);
|
||||
});
|
||||
|
||||
it('getCredentials: token is 0 when no skey/p_skey available', async () => {
|
||||
vi.mocked(oidb.sendOidbAndDecode)
|
||||
.mockResolvedValueOnce({ clientKey: 'ck', keyIndex: 19 })
|
||||
.mockRejectedValueOnce(new Error('no pskey'));
|
||||
vi.mocked(RequestUtil.HttpsGetCookies).mockResolvedValueOnce({});
|
||||
|
||||
const out = await getCredentials(bridge, 'qun.qq.com');
|
||||
expect(out.token).toBe(0);
|
||||
expect(out.cookies).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,68 @@
|
||||
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-essence', () => ({
|
||||
getGroupEssenceMsg: vi.fn(async () => null),
|
||||
getGroupEssenceMsgAll: vi.fn(async () => []),
|
||||
}));
|
||||
|
||||
import * as cookies from '../../src/bridge/web-actions/cookies';
|
||||
import * as essenceWeb from '../../src/bridge/web/group-essence';
|
||||
import {
|
||||
getGroupEssence,
|
||||
getGroupEssenceAll,
|
||||
} from '../../src/bridge/web-actions/group-essence';
|
||||
|
||||
const bridge = {} as any;
|
||||
|
||||
describe('group-essence', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(cookies.getCookies).mockClear();
|
||||
vi.mocked(essenceWeb.getGroupEssenceMsg).mockReset();
|
||||
vi.mocked(essenceWeb.getGroupEssenceMsgAll).mockReset();
|
||||
});
|
||||
|
||||
it('getGroupEssence: passes pageStart + pageLimit through to the web API', async () => {
|
||||
vi.mocked(essenceWeb.getGroupEssenceMsg).mockResolvedValueOnce({
|
||||
retcode: 0,
|
||||
data: { is_end: true, msg_list: [{ a: 1 }] },
|
||||
} as any);
|
||||
|
||||
const out = await getGroupEssence(bridge, 12345, 10, 20);
|
||||
const args = vi.mocked(essenceWeb.getGroupEssenceMsg).mock.calls[0]!;
|
||||
expect(args[1]).toBe('12345'); // groupCode
|
||||
expect(args[2]).toBe(10); // pageStart
|
||||
expect(args[3]).toBe(20); // pageLimit
|
||||
expect((out as any).data.msg_list).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('getGroupEssence: defaults pageStart=0 + pageLimit=50', async () => {
|
||||
vi.mocked(essenceWeb.getGroupEssenceMsg).mockResolvedValueOnce({
|
||||
retcode: 0,
|
||||
data: { is_end: true, msg_list: [] },
|
||||
} as any);
|
||||
await getGroupEssence(bridge, 12345);
|
||||
const args = vi.mocked(essenceWeb.getGroupEssenceMsg).mock.calls[0]!;
|
||||
expect(args[2]).toBe(0);
|
||||
expect(args[3]).toBe(50);
|
||||
});
|
||||
|
||||
it('getGroupEssence: falls back to an empty result envelope when the API returns null', async () => {
|
||||
vi.mocked(essenceWeb.getGroupEssenceMsg).mockResolvedValueOnce(null as any);
|
||||
const out = await getGroupEssence(bridge, 12345);
|
||||
expect(out).toEqual({ retcode: -1, data: { is_end: true, msg_list: [] } });
|
||||
});
|
||||
|
||||
it('getGroupEssenceAll: delegates to the all-pages web helper', async () => {
|
||||
vi.mocked(essenceWeb.getGroupEssenceMsgAll).mockResolvedValueOnce([
|
||||
{ retcode: 0, data: { is_end: false, msg_list: [{}] } },
|
||||
{ retcode: 0, data: { is_end: true, msg_list: [{}, {}] } },
|
||||
] as any);
|
||||
const out = await getGroupEssenceAll(bridge, 12345);
|
||||
expect(out).toHaveLength(2);
|
||||
expect(vi.mocked(essenceWeb.getGroupEssenceMsgAll).mock.calls[0]![1]).toBe('12345');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,86 @@
|
||||
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');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,155 @@
|
||||
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-notice', () => ({
|
||||
setGroupNoticeWebAPI: vi.fn(async () => ({ ec: 0 })),
|
||||
getGroupNoticeWebAPI: vi.fn(async () => ({ feeds: {} })),
|
||||
uploadGroupNoticeImage: vi.fn(async () => null),
|
||||
deleteGroupNotice: vi.fn(async () => true),
|
||||
}));
|
||||
|
||||
import * as cookies from '../../src/bridge/web-actions/cookies';
|
||||
import * as noticeWeb from '../../src/bridge/web/group-notice';
|
||||
import {
|
||||
sendGroupNotice,
|
||||
getGroupNotice,
|
||||
deleteGroupNoticeByFid,
|
||||
} from '../../src/bridge/web-actions/group-notice';
|
||||
|
||||
const bridge = {} as any;
|
||||
|
||||
describe('group-notice — sendGroupNotice', () => {
|
||||
beforeEach(() => {
|
||||
vi.mocked(cookies.getCookies).mockClear();
|
||||
vi.mocked(noticeWeb.setGroupNoticeWebAPI).mockReset();
|
||||
vi.mocked(noticeWeb.uploadGroupNoticeImage).mockReset();
|
||||
});
|
||||
|
||||
it('text-only: dispatches with empty picId and default image dims (540x300)', async () => {
|
||||
vi.mocked(noticeWeb.setGroupNoticeWebAPI).mockResolvedValueOnce({ ec: 0 } as any);
|
||||
await sendGroupNotice(bridge, 12345, 'hello');
|
||||
|
||||
expect(noticeWeb.uploadGroupNoticeImage).not.toHaveBeenCalled();
|
||||
const args = vi.mocked(noticeWeb.setGroupNoticeWebAPI).mock.calls[0]!;
|
||||
expect(args[1]).toBe('12345'); // groupCode
|
||||
expect(args[2]).toBe('hello'); // content
|
||||
expect(args[3]).toBe(0); // pinned default
|
||||
expect(args[4]).toBe(1); // type default
|
||||
expect(args[7]).toBe(1); // confirm_required default
|
||||
expect(args[8]).toBe(''); // picId
|
||||
expect(args[9]).toBe(540); // imgWidth default
|
||||
expect(args[10]).toBe(300); // imgHeight default
|
||||
});
|
||||
|
||||
it('with options: forwards pinned / type / confirm_required overrides', async () => {
|
||||
vi.mocked(noticeWeb.setGroupNoticeWebAPI).mockResolvedValueOnce({ ec: 0 } as any);
|
||||
await sendGroupNotice(bridge, 12345, 'pinned!', {
|
||||
pinned: 1,
|
||||
type: 2,
|
||||
confirm_required: 0,
|
||||
});
|
||||
const args = vi.mocked(noticeWeb.setGroupNoticeWebAPI).mock.calls[0]!;
|
||||
expect(args[3]).toBe(1);
|
||||
expect(args[4]).toBe(2);
|
||||
expect(args[7]).toBe(0);
|
||||
});
|
||||
|
||||
it('with http image: fetches bytes, uploads, threads pic info into the post', async () => {
|
||||
const realFetch = global.fetch;
|
||||
global.fetch = vi.fn(async () => ({
|
||||
ok: true,
|
||||
arrayBuffer: async () => new Uint8Array([1, 2, 3]).buffer,
|
||||
})) as any;
|
||||
|
||||
vi.mocked(noticeWeb.uploadGroupNoticeImage).mockResolvedValueOnce({
|
||||
id: 'pic-xyz', width: 1024, height: 768,
|
||||
} as any);
|
||||
vi.mocked(noticeWeb.setGroupNoticeWebAPI).mockResolvedValueOnce({ ec: 0 } as any);
|
||||
|
||||
await sendGroupNotice(bridge, 12345, 'image notice', { image: 'https://example.com/x.png' });
|
||||
|
||||
expect(global.fetch).toHaveBeenCalledOnce();
|
||||
expect(noticeWeb.uploadGroupNoticeImage).toHaveBeenCalledOnce();
|
||||
const args = vi.mocked(noticeWeb.setGroupNoticeWebAPI).mock.calls[0]!;
|
||||
expect(args[8]).toBe('pic-xyz');
|
||||
expect(args[9]).toBe(1024);
|
||||
expect(args[10]).toBe(768);
|
||||
|
||||
global.fetch = realFetch;
|
||||
});
|
||||
|
||||
it('image fetch failure: throws "Failed to download image"', async () => {
|
||||
const realFetch = global.fetch;
|
||||
global.fetch = vi.fn(async () => ({ ok: false, status: 503 })) as any;
|
||||
await expect(sendGroupNotice(bridge, 12345, 'x', { image: 'https://bad' }))
|
||||
.rejects.toThrow(/Failed to download image: 503/);
|
||||
global.fetch = realFetch;
|
||||
});
|
||||
|
||||
it('throws when the web API returns non-zero ec', async () => {
|
||||
vi.mocked(noticeWeb.setGroupNoticeWebAPI).mockResolvedValueOnce({
|
||||
ec: 42, em: 'permission denied',
|
||||
} as any);
|
||||
await expect(sendGroupNotice(bridge, 12345, 'x'))
|
||||
.rejects.toThrow(/permission denied/);
|
||||
});
|
||||
|
||||
it('throws with a generic message when ec≠0 and em is missing', async () => {
|
||||
vi.mocked(noticeWeb.setGroupNoticeWebAPI).mockResolvedValueOnce({ ec: 7 } as any);
|
||||
await expect(sendGroupNotice(bridge, 12345, 'x'))
|
||||
.rejects.toThrow(/Cookie过期或权限不足/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('group-notice — getGroupNotice', () => {
|
||||
beforeEach(() => { vi.mocked(noticeWeb.getGroupNoticeWebAPI).mockReset(); });
|
||||
|
||||
it('flattens feeds into OneBot-shaped notice list', async () => {
|
||||
vi.mocked(noticeWeb.getGroupNoticeWebAPI).mockResolvedValueOnce({
|
||||
feeds: {
|
||||
f1: {
|
||||
fid: 'f1', u: 10001, pubt: 1700000000,
|
||||
msg: { text: 'first', pics: [{ id: 'p1', h: 100, w: 200 }] },
|
||||
settings: {}, read_num: 5,
|
||||
} as any,
|
||||
f2: {
|
||||
fid: 'f2', u: 10002, pubt: 1700000100,
|
||||
msg: { text: 'second' } as any,
|
||||
settings: {}, read_num: 0,
|
||||
} as any,
|
||||
},
|
||||
} as any);
|
||||
|
||||
const out = await getGroupNotice(bridge, 12345);
|
||||
expect(out).toHaveLength(2);
|
||||
expect(out[0]!.notice_id).toBe('f1');
|
||||
expect(out[0]!.message.text).toBe('first');
|
||||
expect(out[0]!.message.image).toEqual([{ id: 'p1', height: 100, width: 200 }]);
|
||||
expect(out[0]!.message.images).toEqual(out[0]!.message.image); // alias
|
||||
expect(out[1]!.message.image).toEqual([]); // no pics → empty
|
||||
});
|
||||
|
||||
it('throws when the underlying web API returns null', async () => {
|
||||
vi.mocked(noticeWeb.getGroupNoticeWebAPI).mockResolvedValueOnce(null as any);
|
||||
await expect(getGroupNotice(bridge, 12345)).rejects.toThrow(/获取公告失败/);
|
||||
});
|
||||
|
||||
it('returns an empty array when feeds is empty', async () => {
|
||||
vi.mocked(noticeWeb.getGroupNoticeWebAPI).mockResolvedValueOnce({ feeds: {} } as any);
|
||||
expect(await getGroupNotice(bridge, 12345)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('group-notice — deleteGroupNoticeByFid', () => {
|
||||
it('delegates to deleteGroupNotice with cookieObject + groupCode + fid', async () => {
|
||||
vi.mocked(noticeWeb.deleteGroupNotice).mockResolvedValueOnce(true);
|
||||
const ok = await deleteGroupNoticeByFid(bridge, 12345, 'fid-abc');
|
||||
expect(ok).toBe(true);
|
||||
const args = vi.mocked(noticeWeb.deleteGroupNotice).mock.calls[0]!;
|
||||
expect(args[1]).toBe('12345');
|
||||
expect(args[2]).toBe('fid-abc');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user