The best way to control Claude Code, Codex and more from any computer or phone

main
ts 185 lines 7.12 KB
Raw
1 import {
2 SecretProviderId,
3 SecretProviderKind,
4 SecretRequestError,
5 ThreadId,
6 TurnItemId,
7 } from "@t3tools/contracts";
8 import { describe, expect, it } from "vite-plus/test";
9
10 import {
11 secretRequestAnswerInput,
12 secretRequestDisplay,
13 secretRequestFailureMessage,
14 secretRequestLink,
15 secretRequestPrivacyNote,
16 secretRequestSources,
17 } from "./secretRequest.ts";
18
19 describe("secretRequestDisplay", () => {
20 it("never offers the form for a request inherited from another thread", () => {
21 expect(secretRequestDisplay({ secretStatus: "pending" }, "inherited")).toEqual({
22 kind: "pending-elsewhere",
23 label: "Waiting for an answer in the original thread",
24 });
25 expect(secretRequestDisplay({ secretStatus: "saved" }, "inherited")).toMatchObject({
26 outcome: "saved",
27 });
28 });
29 });
30
31 describe("secretRequestAnswerInput", () => {
32 const single = { id: "item", threadId: "thread", label: "Token" } as const;
33 const login = {
34 ...single,
35 delivery: "file",
36 fields: [
37 { name: "email", label: "Email", secret: false },
38 { name: "password", label: "Password" },
39 ],
40 } as const;
41 const ids = { threadId: "thread", turnItemId: "item" };
42
43 it("answers a single request with its trimmed secret", () => {
44 expect(
45 secretRequestAnswerInput(single as never, { type: "save", values: { "": " t " } }),
46 ).toEqual({ ...ids, answer: { type: "save", secret: "t" } });
47 expect(secretRequestAnswerInput(single as never, { type: "save", values: { "": " " } })).toBe(
48 null,
49 );
50 });
51
52 it("answers a fields request only once every field has a value, keyed by name", () => {
53 expect(
54 secretRequestAnswerInput(login as never, {
55 type: "save",
56 values: { email: "a@acme.dev ", password: "pw" },
57 }),
58 ).toEqual({
59 ...ids,
60 answer: { type: "save", values: { email: "a@acme.dev", password: "pw" } },
61 });
62 expect(
63 secretRequestAnswerInput(login as never, { type: "save", values: { email: "a@acme.dev" } }),
64 ).toBe(null);
65 // An unset field named like an Object.prototype member is empty, not that member.
66 const named = { ...single, fields: [{ name: "constructor", label: "Key" }] } as const;
67 expect(secretRequestAnswerInput(named as never, { type: "save", values: {} })).toBe(null);
68 });
69 });
70
71 describe("secretRequestPrivacyNote", () => {
72 it("does not promise to hide a value that the agent's commands can read", () => {
73 expect(secretRequestPrivacyNote({})).toBe("Stored securely, never shown to the agent");
74 expect(secretRequestPrivacyNote({ delivery: "file" })).not.toContain("never shown");
75 });
76 });
77
78 describe("secretRequestLink", () => {
79 it.each([
80 [
81 "Open this link and sign in: https://a.dev/x?s=1. Then paste the code.",
82 "https://a.dev/x?s=1",
83 ],
84 ["Open [https://a.dev/x?s=1](https://a.dev/x?s=1) and sign in.", "https://a.dev/x?s=1"],
85 ["Sign in [here](https://a.dev/login?state=abc(def)).", "https://a.dev/login?state=abc(def)"],
86 ["Sign in at https://a.dev/login?state=abc(def).", "https://a.dev/login?state=abc(def)"],
87 ["(Sign in at https://a.dev/login.)", "https://a.dev/login"],
88 ["Open **https://a.dev/x?s=1** and sign in.", "https://a.dev/x?s=1"],
89 ["Open https://a.dev/x?state=u7o_ and sign in.", "https://a.dev/x?state=u7o_"],
90 ["**Sign in at https://a.dev/device**", "https://a.dev/device"],
91 ["_Open https://a.dev/device_.", "https://a.dev/device"],
92 ["_https://a.dev/x?state=u7o_&client_id=cli_", "https://a.dev/x?state=u7o_&client_id=cli"],
93 ["Use client_id acme at https://a.dev/x?state=u7o_", "https://a.dev/x?state=u7o_"],
94 ["_Sign in_ at https://a.dev/x?state=u7o_", "https://a.dev/x?state=u7o_"],
95 ["_Required_: Open https://a.dev/x?state=u7o_", "https://a.dev/x?state=u7o_"],
96 ["(**https://a.dev/login**)", "https://a.dev/login"],
97 ["Open ***https://a.dev/device***", "https://a.dev/device"],
98 ["Open **_https://a.dev/device_**", "https://a.dev/device"],
99 [
100 "Open [https://a.dev/x?s=u7o_](https://a.dev/x?s=u7o_) and sign in.",
101 "https://a.dev/x?s=u7o_",
102 ],
103 ["Open `https://a.dev/x?s=1.` and sign in.", "https://a.dev/x?s=1."],
104 ["Open https://a.dev/x?s=1, then if needed https://a.dev/x?s=1 again.", "https://a.dev/x?s=1"],
105 ["Open https://a.dev or https://b.dev.", undefined],
106 ["Open https://a.dev (also written https://a.dev/).", "https://a.dev/"],
107 ["Paste the code the CLI printed.", undefined],
108 ])("finds the one link in a reason: %s", (reason, href) => {
109 expect(secretRequestLink({ reason })?.href).toBe(href);
110 });
111
112 it("prefers the agent's url over a link in the reason", () => {
113 expect(
114 secretRequestLink({ url: "https://auth.acme.dev/a", reason: "See https://docs.acme.dev" })
115 ?.host,
116 ).toBe("auth.acme.dev");
117 });
118 });
119
120 describe("secretRequestFailureMessage", () => {
121 it("passes through server errors but hides anything that could echo the payload", () => {
122 expect(
123 secretRequestFailureMessage(new SecretRequestError({ reason: "already_answered" })),
124 ).toBe("This secret request was already answered.");
125 expect(secretRequestFailureMessage(new Error('Expected string, got "whsec_1"'))).toBe(
126 "Could not answer the request. Try again.",
127 );
128 expect(secretRequestFailureMessage(undefined)).toBe("Could not answer the request. Try again.");
129 });
130 });
131
132 describe("secretRequestSources", () => {
133 const onePassword = SecretProviderId.make("1password");
134 const providers = {
135 [onePassword]: {
136 kind: SecretProviderKind.make("one_password"),
137 label: "1Password",
138 config: { account: "my" },
139 },
140 [SecretProviderId.make("later")]: {
141 kind: SecretProviderKind.make("from_a_newer_build"),
142 label: "Later",
143 config: {},
144 },
145 };
146
147 it("offers configured providers only when the server accepts reference answers", () => {
148 expect(secretRequestSources({ secretRequestReferences: true }, providers)).toMatchObject([
149 { id: onePassword, label: "1Password" },
150 ]);
151 expect(secretRequestSources({}, providers)).toEqual([]);
152 expect(secretRequestSources({ secretRequestReferences: true }, {})).toEqual([]);
153 });
154
155 it("answers with a reference only in the provider's syntax", () => {
156 const [source] = secretRequestSources({ secretRequestReferences: true }, providers);
157 if (source === undefined) throw new Error("expected a source");
158 const item = {
159 id: TurnItemId.make("item-1"),
160 threadId: ThreadId.make("thread-1"),
161 label: "Deploy token",
162 };
163 expect(
164 secretRequestAnswerInput(item, {
165 type: "reference",
166 source,
167 reference: " op://vault/item/field ",
168 })?.answer,
169 ).toEqual({
170 type: "reference",
171 reference: { provider: onePassword, reference: "op://vault/item/field" },
172 });
173 expect(
174 secretRequestAnswerInput(item, { type: "reference", source, reference: "vault/item" }),
175 ).toBeNull();
176 // A reference answers one value, never a card with fields.
177 const fields = [{ name: "password", label: "Password" }];
178 expect(
179 secretRequestAnswerInput(
180 { ...item, fields },
181 { type: "reference", source, reference: "op://vault/item/field" },
182 ),
183 ).toBeNull();
184 });
185 });