Commit Graph
2 Commits
Author SHA1 Message Date
DrSmoothl 49281726f1 refactor(core): split EventConverter god switch into themed pure functions
event-converter.ts (541 lines) was a class with a 16-case switch
mapping bridge events to OneBot wire JSON, plus a 13-case helper
mapping bridge elements to OneBot segments, plus four lazily-injected
resolver setters (image-url / media-url / message-id / media-segment
sink). Testing one case required instantiating the class and wiring
four setters; 22 of the 29 cases (10 notices + 2 requests + 13 element
types) had zero tests.

This commit kills the class and replaces it with a pure-function
family that takes a ConverterContext (a plain object holding selfId
+ the four resolver callbacks). Each per-event handler becomes a
small typed function that's trivial to call from a test with two
lines of setup.

New layout: packages/core/src/onebot/event-converter/

  index.ts        133  ConverterContext + four resolver types +
                       convertEvent dispatcher + re-exports.
  to-message.ts   112  friend / group / temp message  (3 handlers)
  to-notice.ts    189  group_member_join / leave / mute / admin /
                       friend_recall / group_recall / friend_poke /
                       group_poke / group_essence / group_file_upload /
                       friend_add                     (11 handlers)
  to-request.ts    37  friend_request / group_invite  (2 handlers)
  to-segment.ts   145  13 element -> segment cases + elementsToJson +
                       elementsToOneBotSegments
  utils.ts        111  parseSelfId / isSameActor / applyMessageIdResolver /
                       resolveReplyId / segmentsToRawMessage + CQ encoding

Caller migration (3 files, all inside onebot/):

  instance.ts:        replaces `new EventConverter()` + four setter
                      calls with a single `converterCtx: ConverterContext`
                      object built once at construction. The four
                      closures that used to be passed through setters
                      are now properties of the context.
  instance-context.ts: `eventConverter: EventConverter` field renamed
                      to `converterCtx: ConverterContext`.
  event-pipeline.ts:  `ctx.eventConverter.convert(ctx.uin, event)` is
                      now `convertEvent(ctx.converterCtx, event)`.

External callers (modules/message-actions.ts) keep working unchanged:
elementsToOneBotSegments is re-exported from the new index.ts with
the same signature.

Adds 28 new tests covering every previously-untested case:

  message kinds (already partly covered):  7 tests
    - friend / group / temp_message peer + self post_type tagging
    - messageIdResolver override
  notice kinds (new):                     11 tests
    - all 11 cases, including the sub-type axes
      (approve/invite, leave/kick/kick_me, ban/lift_ban,
       set/unset, add/delete, etc.)
  request kinds (new):                     2 tests
    - friend_request, group_invite (with sub_type default)
  element segments (new):                 13 tests
    - text / face / image (with + without resolver) / at /
      at-all / reply (with + without resolver) / record /
      video / json / xml (with + without subType) / file /
      mface / poke
  mediaSegmentSink wiring (new):           1 test

Plus 3 tests in media-store.test.ts and 1 in event-pipeline.test.ts
adapted to the new API.

317 tests pass (was 285); 3 stability runs, same pre-existing
macOS-arm64 websocket addon failure, unrelated.
2026-05-12 18:45:28 +08:00
手瓜一十雪 144e0fdd59 Add BridgeEventBus and network manager
Introduce a typed per-kind BridgeEventBus and migrate away from the legacy single-callback firehose. Bridge now exposes `events` and emits via the bus; BridgeManager/OneBotManager no longer wire a global event callback. Add a focused onebot event pipeline (event-pipeline.ts) that subscribes per-kind handlers on instance construction. Replace the in-memory MediaCache with a persistent MediaStore and update all lookups accordingly. Replace the old Http/Ws/HttpPost transport implementations with a unified OneBotNetworkManager and pluggable adapters (server/client/post), and update OneBotInstance to install and manage adapters from config. Remove several legacy files (http-transport, http-post-transport, ws-transport, media-cache) and update imports across the codebase. Also add tests for the bridge event bus, event pipeline, network manager, and rename media tests to reflect MediaStore usage. The bus dispatches handlers in parallel and reports handler errors via an onError hook to avoid one faulty listener blocking others.
2026-05-09 16:30:06 +08:00