File size: 1,648 Bytes
e18726e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it } from "vitest";
import {
  normalizeMessageClientSources,
  readMessageClientSources,
} from "./message-client-source.js";

describe("message client sources", () => {
  it("retains distinct reported apps without treating extra client fields as attribution", () => {
    const client = {
      id: "cli",
      mode: "cli",
      displayName: " Release helper ",
      platform: "linux",
      instanceId: "private-instance",
    };
    const sources = normalizeMessageClientSources([
      client,
      client,
      { id: "cli", mode: "cli", displayName: "Another helper" },
      { id: "not-a-client", mode: "cli", displayName: "Forged sender" },
      { id: "cli", mode: "not-a-mode" },
    ]);
    client.displayName = "Changed after admission";
    expect(sources).toEqual([
      { id: "cli", mode: "cli", displayName: "Release helper" },
      { id: "cli", mode: "cli", displayName: "Another helper" },
    ]);
  });

  it("bounds source labels without splitting a Unicode character", () => {
    expect(
      normalizeMessageClientSources([
        { id: "cli", mode: "cli", displayName: `${"a".repeat(199)}🦞` },
      ]),
    ).toEqual([{ id: "cli", mode: "cli", displayName: "a".repeat(199) }]);
  });

  it("uses only recorded transport sources and leaves older messages unlabeled", () => {
    expect(
      readMessageClientSources({ role: "user", senderLabel: "CLI", content: "From CLI" }),
    ).toEqual([]);
    expect(
      readMessageClientSources({
        __openclaw: { transport: { clients: [{ id: "cli", mode: "cli" }] } },
      }),
    ).toEqual([{ id: "cli", mode: "cli" }]);
  });
});