|
1 | | -import { cleanup, fireEvent, render, screen } from "@testing-library/react"; |
| 1 | +import { |
| 2 | + cleanup, |
| 3 | + fireEvent, |
| 4 | + render, |
| 5 | + screen, |
| 6 | + within, |
| 7 | +} from "@testing-library/react"; |
2 | 8 | import { afterEach, expect, test, vi } from "vitest"; |
| 9 | +import { formatDateTime } from "@/lib/presentation.ts"; |
3 | 10 | import { decodeEnvelope } from "@/lib/spotlight.ts"; |
4 | 11 | import { buildTraceGroups } from "@/lib/trace-model.ts"; |
5 | 12 | import { SpanDetail } from "./span-detail.tsx"; |
@@ -99,6 +106,49 @@ test("span start and end times use local time, not raw epoch seconds", () => { |
99 | 106 | const details = |
100 | 107 | screen.getByRole("table", { name: "Span details" }).textContent ?? ""; |
101 | 108 | expect(details).not.toContain("1795010400.538"); |
102 | | - expect(details).toContain(new Date(1795010400538).toLocaleTimeString()); |
103 | | - expect(details).toContain(new Date(1795010400650).toLocaleTimeString()); |
| 109 | + expect(details).toContain( |
| 110 | + formatDateTime(1795010400.538, { milliseconds: true }), |
| 111 | + ); |
| 112 | + expect(details).toContain( |
| 113 | + formatDateTime(1795010400.65, { milliseconds: true }), |
| 114 | + ); |
| 115 | +}); |
| 116 | + |
| 117 | +test("span times keep milliseconds and show the date when it is not today", () => { |
| 118 | + // A 40 ms span from an earlier day. |
| 119 | + const [trace] = buildTraceGroups( |
| 120 | + decodeEnvelope( |
| 121 | + JSON.stringify([ |
| 122 | + {}, |
| 123 | + [ |
| 124 | + [ |
| 125 | + { type: "transaction" }, |
| 126 | + { |
| 127 | + transaction: "GET /orders", |
| 128 | + start_timestamp: 1_700_000_000.538, |
| 129 | + timestamp: 1_700_000_000.578, |
| 130 | + contexts: { trace: { trace_id: "trace", span_id: "root" } }, |
| 131 | + }, |
| 132 | + ], |
| 133 | + ], |
| 134 | + ]), |
| 135 | + "fixture", |
| 136 | + ), |
| 137 | + ); |
| 138 | + const span = trace?.spans.find((entry) => entry.id === "root"); |
| 139 | + if (!trace || !span) throw new Error("Expected trace span"); |
| 140 | + render(<SpanDetail trace={trace} span={span} />); |
| 141 | + const details = screen.getByRole("table", { name: "Span details" }); |
| 142 | + const time = (ms: number) => |
| 143 | + new Date(ms).toLocaleTimeString(undefined, { |
| 144 | + hour: "numeric", |
| 145 | + minute: "2-digit", |
| 146 | + second: "2-digit", |
| 147 | + fractionalSecondDigits: 3, |
| 148 | + }); |
| 149 | + const start = within(details).getByRole("row", { name: /Start timestamp/ }); |
| 150 | + const end = within(details).getByRole("row", { name: /End timestamp/ }); |
| 151 | + const day = new Date(1_700_000_000_538).toLocaleDateString(); |
| 152 | + expect(start.textContent).toContain(`${day} ${time(1_700_000_000_538)}`); |
| 153 | + expect(end.textContent).toContain(`${day} ${time(1_700_000_000_578)}`); |
104 | 154 | }); |
0 commit comments