File size: 3,156 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it, vi } from "vitest";
import { resolveGatewayBindUrl } from "./gateway-bind-url.js";

describe("shared/gateway-bind-url", () => {
  it("returns null for loopback/default binds", () => {
    const pickTailnetHost = vi.fn(() => "100.64.0.1");
    const pickLanHost = vi.fn(() => "192.168.1.2");

    expect(
      resolveGatewayBindUrl({
        scheme: "ws",
        port: 18789,
        pickTailnetHost,
        pickLanHost,
      }),
    ).toBeNull();
    expect(pickTailnetHost).not.toHaveBeenCalled();
    expect(pickLanHost).not.toHaveBeenCalled();
  });

  it("resolves custom binds only when custom host is present after trimming", () => {
    const pickTailnetHost = vi.fn();
    const pickLanHost = vi.fn();

    expect(
      resolveGatewayBindUrl({
        bind: "custom",
        customBindHost: " gateway.local ",
        scheme: "wss",
        port: 443,
        pickTailnetHost,
        pickLanHost,
      }),
    ).toEqual({
      url: "wss://gateway.local:443",
      source: "gateway.bind=custom",
    });

    expect(
      resolveGatewayBindUrl({
        bind: "custom",
        customBindHost: "   ",
        scheme: "ws",
        port: 18789,
        pickTailnetHost,
        pickLanHost,
      }),
    ).toEqual({
      error: "gateway.bind=custom requires gateway.customBindHost.",
    });
    expect(pickTailnetHost).not.toHaveBeenCalled();
    expect(pickLanHost).not.toHaveBeenCalled();
  });

  it("resolves tailnet and lan binds or returns clear errors", () => {
    expect(
      resolveGatewayBindUrl({
        bind: "tailnet",
        scheme: "ws",
        port: 18789,
        pickTailnetHost: () => "100.64.0.1",
        pickLanHost: vi.fn(),
      }),
    ).toEqual({
      url: "ws://100.64.0.1:18789",
      source: "gateway.bind=tailnet",
    });
    expect(
      resolveGatewayBindUrl({
        bind: "tailnet",
        scheme: "ws",
        port: 18789,
        pickTailnetHost: () => null,
        pickLanHost: vi.fn(),
      }),
    ).toEqual({
      error: "gateway.bind=tailnet set, but no tailnet IP was found.",
    });

    expect(
      resolveGatewayBindUrl({
        bind: "lan",
        scheme: "wss",
        port: 8443,
        pickTailnetHost: vi.fn(),
        pickLanHost: () => "192.168.1.2",
      }),
    ).toEqual({
      url: "wss://192.168.1.2:8443",
      source: "gateway.bind=lan",
    });
    expect(
      resolveGatewayBindUrl({
        bind: "lan",
        scheme: "ws",
        port: 18789,
        pickTailnetHost: vi.fn(),
        pickLanHost: () => null,
      }),
    ).toEqual({
      error: "gateway.bind=lan set, but no private LAN IP was found.",
    });
  });

  it("returns null for unrecognized bind values without probing pickers", () => {
    const pickTailnetHost = vi.fn(() => "100.64.0.1");
    const pickLanHost = vi.fn(() => "192.168.1.2");

    expect(
      resolveGatewayBindUrl({
        bind: "loopbackish",
        scheme: "ws",
        port: 18789,
        pickTailnetHost,
        pickLanHost,
      }),
    ).toBeNull();
    expect(pickTailnetHost).not.toHaveBeenCalled();
    expect(pickLanHost).not.toHaveBeenCalled();
  });
});