Move `packages/core/src/bridge/proto/proton/*.ts` (13 files, ~3.3k LOC) into a new `@snowluma/proto-defs` workspace package. The proton spike landed in the previous commit means bare-specifier imports (`@snowluma/proto-defs/oidb`, `…/message`, …) now resolve correctly through proton's vite plugin, so call-site replacement still fires. Why --- - Lets downstream consumers share the same wire definitions without copy-pasting them — multiple workspace packages can now depend on `@snowluma/proto-defs` directly. - Trims `@snowluma/core`'s surface: the bridge no longer owns the protocol schema, only its consumers. - Sets up future #5 (`message-parsing perf`) and #6 (`Api-on-ctx refactor`) — both want a thinner core. Layout ------ Per-file subpath exports (mirrors the legacy `proto/proton/<sub>` shape 1:1). The barrel `index.ts` intentionally skips `highway` and `oidb-actions/media` because the two files share six NTV2-prefixed type names — `export *` would silently drop one side. Subpath imports are unambiguous, which is also why every call site in core uses them. Changes ------- - New package `packages/proto-defs/` (package.json, tsconfig.json, src/index.ts). - 13 `.ts` files moved via `git mv` to preserve history. - 44 import sites across 26 source files + 15 tests rewritten via sed from `./[…/]proto/proton/<sub>` → `@snowluma/proto-defs/<sub>`. - `@snowluma/core` depends on `@snowluma/proto-defs` (workspace:*). Verification ------------ - proto-defs typecheck: clean - core typecheck: clean - core test suite: 587 passed (same baseline as before the refactor; one failure is the pre-existing websocket native addon, unrelated) - proton test suite: 99 passed (8 files including the new cross-package.test.ts from the spike) - Full workspace `pnpm -r typecheck`: all 8 projects clean Note: proton's `dist/index.js` MUST be rebuilt for the vite plugin to pick up the bare-specifier resolution. The previous commit's source change is only effective after `pnpm -F @snowluma/proton build`.
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 '../src/utils/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');
|
|
});
|
|
});
|