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 { T3_CONNECT_TROUBLESHOOTING_SECTIONS } from "@t3tools/contracts";
5import { describe, expect, it } from "@effect/vitest";
6
7import { classifyReachability, type ReachabilityFacts } from "./ConnectReachability.ts";
8
9const checkedAt = "2026-10-19T00:00:00.000Z";
10const connected = { readyConnections: 4, hostname: "env.example.test", protocol: "quic" };
11const notConnected = { readyConnections: 0, hostname: null, protocol: null };
12const facts = (overrides: Partial<ReachabilityFacts>): ReachabilityFacts => ({
13 managedTunnelConfigured: true,
14 connector: connected,
15 endpoint: null,
16 edgeReachable: null,
17 exitNode: null,
18 ...overrides,
19});
20
21describe("classifyReachability", () => {
22 it.each([
23 [{ kind: "status", status: 502 }, "origin_unreachable"],
24 [{ kind: "status", status: 530 }, "no_connector"],
25 [{ kind: "network_error" }, "timeout"],
26 ] as const)("maps endpoint result %o to %s", (endpoint, cause) => {
27 expect(classifyReachability(facts({ endpoint, edgeReachable: true }), checkedAt)).toMatchObject(
28 { status: "unreachable", cause },
29 );
30 });
31
32 it("keeps the endpoint cause when the TCP edge test cannot explain it", () => {
33 // QUIC (UDP) can connect while TCP 7844 is blocked.
34 expect(
35 classifyReachability(
36 facts({ endpoint: { kind: "status", status: 502 }, edgeReachable: false }),
37 checkedAt,
38 ),
39 ).toMatchObject({ cause: "origin_unreachable" });
40 expect(
41 classifyReachability(
42 facts({
43 connector: { readyConnections: null, hostname: "env.example.test", protocol: null },
44 endpoint: { kind: "status", status: 502 },
45 edgeReachable: false,
46 }),
47 checkedAt,
48 ),
49 ).toMatchObject({ cause: "origin_unreachable" });
50 // Failed DNS also fails the edge lookups.
51 expect(
52 classifyReachability(
53 facts({
54 connector: { readyConnections: null, hostname: "env.example.test", protocol: null },
55 endpoint: { kind: "dns_failed" },
56 edgeReachable: false,
57 }),
58 checkedAt,
59 ),
60 ).toMatchObject({ cause: "dns_failed" });
61 });
62
63 it("waits on a connector that has not connected while the edge answers", () => {
64 expect(
65 classifyReachability(facts({ connector: notConnected, edgeReachable: true }), checkedAt),
66 ).toMatchObject({ cause: "connector_not_ready" });
67 });
68});
69
70describe("T3 Connect troubleshooting links", () => {
71 it("each section names a heading in the user guide", () => {
72 const guide = NodeFS.readFileSync(
73 new URL("../../../../docs/user/t3-connect-troubleshooting.md", import.meta.url),
74 "utf8",
75 );
76 // GitHub's heading anchors: lowercase, drop punctuation, spaces become hyphens.
77 const anchors = new Set(
78 [...guide.matchAll(/^#{2,3} (.+)$/gm)].map(([, heading]) =>
79 heading!
80 .toLowerCase()
81 .replace(/[^\p{L}\p{N} _-]/gu, "")
82 .replace(/ /g, "-"),
83 ),
84 );
85 for (const section of T3_CONNECT_TROUBLESHOOTING_SECTIONS) {
86 expect(anchors, section).toContain(section);
87 }
88 });
89});