Open History to see the last commit
1import {
2 SecretProviderId,
3 SecretProviderKind,
4 SecretRequestError,
5 ThreadId,
6 TurnItemId,
7} from "@t3tools/contracts";
8import { describe, expect, it } from "vite-plus/test";
9
10import {
11 secretRequestAnswerInput,
12 secretRequestDisplay,
13 secretRequestFailureMessage,
14 secretRequestLink,
15 secretRequestPrivacyNote,
16 secretRequestSources,
17} from "./secretRequest.ts";
18
19describe("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
31describe("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
71describe("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
78describe("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 ["Open https://[2001:db8::1]:8443/setup to continue.", "https://[2001:db8::1]:8443/setup"],
86 [
87 "Visit https://example.org/setup?scope[]=repo to continue.",
88 "https://example.org/setup?scope[]=repo",
89 ],
90 [
91 "Open [https://a.dev/x?scope[]=repo](https://a.dev/x?scope[]=repo) and sign in.",
92 "https://a.dev/x?scope[]=repo",
93 ],
94 ["Sign in [here](https://a.dev/login?state=abc(def)).", "https://a.dev/login?state=abc(def)"],
95 ["Sign in at https://a.dev/login?state=abc(def).", "https://a.dev/login?state=abc(def)"],
96 ["(Sign in at https://a.dev/login.)", "https://a.dev/login"],
97 ["Open **https://a.dev/x?s=1** and sign in.", "https://a.dev/x?s=1"],
98 ["Open https://a.dev/x?state=u7o_ and sign in.", "https://a.dev/x?state=u7o_"],
99 ["**Sign in at https://a.dev/device**", "https://a.dev/device"],
100 ["_Open https://a.dev/device_.", "https://a.dev/device"],
101 ["_https://a.dev/x?state=u7o_&client_id=cli_", "https://a.dev/x?state=u7o_&client_id=cli"],
102 ["Use client_id acme at https://a.dev/x?state=u7o_", "https://a.dev/x?state=u7o_"],
103 ["_Sign in_ at https://a.dev/x?state=u7o_", "https://a.dev/x?state=u7o_"],
104 ["_Required_: Open https://a.dev/x?state=u7o_", "https://a.dev/x?state=u7o_"],
105 ["(**https://a.dev/login**)", "https://a.dev/login"],
106 ["Open ***https://a.dev/device***", "https://a.dev/device"],
107 ["Open **_https://a.dev/device_**", "https://a.dev/device"],
108 [
109 "Open [https://a.dev/x?s=u7o_](https://a.dev/x?s=u7o_) and sign in.",
110 "https://a.dev/x?s=u7o_",
111 ],
112 ["Open `https://a.dev/x?s=1.` and sign in.", "https://a.dev/x?s=1."],
113 ["Open https://a.dev/x?s=1, then if needed https://a.dev/x?s=1 again.", "https://a.dev/x?s=1"],
114 ["Open https://a.dev or https://b.dev.", undefined],
115 ["Open https://a.dev (also written https://a.dev/).", "https://a.dev/"],
116 ["Paste the code the CLI printed.", undefined],
117 ])("finds the one link in a reason: %s", (reason, href) => {
118 expect(secretRequestLink({ reason })?.href).toBe(href);
119 });
120
121 it("prefers the agent's url over a link in the reason", () => {
122 expect(
123 secretRequestLink({ url: "https://auth.acme.dev/a", reason: "See https://docs.acme.dev" })
124 ?.host,
125 ).toBe("auth.acme.dev");
126 });
127});
128
129describe("secretRequestFailureMessage", () => {
130 it("passes through server errors but hides anything that could echo the payload", () => {
131 expect(
132 secretRequestFailureMessage(new SecretRequestError({ reason: "already_answered" })),
133 ).toBe("This secret request was already answered.");
134 expect(secretRequestFailureMessage(new Error('Expected string, got "whsec_1"'))).toBe(
135 "Could not answer the request. Try again.",
136 );
137 expect(secretRequestFailureMessage(undefined)).toBe("Could not answer the request. Try again.");
138 });
139});
140
141describe("secretRequestSources", () => {
142 const onePassword = SecretProviderId.make("1password");
143 const providers = {
144 [onePassword]: {
145 kind: SecretProviderKind.make("one_password"),
146 label: "1Password",
147 config: { account: "my" },
148 },
149 [SecretProviderId.make("later")]: {
150 kind: SecretProviderKind.make("from_a_newer_build"),
151 label: "Later",
152 config: {},
153 },
154 };
155
156 it("offers configured providers only when the server accepts reference answers", () => {
157 expect(secretRequestSources({ secretRequestReferences: true }, providers)).toMatchObject([
158 { id: onePassword, label: "1Password" },
159 ]);
160 expect(secretRequestSources({}, providers)).toEqual([]);
161 expect(secretRequestSources({ secretRequestReferences: true }, {})).toEqual([]);
162 });
163
164 it("answers with a reference only in the provider's syntax", () => {
165 const [source] = secretRequestSources({ secretRequestReferences: true }, providers);
166 if (source === undefined) throw new Error("expected a source");
167 const item = {
168 id: TurnItemId.make("item-1"),
169 threadId: ThreadId.make("thread-1"),
170 label: "Deploy token",
171 };
172 expect(
173 secretRequestAnswerInput(item, {
174 type: "reference",
175 source,
176 reference: " op://vault/item/field ",
177 })?.answer,
178 ).toEqual({
179 type: "reference",
180 reference: { provider: onePassword, reference: "op://vault/item/field" },
181 });
182 expect(
183 secretRequestAnswerInput(item, { type: "reference", source, reference: "vault/item" }),
184 ).toBeNull();
185 // A reference answers one value, never a card with fields.
186 const fields = [{ name: "password", label: "Password" }];
187 expect(
188 secretRequestAnswerInput(
189 { ...item, fields },
190 { type: "reference", source, reference: "op://vault/item/field" },
191 ),
192 ).toBeNull();
193 });
194});