1import { describe, expect, it, vi } from "vite-plus/test";
2
3import { connectionFloatingStatus } from "./floating-working-status";
4
5const status = (
6 connectionState: Parameters<typeof connectionFloatingStatus>[0]["connectionState"],
7 overrides: { connectionError?: string | null; environmentLabel?: string | null } = {},
8) =>
9 connectionFloatingStatus({
10 connectionError: overrides.connectionError ?? null,
11 connectionState,
12 environmentLabel:
13 overrides.environmentLabel === undefined ? "Mac mini" : overrides.environmentLabel,
14 onReconnect: () => {},
15 });
16
17describe("connectionFloatingStatus", () => {
18 it("yields the pill to sync and working state once connected", () => {
19 expect(status("connected")).toBeNull();
20 });
21
22 it("names the environment it is retrying, and says so only after a failure", () => {
23 expect(status("connecting")).toMatchObject({
24 tone: "reconnecting",
25 label: "Reconnecting to Mac mini...",
26 });
27 expect(status("reconnecting", { connectionError: "ECONNREFUSED" })).toMatchObject({
28 tone: "reconnecting",
29 label: "Failed to connect. Retrying Mac mini...",
30 });
31 });
32
33 it("reports why the environment is unreachable", () => {
34 expect(status("offline")).toMatchObject({
35 tone: "unavailable",
36 label: "You are offline",
37 });
38 expect(status("available")).toMatchObject({
39 tone: "unavailable",
40 label: "Mac mini is not connected",
41 });
42 expect(status("error", { connectionError: "handshake timed out" })).toMatchObject({
43 label: "Failed to connect to Mac mini: handshake timed out",
44 });
45 expect(status("error")).toMatchObject({ label: "Failed to connect to Mac mini" });
46 });
47
48 it("falls back to a generic name when the environment has no label", () => {
49 expect(status("error", { environmentLabel: null })).toMatchObject({
50 label: "Failed to connect to Environment",
51 });
52 });
53
54 it("carries the reconnect handler so the pill can trigger it", () => {
55 const onReconnect = vi.fn();
56 const pill = connectionFloatingStatus({
57 connectionError: null,
58 connectionState: "offline",
59 environmentLabel: "Mac mini",
60 onReconnect,
61 });
62 if (pill?.kind !== "connection") throw new Error("expected a connection pill");
63 pill.onPress();
64 expect(onReconnect).toHaveBeenCalledOnce();
65 });
66});