import test from 'node:test';
import assert from 'node:assert/strict';
import {runTrace,percentile,latencySLI} from '../src/observability/engine.mjs';
const metric=(r,key)=>r.metrics.find(([name])=>name===key)[1];

test('Real OpenTelemetry spans preserve parentage; tail latency and cardinality remain visible',async()=>{
 assert.equal(percentile([1,2,3,100],.95),100);assert.equal(latencySLI([1,2,100],50),2/3);
 const bad=await runTrace({preset:'lost-context',variant:'broken'}),good=await runTrace({preset:'lost-context',variant:'safe'});
 assert.equal(metric(bad,'Independent roots'),16);assert.equal(metric(good,'Independent roots'),8);assert.equal(metric(good,'Label sets'),1);assert.equal(metric(bad,'Label sets'),8);assert.equal(good.spans.length,3);
 const ids=good.tables[0].rows.map(r=>r[1]);assert.equal(new Set(ids).size,1);assert.ok(Number.parseFloat(metric(good,'p95 latency'))>=100);
});
