| |
| |
| |
|
|
| 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({}, {}); |
| |
| 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); |
| }); |
| }); |
|
|