Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - reads the user guide to check its anchors.
2import * as NodeFS from "node:fs";
3
4import { formatConnectReachable, T3_CONNECT_TROUBLESHOOTING_SECTIONS } from "@t3tools/contracts";
5import { describe, expect, it } from "@effect/vitest";
6
7import {
8 classifyReachability,
9 formatReachabilityFailure,
10 type ReachabilityFacts,
11} from "./ConnectReachability.ts";
12
13const checkedAt = "2026-10-19T00:00:00.000Z";
14const connected = { readyConnections: 4, hostname: "env.example.test", protocol: "quic" };
15const notConnected = { readyConnections: 0, hostname: null, protocol: null };
16const facts = (overrides: Partial<ReachabilityFacts>): ReachabilityFacts => ({
17 managedTunnelConfigured: true,
18 connector: connected,
19 endpoint: null,
20 edgeReachable: null,
21 exitNode: null,
22 ...overrides,
23});
24
25describe("classifyReachability", () => {
26 it("blames the exit node when the tunnel edge times out through it", () => {
27 const result = classifyReachability(
28 facts({
29 connector: notConnected,
30 edgeReachable: false,
31 exitNode: { name: "home-pi", online: true },
32 }),
33 checkedAt,
34 );
35
36 expect(result.status === "unreachable" && formatReachabilityFailure(result)).toBe(
37 [
38 "✕ T3 Connect is NOT reachable from other devices",
39 " Cloudflare's tunnel port (7844) times out from this machine.",
40 ' Tailscale exit node "home-pi" is on. Traffic to Cloudflare goes through it.',
41 " Fix: tailscale set --exit-node= (or allow outbound 7844 on that network)",
42 " More info: https://github.com/pingdotgg/t3code/blob/main/docs/user/t3-connect-troubleshooting.md#port-7844-is-blocked-on-the-host",
43 ].join("\n"),
44 );
45 });
46
47 it("names a generic blocker when no exit node is on", () => {
48 const result = classifyReachability(
49 facts({ connector: notConnected, edgeReachable: false }),
50 checkedAt,
51 );
52
53 expect(result).toMatchObject({
54 cause: "edge_unreachable",
55 hint: "A VPN, proxy, or firewall may block it.",
56 });
57 });
58
59 it.each([
60 [{ kind: "status", status: 502 }, "origin_unreachable"],
61 [{ kind: "status", status: 530 }, "no_connector"],
62 [{ kind: "status", status: 404 }, "unexpected_response"],
63 [{ kind: "dns_failed" }, "dns_failed"],
64 [{ kind: "network_error" }, "timeout"],
65 ] as const)("maps endpoint result %o to %s", (endpoint, cause) => {
66 expect(classifyReachability(facts({ endpoint, edgeReachable: true }), checkedAt)).toMatchObject(
67 { status: "unreachable", cause },
68 );
69 });
70
71 it("keeps the endpoint cause when the TCP edge test cannot explain it", () => {
72 // QUIC (UDP) can connect while TCP 7844 is blocked.
73 expect(
74 classifyReachability(
75 facts({ endpoint: { kind: "status", status: 502 }, edgeReachable: false }),
76 checkedAt,
77 ),
78 ).toMatchObject({ cause: "origin_unreachable" });
79 expect(
80 classifyReachability(
81 facts({
82 connector: { readyConnections: null, hostname: "env.example.test", protocol: null },
83 endpoint: { kind: "status", status: 502 },
84 edgeReachable: false,
85 }),
86 checkedAt,
87 ),
88 ).toMatchObject({ cause: "origin_unreachable" });
89 // Failed DNS also fails the edge lookups.
90 expect(
91 classifyReachability(
92 facts({
93 connector: { readyConnections: null, hostname: "env.example.test", protocol: null },
94 endpoint: { kind: "dns_failed" },
95 edgeReachable: false,
96 }),
97 checkedAt,
98 ),
99 ).toMatchObject({ cause: "dns_failed" });
100 });
101
102 it("waits on a connector that has not connected while the edge answers", () => {
103 expect(
104 classifyReachability(facts({ connector: notConnected, edgeReachable: true }), checkedAt),
105 ).toMatchObject({ cause: "connector_not_ready" });
106 });
107
108 it("reports connections and protocol when the endpoint answers", () => {
109 expect(
110 classifyReachability(facts({ endpoint: { kind: "status", status: 200 } }), checkedAt),
111 ).toEqual({ status: "reachable", checkedAt, readyConnections: 4, protocol: "quic" });
112 });
113
114 it("leaves out the connection count when cloudflared did not report one", () => {
115 const result = classifyReachability(
116 facts({
117 connector: { readyConnections: null, hostname: "env.example.test", protocol: null },
118 endpoint: { kind: "status", status: 200 },
119 }),
120 checkedAt,
121 );
122 expect(result.status === "reachable" && formatConnectReachable(result)).toBe(
123 "Tunnel connected",
124 );
125 });
126
127 it("does not probe a tunnel that was never set up", () => {
128 expect(
129 classifyReachability(facts({ managedTunnelConfigured: false, connector: null }), checkedAt),
130 ).toMatchObject({ cause: "not_linked" });
131 });
132});
133
134describe("T3 Connect troubleshooting links", () => {
135 it("each section names a heading in the user guide", () => {
136 const guide = NodeFS.readFileSync(
137 new URL("../../../../docs/user/t3-connect-troubleshooting.md", import.meta.url),
138 "utf8",
139 );
140 // GitHub's heading anchors: lowercase, drop punctuation, spaces become hyphens.
141 const anchors = new Set(
142 [...guide.matchAll(/^#{2,3} (.+)$/gm)].map(([, heading]) =>
143 heading!
144 .toLowerCase()
145 .replace(/[^\p{L}\p{N} _-]/gu, "")
146 .replace(/ /g, "-"),
147 ),
148 );
149 for (const section of T3_CONNECT_TROUBLESHOOTING_SECTIONS) {
150 expect(anchors, section).toContain(section);
151 }
152 });
153});