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

Code before:
    it("should extract host from proxy deployment URL", () => {
      const result = extractBaseHost(
        "https://openhands.example.com/runtime/55313/api/conversations/abc123",
      );
      expect(result).toBe("openhands.example.com");
    });

    it("should return window.location.host for relative URLs", () => {
      const result = extractBaseHost("/api/conversations/123");
      expect(result).toBe("localhost:3001");
    });

    it("should return window.location.host for null", () => {
      const result = extractBaseHost(null);
      expect(result).toBe("localhost:3001");
    });

    it("should return window.location.host for undefined", () => {
      const result = extractBaseHost(undefined);
      expect(result).toBe("localhost:3001");
    });

    it("should return window.location.host for invalid URL", () => {
      const result = extractBaseHost("not-a-valid-url");
      expect(result).toBe("localhost:3001");
    });
  });

  describe("extractPathPrefix", () => {
    it("should return empty string for URL without path prefix", () => {
      const result = extractPathPrefix(
        "https://example.com/api/conversations/123",
      );
      expect(result).toBe("");
    });

    it("should extract path prefix from proxy deployment URL", () => {
      const result = extractPathPrefix(
        "https://openhands.example.com/runtime/55313/api/conversations/abc123",
      );
      expect(result).toBe("/runtime/55313");
    });

    it("should handle multiple path segments before /api/conversations", () => {
      const result = extractPathPrefix(
        "https://example.com/prefix/sub/path/api/conversations/123",
      );
      expect(result).toBe("/prefix/sub/path");
    });

    it("should remove trailing slash from path prefix", () => {

Review comment (refactor):
All three test the same fallback behavior. Merge into:

```suggestion
    it("should return window.location.host for null, undefined, or invalid URL", () => {
      expect(extractBaseHost(null)).toBe("localhost:3001");
      expect(extractBaseHost(undefined)).toBe("localhost:3001");
      expect(extractBaseHost("not-a-valid-url")).toBe("localhost:3001");
    });
```

Code after:

    it("should return window.location.host for relative URLs", () => {
      const result = extractBaseHost("/api/conversations/123");
      expect(result).toBe("localhost:3001");
    });

    it("should return window.location.host for null, undefined, or invalid URL", () => {
      expect(extractBaseHost(null)).toBe("localhost:3001");
      expect(extractBaseHost(undefined)).toBe("localhost:3001");
      expect(extractBaseHost("not-a-valid-url")).toBe("localhost:3001");
    });
  });

  describe("extractPathPrefix", () => {
    it("should return empty string for URL without path prefix", () => {
      const result = extractPathPrefix(
        "https://example.com/api/conversations/123",
      );
      expect(result).toBe("");
    });

    it("should extract path prefix from proxy deployment URL", () => {
      const result = extractPathPrefix(
        "https://openhands.example.com/runtime/55313/api/conversations/abc123",
      );
      expect(result).toBe("/runtime/55313");
    });

    it("should handle multiple path segments before /api/conversations", () => {
      const result = extractPathPrefix(
        "https://example.com/prefix/sub/path/api/conversations/123",
      );
      expect(result).toBe("/prefix/sub/path");
    });

    it("should remove trailing slash from path prefix", () => {
      // This test ensures the function handles URLs where the path ends with /
      const result = extractPathPrefix(
        "https://example.com/runtime/55313/api/conversations/123",
      );
      expect(result).not.toMatch(/\/$/);
    });

    it("should return empty string for relative URLs, null, undefined, or invalid URL", () => {
      expect(extractPathPrefix("/api/conversations/123")).toBe("");
      expect(extractPathPrefix(null)).toBe("");
      expect(extractPathPrefix(undefined)).toBe("");
      expect(extractPathPrefix("not-a-valid-url")).toBe("");
    });
  });