|
| 1 | +import React from "react" |
| 2 | +import { render, screen } from "@/utils/test-utils" |
| 3 | +import type { ClineMessage } from "@roo-code/types" |
| 4 | + |
| 5 | +import { ChatRowContent } from "../ChatRow" |
| 6 | + |
| 7 | +vi.mock("@src/utils/vscode", () => ({ vscode: { postMessage: vi.fn() } })) |
| 8 | + |
| 9 | +vi.mock("react-i18next", () => ({ |
| 10 | + useTranslation: () => ({ t: (key: string) => key, i18n: { exists: () => true } }), |
| 11 | + Trans: ({ children }: { children?: React.ReactNode }) => <>{children}</>, |
| 12 | + initReactI18next: { type: "3rdParty", init: () => {} }, |
| 13 | +})) |
| 14 | + |
| 15 | +let mockState: Record<string, unknown> = {} |
| 16 | +vi.mock("@src/context/ExtensionStateContext", async (importOriginal) => ({ |
| 17 | + ...(await importOriginal<typeof import("@src/context/ExtensionStateContext")>()), |
| 18 | + useExtensionState: () => ({ |
| 19 | + mcpServers: [], |
| 20 | + alwaysAllowMcp: false, |
| 21 | + currentCheckpoint: null, |
| 22 | + mode: "code", |
| 23 | + apiConfiguration: {}, |
| 24 | + clineMessages: [], |
| 25 | + currentTaskItem: undefined, |
| 26 | + ...mockState, |
| 27 | + }), |
| 28 | +})) |
| 29 | + |
| 30 | +vi.mock("@src/components/ui/hooks/useSelectedModel", () => ({ |
| 31 | + useSelectedModel: () => ({ info: { supportsImages: true } }), |
| 32 | +})) |
| 33 | + |
| 34 | +const renderRow = (message: ClineMessage, isStreaming = false) => |
| 35 | + render( |
| 36 | + <ChatRowContent |
| 37 | + message={message} |
| 38 | + isExpanded={false} |
| 39 | + isLast={false} |
| 40 | + isStreaming={isStreaming} |
| 41 | + onToggleExpand={() => {}} |
| 42 | + onSuggestionClick={() => {}} |
| 43 | + onBatchFileResponse={() => {}} |
| 44 | + onFollowUpUnmount={() => {}} |
| 45 | + isFollowUpAnswered={false} |
| 46 | + />, |
| 47 | + ) |
| 48 | + |
| 49 | +describe("ChatRow – message timestamp", () => { |
| 50 | + beforeEach(() => { |
| 51 | + mockState = {} |
| 52 | + }) |
| 53 | + |
| 54 | + it("shows when a user message was sent", () => { |
| 55 | + renderRow({ ts: Date.now(), type: "say", say: "user_feedback", text: "hello there" }) |
| 56 | + expect(screen.getByTestId("message-timestamp").textContent).toContain("Today") |
| 57 | + }) |
| 58 | + |
| 59 | + it("uses the message's own timestamp, not the current time", () => { |
| 60 | + const d = new Date() |
| 61 | + d.setDate(d.getDate() - 1) |
| 62 | + d.setHours(11, 43, 0, 0) |
| 63 | + renderRow({ ts: d.getTime(), type: "say", say: "user_feedback", text: "an older message" }) |
| 64 | + expect(screen.getByTestId("message-timestamp").textContent).toContain("Yesterday") |
| 65 | + }) |
| 66 | + |
| 67 | + it("is rendered after the message bubble (under it), not before", () => { |
| 68 | + const { container } = renderRow({ ts: Date.now(), type: "say", say: "user_feedback", text: "ordering" }) |
| 69 | + const html = container.innerHTML |
| 70 | + expect(html.indexOf("ordering")).toBeGreaterThan(-1) |
| 71 | + expect(html.indexOf("message-timestamp")).toBeGreaterThan(html.indexOf("ordering")) |
| 72 | + }) |
| 73 | + |
| 74 | + it("applies the user's time and date format settings", () => { |
| 75 | + const d = new Date() |
| 76 | + d.setHours(14, 41, 0, 0) |
| 77 | + mockState = { timeFormat: "24h", dateFormat: "ymd" } |
| 78 | + renderRow({ ts: d.getTime(), type: "say", say: "user_feedback", text: "formats" }) |
| 79 | + expect(screen.getByTestId("message-timestamp").textContent).toContain("14:41") |
| 80 | + }) |
| 81 | + |
| 82 | + describe("assistant text", () => { |
| 83 | + it("shows when the reply was sent, under the reply", () => { |
| 84 | + const { container } = renderRow({ ts: Date.now(), type: "say", say: "text", text: "assistant reply" }) |
| 85 | + expect(screen.getByTestId("message-timestamp").textContent).toContain("Today") |
| 86 | + const html = container.innerHTML |
| 87 | + expect(html.indexOf("message-timestamp")).toBeGreaterThan(html.indexOf("assistant reply")) |
| 88 | + }) |
| 89 | + |
| 90 | + it("uses the reply's own timestamp and the user's formats", () => { |
| 91 | + const d = new Date() |
| 92 | + d.setHours(14, 41, 0, 0) |
| 93 | + mockState = { timeFormat: "24h", dateFormat: "ymd" } |
| 94 | + renderRow({ ts: d.getTime(), type: "say", say: "text", text: "formatted reply" }) |
| 95 | + expect(screen.getByTestId("message-timestamp").textContent).toContain("14:41") |
| 96 | + }) |
| 97 | + |
| 98 | + it("waits until the reply has finished streaming", () => { |
| 99 | + const { rerender } = renderRow({ |
| 100 | + ts: Date.now(), |
| 101 | + type: "say", |
| 102 | + say: "text", |
| 103 | + text: "still typing", |
| 104 | + partial: true, |
| 105 | + }) |
| 106 | + expect(screen.queryByTestId("message-timestamp")).toBeNull() |
| 107 | + |
| 108 | + rerender( |
| 109 | + <ChatRowContent |
| 110 | + message={{ ts: Date.now(), type: "say", say: "text", text: "still typing", partial: false }} |
| 111 | + isExpanded={false} |
| 112 | + isLast={false} |
| 113 | + isStreaming={false} |
| 114 | + onToggleExpand={() => {}} |
| 115 | + onSuggestionClick={() => {}} |
| 116 | + onBatchFileResponse={() => {}} |
| 117 | + onFollowUpUnmount={() => {}} |
| 118 | + isFollowUpAnswered={false} |
| 119 | + />, |
| 120 | + ) |
| 121 | + expect(screen.getByTestId("message-timestamp")).toBeTruthy() |
| 122 | + }) |
| 123 | + |
| 124 | + it("shows nothing for an assistant message with an invalid timestamp", () => { |
| 125 | + renderRow({ ts: 0, type: "say", say: "text", text: "no time" }) |
| 126 | + expect(screen.queryByTestId("message-timestamp")).toBeNull() |
| 127 | + }) |
| 128 | + }) |
| 129 | + |
| 130 | + it("keeps the message visible while streaming", () => { |
| 131 | + renderRow({ ts: Date.now(), type: "say", say: "user_feedback", text: "streaming" }, true) |
| 132 | + expect(screen.getByTestId("message-timestamp")).toBeTruthy() |
| 133 | + }) |
| 134 | +}) |
0 commit comments