Open History to see the last commit
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 development: true,
68 });
69 const second = resolveSessionCookieName({
70 mode: "web",
71 port: 5775,
72 host: "127.0.0.1",
73 instanceKey: "/tmp/t3-agent-two",
74 development: true,
75 });
76
77 expect(first).toMatch(/^t3_session_5775_[a-f0-9]{12}$/);
78 expect(second).toMatch(/^t3_session_5775_[a-f0-9]{12}$/);
79 expect(first).not.toBe(second);
80 });
81
82 it("keeps the hosted web cookie stable across server instances", () => {
83 expect(
84 resolveSessionCookieName({
85 mode: "web",
86 port: 8080,
87 host: "0.0.0.0",
88 instanceKey: "/srv/release-a",
89 development: false,
90 }),
91 ).toBe("t3_session");
92 expect(
93 resolveSessionCookieName({
94 mode: "web",
95 port: 9090,
96 host: "app.example.com",
97 instanceKey: "/srv/release-b",
98 development: false,
99 }),
100 ).toBe("t3_session");
101 });
102
103 it("retains desktop port scoping", () => {
104 expect(
105 resolveSessionCookieName({
106 mode: "desktop",
107 port: 3773,
108 host: "127.0.0.1",
109 instanceKey: "/tmp/desktop",
110 development: true,
111 }),
112 ).toBe("t3_session_3773");
113 });
114
115 it("isolates development servers even when they bind a wildcard host", () => {
116 expect(
117 resolveSessionCookieName({
118 mode: "web",
119 port: 5775,
120 host: "0.0.0.0",
121 instanceKey: "/tmp/t3-wildcard-dev",
122 development: true,
123 }),
124 ).toMatch(/^t3_session_5775_[a-f0-9]{12}$/);
125 });
126
127 it("classifies loopback aliases separately from remotely reachable hosts", () => {
128 expect(isRemoteReachableHost(undefined)).toBe(false);
129 expect(isRemoteReachableHost("localhost")).toBe(false);
130 expect(isRemoteReachableHost("127.12.0.1")).toBe(false);
131 expect(isRemoteReachableHost("[::1]")).toBe(false);
132 expect(isRemoteReachableHost("0.0.0.0")).toBe(true);
133 expect(isRemoteReachableHost("192.168.1.50")).toBe(true);
134 });
135});