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