1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 deriveAuthClientMetadata,
5 isRemoteReachableHost,
6 resolveSessionCookieName,
7} from "./utils.ts";
8
9describe("deriveAuthClientMetadata", () => {
10 it("labels Electron user agents as Electron instead of Chrome", () => {
11 const metadata = deriveAuthClientMetadata({
12 request: {
13 headers: {
14 "user-agent":
15 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) t3code/0.0.15 Chrome/136.0.7103.93 Electron/36.3.2 Safari/537.36",
16 },
17 source: {
18 remoteAddress: "::ffff:127.0.0.1",
19 },
20 } as never,
21 });
22
23 expect(metadata).toMatchObject({
24 browser: "Electron",
25 deviceType: "desktop",
26 ipAddress: "127.0.0.1",
27 os: "macOS",
28 });
29 });
30
31 it("applies client-presented display identity without replacing transport metadata", () => {
32 const metadata = deriveAuthClientMetadata({
33 request: {
34 headers: {
35 "user-agent":
36 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/136.0.7103.93 Electron/36.3.2 Safari/537.36",
37 },
38 source: {
39 remoteAddress: "::ffff:192.168.213.72",
40 },
41 } as never,
42 presented: {
43 label: "T3 Code Mobile",
44 deviceType: "mobile",
45 os: "iOS",
46 },
47 });
48
49 expect(metadata).toMatchObject({
50 label: "T3 Code Mobile",
51 browser: "Electron",
52 deviceType: "mobile",
53 ipAddress: "192.168.213.72",
54 os: "iOS",
55 });
56 expect(metadata.userAgent).toContain("Electron/36.3.2");
57 });
58});
59
60describe("session cookie isolation", () => {
61 it("isolates loopback web servers by port and server state", () => {
62 const first = resolveSessionCookieName({
63 mode: "web",
64 port: 5775,
65 host: "127.0.0.1",
66 instanceKey: "/tmp/t3-agent-one",
67 environmentId: "environment-one",
68 development: true,
69 });
70 const second = resolveSessionCookieName({
71 mode: "web",
72 port: 5775,
73 host: "127.0.0.1",
74 instanceKey: "/tmp/t3-agent-two",
75 environmentId: "environment-two",
76 development: true,
77 });
78
79 expect(first).toMatch(/^t3_session_5775_[a-f0-9]{12}$/);
80 expect(second).toMatch(/^t3_session_5775_[a-f0-9]{12}$/);
81 expect(first).not.toBe(second);
82 });
83
84 it("isolates remote web servers by server state", () => {
85 const first = resolveSessionCookieName({
86 mode: "web",
87 port: 3773,
88 host: "192.168.1.50",
89 instanceKey: "/srv/t3-one",
90 environmentId: "environment-one",
91 development: false,
92 });
93 const second = resolveSessionCookieName({
94 mode: "web",
95 port: 5775,
96 host: "192.168.1.50",
97 instanceKey: "/srv/t3-two",
98 environmentId: "environment-two",
99 development: false,
100 });
101
102 expect(first).toMatch(/^t3_session_[a-f0-9]{12}$/);
103 expect(second).toMatch(/^t3_session_[a-f0-9]{12}$/);
104 expect(first).not.toBe(second);
105 });
106
107 it("keeps a remote web server cookie stable across port changes", () => {
108 const first = resolveSessionCookieName({
109 mode: "web",
110 port: 8080,
111 host: "0.0.0.0",
112 instanceKey: "/srv/t3",
113 environmentId: "environment-one",
114 development: false,
115 });
116 const second = resolveSessionCookieName({
117 mode: "web",
118 port: 9090,
119 host: "app.example.com",
120 instanceKey: "/srv/t3",
121 environmentId: "environment-one",
122 development: false,
123 });
124
125 expect(first).toBe(second);
126 });
127
128 it("retains desktop port scoping", () => {
129 expect(
130 resolveSessionCookieName({
131 mode: "desktop",
132 port: 3773,
133 host: "127.0.0.1",
134 instanceKey: "/tmp/desktop",
135 environmentId: "environment-one",
136 development: true,
137 }),
138 ).toBe("t3_session_3773");
139 });
140
141 it("isolates development servers even when they bind a wildcard host", () => {
142 expect(
143 resolveSessionCookieName({
144 mode: "web",
145 port: 5775,
146 host: "0.0.0.0",
147 instanceKey: "/tmp/t3-wildcard-dev",
148 environmentId: "environment-one",
149 development: true,
150 }),
151 ).toMatch(/^t3_session_5775_[a-f0-9]{12}$/);
152 });
153
154 it("classifies loopback aliases separately from remotely reachable hosts", () => {
155 expect(isRemoteReachableHost(undefined)).toBe(false);
156 expect(isRemoteReachableHost("localhost")).toBe(false);
157 expect(isRemoteReachableHost("127.12.0.1")).toBe(false);
158 expect(isRemoteReachableHost("[::1]")).toBe(false);
159 expect(isRemoteReachableHost("0.0.0.0")).toBe(true);
160 expect(isRemoteReachableHost("192.168.1.50")).toBe(true);
161 });
162});