Skip to content

Commit 9459981

Browse files
betegonclaude
andcommitted
test(trace): add tests for normalizeTraceSpan
Export the function and add unit tests covering event_id→span_id fallback, existing span_id preservation, and child recursion. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent dff8027 commit 9459981

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/lib/api-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ export async function getDetailedTrace(
15121512
* ID that `parent_span_id` references on child spans. We copy it to `span_id`
15131513
* so the rest of the codebase can use a single, predictable field name.
15141514
*/
1515-
function normalizeTraceSpan(span: TraceSpan): TraceSpan {
1515+
export function normalizeTraceSpan(span: TraceSpan): TraceSpan {
15161516
const normalized = { ...span };
15171517
if (!normalized.span_id && normalized.event_id) {
15181518
normalized.span_id = normalized.event_id;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { normalizeTraceSpan } from "../../src/lib/api-client.js";
3+
4+
describe("normalizeTraceSpan", () => {
5+
test("copies event_id to span_id when span_id is missing", () => {
6+
const span = { event_id: "abc123", start_timestamp: 0 } as Parameters<
7+
typeof normalizeTraceSpan
8+
>[0];
9+
const result = normalizeTraceSpan(span);
10+
expect(result.span_id).toBe("abc123");
11+
});
12+
13+
test("preserves existing span_id", () => {
14+
const result = normalizeTraceSpan({
15+
span_id: "existing",
16+
event_id: "other",
17+
start_timestamp: 0,
18+
});
19+
expect(result.span_id).toBe("existing");
20+
});
21+
22+
test("recurses into children", () => {
23+
const result = normalizeTraceSpan({
24+
span_id: "parent",
25+
start_timestamp: 0,
26+
children: [{ event_id: "child1", start_timestamp: 1 } as any],
27+
});
28+
expect(result.children?.[0]?.span_id).toBe("child1");
29+
});
30+
});

0 commit comments

Comments
 (0)