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