Repository: All-Hands-AI/OpenHands
File: frontend/__tests__/utils/websocket-url.test.ts (TypeScript)

Code before:
      );
      expect(result).toBe("http://localhost:3000");
    });

    it("should fallback to window.location for null URL", () => {
      const result = buildHttpBaseUrl(null);
      expect(result).toBe("https://localhost:3001");
    });
  });

  describe("buildWebSocketUrl", () => {
    it("should return null when conversationId is undefined", () => {
      const result = buildWebSocketUrl(
        undefined,
        "https://example.com/api/conversations/123",
      );
      expect(result).toBeNull();
    });

    it("should return null when conversationId is empty string", () => {
      const result = buildWebSocketUrl(
        "",
        "https://example.com/api/conversations/123",
      );
      expect(result).toBeNull();
    });

    it("should build WebSocket URL without path prefix", () => {
      const result = buildWebSocketUrl(
        "conv-123",
        "https://example.com/api/conversations/conv-123",
      );
      expect(result).toBe("wss://example.com/sockets/events/conv-123");
    });

    it("should build WebSocket URL with path prefix for proxy deployment", () => {
      const result = buildWebSocketUrl(
        "abc123",
        "https://openhands.example.com/runtime/55313/api/conversations/abc123",
      );
      expect(result).toBe(
        "wss://openhands.example.com/runtime/55313/sockets/events/abc123",
      );
    });

    it("should use ws protocol when window.location.protocol is http:", () => {
      window.location.protocol = "http:";
      const result = buildWebSocketUrl(
        "conv-123",
        "http://localhost:3000/api/conversations/conv-123",
      );

Review comment (suggestion):
```suggestion
    it("should return null when conversationId is undefined or empty", () => {
      expect(
        buildWebSocketUrl(
          undefined,
          "https://example.com/api/conversations/123",
        ),
      ).toBeNull();
      expect(
        buildWebSocketUrl("", "https://example.com/api/conversations/123"),
      ).toBeNull();
    });
```

Code after:
    it("should build WebSocket URL with path prefix for proxy deployment", () => {
      const result = buildWebSocketUrl(
        "abc123",
        "https://openhands.example.com/runtime/55313/api/conversations/abc123",
      );
      expect(result).toBe(
        "wss://openhands.example.com/runtime/55313/sockets/events/abc123",
      );
    });

    it("should use ws protocol when window.location.protocol is http:", () => {
      vi.stubGlobal("location", {
        host: "localhost:3001",
        protocol: "http:",
      });

      const result = buildWebSocketUrl(
        "conv-123",
        "http://localhost:3000/api/conversations/conv-123",
      );
      expect(result).toBe("ws://localhost:3000/sockets/events/conv-123");
    });

    it("should fallback to window.location.host for null URL", () => {
      const result = buildWebSocketUrl("conv-123", null);
      expect(result).toBe("wss://localhost:3001/sockets/events/conv-123");
    });

    it("should handle complex path prefixes", () => {
      const result = buildWebSocketUrl(
        "test-conv",
        "https://app.example.com/org/team/runtime/12345/api/conversations/test-conv",
      );
      expect(result).toBe(
        "wss://app.example.com/org/team/runtime/12345/sockets/events/test-conv",
      );
    });
  });
});