bridge-actions.ts was a single 1814-line file holding 55 unrelated exported functions — admin/file/forward/interaction/recall/profile/ status/translate/sign/avatar all jumbled together. Each function already takes `bridge: Bridge` as its first parameter, so the "deepening" target (per-UIN state object threaded into themed action functions) was structurally there — what was missing was a coherent file-level grouping that AI tools and humans could navigate. This commit is candidate #2 from the deepening list, A-only variant: pure file split, no facade change, no caller change outside the bridge module. New layout under packages/core/src/bridge/actions/ : shared.ts 82 private helpers (toInt, ensureRetCodeZero, resolveSelfUid) and the MediaIndexNode type. group-admin.ts 153 mute/kick/leave + setGroup{Admin/Card/Name/ SpecialTitle/AddOption/Search/Remark/AddRequest} + getGroupAtAllRemain. (14 fns) group-file.ts 821 upload/list/url/delete/move/folder ops, both group + private files, media-URL fetchers for ptt/video, fetchGroupFileCount. Owns file- specific helpers (normalizeDirectory, buildGroupFileUploadExt, normalizeMediaNode, fetchNtv2DownloadUrl, ...). (15 fns) group-message 101 recall* / markRead* / setGroupEssence. (5 fns) interaction.ts 61 poke / like / reaction / emoji-likes. (4 fns) forward.ts 197 uploadForwardNodes, fetchForwardNodes + in-memory resId cache. (2 fns) friend.ts 53 setFriendAddRequest, deleteFriend, setFriendRemark. (3 fns) profile.ts 269 setOnlineStatus, setProfile, setSelfLongNick, setInputStatus, setAvatar, fetchCustomFace, getProfileLike, getUnidirectionalFriendList. (8 fns) misc.ts 173 translateEn2Zh, getMiniAppArk, clickInlineKeyboardButton, sendGroupSign. (4 fns) bridge.ts updates its imports — one themed import block per action module instead of one giant block from bridge-actions.ts. GroupFilesResult and MediaIndexNode type imports now point at actions/group-file and actions/shared. Public Bridge facade API is unchanged: every caller outside `bridge/` keeps the same `bridge.X(...)` surface and the delegated function it points at. bridge-actions.ts is deleted. The new shared.ts holds the three helpers that span themes; everything else lives co-located with the functions that use it. Adds 60 happy-path tests across tests/actions/ — one .test.ts per themed file plus a shared `_helpers.ts` (mockBridge fixture): group-admin 10 command + payload dispatch, UID resolution, flag-driven sub-command selection (mute on/off, add-request filtered/unfiltered). group-file 13 fetchGroupFileCount, upload happy path with and without highway, OIDB error bubbling, file listing pagination + folder/file split, URL builder shape, delete/move/folder dispatch, NTV2 URL assembly. group-message 5 recall (group/private), markRead, essence on/off, transport failure propagation. interaction 6 poke (group vs friend payload), like, reaction on/off, emoji-likes cookie/isLast logic. forward 5 empty-array rejection, dispatch + res_id return, transport failure, cache reuse after upload. friend 5 numeric flag → UID resolution path, opaque flag passthrough, delete + best-effort cache refresh. profile 9 status dispatch, setProfile no-op when undefined, setAvatar highway cmd 90, getProfileLike + getUnidirectionalFriendList JSON parsing. misc 6 translate, mini-app type guard, click + sign dispatch. Tests use vi.mock on `bridge-oidb` (and `highway-client`/`highway-utils` where relevant) so each action runs against a tiny fake Bridge, no native pipe / real OIDB exchange needed. Previously these 55 functions had zero unit-test coverage. 244 tests pass (was 184); only failing suite remains the pre-existing macOS-arm64 missing websocket native addon, unrelated.
194 lines
8.0 KiB
TypeScript
194 lines
8.0 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
vi.mock('../../src/bridge/bridge-oidb', () => ({
|
|
sendOidbAndCheck: vi.fn(async () => undefined),
|
|
sendOidbAndDecode: vi.fn(async () => ({})),
|
|
resolveUserUid: vi.fn(async () => 'resolved-uid'),
|
|
makeOidbRequest: vi.fn(() => new Uint8Array(0)),
|
|
}));
|
|
|
|
vi.mock('../../src/bridge/highway/highway-client', () => ({
|
|
fetchHighwaySession: vi.fn(async () => ({})),
|
|
uploadHighwayHttp: vi.fn(async () => undefined),
|
|
}));
|
|
|
|
vi.mock('../../src/bridge/highway/utils', () => ({
|
|
loadBinarySource: vi.fn(async (_src: string, fallback: string) => ({
|
|
bytes: new Uint8Array([1, 2, 3]),
|
|
fileName: `${fallback}.bin`,
|
|
})),
|
|
computeHashes: vi.fn(() => ({ md5: new Uint8Array(16), sha1: new Uint8Array(20) })),
|
|
computeMd5: vi.fn(() => new Uint8Array(16)),
|
|
}));
|
|
|
|
import * as oidb from '../../src/bridge/bridge-oidb';
|
|
import * as highwayClient from '../../src/bridge/highway/highway-client';
|
|
import * as groupFile from '../../src/bridge/actions/group-file';
|
|
import { mockBridge } from './_helpers';
|
|
|
|
describe('actions/group-file', () => {
|
|
beforeEach(() => {
|
|
vi.mocked(oidb.sendOidbAndCheck).mockClear();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockClear();
|
|
vi.mocked(oidb.resolveUserUid).mockClear();
|
|
vi.mocked(highwayClient.fetchHighwaySession).mockClear();
|
|
vi.mocked(highwayClient.uploadHighwayHttp).mockClear();
|
|
});
|
|
|
|
it('fetchGroupFileCount returns { fileCount, maxCount } from the OIDB response', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
|
count: { fileCount: 42, maxCount: 1000 },
|
|
});
|
|
const out = await groupFile.fetchGroupFileCount(bridge as any, 12345);
|
|
expect(out).toEqual({ fileCount: 42, maxCount: 1000 });
|
|
});
|
|
|
|
it('fetchGroupFileCount falls back to defaults on partial response', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({ count: {} });
|
|
const out = await groupFile.fetchGroupFileCount(bridge as any, 12345);
|
|
expect(out).toEqual({ fileCount: 0, maxCount: 10000 });
|
|
});
|
|
|
|
it('uploadGroupFile skips highway when boolFileExist is true', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
|
upload: {
|
|
retCode: 0,
|
|
fileId: 'fid-xyz',
|
|
boolFileExist: true,
|
|
},
|
|
});
|
|
const out = await groupFile.uploadGroupFile(bridge as any, 12345, '/path/file.bin');
|
|
expect(out).toEqual({ fileId: 'fid-xyz' });
|
|
expect(highwayClient.fetchHighwaySession).not.toHaveBeenCalled();
|
|
expect(highwayClient.uploadHighwayHttp).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('uploadGroupFile runs highway PUT when boolFileExist is false', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
|
upload: {
|
|
retCode: 0,
|
|
fileId: 'fid-xyz',
|
|
boolFileExist: false,
|
|
uploadIp: '1.2.3.4',
|
|
uploadPort: 8080,
|
|
fileKey: new Uint8Array([9]),
|
|
checkKey: new Uint8Array([8]),
|
|
},
|
|
});
|
|
await groupFile.uploadGroupFile(bridge as any, 12345, '/path/file.bin');
|
|
expect(highwayClient.fetchHighwaySession).toHaveBeenCalledOnce();
|
|
expect(highwayClient.uploadHighwayHttp).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it('uploadGroupFile throws on missing upload response', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({});
|
|
await expect(groupFile.uploadGroupFile(bridge as any, 12345, '/path/file.bin'))
|
|
.rejects.toThrow(/response missing/);
|
|
});
|
|
|
|
it('uploadGroupFile bubbles up OIDB retCode errors', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
|
upload: { retCode: 999, retMsg: 'quota exceeded' },
|
|
});
|
|
await expect(groupFile.uploadGroupFile(bridge as any, 12345, '/path/file.bin'))
|
|
.rejects.toThrow(/code=999/);
|
|
});
|
|
|
|
it('uploadPrivateFile resolves both target + self UID before OIDB call', async () => {
|
|
const bridge = mockBridge({
|
|
qqInfo: { uin: '10001', selfUid: '', findGroupMember: vi.fn(() => null) },
|
|
});
|
|
vi.mocked(oidb.resolveUserUid)
|
|
.mockResolvedValueOnce('target-uid') // target user
|
|
.mockResolvedValueOnce('self-uid-resolved'); // self fallback
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
|
upload: { retCode: 0, uuid: 'fid', fileAddon: 'hash', boolFileExist: true },
|
|
});
|
|
const out = await groupFile.uploadPrivateFile(bridge as any, 67890, '/path/file');
|
|
expect(out).toEqual({ fileId: 'fid', fileHash: 'hash' });
|
|
expect(oidb.resolveUserUid).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it('fetchGroupFiles paginates files + folders out of OIDB items', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
|
list: {
|
|
retCode: 0,
|
|
isEnd: true,
|
|
items: [
|
|
{ type: 1, fileInfo: { fileId: 'f1', fileName: 'a.txt', uploaderUin: 1, uploaderName: 'alice' } },
|
|
{ type: 2, folderInfo: { folderId: 'd1', folderName: 'dir', creatorUin: 2, creatorName: 'bob' } },
|
|
],
|
|
},
|
|
});
|
|
const out = await groupFile.fetchGroupFiles(bridge as any, 12345);
|
|
expect(out.files).toHaveLength(1);
|
|
expect(out.files[0]).toMatchObject({ fileId: 'f1', fileName: 'a.txt', uploader: 1, uploaderName: 'alice' });
|
|
expect(out.folders).toHaveLength(1);
|
|
expect(out.folders[0]).toMatchObject({ folderId: 'd1', folderName: 'dir', creator: 2, creatorName: 'bob' });
|
|
});
|
|
|
|
it('fetchGroupFileUrl builds the https URL from downloadDns + hex-encoded path', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
|
download: {
|
|
retCode: 0,
|
|
downloadDns: 'cdn.example.com',
|
|
downloadUrl: new Uint8Array([0x01, 0x02]),
|
|
},
|
|
});
|
|
const url = await groupFile.fetchGroupFileUrl(bridge as any, 12345, 'fid-xyz');
|
|
expect(url).toBe('https://cdn.example.com/ftn_handler/0102/?fname=fid-xyz');
|
|
});
|
|
|
|
it('fetchGroupFileUrl throws when response is missing dns or url', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValueOnce({
|
|
download: { retCode: 0 },
|
|
});
|
|
await expect(groupFile.fetchGroupFileUrl(bridge as any, 12345, 'fid-xyz'))
|
|
.rejects.toThrow(/invalid/);
|
|
});
|
|
|
|
it('deleteGroupFile / moveGroupFile dispatch the right sub-commands', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode)
|
|
.mockResolvedValueOnce({ delete: { retCode: 0 } })
|
|
.mockResolvedValueOnce({ move: { retCode: 0 } });
|
|
await groupFile.deleteGroupFile(bridge as any, 12345, 'fid');
|
|
await groupFile.moveGroupFile(bridge as any, 12345, 'fid', '/a', '/b');
|
|
expect(vi.mocked(oidb.sendOidbAndDecode).mock.calls.map(c => c[3])).toEqual([3, 5]);
|
|
});
|
|
|
|
it('createGroupFileFolder / deleteGroupFileFolder / renameGroupFileFolder dispatch 0x6d7 family', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode)
|
|
.mockResolvedValueOnce({ create: { retcode: 0 } })
|
|
.mockResolvedValueOnce({ delete: { retcode: 0 } })
|
|
.mockResolvedValueOnce({ rename: { retcode: 0 } });
|
|
await groupFile.createGroupFileFolder(bridge as any, 1, 'folder');
|
|
await groupFile.deleteGroupFileFolder(bridge as any, 1, 'fid');
|
|
await groupFile.renameGroupFileFolder(bridge as any, 1, 'fid', 'newname');
|
|
expect(vi.mocked(oidb.sendOidbAndDecode).mock.calls.map(c => c[2])).toEqual([0x6D7, 0x6D7, 0x6D7]);
|
|
});
|
|
|
|
it('fetch*UrlByNode builds https://domain/path?rkey from the NTV2 response', async () => {
|
|
const bridge = mockBridge();
|
|
vi.mocked(oidb.sendOidbAndDecode).mockResolvedValue({
|
|
respHead: { retCode: 0 },
|
|
download: {
|
|
info: { domain: 'media.example.com', urlPath: '/path/x' },
|
|
rKeyParam: '?rkey=abc',
|
|
},
|
|
});
|
|
const url = await groupFile.fetchGroupVideoUrlByNode(bridge as any, 12345, { fileUuid: 'uuid' });
|
|
expect(url).toBe('https://media.example.com/path/x?rkey=abc');
|
|
});
|
|
});
|