Skip to content

Commit c849f1e

Browse files
committed
test(s3): align checks with stream lifecycle
1 parent 6643028 commit c849f1e

4 files changed

Lines changed: 30 additions & 26 deletions

File tree

packages/service/test/core/workflow/dispatch/ai/toolcall/toolCall.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,10 @@ describe('runToolCall compression node responses', () => {
866866
}
867867
];
868868
runAgentLoopMock.mockImplementation(async (options) => {
869-
options.onAfterToolCall({
869+
options.runtime.emitEvent({
870+
type: 'tool_run_end',
870871
call,
872+
rawResponse: '[{"fileUrl":"https://files/report","filename":"report.csv"}]',
871873
response: '[{"fileUrl":"https://files/report","filename":"report.csv"}]',
872874
seconds: 0.1,
873875
fileRefs

projects/app/test/api/system/file/sourceContentType.test.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Readable } from 'node:stream';
1+
import { PassThrough, Readable } from 'node:stream';
22
import { beforeEach, describe, expect, it, vi } from 'vitest';
33
import proxyDownloadHandler from '@/pages/api/system/file/download/[token]';
44
import proxyUploadHandler from '@/pages/api/system/file/upload/[token]';
@@ -29,37 +29,36 @@ vi.mock('@fastgpt/service/common/s3/sources/chat', () => ({
2929
}));
3030

3131
const makeMockStream = () => {
32-
const stream = {
33-
pipe: vi.fn(),
34-
on: vi.fn(() => stream)
35-
};
32+
const stream = Readable.from([Buffer.from('mock file content')]);
33+
vi.spyOn(stream, 'pipe');
3634
return stream;
3735
};
3836

37+
const makeMockReq = (overrides: Record<string, unknown>) => ({
38+
aborted: false,
39+
once: vi.fn(),
40+
off: vi.fn(),
41+
...overrides
42+
});
43+
3944
const makeMockRes = () => {
4045
const headers: Record<string, string | number> = {};
41-
const res = {
46+
const res = Object.assign(new PassThrough(), {
4247
headers,
4348
statusCode: 200,
4449
headersSent: false,
45-
writableFinished: false,
4650
setHeader: vi.fn((key: string, value: string | number) => {
4751
headers[key] = value;
4852
}),
4953
getHeader: vi.fn((key: string) => headers[key]),
50-
once: vi.fn(() => res),
5154
status: vi.fn((statusCode: number) => {
5255
res.statusCode = statusCode;
5356
return res;
5457
}),
55-
end: vi.fn(() => {
56-
res.writableFinished = true;
57-
}),
5858
json: vi.fn(() => {
59-
res.writableFinished = true;
60-
}),
61-
destroy: vi.fn()
62-
};
59+
res.end();
60+
})
61+
});
6362
return res;
6463
};
6564

@@ -88,19 +87,19 @@ describe('system file response content type', () => {
8887
type: 'download'
8988
});
9089

91-
const req = {
90+
const req = makeMockReq({
9291
method: 'GET',
9392
url: '/api/system/file/download/token',
9493
headers: {},
9594
query: { token: 'token' }
96-
} as any;
95+
}) as any;
9796
const res = makeMockRes() as any;
9897

9998
await proxyDownloadHandler(req, res);
10099

101100
expect(res.headers['Content-Type']).toBe('text/markdown; charset=utf-8');
102101
expect(res.headers['Content-Length']).toBe(32);
103-
expect(stream.pipe).toHaveBeenCalledWith(res);
102+
expect(stream.pipe).toHaveBeenCalledWith(res, expect.any(Object));
104103
});
105104

106105
it('keeps binary content type unchanged in proxy download mode', async () => {
@@ -122,12 +121,12 @@ describe('system file response content type', () => {
122121
type: 'download'
123122
});
124123

125-
const req = {
124+
const req = makeMockReq({
126125
method: 'GET',
127126
url: '/api/system/file/download/token',
128127
headers: {},
129128
query: { token: 'token' }
130-
} as any;
129+
}) as any;
131130
const res = makeMockRes() as any;
132131

133132
await proxyDownloadHandler(req, res);
@@ -193,25 +192,29 @@ describe('system file response content type', () => {
193192
it('adds utf-8 charset for text files in legacy file entry', async () => {
194193
const stream = makeMockStream();
195194
const datasetSource = {
195+
bucketName: 'fastgpt-private',
196196
getFileStream: vi.fn().mockResolvedValue(stream),
197197
getFileMetadata: vi.fn().mockResolvedValue({
198198
filename: 'page.html',
199199
contentType: 'text/html',
200200
contentLength: 128
201201
})
202202
};
203+
(global as any).s3BucketMap = {
204+
'fastgpt-private': datasetSource
205+
};
203206
vi.mocked(getS3DatasetSource).mockReturnValue(datasetSource as any);
204207
vi.mocked(getS3ChatSource).mockReturnValue({} as any);
205208
vi.mocked(verifyToken).mockResolvedValue({
206209
objectKey: 'dataset/team/page.html'
207210
});
208211

209-
const req = { query: { jwt: 'jwt' } } as any;
212+
const req = makeMockReq({ query: { jwt: 'jwt' } }) as any;
210213
const res = makeMockRes() as any;
211214

212215
await legacyFileHandler(req, res);
213216

214217
expect(res.headers['Content-Type']).toBe('text/html; charset=utf-8');
215-
expect(stream.pipe).toHaveBeenCalledWith(res);
218+
expect(stream.pipe).toHaveBeenCalledWith(res, expect.any(Object));
216219
});
217220
});

projects/app/test/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@fastgpt/*": ["../../../packages/*"],
2626
"@test/*": ["../../../test/*"],
2727

28-
"#fastgpt/app/test/*": ["./*"],
28+
"#fastgpt/app/test/*": ["./*"]
2929
}
3030
},
3131
"include": ["**/*.test.ts", "../../../packages/service/**/*.d.ts"],

vitest.config.mts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ export default defineConfig({
2222
process.env.FILE_TOKEN_KEY ??
2323
'bfd697e7e798f75deaf2d31210bc93a2e41ad4eed9e7831071d77821b7b97cff',
2424
AES256_SECRET_KEY: process.env.AES256_SECRET_KEY ?? 'fastgpt_test_aes256_secret_key',
25-
INVOKE_TOKEN_SECRET:
26-
process.env.INVOKE_TOKEN_SECRET ?? 'fastgpt_test_invoke_token_secret_32'
25+
INVOKE_TOKEN_SECRET: process.env.INVOKE_TOKEN_SECRET ?? 'fastgpt_test_invoke_token_secret_32'
2726
},
2827
coverage: {
2928
enabled: true,

0 commit comments

Comments
 (0)