Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import { MessageId } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import { AsyncResult } from "effect/unstable/reactivity";
5
6import {
7 codexFeedbackMessage,
8 parseCodexFeedbackCommand,
9 submitCodexFeedback,
10 type CodexFeedbackSubmission,
11} from "./threadFeedback.ts";
12
13describe("parseCodexFeedbackCommand", () => {
14 it("accepts feedback without a reason", () => {
15 expect(parseCodexFeedbackCommand(" /feedback ")).toEqual({});
16 });
17
18 it("preserves a feedback description", () => {
19 expect(parseCodexFeedbackCommand("/feedback The agent stopped early.")).toEqual({
20 reason: "The agent stopped early.",
21 });
22 });
23
24 it("accepts mixed-case feedback commands", () => {
25 expect(parseCodexFeedbackCommand("/Feedback Retry failed.")).toEqual({
26 reason: "Retry failed.",
27 });
28 });
29
30 it("ignores other slash commands and ordinary messages", () => {
31 expect(parseCodexFeedbackCommand("/feedback-status")).toBeNull();
32 expect(parseCodexFeedbackCommand("Please send /feedback")).toBeNull();
33 });
34});
35
36describe("submitCodexFeedback", () => {
37 const submission = {
38 id: MessageId.make("feedback-message-1"),
39 command: "/feedback The agent stopped early.",
40 createdAt: "2026-08-23T00:00:00.000Z",
41 } as const;
42
43 it("shows the command and clears the draft before the upload finishes", async () => {
44 let draft: string = submission.command;
45 let finishUpload:
46 | ((result: ReturnType<typeof AsyncResult.success<{ feedbackId: string }>>) => void)
47 | undefined;
48 const states: CodexFeedbackSubmission[] = [];
49 const upload = new Promise<ReturnType<typeof AsyncResult.success<{ feedbackId: string }>>>(
50 (resolve) => {
51 finishUpload = resolve;
52 },
53 );
54
55 const result = submitCodexFeedback({
56 submission,
57 clearDraft: () => {
58 draft = "";
59 },
60 onUpdate: (state) => states.push(state),
61 upload: () => {
62 expect(draft).toBe("");
63 return upload;
64 },
65 });
66
67 expect(draft).toBe("");
68 expect(states).toEqual([{ ...submission, status: "uploading" }]);
69 expect(codexFeedbackMessage(states[0]!)).toMatchObject({
70 id: submission.id,
71 role: "user",
72 text: submission.command,
73 });
74 expect(codexFeedbackMessage(states[0]!, "assistant").text).toBe(
75 "Sending feedback to OpenAI...",
76 );
77
78 draft = "Keep this newer message.";
79 finishUpload?.(AsyncResult.success({ feedbackId: "codex-thread-1" }));
80 await result;
81
82 expect(draft).toBe("Keep this newer message.");
83 expect(states.at(-1)).toEqual({
84 ...submission,
85 status: "sent",
86 feedbackId: "codex-thread-1",
87 });
88 expect(codexFeedbackMessage(states.at(-1)!, "assistant").text).toContain("codex-thread-1");
89 });
90
91 it("records a failed upload without losing its user-facing error", async () => {
92 const states: CodexFeedbackSubmission[] = [];
93 const error = new Error("Upload rejected.");
94
95 await submitCodexFeedback({
96 submission,
97 clearDraft: () => undefined,
98 onUpdate: (state) => states.push(state),
99 upload: () =>
100 Promise.resolve(AsyncResult.failure<{ feedbackId: string }, Error>(Cause.fail(error))),
101 });
102
103 expect(states.at(-1)).toEqual({
104 ...submission,
105 status: "failed",
106 errorMessage: "Upload rejected.",
107 });
108 });
109
110 it("marks interruptions without reporting them as upload failures", async () => {
111 const states: CodexFeedbackSubmission[] = [];
112
113 await submitCodexFeedback({
114 submission,
115 clearDraft: () => undefined,
116 onUpdate: (state) => states.push(state),
117 upload: () =>
118 Promise.resolve(AsyncResult.failure<{ feedbackId: string }, never>(Cause.interrupt(1))),
119 });
120
121 expect(states.at(-1)).toEqual({ ...submission, status: "interrupted" });
122 });
123
124 it("lets another feedback submission finish while the first remains in flight", async () => {
125 let finishFirstUpload:
126 | ((result: ReturnType<typeof AsyncResult.success<{ feedbackId: string }>>) => void)
127 | undefined;
128 const firstUpload = new Promise<ReturnType<typeof AsyncResult.success<{ feedbackId: string }>>>(
129 (resolve) => {
130 finishFirstUpload = resolve;
131 },
132 );
133 const firstStates: CodexFeedbackSubmission[] = [];
134 const secondStates: CodexFeedbackSubmission[] = [];
135
136 const first = submitCodexFeedback({
137 submission,
138 clearDraft: () => undefined,
139 onUpdate: (state) => firstStates.push(state),
140 upload: () => firstUpload,
141 });
142 const second = await submitCodexFeedback({
143 submission: {
144 ...submission,
145 id: MessageId.make("feedback-message-2"),
146 },
147 clearDraft: () => undefined,
148 onUpdate: (state) => secondStates.push(state),
149 upload: () => Promise.resolve(AsyncResult.success({ feedbackId: "codex-thread-2" })),
150 });
151
152 expect(firstStates.at(-1)?.status).toBe("uploading");
153 expect(second._tag).toBe("Success");
154 expect(secondStates.at(-1)).toMatchObject({
155 status: "sent",
156 feedbackId: "codex-thread-2",
157 });
158
159 finishFirstUpload?.(AsyncResult.success({ feedbackId: "codex-thread-1" }));
160 await first;
161 });
162});