Spaces:
Sleeping
Sleeping
File size: 3,971 Bytes
1e7b565 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
/**
* Test setup and utilities for RobotHub TransportServer JS Client Tests
* Equivalent to Python's conftest.py
*/
import { expect } from "bun:test";
// Default server URL for tests
export const TEST_SERVER_URL = "http://localhost:8000";
// Test timeout configuration
export const TEST_TIMEOUT = 10000; // 10 seconds
// Helper to generate test IDs
export function generateTestId(prefix: string = "test"): string {
return `${prefix}-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
}
// Helper to wait for async operations
export function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
// Test workspace and room management
export class TestRoomManager {
private createdRooms: Array<{ workspaceId: string; roomId: string }> = [];
addRoom(workspaceId: string, roomId: string) {
this.createdRooms.push({ workspaceId, roomId });
}
async cleanup(client: any) {
for (const { workspaceId, roomId } of this.createdRooms) {
try {
await client.deleteRoom(workspaceId, roomId);
} catch (error) {
// Ignore cleanup errors
console.warn(`Failed to cleanup room ${roomId}:`, error);
}
}
this.createdRooms = [];
}
}
// Mock frame source for video tests
export async function mockFrameSource(): Promise<ArrayBuffer | null> {
// Create a simple test frame (RGBA data)
const width = 320;
const height = 240;
const size = width * height * 4; // RGBA
const buffer = new ArrayBuffer(size);
const view = new Uint8Array(buffer);
// Fill with test pattern
for (let i = 0; i < size; i += 4) {
view[i] = 255; // R
view[i + 1] = 0; // G
view[i + 2] = 0; // B
view[i + 3] = 255; // A
}
return buffer;
}
// Test assertion helpers
export function assertIsConnected(client: any, workspaceId: string, roomId: string) {
if (!client.isConnected()) {
throw new Error("Expected client to be connected");
}
const info = client.getConnectionInfo();
if (!info.connected) {
throw new Error("Expected connection info to show connected");
}
if (info.workspace_id !== workspaceId) {
throw new Error(`Expected workspace_id ${workspaceId}, got ${info.workspace_id}`);
}
if (info.room_id !== roomId) {
throw new Error(`Expected room_id ${roomId}, got ${info.room_id}`);
}
}
export function assertIsDisconnected(client: any) {
if (client.isConnected()) {
throw new Error("Expected client to be disconnected");
}
const info = client.getConnectionInfo();
if (info.connected) {
throw new Error("Expected connection info to show disconnected");
}
}
// Message collection helper for testing callbacks
export class MessageCollector<T = any> {
private messages: T[] = [];
private promise: Promise<T[]> | null = null;
private resolve: ((value: T[]) => void) | null = null;
constructor(private expectedCount: number = 1) {}
collect = (message: T) => {
this.messages.push(message);
if (this.messages.length >= this.expectedCount && this.resolve) {
this.resolve([...this.messages]);
}
};
async waitForMessages(timeoutMs: number = 5000): Promise<T[]> {
if (this.messages.length >= this.expectedCount) {
return [...this.messages];
}
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
setTimeout(() => {
reject(new Error(`Timeout waiting for ${this.expectedCount} messages, got ${this.messages.length}`));
}, timeoutMs);
});
return this.promise;
}
getMessages(): T[] {
return [...this.messages];
}
clear() {
this.messages = [];
this.promise = null;
this.resolve = null;
}
} |