refactor(api): introduce MessageApi + BridgeContext + ApiHub skeleton (#6 commit 1/13)
First of 13 commits decomposing the Bridge god-object into typed Api
classes following NapCat's `ctx.apis.XxxApi.method()` pattern. This
commit lays the foundation (BridgeContext, ApiHub, eager construction)
plus moves the message-send/recall/mark-read methods onto MessageApi.
Subsequent commits will move the other 12 areas (group admin, files,
profile, …) similarly.
Architecture
------------
* `bridge-context.ts` — new interface, the surface Api classes see.
Carries identity / events / apis / sendRawPacket / resolveUserUid /
next{Message,Client}Sequence helpers / upload meta cache. Replaces
the previous `BridgeInterface` for Api-class consumption (the
legacy interface still exists, just slimmed; full removal happens
in commit 13).
* `apis/index.ts` — `ApiHub` interface + `buildApiHub(ctx)` factory.
Each commit grows `ApiHub` by one entry; today only `message` is
populated.
* `apis/message.ts` — `MessageApi` with seven methods inlined from
the previous Bridge implementation:
sendGroup ← Bridge.sendGroupMessage
sendPrivate ← Bridge.sendPrivateMessage
sendC2cFile ← Bridge.sendC2cFileMessage
recallGroup ← actions/group-message.ts::recallGroupMessage
recallPrivate ← actions/group-message.ts::recallPrivateMessage
markGroupRead ← actions/group-message.ts::markGroupMessageRead
markPrivateRead ← actions/group-message.ts::markPrivateMessageRead
(Names trimmed: the `apis.message` prefix already signals the
context, so `apis.message.sendGroupMessage` would be redundant.)
Bridge changes
--------------
* Removed the 7 methods listed above (moved to MessageApi).
* Removed unused imports + `SEND_MSG_CMD` constant + `buildSendElems`
+ `protobuf_encode/decode` (now used only inside MessageApi).
* Added `readonly apis: ApiHub` field, built eagerly in constructor.
* Promoted `nextMessageRandom` / `nextClientSequence` from private →
public (Api classes need them to build SendMessageRequest packets).
* `BridgeInterface` now also lists `apis: ApiHub` + the two
generators; the 7 deleted method signatures are gone.
Caller-site updates (~9 files)
------------------------------
* `bridge/actions/group-file.ts` — `bridge.sendC2cFileMessage(...)`
→ `bridge.apis.message.sendC2cFile(...)`.
* `onebot/instance-context.ts` — markGroupMsgAsRead / markPrivateMsgAsRead
bindings now route through `apis.message.markGroup/PrivateRead`.
* `onebot/modules/message-actions.ts` — every send/recall site
rerouted; `typeof ref.bridge.sendXxx` type refs also updated.
* `onebot/actions/extended.ts` — same.
Tests (~5 files)
----------------
* `tests/actions/_helpers.ts` — `mockBridge()` grows an `apis` field
built by `mockApiHub()`; the previous flat `sendGroupMessage` etc.
vi.fn stubs are replaced with `mockMessageApi()`.
* All test files using direct `bridge.sendXxx` / `bridge.recallXxx` /
`bridge.markXxxAsRead` calls or stubs updated to `bridge.apis.message.*`.
* No behaviour changes — 587/587 pass (same baseline; pre-existing
network-manager native-addon failure unchanged).
This commit is contained in:
1 parent
9a678442e9
commit
3e5dca1bd4
15 files changed
+598
-332
No files matched your search
@@ -159,7 +159,7 @@ describe('actions/group-file', () => {
|
||||
expect(fileId).toBe('fid-pub');
|
||||
// sendGroupMessage must NOT be touched — the previous wire shape
|
||||
// (PbSendMsg w/ transElem(24)) is the bug we're guarding against.
|
||||
expect(bridge.sendGroupMessage).not.toHaveBeenCalled();
|
||||
expect(bridge.apis.message.sendGroup).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uploadGroupFile skips the chat post when uploadFile=false', async () => {
|
||||
@@ -272,12 +272,12 @@ describe('actions/group-file', () => {
|
||||
}),
|
||||
);
|
||||
await groupFile.uploadPrivateFile(bridge as any, 67890, '/path/private-file.bin', 'doc.pdf');
|
||||
expect(bridge.sendC2cFileMessage).toHaveBeenCalledOnce();
|
||||
const [userUin, userUid, info] = bridge.sendC2cFileMessage.mock.calls[0]!;
|
||||
expect(bridge.apis.message.sendC2cFile).toHaveBeenCalledOnce();
|
||||
const [userUin, userUid, info] = bridge.apis.message.sendC2cFile.mock.calls[0]!;
|
||||
expect(userUin).toBe(67890);
|
||||
expect(userUid).toBe('target-uid');
|
||||
expect(info).toMatchObject({ fileId: 'pfid', fileName: 'doc.pdf', fileHash: 'phash' });
|
||||
expect(bridge.sendPrivateMessage).not.toHaveBeenCalled();
|
||||
expect(bridge.apis.message.sendPrivate).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uploadPrivateFile caches the upload metadata for later resend by file_id', async () => {
|
||||
@@ -314,7 +314,7 @@ describe('actions/group-file', () => {
|
||||
}),
|
||||
);
|
||||
await groupFile.uploadPrivateFile(bridge as any, 67890, '/path/file', '', false);
|
||||
expect(bridge.sendC2cFileMessage).not.toHaveBeenCalled();
|
||||
expect(bridge.apis.message.sendC2cFile).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('uploadPrivateFile reads host from rtpMediaPlatformUploadAddress[0].inIP when populated', async () => {
|
||||
|
||||
Reference in new issue
Block a user