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 secretRequestPrivacyNote,
15 secretRequestSources,
16} from "./secretRequest.ts";
17
18describe("secretRequestDisplay", () => {
19 it("never offers the form for a request inherited from another thread", () => {
20 expect(secretRequestDisplay({ secretStatus: "pending" }, "inherited")).toEqual({
21 kind: "pending-elsewhere",
22 label: "Waiting for an answer in the original thread",
23 });
24 expect(secretRequestDisplay({ secretStatus: "saved" }, "inherited")).toMatchObject({
25 outcome: "saved",
26 });
27 });
28});
29
30describe("secretRequestPrivacyNote", () => {
31 it("does not promise to hide a value that the agent's commands can read", () => {
32 expect(secretRequestPrivacyNote({})).toBe("Stored securely, never shown to the agent");
33 expect(secretRequestPrivacyNote({ delivery: "file" })).not.toContain("never shown");
34 });
35});
36
37describe("secretRequestFailureMessage", () => {
38 it("passes through server errors but hides anything that could echo the payload", () => {
39 expect(
40 secretRequestFailureMessage(new SecretRequestError({ reason: "already_answered" })),
41 ).toBe("This secret request was already answered.");
42 expect(secretRequestFailureMessage(new Error('Expected string, got "whsec_1"'))).toBe(
43 "Could not answer the request. Try again.",
44 );
45 expect(secretRequestFailureMessage(undefined)).toBe("Could not answer the request. Try again.");
46 });
47});
48
49describe("secretRequestSources", () => {
50 const onePassword = SecretProviderId.make("1password");
51 const providers = {
52 [onePassword]: {
53 kind: SecretProviderKind.make("one_password"),
54 label: "1Password",
55 config: { account: "my" },
56 },
57 [SecretProviderId.make("later")]: {
58 kind: SecretProviderKind.make("from_a_newer_build"),
59 label: "Later",
60 config: {},
61 },
62 };
63
64 it("offers configured providers only when the server accepts reference answers", () => {
65 expect(secretRequestSources({ secretRequestReferences: true }, providers)).toMatchObject([
66 { id: onePassword, label: "1Password" },
67 ]);
68 expect(secretRequestSources({}, providers)).toEqual([]);
69 expect(secretRequestSources({ secretRequestReferences: true }, {})).toEqual([]);
70 });
71
72 it("answers with a reference only in the provider's syntax", () => {
73 const [source] = secretRequestSources({ secretRequestReferences: true }, providers);
74 if (source === undefined) throw new Error("expected a source");
75 const item = { id: TurnItemId.make("item-1"), threadId: ThreadId.make("thread-1") };
76 expect(
77 secretRequestAnswerInput(item, {
78 type: "reference",
79 source,
80 reference: " op://vault/item/field ",
81 })?.answer,
82 ).toEqual({
83 type: "reference",
84 reference: { provider: onePassword, reference: "op://vault/item/field" },
85 });
86 expect(
87 secretRequestAnswerInput(item, { type: "reference", source, reference: "vault/item" }),
88 ).toBeNull();
89 });
90});