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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// @vitest-environment jsdom

import { act } from "react";
import type { ReactNode } from "react";
import { createRoot } from "react-dom/client";
import { MemoryRouter } from "react-router-dom";
import type { Agent, Approval } from "@penclipai/shared";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { CommentThread } from "./CommentThread";

vi.mock("./MarkdownBody", () => ({
  MarkdownBody: ({ children, className }: { children: ReactNode; className?: string }) => (
    <div className={className}>{children}</div>
  ),
}));

vi.mock("./MarkdownEditor", () => ({
  MarkdownEditor: ({ value, onChange, placeholder }: {
    value: string;
    onChange: (value: string) => void;
    placeholder?: string;
  }) => (
    <textarea
      aria-label="Comment editor"
      value={value}
      placeholder={placeholder}
      onChange={(event) => onChange(event.target.value)}
    />
  ),
}));

vi.mock("./InlineEntitySelector", () => ({
  InlineEntitySelector: () => null,
}));

vi.mock("react-i18next", async (importOriginal) => {
  const actual = await importOriginal<typeof import("react-i18next")>();
  const translations: Record<string, string> = {
    Assignee: "负责人",
    Board: "董事会",
    None: "无",
    Status: "状态",
    System: "系统",
    Unassigned: "未分配",
    "updated this task": "更新了这个任务",
    "status.done": "已完成",
    "status.todo": "待办",
  };
  return {
    ...actual,
    initReactI18next: { type: "3rdParty", init: () => {} },
    useTranslation: () => ({
      t: (key: string, options?: Record<string, unknown>) => {
        if (key in translations) return translations[key];
        const template = typeof options?.defaultValue === "string" ? options.defaultValue : key;
        return template.replace(/\{\{(\w+)\}\}/g, (_match, token) => String(options?.[token] ?? ""));
      },
    }),
  };
});

vi.mock("./ApprovalCard", () => ({
  ApprovalCard: ({
    approval,
    onApprove,
    onReject,
  }: {
    approval: Approval;
    onApprove?: () => void;
    onReject?: () => void;
  }) => (
    <div>
      <div>{approval.type}</div>
      <div>{String(approval.payload.title ?? "")}</div>
      {onApprove ? <button type="button" onClick={onApprove}>Approve</button> : null}
      {onReject ? <button type="button" onClick={onReject}>Reject</button> : null}
    </div>
  ),
}));

vi.mock("@/plugins/slots", () => ({
  PluginSlotOutlet: () => null,
}));

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;

describe("CommentThread", () => {
  let container: HTMLDivElement;
  let writeTextMock: ReturnType<typeof vi.fn>;
  let execCommandMock: ReturnType<typeof vi.fn>;

  beforeEach(() => {
    container = document.createElement("div");
    document.body.appendChild(container);
    vi.useFakeTimers();
    vi.setSystemTime(new Date("2026-03-11T12:00:00.000Z"));
    writeTextMock = vi.fn(async () => {});
    execCommandMock = vi.fn(() => true);
    Object.assign(navigator, {
      clipboard: {
        writeText: writeTextMock,
      },
    });
    Object.defineProperty(window, "isSecureContext", {
      value: true,
      configurable: true,
    });
    document.execCommand = execCommandMock;
  });

  afterEach(() => {
    vi.useRealTimers();
    container.remove();
  });

  it("renders historical runs as timeline rows using the finished time", () => {
    const root = createRoot(container);
    const agent: Agent = {
      id: "agent-1",
      companyId: "company-1",
      name: "CodexCoder",
      urlKey: "codexcoder",
      role: "engineer",
      title: null,
      icon: "code",
      status: "active",
      reportsTo: null,
      capabilities: null,
      adapterType: "process",
      adapterConfig: {},
      runtimeConfig: {},
      budgetMonthlyCents: 0,
      spentMonthlyCents: 0,
      pauseReason: null,
      pausedAt: null,
      permissions: { canCreateAgents: false },
      lastHeartbeatAt: null,
      metadata: null,
      createdAt: new Date("2026-03-11T00:00:00.000Z"),
      updatedAt: new Date("2026-03-11T00:00:00.000Z"),
    };

    act(() => {
      root.render(
        <MemoryRouter>
          <CommentThread
            comments={[]}
            linkedRuns={[{
              runId: "run-12345678abcd",
              status: "succeeded",
              agentId: "agent-1",
              createdAt: "2026-03-11T07:00:00.000Z",
              startedAt: "2026-03-11T08:00:00.000Z",
              finishedAt: "2026-03-11T10:00:00.000Z",
            }]}
            agentMap={new Map([["agent-1", agent]])}
            onAdd={async () => {}}
          />
        </MemoryRouter>,
      );
    });

    const runRow = container.querySelector("#run-run-12345678abcd") as HTMLDivElement | null;
    expect(runRow).not.toBeNull();
    expect(runRow?.className).toContain("py-1.5");
    expect(runRow?.className).toContain("items-center");
    expect(runRow?.className).not.toContain("border");
    expect(container.textContent).toContain("CodexCoder");
    expect(container.textContent).toContain("succeeded");
    expect(container.textContent).toContain("2 hours ago");
    expect(container.textContent).not.toContain("4 hours ago");
    const runLink = container.querySelector('a[href="/agents/agent-1/runs/run-12345678abcd"]') as HTMLAnchorElement | null;
    expect(runLink?.textContent).toContain("run-1234");
    expect(runLink?.className).toContain("rounded-md");
    expect(runLink?.className).toContain("px-2");

    act(() => {
      root.unmount();
    });
  });

  it("replaces the composer with a warning when comments are disabled", () => {
    const root = createRoot(container);

    act(() => {
      root.render(
        <MemoryRouter>
          <CommentThread
            comments={[]}
            composerDisabledReason="Workspace is closed."
            onAdd={async () => {}}
          />
        </MemoryRouter>,
      );
    });

    expect(container.textContent).toContain("Workspace is closed.");
    expect(container.querySelector('textarea[aria-label="Comment editor"]')).toBeNull();
    expect(container.textContent).not.toContain("Comment");

    act(() => {
      root.unmount();
    });
  });

  it("shows follow-up badges on explicit follow-up comments and timeline rows", () => {
    const root = createRoot(container);

    act(() => {
      root.render(
        <MemoryRouter>
          <CommentThread
            comments={[{
              id: "comment-1",
              companyId: "company-1",
              issueId: "issue-1",
              authorAgentId: null,
              authorUserId: "local-board",
              body: "Please continue validation.",
              followUpRequested: true,
              createdAt: new Date("2026-03-11T10:00:00.000Z"),
              updatedAt: new Date("2026-03-11T10:00:00.000Z"),
            }]}
            timelineEvents={[{
              id: "event-1",
              actorType: "agent",
              actorId: "agent-1",
              createdAt: new Date("2026-03-11T10:00:00.000Z"),
              commentId: "comment-1",
              followUpRequested: true,
            }]}
            onAdd={async () => {}}
          />
        </MemoryRouter>,
      );
    });

    expect(container.textContent).toContain("Follow-up");
    expect(container.textContent).toContain("requested follow-up");

    act(() => {
      root.unmount();
    });
  });

  it("hides the reopen control and infers reopen for closed agent-assigned issues", async () => {
    const root = createRoot(container);
    const onAdd = vi.fn(async () => {});

    act(() => {
      root.render(
        <MemoryRouter>
          <CommentThread
            comments={[]}
            issueStatus="done"
            currentAssigneeValue="agent:agent-1"
            onAdd={onAdd}
          />
        </MemoryRouter>,
      );
    });

    expect(container.textContent).not.toContain("Re-open");

    const editor = container.querySelector('textarea[aria-label="Comment editor"]') as HTMLTextAreaElement | null;
    const submitButton = Array.from(container.querySelectorAll("button")).find(
      (element) => element.textContent === "Comment",
    ) as HTMLButtonElement | undefined;
    expect(editor).not.toBeNull();
    expect(submitButton).toBeDefined();

    act(() => {
      const valueSetter = Object.getOwnPropertyDescriptor(
        window.HTMLTextAreaElement.prototype,
        "value",
      )?.set;
      valueSetter?.call(editor, "Please pick this back up");
      editor?.dispatchEvent(new Event("input", { bubbles: true }));
    });

    await act(async () => {
      submitButton?.click();
    });

    expect(onAdd).toHaveBeenCalledWith("Please pick this back up", true, undefined);

    act(() => {
      root.unmount();
    });
  });

  it("renders linked approvals inline in the timeline", () => {
    const root = createRoot(container);
    const agent: Agent = {
      id: "agent-1",
      companyId: "company-1",
      name: "CodexCoder",
      urlKey: "codexcoder",
      role: "engineer",
      title: null,
      icon: "code",
      status: "active",
      reportsTo: null,
      capabilities: null,
      adapterType: "process",
      adapterConfig: {},
      runtimeConfig: {},
      budgetMonthlyCents: 0,
      spentMonthlyCents: 0,
      pauseReason: null,
      pausedAt: null,
      permissions: { canCreateAgents: false },
      lastHeartbeatAt: null,
      metadata: null,
      createdAt: new Date("2026-03-11T00:00:00.000Z"),
      updatedAt: new Date("2026-03-11T00:00:00.000Z"),
    };
    const approval: Approval = {
      id: "approval-1",
      companyId: "company-1",
      type: "request_board_approval",
      requestedByAgentId: "agent-1",
      requestedByUserId: null,
      status: "pending",
      payload: {
        title: "Approve hosting spend",
        text: "Estimated monthly cost is $42.",
      },
      decisionNote: null,
      decidedByUserId: null,
      decidedAt: null,
      createdAt: new Date("2026-03-11T09:00:00.000Z"),
      updatedAt: new Date("2026-03-11T09:00:00.000Z"),
    };

    act(() => {
      root.render(
        <MemoryRouter>
          <CommentThread
            comments={[]}
            linkedApprovals={[approval]}
            agentMap={new Map([["agent-1", agent]])}
            onAdd={async () => {}}
            onApproveApproval={async () => {}}
            onRejectApproval={async () => {}}
          />
        </MemoryRouter>,
      );
    });

    const approvalRow = container.querySelector("#approval-approval-1") as HTMLDivElement | null;
    expect(approvalRow).not.toBeNull();
    expect(container.textContent).toContain("request_board_approval");
    expect(container.textContent).toContain("Approve hosting spend");
    expect(container.textContent).toContain("Approve");
    expect(container.textContent).toContain("Reject");

    act(() => {
      root.unmount();
    });
  });

  it("uses a larger copy control with feedback and a clipboard fallback", async () => {
    const root = createRoot(container);

    act(() => {
      root.render(
        <MemoryRouter>
          <CommentThread
            comments={[{
              id: "comment-1",
              companyId: "company-1",
              issueId: "issue-1",
              authorAgentId: null,
              authorUserId: "user-1",
              body: "Hello from the comment body",
              createdAt: new Date("2026-03-11T11:00:00.000Z"),
              updatedAt: new Date("2026-03-11T11:00:00.000Z"),
            }]}
            onAdd={async () => {}}
          />
        </MemoryRouter>,
      );
    });

    const copyButton = Array.from(container.querySelectorAll("button")).find(
      (element) => element.getAttribute("aria-label") === "Copy comment as markdown",
    ) as HTMLButtonElement | undefined;

    expect(copyButton).toBeDefined();
    expect(copyButton?.className).toContain("min-h-8");
    expect(copyButton?.textContent).toContain("Copy");

    Object.defineProperty(window, "isSecureContext", {
      value: false,
      configurable: true,
    });

    await act(async () => {
      copyButton?.click();
    });

    expect(writeTextMock).not.toHaveBeenCalled();
    expect(execCommandMock).toHaveBeenCalledWith("copy");
    expect(copyButton?.textContent).toContain("Copied");

    act(() => {
      vi.advanceTimersByTime(1500);
    });

    expect(copyButton?.textContent).toContain("Copy");

    act(() => {
      root.unmount();
    });
  });
});