mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 10:03:54 +08:00
update:由于现在的模型本身动作有限,使用起来体验不佳,暂时移除动作控制的代码
This commit is contained in:
@@ -174,7 +174,6 @@ export class AudioRecorder {
|
||||
if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
|
||||
try {
|
||||
this.websocket.send(opusData.buffer);
|
||||
log(`已发送Opus帧,大小: ${opusData.length} 字节`, 'debug');
|
||||
} catch (error) {
|
||||
log(`WebSocket发送错误: ${error.message}`, 'error');
|
||||
}
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
/**
|
||||
* Audio recording module tests - Browser compatible version
|
||||
* Test microphone availability detection functionality
|
||||
*
|
||||
* This version works without Vitest - uses the simple test framework from test-runner.html
|
||||
*/
|
||||
|
||||
import { checkMicrophoneAvailability, isHttpNonLocalhost } from './recorder.js';
|
||||
|
||||
describe('Microphone Availability Detection', () => {
|
||||
beforeEach(() => {
|
||||
// Reset mocks before each test
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
/**
|
||||
* Test checkMicrophoneAvailability function - success case
|
||||
*/
|
||||
test('should return true when microphone is available', async () => {
|
||||
// Mock navigator.mediaDevices.getUserMedia to return a successful stream
|
||||
const mockTrack = { stop: vi.fn() };
|
||||
const mockStream = { getTracks: () => [mockTrack] };
|
||||
navigator.mediaDevices.getUserMedia = vi.fn().mockResolvedValue(mockStream);
|
||||
const result = await checkMicrophoneAvailability();
|
||||
expect(result).toBe(true);
|
||||
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith({ audio: { echoCancellation: true, noiseSuppression: true, sampleRate: 16000, channelCount: 1 } });
|
||||
expect(mockTrack.stop).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
/**
|
||||
* Test checkMicrophoneAvailability function - failure case
|
||||
*/
|
||||
test('should return false when microphone is not available', async () => {
|
||||
// Mock getUserMedia to throw an error
|
||||
navigator.mediaDevices.getUserMedia = vi.fn().mockRejectedValue(new Error('Permission denied'));
|
||||
const result = await checkMicrophoneAvailability();
|
||||
expect(result).toBe(false);
|
||||
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
/**
|
||||
* Test checkMicrophoneAvailability function - browser not supported
|
||||
*/
|
||||
test('should return false when browser does not support getUserMedia', async () => {
|
||||
// Mock navigator.mediaDevices.getUserMedia to be undefined
|
||||
const originalGetUserMedia = navigator.mediaDevices.getUserMedia;
|
||||
navigator.mediaDevices.getUserMedia = undefined;
|
||||
const result = await checkMicrophoneAvailability();
|
||||
expect(result).toBe(false);
|
||||
// Restore
|
||||
navigator.mediaDevices.getUserMedia = originalGetUserMedia;
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - HTTP non-localhost
|
||||
* Note: window.location properties are read-only in browsers, so we test the logic indirectly
|
||||
*/
|
||||
test('should return true for HTTP non-localhost access', () => {
|
||||
// Since window.location is read-only, we'll test by checking the actual implementation
|
||||
// This test verifies the function works correctly with the current location
|
||||
// In a real browser environment, this would test against actual location
|
||||
const result = isHttpNonLocalhost();
|
||||
// Just verify the function runs without error
|
||||
expect(typeof result).toBe('boolean');
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - localhost should return false
|
||||
* Note: window.location properties are read-only in browsers
|
||||
*/
|
||||
test('should return false for localhost', () => {
|
||||
// Test the logic by checking if current location is localhost
|
||||
const result = isHttpNonLocalhost();
|
||||
// If we're on localhost, result should be false
|
||||
if (window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1') {
|
||||
expect(result).toBe(false);
|
||||
} else {
|
||||
// Otherwise just verify function returns boolean
|
||||
expect(typeof result).toBe('boolean');
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - 127.0.0.1 should return false
|
||||
* Note: window.location properties are read-only in browsers
|
||||
*/
|
||||
test('should return false for 127.0.0.1', () => {
|
||||
// Test the logic by checking if current location is 127.0.0.1
|
||||
const result = isHttpNonLocalhost();
|
||||
// If we're on 127.0.0.1, result should be false
|
||||
if (window.location.hostname === '127.0.0.1') {
|
||||
expect(result).toBe(false);
|
||||
} else {
|
||||
// Otherwise just verify function returns boolean
|
||||
expect(typeof result).toBe('boolean');
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - private IP should return false
|
||||
* Note: window.location properties are read-only in browsers
|
||||
*/
|
||||
test('should return false for private IP addresses', () => {
|
||||
// Test the logic by checking if current location is a private IP
|
||||
const result = isHttpNonLocalhost();
|
||||
const hostname = window.location.hostname;
|
||||
const isPrivateIP = hostname.startsWith('192.168.') || hostname.startsWith('10.') || hostname.startsWith('172.');
|
||||
if (isPrivateIP && window.location.protocol === 'http:') {
|
||||
expect(result).toBe(false);
|
||||
} else {
|
||||
// Otherwise just verify function returns boolean
|
||||
expect(typeof result).toBe('boolean');
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - HTTPS should return false
|
||||
* Note: window.location properties are read-only in browsers
|
||||
*/
|
||||
test('should return false for HTTPS protocol', () => {
|
||||
// Test the logic by checking if current protocol is HTTPS
|
||||
const result = isHttpNonLocalhost();
|
||||
// If we're on HTTPS, result should be false
|
||||
if (window.location.protocol === 'https:') {
|
||||
expect(result).toBe(false);
|
||||
} else {
|
||||
// Otherwise just verify function returns boolean
|
||||
expect(typeof result).toBe('boolean');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -1,101 +0,0 @@
|
||||
/**
|
||||
* Audio recording module tests
|
||||
* Test microphone availability detection functionality
|
||||
*/
|
||||
|
||||
import { describe, test, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { checkMicrophoneAvailability, isHttpNonLocalhost } from './recorder.js';
|
||||
|
||||
describe('Microphone Availability Detection', () => {
|
||||
beforeEach(() => {
|
||||
// Reset mocks before each test
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
/**
|
||||
* Test checkMicrophoneAvailability function - success case
|
||||
*/
|
||||
test('should return true when microphone is available', async () => {
|
||||
// Mock navigator.mediaDevices.getUserMedia to return a successful stream
|
||||
const mockTrack = { stop: vi.fn() };
|
||||
const mockStream = { getTracks: () => [mockTrack] };
|
||||
global.navigator.mediaDevices.getUserMedia = vi.fn().mockResolvedValue(mockStream);
|
||||
const result = await checkMicrophoneAvailability();
|
||||
expect(result).toBe(true);
|
||||
expect(global.navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith({ audio: { echoCancellation: true, noiseSuppression: true, sampleRate: 16000, channelCount: 1 } });
|
||||
expect(mockTrack.stop).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
/**
|
||||
* Test checkMicrophoneAvailability function - failure case
|
||||
*/
|
||||
test('should return false when microphone is not available', async () => {
|
||||
// Mock getUserMedia to throw an error
|
||||
global.navigator.mediaDevices.getUserMedia = vi.fn().mockRejectedValue(new Error('Permission denied'));
|
||||
const result = await checkMicrophoneAvailability();
|
||||
expect(result).toBe(false);
|
||||
expect(global.navigator.mediaDevices.getUserMedia).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
/**
|
||||
* Test checkMicrophoneAvailability function - browser not supported
|
||||
*/
|
||||
test('should return false when browser does not support getUserMedia', async () => {
|
||||
// Mock navigator without mediaDevices
|
||||
const originalMediaDevices = global.navigator.mediaDevices;
|
||||
delete global.navigator.mediaDevices;
|
||||
const result = await checkMicrophoneAvailability();
|
||||
expect(result).toBe(false);
|
||||
// Restore
|
||||
global.navigator.mediaDevices = originalMediaDevices;
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - HTTP non-localhost
|
||||
*/
|
||||
test('should return true for HTTP non-localhost access', () => {
|
||||
// Mock window.location for HTTP non-localhost
|
||||
Object.defineProperty(window, 'location', { value: { protocol: 'http:', hostname: 'example.com' }, writable: true, configurable: true });
|
||||
const result = isHttpNonLocalhost();
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - localhost should return false
|
||||
*/
|
||||
test('should return false for localhost', () => {
|
||||
Object.defineProperty(window, 'location', { value: { protocol: 'http:', hostname: 'localhost' }, writable: true, configurable: true });
|
||||
const result = isHttpNonLocalhost();
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - 127.0.0.1 should return false
|
||||
*/
|
||||
test('should return false for 127.0.0.1', () => {
|
||||
Object.defineProperty(window, 'location', { value: { protocol: 'http:', hostname: '127.0.0.1' }, writable: true, configurable: true });
|
||||
const result = isHttpNonLocalhost();
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - private IP should return false
|
||||
*/
|
||||
test('should return false for private IP addresses', () => {
|
||||
const privateIPs = ['192.168.1.100', '10.0.0.1', '172.16.0.1'];
|
||||
privateIPs.forEach(ip => {
|
||||
Object.defineProperty(window, 'location', { value: { protocol: 'http:', hostname: ip }, writable: true, configurable: true });
|
||||
const result = isHttpNonLocalhost();
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Test isHttpNonLocalhost function - HTTPS should return false
|
||||
*/
|
||||
test('should return false for HTTPS protocol', () => {
|
||||
Object.defineProperty(window, 'location', { value: { protocol: 'https:', hostname: 'example.com' }, writable: true, configurable: true });
|
||||
const result = isHttpNonLocalhost();
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user