1
10import { describe, it, expect } from "vitest";
11import { realtimeUrl } from "../realtime";
12
13describe("realtimeUrl", () => {
14 it("appends the /realtime path (the server's only accepted path)", () => {
15 expect(realtimeUrl("tok", "ws://localhost:4001")).toBe(
16 "ws://localhost:4001/realtime?token=tok",
17 );
18 });
19
20 it("token-only URL is still on the /realtime path", () => {
21 expect(realtimeUrl(null, "ws://localhost:4001")).toBe(
22 "ws://localhost:4001/realtime",
23 );
24 expect(realtimeUrl(undefined, "ws://localhost:4001")).toBe(
25 "ws://localhost:4001/realtime",
26 );
27 });
28
29 it("percent-encodes the token (JWTs are URL-safe, but be exact)", () => {
30 expect(realtimeUrl("a b/c+d", "ws://localhost:4001")).toBe(
31 "ws://localhost:4001/realtime?token=a%20b%2Fc%2Bd",
32 );
33 });
34
35 it("trims a trailing slash on the base (NEXT_PUBLIC_WS_URL hygiene)", () => {
36 expect(realtimeUrl("t", "ws://localhost:4001/")).toBe(
37 "ws://localhost:4001/realtime?token=t",
38 );
39 expect(realtimeUrl("t", "ws://localhost:4001//")).toBe(
40 "ws://localhost:4001/realtime?token=t",
41 );
42 });
43
44 it("works with the production-shape base (origin only, per constants.ts)", () => {
45
46
47 expect(realtimeUrl("t", "ws://realtime.factory.example")).toBe(
48 "ws://realtime.factory.example/realtime?token=t",
49 );
50 });
51});
52