项目文件夹

文件
Mukunda Rao Katta 3a24d71f2c test: save WebP responses with the right extension (#1901)
## Summary
- fix the WebP MIME type match in daemon response handling
- save  responses with a  suffix instead of falling back to 
- add a regression test for WebP image handling

## Testing
- npm run build
- node --test --test-name-pattern="parsing"
build/tests/daemon/client.test.js

Fixes #1898
2026-04-20 12:05:47 +00:00

127 行
3.6 KiB
TypeScript

/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import {describe, it, afterEach, beforeEach} from 'node:test';
import {
handleResponse,
startDaemon,
stopDaemon,
} from '../../src/daemon/client.js';
import {isDaemonRunning} from '../../src/daemon/utils.js';
describe('daemon client', () => {
describe('start/stop', () => {
beforeEach(async () => {
await stopDaemon();
});
afterEach(async () => {
await stopDaemon();
});
it('should start and stop daemon', async () => {
assert.ok(!isDaemonRunning(), 'Daemon should not be running initially');
await startDaemon();
assert.ok(isDaemonRunning(), 'Daemon should be running after start');
await stopDaemon();
assert.ok(!isDaemonRunning(), 'Daemon should not be running after stop');
});
it('should handle starting daemon when already running', async () => {
await startDaemon();
assert.ok(isDaemonRunning(), 'Daemon should be running');
// Starting again should be a no-op
await startDaemon();
assert.ok(isDaemonRunning(), 'Daemon should still be running');
});
it('should handle stopping daemon when not running', async () => {
assert.ok(!isDaemonRunning(), 'Daemon should not be running initially');
// Stopping when not running should be a no-op
await stopDaemon();
assert.ok(!isDaemonRunning(), 'Daemon should still not be running');
});
});
describe('parsing', () => {
it('handles MCP response with text format', async () => {
const textResponse = {content: [{type: 'text' as const, text: 'test'}]};
assert.strictEqual(await handleResponse(textResponse, 'md'), 'test');
});
it('handles JSON response', async () => {
const jsonResponse = {
content: [],
structuredContent: {
test: 'data',
number: 123,
},
};
assert.strictEqual(
await handleResponse(jsonResponse, 'json'),
JSON.stringify(jsonResponse.structuredContent),
);
});
it('handles error response when isError is true', async () => {
const errorResponse = {
isError: true,
content: [{type: 'text' as const, text: 'Something went wrong'}],
};
assert.strictEqual(
await handleResponse(errorResponse, 'md'),
JSON.stringify(errorResponse.content),
);
});
it('handles text response when json format is requested but no structured content', async () => {
const textResponse = {
content: [{type: 'text' as const, text: 'Fall through text'}],
};
assert.deepStrictEqual(
await handleResponse(textResponse, 'json'),
JSON.stringify(['Fall through text']),
);
});
it('supports images', async () => {
const unsupportedContentResponse = {
content: [
{
type: 'image' as const,
data: 'base64data',
mimeType: 'image/png',
},
],
structuredContent: {},
};
const response = await handleResponse(unsupportedContentResponse, 'md');
assert.ok(response.includes('.png'));
});
it('uses the webp extension for WebP images', async () => {
const webpContentResponse = {
content: [
{
type: 'image' as const,
data: 'base64data',
mimeType: 'image/webp',
},
],
structuredContent: {},
};
const response = await handleResponse(webpContentResponse, 'md');
assert.ok(response.includes('.webp'));
});
});
});