File size: 2,994 Bytes
488aedf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Tests for the metadata.user_id builder (bundle `E2e`/`UIo`/`bnt` mirror)
 * and the shared prefix stripper (`jfr`).
 */
import { describe, it, expect } from "bun:test";
import { buildAnthropicMetadataUserId, buildZcodeTraceHeaders, stripHeaderInternalPrefixes } from "./trace-headers.js";

describe("buildAnthropicMetadataUserId (bundle UIo)", () => {
  it("emits the exact JSON blob: device_id, always-empty account_uuid, prefix-stripped session_id", () => {
    expect(buildAnthropicMetadataUserId("mid-123", "sess_abc")).toBe(
      '{"device_id":"mid-123","account_uuid":"","session_id":"abc"}',
    );
  });

  it("strips subagent_agent_ prefixes too (bnt prefix set = NIo/LIo)", () => {
    expect(buildAnthropicMetadataUserId("mid-123", "subagent_agent_xyz")).toBe(
      '{"device_id":"mid-123","account_uuid":"","session_id":"xyz"}',
    );
  });

  it("omits device_id when deviceMid is absent (UIo passes the property through; JSON.stringify drops undefined)", () => {
    expect(buildAnthropicMetadataUserId(undefined, "sess_abc")).toBe(
      '{"account_uuid":"","session_id":"abc"}',
    );
  });

  it("falls back to empty session_id when no session is available (bnt(undefined) → ?? \"\")", () => {
    expect(buildAnthropicMetadataUserId("mid-123", undefined)).toBe(
      '{"device_id":"mid-123","account_uuid":"","session_id":""}',
    );
  });

  it("never carries the account uuid — account_uuid is hardcoded empty in the bundle", () => {
    const out = buildAnthropicMetadataUserId("mid", "sess_s");
    expect(JSON.parse(out).account_uuid).toBe("");
  });
});

describe("stripHeaderInternalPrefixes (bundle jfr)", () => {
  it("returns the original when stripping everything would leave an empty string", () => {
    expect(stripHeaderInternalPrefixes("sess_", ["sess_"])).toBe("sess_");
  });
});

describe("buildZcodeTraceHeaders — session-type derivation (gateway system-shape pairing)", () => {
  it("derives subagent from a subagent_agent_ session id, strips the prefix on the wire x-session-id", () => {
    const h = buildZcodeTraceHeaders({ sessionId: "subagent_agent_656fafe3-1" });
    expect(h["x-zcode-session-type"]).toBe("subagent");
    expect(h["x-session-id"]).toBe("656fafe3-1"); // prefix is internal-only (bundle loo strip)
  });

  it("explicit sessionType wins even without a session id (observe/lineage-fail pairing fallback)", () => {
    const h = buildZcodeTraceHeaders({ sessionType: "subagent" });
    expect(h["x-zcode-session-type"]).toBe("subagent");
    expect(h["x-session-id"]).toBeUndefined();
  });

  it("explicit sessionType beats a contradicting session-id prefix", () => {
    const h = buildZcodeTraceHeaders({ sessionId: "subagent_agent_x", sessionType: "main" });
    expect(h["x-zcode-session-type"]).toBe("main");
  });

  it("default stays main for unprefixed session ids", () => {
    const h = buildZcodeTraceHeaders({ sessionId: "656fafe3-2" });
    expect(h["x-zcode-session-type"]).toBe("main");
  });
});