File size: 7,692 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
 * Tests for RoboticsProducer - equivalent to Python's test_producer.py
 */
import { test, expect, describe, beforeEach, afterEach } from "bun:test";
import { robotics } from "../src/index";
import { TEST_SERVER_URL, TestRoomManager, MessageCollector, sleep, assertIsConnected, assertIsDisconnected } from "./setup";

const { RoboticsProducer } = robotics;

describe("RoboticsProducer", () => {
    let producer: robotics.RoboticsProducer;
    let roomManager: TestRoomManager;

    beforeEach(() => {
        producer = new RoboticsProducer(TEST_SERVER_URL);
        roomManager = new TestRoomManager();
    });

    afterEach(async () => {
        if (producer.isConnected()) {
            await producer.disconnect();
        }
        await roomManager.cleanup(producer);
    });

    test("producer connection", async () => {
        // Create room first
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);

        expect(producer.isConnected()).toBe(false);

        const success = await producer.connect(workspaceId, roomId);
        expect(success).toBe(true);
        assertIsConnected(producer, workspaceId, roomId);

        const info = producer.getConnectionInfo();
        expect(info.role).toBe("producer");

        await producer.disconnect();
        assertIsDisconnected(producer);
    });

    test("producer connection info", async () => {
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);
        await producer.connect(workspaceId, roomId);

        const info = producer.getConnectionInfo();
        expect(info.connected).toBe(true);
        expect(info.room_id).toBe(roomId);
        expect(info.workspace_id).toBe(workspaceId);
        expect(info.role).toBe("producer");
        expect(info.participant_id).toBeTruthy();
        expect(info.base_url).toBe(TEST_SERVER_URL);
    });

    test("send joint update", async () => {
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);
        await producer.connect(workspaceId, roomId);

        const joints = [
            { name: "shoulder", value: 45.0 },
            { name: "elbow", value: -20.0 },
            { name: "wrist", value: 10.0 }
        ];

        // Should not throw an exception
        await producer.sendJointUpdate(joints);
    });

    test("send state sync", async () => {
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);
        await producer.connect(workspaceId, roomId);

        const state = { shoulder: 45.0, elbow: -20.0, wrist: 10.0 };

        // Should not throw an exception
        await producer.sendStateSync(state);
    });

    test("send emergency stop", async () => {
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);
        await producer.connect(workspaceId, roomId);

        // Should not throw an exception
        await producer.sendEmergencyStop("Test emergency stop");
        await producer.sendEmergencyStop(); // Default reason
    });

    test("producer callbacks", async () => {
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);

        let connectedCalled = false;
        let disconnectedCalled = false;
        let errorCalled = false;
        let errorMessage: string | null = null;

        producer.onConnected(() => {
            connectedCalled = true;
        });

        producer.onDisconnected(() => {
            disconnectedCalled = true;
        });

        producer.onError((error) => {
            errorCalled = true;
            errorMessage = error;
        });

        // Connect and disconnect
        await producer.connect(workspaceId, roomId);
        await sleep(100); // Give callbacks time to execute
        expect(connectedCalled).toBe(true);

        await producer.disconnect();
        await sleep(100); // Give callbacks time to execute
        expect(disconnectedCalled).toBe(true);
    });

    test("send without connection", async () => {
        expect(producer.isConnected()).toBe(false);

        await expect(producer.sendJointUpdate([{ name: "test", value: 0 }]))
            .rejects.toThrow("Must be connected");

        await expect(producer.sendStateSync({ test: 0 }))
            .rejects.toThrow("Must be connected");

        await expect(producer.sendEmergencyStop())
            .rejects.toThrow("Must be connected");
    });

    test("multiple connections", async () => {
        // Connect to first room
        const { workspaceId: workspaceId1, roomId: roomId1 } = await producer.createRoom();
        roomManager.addRoom(workspaceId1, roomId1);
        await producer.connect(workspaceId1, roomId1);
        
        expect(producer.getConnectionInfo().room_id).toBe(roomId1);
        expect(producer.getConnectionInfo().workspace_id).toBe(workspaceId1);

        // Create second room
        const { workspaceId: workspaceId2, roomId: roomId2 } = await producer.createRoom();
        roomManager.addRoom(workspaceId2, roomId2);

        // Connect to second room (should disconnect from first)
        await producer.connect(workspaceId2, roomId2);
        expect(producer.getConnectionInfo().room_id).toBe(roomId2);
        expect(producer.getConnectionInfo().workspace_id).toBe(workspaceId2);
        expect(producer.isConnected()).toBe(true);
    });

    test("duplicate producer connection", async () => {
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);
        
        const producer2 = new RoboticsProducer(TEST_SERVER_URL);

        try {
            // First producer connects successfully
            const success1 = await producer.connect(workspaceId, roomId);
            expect(success1).toBe(true);

            // Second producer should fail to connect as producer
            const success2 = await producer2.connect(workspaceId, roomId);
            expect(success2).toBe(false); // Should fail since room already has producer
        } finally {
            if (producer2.isConnected()) {
                await producer2.disconnect();
            }
        }
    });

    test("custom participant id", async () => {
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);
        const customId = "custom-producer-123";

        await producer.connect(workspaceId, roomId, customId);

        const info = producer.getConnectionInfo();
        expect(info.participant_id).toBe(customId);
    });

    test("large joint update", async () => {
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);
        await producer.connect(workspaceId, roomId);

        // Create a large joint update
        const joints = Array.from({ length: 100 }, (_, i) => ({
            name: `joint_${i}`,
            value: i
        }));

        // Should handle large updates without issue
        await producer.sendJointUpdate(joints);
    });

    test("rapid updates", async () => {
        const { workspaceId, roomId } = await producer.createRoom();
        roomManager.addRoom(workspaceId, roomId);
        await producer.connect(workspaceId, roomId);

        // Send multiple rapid updates
        for (let i = 0; i < 10; i++) {
            await producer.sendStateSync({ joint1: i, joint2: i * 2 });
            await sleep(10); // Small delay
        }
    });
});