testReceivedSpansAreChecked

tcmts.cli.web.testReceivedSpansAreChecked · #inxz77nz · term

import { receivedSpans } from "#codebase/tcmts/cli/web";
import assert from "node:assert/strict";

export const testReceivedSpansAreChecked = () => {
  const now = 1_700_000_000_000;
  const h = "a".repeat(52);
  const ok = {
    trace: "1".repeat(32),
    id: "2".repeat(16),
    kind: "client",
    name: "app.add",
    start: now,
    dur: 5,
  };
  assert.equal(
    receivedSpans({}, () => true, now),
    undefined,
  );
  assert.equal(
    receivedSpans({ spans: Array(201).fill(ok) }, () => true, now),
    undefined,
  );
  const [s] = receivedSpans(
    {
      spans: [
        {
          ...ok,
          hash: h,
          parent: "3".repeat(16),
          attrs: {
            "tcm.from": "b".repeat(52),
            "url.path": "/x",
            "bad key!": 1,
            n: Infinity,
            ok: true,
          },
          error: "e".repeat(5000),
        },
      ],
    },
    (x) => x === h,
    now,
  )!;
  assert.deepEqual(
    s!.attrs,
    { "url.path": "/x", ok: true },
    "an unknown definition, a bad key and a non-number are dropped",
  );
  assert.equal(s!.hash, h);
  assert.equal(s!.error!.length, 4000);
  // a span that doesn't fit is left out: a server's kind, no trace, a time far off
  assert.deepEqual(
    receivedSpans(
      {
        spans: [
          { ...ok, kind: "call" },
          { ...ok, trace: "0".repeat(32) },
          { ...ok, start: now - 2 * 86_400_000 },
          ok,
        ],
      },
      () => true,
      now,
    )!.length,
    1,
  );
}

uses