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 codexFeedbackNotice,
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("reports upload progress 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(codexFeedbackNotice(states[0]!)).toEqual({
70 title: "Sending feedback to OpenAI...",
71 description: undefined,
72 });
73
74 draft = "Keep this newer message.";
75 finishUpload?.(AsyncResult.success({ feedbackId: "codex-thread-1" }));
76 await result;
77
78 expect(draft).toBe("Keep this newer message.");
79 expect(states.at(-1)).toEqual({
80 ...submission,
81 status: "sent",
82 feedbackId: "codex-thread-1",
83 });
84 expect(codexFeedbackNotice(states.at(-1)!)?.description).toContain("codex-thread-1");
85 });
86
87 it("records a failed upload without losing its user-facing error", async () => {
88 const states: CodexFeedbackSubmission[] = [];
89 const error = new Error("Upload rejected.");
90
91 await submitCodexFeedback({
92 submission,
93 clearDraft: () => undefined,
94 onUpdate: (state) => states.push(state),
95 upload: () =>
96 Promise.resolve(AsyncResult.failure<{ feedbackId: string }, Error>(Cause.fail(error))),
97 });
98
99 expect(states.at(-1)).toEqual({
100 ...submission,
101 status: "failed",
102 errorMessage: "Upload rejected.",
103 });
104 expect(codexFeedbackNotice(states.at(-1)!)?.description).toBe("Upload rejected.");
105 });
106
107 it("marks interruptions without reporting them as upload failures", async () => {
108 const states: CodexFeedbackSubmission[] = [];
109
110 await submitCodexFeedback({
111 submission,
112 clearDraft: () => undefined,
113 onUpdate: (state) => states.push(state),
114 upload: () =>
115 Promise.resolve(AsyncResult.failure<{ feedbackId: string }, never>(Cause.interrupt(1))),
116 });
117
118 expect(states.at(-1)).toEqual({ ...submission, status: "interrupted" });
119 expect(codexFeedbackNotice(states.at(-1)!)).toBeNull();
120 });
121
122 it("lets another feedback submission finish while the first remains in flight", async () => {
123 let finishFirstUpload:
124 | ((result: ReturnType<typeof AsyncResult.success<{ feedbackId: string }>>) => void)
125 | undefined;
126 const firstUpload = new Promise<ReturnType<typeof AsyncResult.success<{ feedbackId: string }>>>(
127 (resolve) => {
128 finishFirstUpload = resolve;
129 },
130 );
131 const firstStates: CodexFeedbackSubmission[] = [];
132 const secondStates: CodexFeedbackSubmission[] = [];
133
134 const first = submitCodexFeedback({
135 submission,
136 clearDraft: () => undefined,
137 onUpdate: (state) => firstStates.push(state),
138 upload: () => firstUpload,
139 });
140 const second = await submitCodexFeedback({
141 submission: {
142 ...submission,
143 id: MessageId.make("feedback-message-2"),
144 },
145 clearDraft: () => undefined,
146 onUpdate: (state) => secondStates.push(state),
147 upload: () => Promise.resolve(AsyncResult.success({ feedbackId: "codex-thread-2" })),
148 });
149
150 expect(firstStates.at(-1)?.status).toBe("uploading");
151 expect(second._tag).toBe("Success");
152 expect(secondStates.at(-1)).toMatchObject({
153 status: "sent",
154 feedbackId: "codex-thread-2",
155 });
156
157 finishFirstUpload?.(AsyncResult.success({ feedbackId: "codex-thread-1" }));
158 await first;
159 });
160});