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

pendingUserInput.test.ts

139 lines4.5 KB

1import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { formatInlineContextReference } from "./lib/composerContextReferences";
4import { threadContextReference } from "./lib/composerContextRecords";
5import { threadContextRecord } from "@t3tools/shared/composerContextReferences";
6​
7import {
8 carryDisplacedCustomAnswerIntoPrompt,
9 derivePendingUserInputProgress,
10 projectPendingUserInputAnswer,
11 threadContextsReferencedByAnswer,
12} from "./pendingUserInput";
13​
14describe("projectPendingUserInputAnswer", () => {
15 const record = threadContextRecord(
16 {
17 environmentId: EnvironmentId.make("env-answer"),
18 threadId: ThreadId.make("reference-thread"),
19 },
20 "Reference work",
21 );
22​
23 it("sends thread chips as readable links with the actual thread ID", () => {
24 const answer = `Compare ${formatInlineContextReference(threadContextReference(record))}`;
25 expect(projectPendingUserInputAnswer(answer, [record])).toBe(
26 "Compare [Reference work](t3-thread://v2/env-answer/reference-thread)",
27 );
28 });
29​
30 // regression: #18789 answers to pending questions lost exact text and option values
31 it("preserves option values and exact answer text", () => {
32 const options = [" first\t", "second"];
33 expect(projectPendingUserInputAnswer(options, [record])).toBe(options);
34 expect(projectPendingUserInputAnswer(" Edited answer\n", [record])).toBe(" Edited answer\n");
35 });
36});
37​
38describe("threadContextsReferencedByAnswer", () => {
39 const kept = threadContextRecord(
40 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("kept-thread") },
41 "Kept work",
42 );
43 const deleted = threadContextRecord(
44 { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("deleted-thread") },
45 "Deleted work",
46 );
47​
48 it("preserves remaining chips without restoring deleted thread records", () => {
49 const answer = `Compare ${formatInlineContextReference(threadContextReference(kept))}`;
50 expect(threadContextsReferencedByAnswer(answer, [deleted, kept])).toEqual([kept]);
51 });
52});
53​
54const singleSelectQuestion = {
55 id: "scope",
56 header: "Scope",
57 question: "What should the plan target first?",
58 options: [
59 {
60 label: "Orchestration-first",
61 description: "Focus on orchestration first",
62 },
63 ],
64 multiSelect: false,
65} as const;
66​
67describe("pending user input question progress", () => {
68 const questions = [
69 singleSelectQuestion,
70 {
71 id: "compat",
72 header: "Compat",
73 question: "How strict should compatibility be?",
74 options: [
75 {
76 label: "Keep current envelope",
77 description: "Preserve current wire format",
78 },
79 ],
80 multiSelect: false,
81 },
82 ] as const;
83​
84 it("counts a deliberately cleared answer as answered", () => {
85 const question = { ...singleSelectQuestion, initialAnswer: " Proposed message\n" };
86 const drafts = { scope: { customAnswer: "" } };
87 expect(derivePendingUserInputProgress([question], drafts, 0)).toMatchObject({
88 customAnswer: "",
89 resolvedAnswer: "",
90 canAdvance: true,
91 isComplete: true,
92 });
93 });
94​
95 it("derives the active question and advancement state", () => {
96 expect(
97 derivePendingUserInputProgress(
98 questions,
99 {
100 scope: {
101 selectedOptionValues: ["Orchestration-first"],
102 },
103 },
104 0,
105 ),
106 ).toMatchObject({
107 questionIndex: 0,
108 activeQuestion: questions[0],
109 selectedOptionValues: ["Orchestration-first"],
110 customAnswer: "",
111 resolvedAnswer: "Orchestration-first",
112 answeredQuestionCount: 1,
113 isLastQuestion: false,
114 isComplete: false,
115 canAdvance: true,
116 });
117 });
118});
119​
120describe("carryDisplacedCustomAnswerIntoPrompt", () => {
121 // regression: #12577 clicking a question option discarded typed text
122 it("keeps the thread draft when nothing was typed into the answer", () => {
123 expect(carryDisplacedCustomAnswerIntoPrompt("draft", undefined)).toBe("draft");
124 expect(carryDisplacedCustomAnswerIntoPrompt("draft", " ")).toBe("draft");
125 });
126​
127 // regression: #12577 clicking a question option discarded typed text
128 it("moves the typed answer into an empty thread draft", () => {
129 expect(carryDisplacedCustomAnswerIntoPrompt("", "also rename the flag ")).toBe(
130 "also rename the flag",
131 );
132 });
133​
134 it("appends the typed answer after an existing thread draft", () => {
135 expect(carryDisplacedCustomAnswerIntoPrompt("first half\n", "second half")).toBe(
136 "first half\n\nsecond half",
137 );
138 });
139});