1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 buildConnectAuthorizeRequestUrl,
5 connectLoopbackRedirectUri,
6 readConnectAuthorizeRequest,
7} from "./connectAuth.ts";
8
9describe("connectAuth", () => {
10 it("round-trips state, challenge, and loopback port through the authorize URL fragment", () => {
11 const url = buildConnectAuthorizeRequestUrl({
12 hostedAppUrl: "https://app.t3.codes",
13 state: "q7mK9xV2pL4nR8sT6wYzAQ",
14 challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
15 loopbackPort: 34338,
16 });
17 const parsed = new URL(url);
18
19 expect(parsed.origin).toBe("https://app.t3.codes");
20 expect(parsed.pathname).toBe("/connect");
21 expect(parsed.search).toBe("");
22 expect(readConnectAuthorizeRequest(parsed)).toEqual({
23 state: "q7mK9xV2pL4nR8sT6wYzAQ",
24 challenge: "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM",
25 loopbackPort: 34338,
26 });
27 expect(connectLoopbackRedirectUri(34338)).toBe("http://127.0.0.1:34338/callback");
28 });
29
30 it("rejects authorize requests missing state, challenge, or port", () => {
31 expect(readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect"))).toBeNull();
32 expect(
33 readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect#state=abc&port=34338")),
34 ).toBeNull();
35 expect(
36 readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect#challenge=abc&port=34338")),
37 ).toBeNull();
38 expect(
39 readConnectAuthorizeRequest(new URL("https://app.t3.codes/connect#state=abc&challenge=abc")),
40 ).toBeNull();
41 });
42
43 it("rejects authorize requests whose loopback port is corrupted", () => {
44 for (const port of ["", "abc", "-1", "0", "65536", "34338x", "34 38"]) {
45 const url = new URL(
46 `https://app.t3.codes/connect#state=state-1&challenge=challenge-1&port=${encodeURIComponent(port)}`,
47 );
48 expect(readConnectAuthorizeRequest(url), port).toBeNull();
49 }
50 });
51});