lint
This commit is contained in:
977 files changed
+198426
-158324
No files matched your search
@@ -27,9 +27,9 @@ import { describe, it, expect, vi } from 'vitest';
|
||||
import { gunzipSync } from 'zlib';
|
||||
|
||||
vi.mock('@snowluma/protocol/bridge-oidb', () => ({
|
||||
makeOidbEnvelope: vi.fn((_oidbCmd, _subCmd, body) => ({ body })),
|
||||
encodeOidbEnv: vi.fn(() => new Uint8Array()),
|
||||
decodeOidbEnv: vi.fn(() => ({ body: {} })),
|
||||
makeOidbEnvelope: vi.fn((_oidbCmd, _subCmd, body) => ({ body })),
|
||||
encodeOidbEnv: vi.fn(() => new Uint8Array()),
|
||||
decodeOidbEnv: vi.fn(() => ({ body: {} })),
|
||||
}));
|
||||
|
||||
// `vi.mock` factories are hoisted above the imports — declare the spy
|
||||
@@ -38,10 +38,10 @@ vi.mock('@snowluma/protocol/bridge-oidb', () => ({
|
||||
// of [elements, ctx] instead of `[]`, which makes the destructuring
|
||||
// a tsc error under strict tuple checking on CI.
|
||||
const { buildSendElemsMock } = vi.hoisted(() => ({
|
||||
buildSendElemsMock: vi.fn(async (_elements: unknown[], _ctx?: Record<string, unknown>) => []),
|
||||
buildSendElemsMock: vi.fn(async (_elements: unknown[], _ctx?: Record<string, unknown>) => []),
|
||||
}));
|
||||
vi.mock('@snowluma/protocol/element-builder', () => ({
|
||||
buildSendElems: buildSendElemsMock,
|
||||
buildSendElems: buildSendElemsMock,
|
||||
}));
|
||||
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
@@ -51,382 +51,477 @@ import { ForwardApi } from '../../src/bridge/apis/forward';
|
||||
import { mockBridge } from './_helpers';
|
||||
|
||||
function uploadResponseWithResId(resId: string) {
|
||||
const encoded = protobuf_encode<SendLongMsgResp>({ result: { resId } });
|
||||
return {
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(encoded),
|
||||
};
|
||||
const encoded = protobuf_encode<SendLongMsgResp>({ result: { resId } });
|
||||
return {
|
||||
success: true,
|
||||
gotResponse: true,
|
||||
errorCode: 0,
|
||||
errorMessage: '',
|
||||
responseData: Buffer.from(encoded),
|
||||
};
|
||||
}
|
||||
|
||||
function decodeLongMsgRequest(rawBytes: Uint8Array): LongMsgResult {
|
||||
const env = protobuf_decode<SendLongMsgReq>(rawBytes);
|
||||
const payload = env.info?.payload;
|
||||
if (!(payload instanceof Uint8Array)) throw new Error('payload missing on SendLongMsgReq');
|
||||
return protobuf_decode<LongMsgResult>(gunzipSync(Buffer.from(payload)));
|
||||
const env = protobuf_decode<SendLongMsgReq>(rawBytes);
|
||||
const payload = env.info?.payload;
|
||||
|
||||
if (!(payload instanceof Uint8Array)) {
|
||||
throw new Error('payload missing on SendLongMsgReq');
|
||||
}
|
||||
|
||||
return protobuf_decode<LongMsgResult>(gunzipSync(Buffer.from(payload)));
|
||||
}
|
||||
|
||||
describe('actions/forward — file segment inside forward node', () => {
|
||||
it('rejects a recursive window shake before identity resolution or upload', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const resolveUserUid = vi.fn();
|
||||
const uploadPrivate = vi.fn();
|
||||
const sendRawPacket = vi.fn();
|
||||
const bridge = mockBridge({
|
||||
resolveUserUid,
|
||||
sendRawPacket: sendRawPacket as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
uploadPrivate,
|
||||
},
|
||||
},
|
||||
it('rejects a recursive window shake before identity resolution or upload', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const resolveUserUid = vi.fn();
|
||||
const uploadPrivate = vi.fn();
|
||||
const sendRawPacket = vi.fn();
|
||||
const bridge = mockBridge({
|
||||
resolveUserUid,
|
||||
sendRawPacket: sendRawPacket as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
uploadPrivate,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await expect(
|
||||
new ForwardApi(bridge as any).upload(
|
||||
[
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'image', url: 'base64://AQID' }],
|
||||
innerForward: [
|
||||
{
|
||||
userUin: 10002,
|
||||
nickname: 'bob',
|
||||
elements: [{ type: 'poke', subType: 1 }],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
undefined,
|
||||
67890,
|
||||
),
|
||||
).rejects.toMatchObject({
|
||||
code: 'UNSENDABLE_TYPE',
|
||||
elementType: 'poke',
|
||||
});
|
||||
|
||||
expect(resolveUserUid).not.toHaveBeenCalled();
|
||||
expect(uploadPrivate).not.toHaveBeenCalled();
|
||||
expect(buildSendElemsMock).not.toHaveBeenCalled();
|
||||
expect(sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
await expect(new ForwardApi(bridge as any).upload([{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'image', url: 'base64://AQID' }],
|
||||
innerForward: [{
|
||||
userUin: 10002,
|
||||
nickname: 'bob',
|
||||
elements: [{ type: 'poke', subType: 1 }],
|
||||
}],
|
||||
}], undefined, 67890)).rejects.toMatchObject({
|
||||
code: 'UNSENDABLE_TYPE',
|
||||
elementType: 'poke',
|
||||
it('group forward sets forwardFake:true on the SendContext so transElem(24) is emitted', async () => {
|
||||
// Group case: the file element rides on elems[] as transElem(24).
|
||||
// The element-builder receives ctx.forwardFake=true so it knows to
|
||||
// emit the receive-side shape instead of dropping the segment.
|
||||
buildSendElemsMock.mockClear();
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-grp-file')) as any,
|
||||
});
|
||||
|
||||
await new ForwardApi(bridge as any).upload(
|
||||
[
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [
|
||||
{ type: 'file', fileId: 'gfid-1', fileName: 'a.txt', fileSize: 99 } as any,
|
||||
],
|
||||
},
|
||||
],
|
||||
12345,
|
||||
);
|
||||
|
||||
expect(buildSendElemsMock).toHaveBeenCalled();
|
||||
const [, ctx] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(ctx).toMatchObject({ groupId: 12345, forwardFake: true });
|
||||
});
|
||||
|
||||
expect(resolveUserUid).not.toHaveBeenCalled();
|
||||
expect(uploadPrivate).not.toHaveBeenCalled();
|
||||
expect(buildSendElemsMock).not.toHaveBeenCalled();
|
||||
expect(sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
it('c2c forward writes the file as msgContent (FileExtra { file: NotOnlineFile })', async () => {
|
||||
// Private case: c2c files live on `body.msgContent`, not in elems[].
|
||||
// The forward-builder pulls the file segment off and synthesises a
|
||||
// FileExtra payload so the recipient's decoder (which reads
|
||||
// msgContent first) renders the file bubble.
|
||||
buildSendElemsMock.mockClear();
|
||||
let captured: Uint8Array | undefined;
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async (_cmd: string, body: Uint8Array) => {
|
||||
captured = body;
|
||||
return uploadResponseWithResId('res-c2c-file');
|
||||
}) as any,
|
||||
// Inline fields cover everything we need; cache lookup must not be
|
||||
// required for the path to work.
|
||||
recallUploadedFile: vi.fn(() => undefined),
|
||||
});
|
||||
|
||||
it('group forward sets forwardFake:true on the SendContext so transElem(24) is emitted', async () => {
|
||||
// Group case: the file element rides on elems[] as transElem(24).
|
||||
// The element-builder receives ctx.forwardFake=true so it knows to
|
||||
// emit the receive-side shape instead of dropping the segment.
|
||||
buildSendElemsMock.mockClear();
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-grp-file')) as any,
|
||||
await new ForwardApi(bridge as any).upload(
|
||||
[
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [
|
||||
{
|
||||
type: 'file',
|
||||
fileId: 'pfid-1',
|
||||
fileName: 'invoice.pdf',
|
||||
fileSize: 4096,
|
||||
md5Hex: 'aabbccddeeff00112233445566778899',
|
||||
fileHash: 'srv-hash-abc',
|
||||
} as any,
|
||||
],
|
||||
},
|
||||
],
|
||||
undefined,
|
||||
67890,
|
||||
);
|
||||
|
||||
expect(buildSendElemsMock).toHaveBeenCalled();
|
||||
const [, ctx] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(ctx).toMatchObject({ forwardFake: true });
|
||||
expect(ctx?.groupId).toBeUndefined();
|
||||
|
||||
expect(captured).toBeDefined();
|
||||
const longMsg = decodeLongMsgRequest(captured!);
|
||||
const body = longMsg.action?.[0]?.actionData?.msgBody?.[0]?.body;
|
||||
expect(body?.msgContent).toBeInstanceOf(Uint8Array);
|
||||
expect(body?.richText?.notOnlineFile ?? undefined).toBeUndefined();
|
||||
|
||||
const fileExtra = protobuf_decode<FileExtra>(body!.msgContent as Uint8Array);
|
||||
expect(fileExtra.file).toMatchObject({
|
||||
fileUuid: 'pfid-1',
|
||||
fileName: 'invoice.pdf',
|
||||
fileHash: 'srv-hash-abc',
|
||||
});
|
||||
expect(fileExtra.file?.fileSize).toBe(4096n);
|
||||
});
|
||||
|
||||
await new ForwardApi(bridge as any).upload([
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', fileId: 'gfid-1', fileName: 'a.txt', fileSize: 99 } as any],
|
||||
},
|
||||
], 12345);
|
||||
it('c2c forward falls back to the upload metadata cache when inline fields are missing', async () => {
|
||||
// The OneBot caller often passes only `file_id` — the rest comes
|
||||
// from the bridge's uploaded-file cache. This is the same
|
||||
// hydration path send_private_msg uses; the forward builder must
|
||||
// honour it too or every cached-only forward ships a 0 B bubble.
|
||||
buildSendElemsMock.mockClear();
|
||||
const recallUploadedFile = vi.fn((id: string) =>
|
||||
id === 'pfid-cached'
|
||||
? {
|
||||
fileId: 'pfid-cached',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'cached-name.zip',
|
||||
fileSize: 8192,
|
||||
fileMd5: Buffer.from('ffeeddccbbaa99887766554433221100', 'hex'),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'cached-hash',
|
||||
rememberedAt: 0,
|
||||
}
|
||||
: undefined,
|
||||
);
|
||||
|
||||
expect(buildSendElemsMock).toHaveBeenCalled();
|
||||
const [, ctx] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(ctx).toMatchObject({ groupId: 12345, forwardFake: true });
|
||||
});
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-cache')) as any,
|
||||
recallUploadedFile,
|
||||
});
|
||||
|
||||
it('c2c forward writes the file as msgContent (FileExtra { file: NotOnlineFile })', async () => {
|
||||
// Private case: c2c files live on `body.msgContent`, not in elems[].
|
||||
// The forward-builder pulls the file segment off and synthesises a
|
||||
// FileExtra payload so the recipient's decoder (which reads
|
||||
// msgContent first) renders the file bubble.
|
||||
buildSendElemsMock.mockClear();
|
||||
let captured: Uint8Array | undefined;
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async (_cmd: string, body: Uint8Array) => {
|
||||
captured = body;
|
||||
return uploadResponseWithResId('res-c2c-file');
|
||||
}) as any,
|
||||
// Inline fields cover everything we need; cache lookup must not be
|
||||
// required for the path to work.
|
||||
recallUploadedFile: vi.fn(() => undefined),
|
||||
await new ForwardApi(bridge as any).upload(
|
||||
[
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', fileId: 'pfid-cached' } as any],
|
||||
},
|
||||
],
|
||||
undefined,
|
||||
67890,
|
||||
);
|
||||
|
||||
expect(recallUploadedFile).toHaveBeenCalledWith('pfid-cached');
|
||||
});
|
||||
|
||||
await new ForwardApi(bridge as any).upload([
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{
|
||||
type: 'file',
|
||||
fileId: 'pfid-1',
|
||||
fileName: 'invoice.pdf',
|
||||
fileSize: 4096,
|
||||
md5Hex: 'aabbccddeeff00112233445566778899',
|
||||
fileHash: 'srv-hash-abc',
|
||||
} as any],
|
||||
},
|
||||
], undefined, 67890);
|
||||
it('group forward pre-uploads file sources into the target group without publishing a separate file message', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const upload = vi.fn(async () => ({ fileId: 'gfid-from-base64' }));
|
||||
const publish = vi.fn();
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-grp-source')) as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
upload,
|
||||
publish,
|
||||
},
|
||||
},
|
||||
recallUploadedFile: vi.fn((id: string) =>
|
||||
id === 'gfid-from-base64'
|
||||
? {
|
||||
fileId: 'gfid-from-base64',
|
||||
scope: 'group' as const,
|
||||
groupId: 12345,
|
||||
fileName: 'note.txt',
|
||||
fileSize: 3,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
rememberedAt: 0,
|
||||
}
|
||||
: undefined,
|
||||
),
|
||||
});
|
||||
|
||||
expect(buildSendElemsMock).toHaveBeenCalled();
|
||||
const [, ctx] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(ctx).toMatchObject({ forwardFake: true });
|
||||
expect(ctx?.groupId).toBeUndefined();
|
||||
await new ForwardApi(bridge as any).upload(
|
||||
[
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', url: 'base64://AQID', fileName: 'note.txt' } as any],
|
||||
},
|
||||
],
|
||||
12345,
|
||||
);
|
||||
|
||||
expect(captured).toBeDefined();
|
||||
const longMsg = decodeLongMsgRequest(captured!);
|
||||
const body = longMsg.action?.[0]?.actionData?.msgBody?.[0]?.body;
|
||||
expect(body?.msgContent).toBeInstanceOf(Uint8Array);
|
||||
expect(body?.richText?.notOnlineFile ?? undefined).toBeUndefined();
|
||||
|
||||
const fileExtra = protobuf_decode<FileExtra>(body!.msgContent as Uint8Array);
|
||||
expect(fileExtra.file).toMatchObject({
|
||||
fileUuid: 'pfid-1',
|
||||
fileName: 'invoice.pdf',
|
||||
fileHash: 'srv-hash-abc',
|
||||
});
|
||||
expect(fileExtra.file?.fileSize).toBe(4096n);
|
||||
});
|
||||
|
||||
it('c2c forward falls back to the upload metadata cache when inline fields are missing', async () => {
|
||||
// The OneBot caller often passes only `file_id` — the rest comes
|
||||
// from the bridge's uploaded-file cache. This is the same
|
||||
// hydration path send_private_msg uses; the forward builder must
|
||||
// honour it too or every cached-only forward ships a 0 B bubble.
|
||||
buildSendElemsMock.mockClear();
|
||||
const recallUploadedFile = vi.fn((id: string) => id === 'pfid-cached' ? {
|
||||
fileId: 'pfid-cached',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'cached-name.zip',
|
||||
fileSize: 8192,
|
||||
fileMd5: Buffer.from('ffeeddccbbaa99887766554433221100', 'hex'),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'cached-hash',
|
||||
rememberedAt: 0,
|
||||
} : undefined);
|
||||
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-cache')) as any,
|
||||
recallUploadedFile,
|
||||
expect(upload).toHaveBeenCalledWith(12345, 'base64://AQID', 'note.txt', '/', true, false);
|
||||
expect(publish).not.toHaveBeenCalled();
|
||||
const [elements] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(elements).toEqual([
|
||||
expect.objectContaining({
|
||||
type: 'file',
|
||||
fileId: 'gfid-from-base64',
|
||||
fileName: 'note.txt',
|
||||
fileSize: 3,
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
await new ForwardApi(bridge as any).upload([{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', fileId: 'pfid-cached' } as any],
|
||||
}], undefined, 67890);
|
||||
it('private forward pre-uploads file sources without sending a standalone c2c file message', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const uploadPrivate = vi.fn(async () => ({
|
||||
fileId: 'pfid-from-base64',
|
||||
fileHash: 'phash',
|
||||
}));
|
||||
const sendC2cFile = vi.fn();
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-c2c-source')) as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
message: {
|
||||
...mockBridge().apis.message,
|
||||
sendC2cFile,
|
||||
},
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
uploadPrivate,
|
||||
},
|
||||
},
|
||||
recallUploadedFile: vi.fn((id: string) =>
|
||||
id === 'pfid-from-base64'
|
||||
? {
|
||||
fileId: 'pfid-from-base64',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'note.txt',
|
||||
fileSize: 3,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'phash',
|
||||
rememberedAt: 0,
|
||||
}
|
||||
: undefined,
|
||||
),
|
||||
});
|
||||
|
||||
expect(recallUploadedFile).toHaveBeenCalledWith('pfid-cached');
|
||||
});
|
||||
await new ForwardApi(bridge as any).upload(
|
||||
[
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', url: 'base64://AQID', fileName: 'note.txt' } as any],
|
||||
},
|
||||
],
|
||||
undefined,
|
||||
67890,
|
||||
);
|
||||
|
||||
it('group forward pre-uploads file sources into the target group without publishing a separate file message', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const upload = vi.fn(async () => ({ fileId: 'gfid-from-base64' }));
|
||||
const publish = vi.fn();
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-grp-source')) as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
upload,
|
||||
publish,
|
||||
},
|
||||
},
|
||||
recallUploadedFile: vi.fn((id: string) => id === 'gfid-from-base64' ? {
|
||||
fileId: 'gfid-from-base64',
|
||||
scope: 'group' as const,
|
||||
groupId: 12345,
|
||||
fileName: 'note.txt',
|
||||
fileSize: 3,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
rememberedAt: 0,
|
||||
} : undefined),
|
||||
expect(uploadPrivate).toHaveBeenCalledWith(67890, 'base64://AQID', 'note.txt', true, false);
|
||||
expect(sendC2cFile).not.toHaveBeenCalled();
|
||||
const [elements] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(elements).toEqual([
|
||||
expect.objectContaining({
|
||||
type: 'file',
|
||||
fileId: 'pfid-from-base64',
|
||||
fileName: 'note.txt',
|
||||
fileSize: 3,
|
||||
fileHash: 'phash',
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
await new ForwardApi(bridge as any).upload([{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', url: 'base64://AQID', fileName: 'note.txt' } as any],
|
||||
}], 12345);
|
||||
it('rejects two files in one private forward node before uploading either file', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const uploadPrivate = vi.fn();
|
||||
const sendRawPacket = vi.fn();
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: sendRawPacket as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
uploadPrivate,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(upload).toHaveBeenCalledWith(12345, 'base64://AQID', 'note.txt', '/', true, false);
|
||||
expect(publish).not.toHaveBeenCalled();
|
||||
const [elements] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(elements).toEqual([expect.objectContaining({
|
||||
type: 'file',
|
||||
fileId: 'gfid-from-base64',
|
||||
fileName: 'note.txt',
|
||||
fileSize: 3,
|
||||
})]);
|
||||
});
|
||||
await expect(
|
||||
new ForwardApi(bridge as any).upload(
|
||||
[
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [
|
||||
{ type: 'file', url: 'base64://AQID', fileName: 'one.txt' },
|
||||
{ type: 'file', url: 'base64://BAUG', fileName: 'two.txt' },
|
||||
] as any,
|
||||
},
|
||||
],
|
||||
undefined,
|
||||
67890,
|
||||
),
|
||||
).rejects.toMatchObject({
|
||||
code: 'UNSENDABLE_TYPE',
|
||||
elementType: 'file',
|
||||
});
|
||||
|
||||
it('private forward pre-uploads file sources without sending a standalone c2c file message', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const uploadPrivate = vi.fn(async () => ({ fileId: 'pfid-from-base64', fileHash: 'phash' }));
|
||||
const sendC2cFile = vi.fn();
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-c2c-source')) as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
message: {
|
||||
...mockBridge().apis.message,
|
||||
sendC2cFile,
|
||||
},
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
uploadPrivate,
|
||||
},
|
||||
},
|
||||
recallUploadedFile: vi.fn((id: string) => id === 'pfid-from-base64' ? {
|
||||
fileId: 'pfid-from-base64',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'note.txt',
|
||||
fileSize: 3,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'phash',
|
||||
rememberedAt: 0,
|
||||
} : undefined),
|
||||
expect(uploadPrivate).not.toHaveBeenCalled();
|
||||
expect(buildSendElemsMock).not.toHaveBeenCalled();
|
||||
expect(sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
await new ForwardApi(bridge as any).upload([{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', url: 'base64://AQID', fileName: 'note.txt' } as any],
|
||||
}], undefined, 67890);
|
||||
it('private file forward keeps the long-msg upload under the self uid namespace', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
let captured: Uint8Array | undefined;
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async (_cmd: string, body: Uint8Array) => {
|
||||
captured = body;
|
||||
return uploadResponseWithResId('res-c2c-target-uid');
|
||||
}) as any,
|
||||
resolveUserUid: vi.fn(async () => 'target-uid'),
|
||||
recallUploadedFile: vi.fn((id: string) =>
|
||||
id === 'pfid-1'
|
||||
? {
|
||||
fileId: 'pfid-1',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'invoice.pdf',
|
||||
fileSize: 4096,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'srv-hash',
|
||||
rememberedAt: 0,
|
||||
}
|
||||
: undefined,
|
||||
),
|
||||
});
|
||||
|
||||
expect(uploadPrivate).toHaveBeenCalledWith(67890, 'base64://AQID', 'note.txt', true, false);
|
||||
expect(sendC2cFile).not.toHaveBeenCalled();
|
||||
const [elements] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(elements).toEqual([expect.objectContaining({
|
||||
type: 'file',
|
||||
fileId: 'pfid-from-base64',
|
||||
fileName: 'note.txt',
|
||||
fileSize: 3,
|
||||
fileHash: 'phash',
|
||||
})]);
|
||||
});
|
||||
await new ForwardApi(bridge as any).upload(
|
||||
[
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', fileId: 'pfid-1' } as any],
|
||||
},
|
||||
],
|
||||
undefined,
|
||||
67890,
|
||||
);
|
||||
|
||||
it('rejects two files in one private forward node before uploading either file', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const uploadPrivate = vi.fn();
|
||||
const sendRawPacket = vi.fn();
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: sendRawPacket as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
uploadPrivate,
|
||||
},
|
||||
},
|
||||
expect(bridge.resolveUserUid).not.toHaveBeenCalled();
|
||||
expect(captured).toBeDefined();
|
||||
const request = protobuf_decode<SendLongMsgReq>(captured!);
|
||||
expect(request.info?.uid?.uid).toBe('self-uid');
|
||||
});
|
||||
|
||||
await expect(new ForwardApi(bridge as any).upload([{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [
|
||||
{ type: 'file', url: 'base64://AQID', fileName: 'one.txt' },
|
||||
{ type: 'file', url: 'base64://BAUG', fileName: 'two.txt' },
|
||||
] as any,
|
||||
}], undefined, 67890)).rejects.toMatchObject({
|
||||
code: 'UNSENDABLE_TYPE',
|
||||
elementType: 'file',
|
||||
it('group forward re-uploads a cached private file_id into the target group scope', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const getPrivateUrl = vi.fn(async () => 'https://download.test/private-file');
|
||||
const upload = vi.fn(async () => ({ fileId: 'gfid-reuploaded' }));
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-cross-scope')) as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
getPrivateUrl,
|
||||
upload,
|
||||
},
|
||||
},
|
||||
recallUploadedFile: vi.fn((id: string) => {
|
||||
if (id === 'pfid-cached') {
|
||||
return {
|
||||
fileId: 'pfid-cached',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'cached.txt',
|
||||
fileSize: 5,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'cached-hash',
|
||||
rememberedAt: 0,
|
||||
};
|
||||
}
|
||||
|
||||
if (id === 'gfid-reuploaded') {
|
||||
return {
|
||||
fileId: 'gfid-reuploaded',
|
||||
scope: 'group' as const,
|
||||
groupId: 12345,
|
||||
fileName: 'cached.txt',
|
||||
fileSize: 5,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
rememberedAt: 1,
|
||||
};
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}),
|
||||
});
|
||||
|
||||
await new ForwardApi(bridge as any).upload(
|
||||
[
|
||||
{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', fileId: 'pfid-cached' } as any],
|
||||
},
|
||||
],
|
||||
12345,
|
||||
);
|
||||
|
||||
expect(getPrivateUrl).toHaveBeenCalledWith(67890, 'pfid-cached', 'cached-hash');
|
||||
expect(upload).toHaveBeenCalledWith(
|
||||
12345,
|
||||
'https://download.test/private-file',
|
||||
'cached.txt',
|
||||
'/',
|
||||
true,
|
||||
false,
|
||||
);
|
||||
const [elements] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(elements).toEqual([
|
||||
expect.objectContaining({
|
||||
type: 'file',
|
||||
fileId: 'gfid-reuploaded',
|
||||
fileName: 'cached.txt',
|
||||
fileSize: 5,
|
||||
}),
|
||||
]);
|
||||
});
|
||||
|
||||
expect(uploadPrivate).not.toHaveBeenCalled();
|
||||
expect(buildSendElemsMock).not.toHaveBeenCalled();
|
||||
expect(sendRawPacket).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('private file forward keeps the long-msg upload under the self uid namespace', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
let captured: Uint8Array | undefined;
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async (_cmd: string, body: Uint8Array) => {
|
||||
captured = body;
|
||||
return uploadResponseWithResId('res-c2c-target-uid');
|
||||
}) as any,
|
||||
resolveUserUid: vi.fn(async () => 'target-uid'),
|
||||
recallUploadedFile: vi.fn((id: string) => id === 'pfid-1' ? {
|
||||
fileId: 'pfid-1',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'invoice.pdf',
|
||||
fileSize: 4096,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'srv-hash',
|
||||
rememberedAt: 0,
|
||||
} : undefined),
|
||||
});
|
||||
|
||||
await new ForwardApi(bridge as any).upload([{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', fileId: 'pfid-1' } as any],
|
||||
}], undefined, 67890);
|
||||
|
||||
expect(bridge.resolveUserUid).not.toHaveBeenCalled();
|
||||
expect(captured).toBeDefined();
|
||||
const request = protobuf_decode<SendLongMsgReq>(captured!);
|
||||
expect(request.info?.uid?.uid).toBe('self-uid');
|
||||
});
|
||||
|
||||
it('group forward re-uploads a cached private file_id into the target group scope', async () => {
|
||||
buildSendElemsMock.mockClear();
|
||||
const getPrivateUrl = vi.fn(async () => 'https://download.test/private-file');
|
||||
const upload = vi.fn(async () => ({ fileId: 'gfid-reuploaded' }));
|
||||
const bridge = mockBridge({
|
||||
sendRawPacket: vi.fn(async () => uploadResponseWithResId('res-cross-scope')) as any,
|
||||
apis: {
|
||||
...mockBridge().apis,
|
||||
groupFile: {
|
||||
...mockBridge().apis.groupFile,
|
||||
getPrivateUrl,
|
||||
upload,
|
||||
},
|
||||
},
|
||||
recallUploadedFile: vi.fn((id: string) => {
|
||||
if (id === 'pfid-cached') {
|
||||
return {
|
||||
fileId: 'pfid-cached',
|
||||
scope: 'private' as const,
|
||||
userId: 67890,
|
||||
fileName: 'cached.txt',
|
||||
fileSize: 5,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
fileHash: 'cached-hash',
|
||||
rememberedAt: 0,
|
||||
};
|
||||
}
|
||||
if (id === 'gfid-reuploaded') {
|
||||
return {
|
||||
fileId: 'gfid-reuploaded',
|
||||
scope: 'group' as const,
|
||||
groupId: 12345,
|
||||
fileName: 'cached.txt',
|
||||
fileSize: 5,
|
||||
fileMd5: new Uint8Array(16),
|
||||
fileSha1: new Uint8Array(20),
|
||||
rememberedAt: 1,
|
||||
};
|
||||
}
|
||||
return undefined;
|
||||
}),
|
||||
});
|
||||
|
||||
await new ForwardApi(bridge as any).upload([{
|
||||
userUin: 10001,
|
||||
nickname: 'alice',
|
||||
elements: [{ type: 'file', fileId: 'pfid-cached' } as any],
|
||||
}], 12345);
|
||||
|
||||
expect(getPrivateUrl).toHaveBeenCalledWith(67890, 'pfid-cached', 'cached-hash');
|
||||
expect(upload).toHaveBeenCalledWith(12345, 'https://download.test/private-file', 'cached.txt', '/', true, false);
|
||||
const [elements] = buildSendElemsMock.mock.calls[0]!;
|
||||
expect(elements).toEqual([expect.objectContaining({
|
||||
type: 'file',
|
||||
fileId: 'gfid-reuploaded',
|
||||
fileName: 'cached.txt',
|
||||
fileSize: 5,
|
||||
})]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user