Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

cliAuthFormat.test.ts

126 lines3.8 KB

1import { expect, it } from "@effect/vitest";
2import * as DateTime from "effect/DateTime";
3​
4import {
5 formatIssuedPairingCredential,
6 formatIssuedSession,
7 formatPairingCredentialList,
8 formatSessionList,
9} from "./cliAuthFormat.ts";
10​
11it("formats issued pairing credentials with the secret and optional pair URL", () => {
12 const output = formatIssuedPairingCredential(
13 {
14 id: "pairing-1",
15 credential: "secret-pairing-token",
16 scopes: ["orchestration:read"],
17 subject: "one-time-token",
18 createdAt: DateTime.makeUnsafe("2026-04-08T09:00:00.000Z"),
19 expiresAt: DateTime.makeUnsafe("2026-04-08T10:00:00.000Z"),
20 },
21 { baseUrl: "https://example.com", json: false },
22 );
23​
24 expect(output).toContain("secret-pairing-token");
25 expect(output).toContain("https://example.com/pair#token=secret-pairing-token");
26 // Scannable from another device, matching `t3 serve`'s startup output.
27 expect(output).toContain("█");
28});
29​
30it("omits the QR code when there is no pair URL worth scanning", () => {
31 const output = formatIssuedPairingCredential(
32 {
33 id: "pairing-1",
34 credential: "secret-pairing-token",
35 scopes: ["orchestration:read"],
36 subject: "one-time-token",
37 createdAt: DateTime.makeUnsafe("2026-04-08T09:00:00.000Z"),
38 expiresAt: DateTime.makeUnsafe("2026-04-08T10:00:00.000Z"),
39 },
40 { json: false },
41 );
42​
43 expect(output).toContain("secret-pairing-token");
44 expect(output).not.toContain("█");
45});
46​
47it("keeps JSON output machine-readable rather than embedding a QR code", () => {
48 const output = formatIssuedPairingCredential(
49 {
50 id: "pairing-1",
51 credential: "secret-pairing-token",
52 scopes: ["orchestration:read"],
53 subject: "one-time-token",
54 createdAt: DateTime.makeUnsafe("2026-04-08T09:00:00.000Z"),
55 expiresAt: DateTime.makeUnsafe("2026-04-08T10:00:00.000Z"),
56 },
57 { baseUrl: "https://example.com", json: true },
58 );
59​
60 expect(output).not.toContain("█");
61 expect(JSON.parse(output)).toMatchObject({
62 pairUrl: "https://example.com/pair#token=secret-pairing-token",
63 });
64});
65​
66it("formats pairing listings without exposing the secret token", () => {
67 const output = formatPairingCredentialList(
68 [
69 {
70 id: "pairing-1",
71 credential: "secret-pairing-token",
72 subject: "one-time-token",
73 label: "Phone",
74 scopes: ["orchestration:read"],
75 createdAt: DateTime.makeUnsafe("2026-04-08T09:00:00.000Z"),
76 expiresAt: DateTime.makeUnsafe("2026-04-08T10:00:00.000Z"),
77 },
78 ],
79 { json: false },
80 );
81​
82 expect(output).toContain("pairing-1");
83 expect(output).not.toContain("secret-pairing-token");
84});
85​
86it("formats issued sessions with the bearer token but omits tokens from listings", () => {
87 const issuedOutput = formatIssuedSession(
88 {
89 sessionId: "session-1" as never,
90 token: "secret-session-token",
91 method: "bearer-access-token",
92 scopes: ["orchestration:read", "access:write"],
93 subject: "cli-issued-session",
94 client: {
95 label: "deploy-bot",
96 deviceType: "bot",
97 },
98 expiresAt: DateTime.makeUnsafe("2026-04-08T10:00:00.000Z"),
99 },
100 { json: false },
101 );
102​
103 const listedOutput = formatSessionList(
104 [
105 {
106 sessionId: "session-1" as never,
107 method: "bearer-access-token",
108 scopes: ["orchestration:read", "access:write"],
109 subject: "cli-issued-session",
110 client: {
111 label: "deploy-bot",
112 deviceType: "bot",
113 },
114 connected: false,
115 current: false,
116 issuedAt: DateTime.makeUnsafe("2026-04-08T09:00:00.000Z"),
117 expiresAt: DateTime.makeUnsafe("2026-04-08T10:00:00.000Z"),
118 lastConnectedAt: null,
119 },
120 ],
121 { json: false },
122 );
123​
124 expect(issuedOutput).toContain("secret-session-token");
125 expect(listedOutput).not.toContain("secret-session-token");
126});