1import { SecretRequestError, ThreadId, TurnItemId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import {
5 secretRequestAnswerInput,
6 secretRequestDisplay,
7 secretRequestFailureMessage,
8 type SecretRequestItem,
9} from "./secretRequest.ts";
10
11const item = {
12 id: TurnItemId.make("turn-item:secret-request:1"),
13 threadId: ThreadId.make("thread-1"),
14};
15
16describe("secretRequestDisplay", () => {
17 it("shows the form only while pending and maps every answer to its outcome copy", () => {
18 const display = (secretStatus: SecretRequestItem["secretStatus"]) =>
19 secretRequestDisplay({ secretStatus }, "local");
20 expect(display("pending")).toEqual({ kind: "pending" });
21 expect(display("saved")).toEqual({
22 kind: "answered",
23 outcome: "saved",
24 label: "Saved securely and kept private",
25 });
26 expect(display("declined")).toEqual({
27 kind: "answered",
28 outcome: "declined",
29 label: "Declined",
30 });
31 expect(display("cancelled")).toEqual({
32 kind: "answered",
33 outcome: "ended",
34 label: "Request ended",
35 });
36 });
37
38 it("never offers the form for a request inherited from another thread", () => {
39 expect(secretRequestDisplay({ secretStatus: "pending" }, "inherited")).toEqual({
40 kind: "pending-elsewhere",
41 label: "Waiting for an answer in the original thread",
42 });
43 expect(secretRequestDisplay({ secretStatus: "saved" }, "inherited")).toMatchObject({
44 outcome: "saved",
45 });
46 });
47});
48
49describe("secretRequestAnswerInput", () => {
50 it("refuses a blank save and trims the value the server will store", () => {
51 expect(secretRequestAnswerInput(item, { type: "save", secret: " " })).toBeNull();
52 expect(secretRequestAnswerInput(item, { type: "save", secret: " whsec_1 " })).toEqual({
53 threadId: item.threadId,
54 turnItemId: item.id,
55 answer: { type: "save", secret: "whsec_1" },
56 });
57 expect(secretRequestAnswerInput(item, { type: "decline" })).toEqual({
58 threadId: item.threadId,
59 turnItemId: item.id,
60 answer: { type: "decline" },
61 });
62 });
63});
64
65describe("secretRequestFailureMessage", () => {
66 it("passes through server errors but hides anything that could echo the payload", () => {
67 expect(
68 secretRequestFailureMessage(new SecretRequestError({ reason: "already_answered" })),
69 ).toBe("This secret request was already answered.");
70 expect(secretRequestFailureMessage(new Error('Expected string, got "whsec_1"'))).toBe(
71 "Could not answer the request. Try again.",
72 );
73 expect(secretRequestFailureMessage(undefined)).toBe("Could not answer the request. Try again.");
74 });
75});