File size: 3,433 Bytes
9c28998
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SPDX-License-Identifier: Apache-2.0
// Author: Lutar, Stephen P. | ORCID 0009-0001-0110-4173 | SZL Holdings
// Tests: vsp-otel exporter — VSP

import { describe, it, expect, vi } from "vitest";
import { axesFromSpan, spanHash, signSpan, exportSpans, type OtelSpan } from "./exporter.js";

function makeSpan(
  overrides: Partial<OtelSpan> = {},
  axisOverrides: Record<string, number> = {},
): OtelSpan {
  const base: Record<string, number> = {
    "lambda.moralGrounding":       0.96,
    "lambda.measurabilityHonesty": 0.96,
    "lambda.epistemicHumility":    0.92,
    "lambda.harmAvoidance":        0.91,
    "lambda.logicalCoherence":     0.93,
    "lambda.citationIntegrity":    0.91,
    "lambda.noveltyContribution":  0.91,
    "lambda.reproducibility":      0.92,
    "lambda.stakeholderAlignment": 0.91,
    ...axisOverrides,
  };
  return {
    spanId:    "span-001",
    traceId:   "trace-abc",
    name:      "test-operation",
    startTime: Date.now() - 100,
    endTime:   Date.now(),
    attributes: base,
    status:    "OK",
    ...overrides,
  };
}

describe("axesFromSpan", () => {
  it("reads axis scores from span attributes", () => {
    const axes = axesFromSpan(makeSpan());
    expect(axes.moralGrounding).toBe(0.96);
    expect(axes.measurabilityHonesty).toBe(0.96);
  });

  it("defaults to 0.90 for missing axes", () => {
    const span = makeSpan({}, {});
    // Remove one axis
    const { attributes } = span;
    delete (attributes as Record<string, unknown>)["lambda.epistemicHumility"];
    const axes = axesFromSpan({ ...span, attributes });
    expect(axes.epistemicHumility).toBe(0.90);
  });

  it("clamps axis values to [0, 1]", () => {
    const span = makeSpan({}, { "lambda.moralGrounding": 2.0 });
    const axes = axesFromSpan(span);
    expect(axes.moralGrounding).toBe(1.0);
  });
});

describe("spanHash", () => {
  it("returns a 64-char hex string", () => {
    expect(spanHash(makeSpan())).toHaveLength(64);
  });

  it("is deterministic for same span", () => {
    const span = makeSpan();
    expect(spanHash(span)).toBe(spanHash(span));
  });

  it("differs for different spans", () => {
    const s1 = makeSpan({ spanId: "span-001" });
    const s2 = makeSpan({ spanId: "span-002" });
    expect(spanHash(s1)).not.toBe(spanHash(s2));
  });
});

describe("signSpan", () => {
  it("returns a LambdaSignedSpan with pass=true for good axes", () => {
    const result = signSpan(makeSpan());
    expect(result.pass).toBe(true);
    expect(result.lambda).toBeGreaterThanOrEqual(0.90);
  });

  it("returns pass=false for failing axes", () => {
    const span = makeSpan({}, {
      "lambda.moralGrounding":       0.50,
      "lambda.measurabilityHonesty": 0.50,
    });
    const result = signSpan(span);
    expect(result.pass).toBe(false);
  });
});

describe("exportSpans", () => {
  it("batches multiple spans", () => {
    const spans = [makeSpan({ spanId: "s1" }), makeSpan({ spanId: "s2" })];
    const result = exportSpans(spans);
    expect(result.total).toBe(2);
    expect(result.signed).toHaveLength(2);
  });

  it("counts pass/fail correctly", () => {
    const good = makeSpan({ spanId: "good" });
    const bad  = makeSpan({ spanId: "bad" }, {
      "lambda.moralGrounding":       0.50,
      "lambda.measurabilityHonesty": 0.50,
    });
    const result = exportSpans([good, bad]);
    expect(result.passed).toBe(1);
    expect(result.failed).toBe(1);
  });
});