Phase 1 of the 4-package split called out in RoadMap #3. Moves the "infrastructure" leaves — files with zero business dependencies that @snowluma/bridge, @snowluma/core, and @snowluma/onebot all consume — into a dedicated workspace package. Mirrors what #4 (proto-defs) did for wire-protobuf definitions. Why --- - Cleaner layering: these were never bridge- or onebot-specific. Logger / hex codec / runtime config sit at the bottom of every module's import graph; giving them their own package makes that graph readable and prevents future leaks the other direction. - Sets up #3's bridge + onebot extractions: those need a clean leaf to depend on. Without this, every cross-package import would have to re-export through core, which defeats the split. - Mirrors @snowluma/proto-defs layout — per-file subpath exports (`@snowluma/common/logger`, `@snowluma/common/hex`, …) keep import sites unambiguous and let bundlers tree-shake aggressively. Layout ------ Moved (via `git mv` — history preserved): packages/core/src/utils/logger.ts → @snowluma/common/logger packages/core/src/utils/hex.ts → @snowluma/common/hex packages/core/src/utils/log-file-transport.ts → @snowluma/common/log-file-transport packages/core/src/utils/log-summary.ts → @snowluma/common/log-summary packages/core/src/protocol/packet-sender.ts → @snowluma/common/packet-sender packages/core/src/protocol/types.ts → @snowluma/common/protocol-types packages/core/src/common/runtime.ts → @snowluma/common/runtime Not moved (deferred to Phase 2): `packages/core/src/utils/event-format.ts` — it has reverse imports on `bridge/events`, `bridge/identity-service`, `onebot/message-store`, `onebot/types`. It moves into @snowluma/bridge in Phase 2, with the MessageStore parameter decoupled via a structural type so bridge doesn't end up depending on onebot. Changes ------- - New package `packages/common/` (package.json, tsconfig.json — no src/index.ts barrel, callers use the subpath exports directly to avoid pulling in `node:fs` when only the hex codec is needed). - 7 `.ts` files moved via `git mv`. - 50+ import sites rewritten via sed across `packages/core/src` and `packages/core/tests`. Old shapes (`../utils/logger`, `../../protocol/types`, `./common/runtime`, `../src/utils/hex`) all collapse to `@snowluma/common/<sub>`. - `@snowluma/core` depends on `@snowluma/common` (workspace:*). - `packages/core/src/{protocol,common}/` directories removed (empty). Verification ------------ - core typecheck: clean - common typecheck: clean - Full workspace `pnpm -r typecheck`: all 9 projects clean - core test suite: 552 passed / 552 total (pre-existing websocket-darwin-arm64.node addon failure unchanged — same baseline as the previous refactor commits) - Vite build with stubbed native binary: clean (143 modules, 2.84 MB bundle, 224 KB gzip — same size envelope as before) Note: `utils/event-format.ts` is the only file remaining under the old `utils/` namespace. It moves into @snowluma/bridge alongside the events / identity-service / packet-pipeline trio in Phase 2.
186 lines
5.7 KiB
TypeScript
186 lines
5.7 KiB
TypeScript
// Tests for the new PR5 observability log lines:
|
|
// - [Highway.Image] entry log on uploadImageMsgInfo
|
|
// - [Highway] fast-upload hit when OIDB returns no uKey
|
|
// - [Highway] OIDB-requires-bytes + PUT-done with elapsed ms
|
|
// - [MsgPush.Unknown] unrecognized PkgType
|
|
// - [MsgPush.Unknown] wrapper decoder unknown subType
|
|
//
|
|
// Highway transport is mocked the same way pipeline.test.ts does it so
|
|
// runNtv2Upload exercises real OIDB encode/decode but skips actual
|
|
// network I/O.
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { protobuf_encode } from '@snowluma/proton';
|
|
import { subscribeLogs, type LogEntry } from '@snowluma/common/logger';
|
|
|
|
vi.mock('../src/bridge/highway/highway-client', () => ({
|
|
fetchHighwaySession: vi.fn(async () => ({ sessionId: 'fake-session' })),
|
|
uploadHighwayHttp: vi.fn(async () => undefined),
|
|
buildHighwayExtend: vi.fn(() => new Uint8Array([0xAA, 0xBB])),
|
|
PRIVATE_IMAGE_CMD_ID: 1003,
|
|
GROUP_IMAGE_CMD_ID: 1004,
|
|
}));
|
|
|
|
import {
|
|
runNtv2Upload,
|
|
type MediaSubFileUpload,
|
|
} from '../src/bridge/highway/pipeline';
|
|
import type { OidbBase } from '@snowluma/proto-defs/oidb';
|
|
import type { NTV2UploadRichMediaResp } from '@snowluma/proto-defs/highway';
|
|
import { MsgPushRegistry } from '../src/bridge/msg-push/registry';
|
|
import type { MsgPushContext } from '../src/bridge/msg-push/context';
|
|
import { decodeEvent0x2DC } from '../src/bridge/msg-push/decoders/event-0x2dc';
|
|
|
|
function encodeOidbResponse(body: unknown): Buffer {
|
|
return Buffer.from(protobuf_encode<OidbBase<NTV2UploadRichMediaResp>>({
|
|
command: 0x11C4, subCommand: 100, errorCode: 0,
|
|
body: body as Record<string, unknown>,
|
|
errorMsg: '', reserved: 1,
|
|
} as OidbBase<NTV2UploadRichMediaResp>));
|
|
}
|
|
|
|
function fakeBridge(responseData: Buffer, uin = '12345'): any {
|
|
return {
|
|
identity: { uin },
|
|
sendRawPacket: vi.fn(async () => ({
|
|
success: true,
|
|
gotResponse: true,
|
|
errorCode: 0,
|
|
errorMessage: '',
|
|
responseData,
|
|
})),
|
|
};
|
|
}
|
|
|
|
let captured: LogEntry[];
|
|
let unsub: () => void;
|
|
|
|
beforeEach(() => {
|
|
captured = [];
|
|
unsub = subscribeLogs((e) => captured.push(e));
|
|
});
|
|
|
|
afterEach(() => {
|
|
unsub();
|
|
});
|
|
|
|
describe('PR5 — highway pipeline logging', () => {
|
|
it('logs fast-upload hit when the server returns no uKey', async () => {
|
|
// upload.uKey absent → no PUT performed.
|
|
const bridge = fakeBridge(encodeOidbResponse({
|
|
upload: { msgInfo: { msgInfoBody: [], extBizInfo: {} } },
|
|
}));
|
|
|
|
const params = {
|
|
bridge,
|
|
isGroup: true,
|
|
targetIdOrUid: 67890,
|
|
oidbCmd: 0x11C4,
|
|
serviceCmd: 'OidbSvcTrpcTcp.0x11c4_100',
|
|
requestId: 1,
|
|
businessType: 1,
|
|
uploadInfo: [{ fileInfo: { fileName: 'a.jpg' }, subFileType: 0 }],
|
|
compatQmsgSceneType: 2,
|
|
extBizInfo: { pic: {} },
|
|
uploads: [{
|
|
source: 'top',
|
|
cmdId: 1004,
|
|
bytes: new Uint8Array([1, 2, 3]),
|
|
md5: new Uint8Array(16),
|
|
sha1: new Uint8Array(20),
|
|
}] as MediaSubFileUpload[],
|
|
label: 'image',
|
|
};
|
|
|
|
await runNtv2Upload(params);
|
|
|
|
const hit = captured.find((e) =>
|
|
e.scope === 'Highway' && e.message.includes('fast-upload hit'));
|
|
expect(hit).toBeDefined();
|
|
expect(hit!.uin).toBe(12345);
|
|
});
|
|
|
|
it('logs OIDB-requires-bytes and PUT-done with elapsed ms when uKey is present', async () => {
|
|
const bridge = fakeBridge(encodeOidbResponse({
|
|
upload: {
|
|
uKey: 'server-needs-bytes-please',
|
|
ipv4s: [{ ipv4: '1.2.3.4', port: 80 }],
|
|
msgInfo: { msgInfoBody: [], extBizInfo: {} },
|
|
},
|
|
}));
|
|
|
|
const params = {
|
|
bridge,
|
|
isGroup: true,
|
|
targetIdOrUid: 67890,
|
|
oidbCmd: 0x11C4,
|
|
serviceCmd: 'OidbSvcTrpcTcp.0x11c4_100',
|
|
requestId: 1,
|
|
businessType: 1,
|
|
uploadInfo: [{ fileInfo: { fileName: 'a.jpg' }, subFileType: 0 }],
|
|
compatQmsgSceneType: 2,
|
|
extBizInfo: { pic: {} },
|
|
uploads: [{
|
|
source: 'top',
|
|
cmdId: 1004,
|
|
bytes: new Uint8Array(1024),
|
|
md5: new Uint8Array(16),
|
|
sha1: new Uint8Array(20),
|
|
}] as MediaSubFileUpload[],
|
|
label: 'image',
|
|
};
|
|
|
|
await runNtv2Upload(params);
|
|
|
|
const requiresBytes = captured.find((e) =>
|
|
e.scope === 'Highway' && e.message.includes('OIDB requires bytes'));
|
|
const putDone = captured.find((e) =>
|
|
e.scope === 'Highway' && /PUT done in \d+ms/.test(e.message));
|
|
|
|
expect(requiresBytes).toBeDefined();
|
|
expect(putDone).toBeDefined();
|
|
|
|
const fastHit = captured.find((e) =>
|
|
e.scope === 'Highway' && e.message.includes('fast-upload hit'));
|
|
expect(fastHit).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
describe('PR5 — MsgPush unknown dispatch logging', () => {
|
|
it('logs an unknown PkgType under [MsgPush.Unknown]', () => {
|
|
const reg = new MsgPushRegistry();
|
|
// No decoders registered → any pkgType is unknown.
|
|
const ctx = {
|
|
head: { msgType: 9999, subType: 0, timestamp: 0 },
|
|
content: new Uint8Array(0),
|
|
identity: {} as any,
|
|
fromUin: 0,
|
|
selfUin: 0,
|
|
} as unknown as MsgPushContext;
|
|
|
|
reg.decode(ctx);
|
|
|
|
const entry = captured.find((e) =>
|
|
e.scope === 'MsgPush.Unknown' && e.message.includes('PkgType=9999'));
|
|
expect(entry).toBeDefined();
|
|
expect(entry!.level).toBe('debug');
|
|
});
|
|
|
|
it('logs an unknown wrapper subType under [MsgPush.Unknown] (Event0x2DC)', () => {
|
|
const ctx = {
|
|
head: { msgType: 732, subType: 999, timestamp: 0 },
|
|
content: new Uint8Array(0),
|
|
identity: {} as any,
|
|
fromUin: 0,
|
|
selfUin: 0,
|
|
} as unknown as MsgPushContext;
|
|
|
|
decodeEvent0x2DC(ctx);
|
|
|
|
const entry = captured.find((e) =>
|
|
e.scope === 'MsgPush.Unknown' && /Event0x2DC unknown subType=999/.test(e.message));
|
|
expect(entry).toBeDefined();
|
|
expect(entry!.level).toBe('debug');
|
|
});
|
|
});
|