Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

1.4 KB
Raw Blame
import { describe, expect, it } from "vitest";
import type { Observation } from "../../domain/observations";
import { orderObservations } from "./ordering";

describe("orderObservations", () => {
  const observation = (
    id: string,
    startSecond: number,
    parentObservationId: string | null = null,
  ) =>
    ({
      id,
      parentObservationId,
      startTime: new Date(Date.UTC(2026, 0, 1, 12, 0, startSecond)),
    }) as Observation;

  it("walks the tree depth first with roots and siblings by start time", () => {
    // Span A starts first, but its generation starts after span B's.
    const ordered = orderObservations([
      observation("g2", 2, "B"),
      observation("B", 1),
      observation("g1", 5, "A"),
      observation("A", 0),
    ]);
    expect(ordered.map(({ id, nestingLevel }) => [id, nestingLevel])).toEqual([
      ["A", 0],
      ["g1", 1],
      ["B", 0],
      ["g2", 1],
    ]);
  });

  it("keeps the earliest row per id and roots rows without a known parent", () => {
    const ordered = orderObservations([
      observation("x", 3),
      observation("x", 1),
      observation("orphan", 2, "missing"),
    ]);
    expect(ordered.map(({ nestingLevel }) => nestingLevel)).toEqual([0, 0]);
    expect(
      ordered.map(({ id, startTime }) => [id, startTime.getUTCSeconds()]),
    ).toEqual([
      ["x", 1],
      ["orphan", 2],
    ]);
  });
});