src.nth.io/

summaryrefslogtreecommitdiff
path: root/test/fabrics.test.ts
blob: b3cd18eddd3a1884f8cfa5a6e04cc1977678eca5 (plain)
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
import { describe, expect, it } from "vitest";
import { findLikelyStrays, formatFabricTable, type FabricTable } from "../src/fabrics.js";

const table: FabricTable = {
  supportedFabrics: 5,
  commissionedFabrics: 4,
  entries: [
    { fabricIndex: 1, fabricId: 100n, nodeId: 10n, vendorId: 0x1349, label: "Apple Home", isOurs: false },
    { fabricIndex: 2, fabricId: 200n, nodeId: 20n, vendorId: 0xfff1, label: "mattertimesync", isOurs: true },
    { fabricIndex: 3, fabricId: 300n, nodeId: 30n, vendorId: 0xfff1, label: "mattertimesync", isOurs: false },
    { fabricIndex: 4, fabricId: 400n, nodeId: 40n, vendorId: 0xfff1, label: "mattertimesync", isOurs: false },
  ],
};

describe("findLikelyStrays", () => {
  it("flags entries with our label that are not our identity", () => {
    expect(findLikelyStrays(table).map(entry => entry.fabricIndex)).toEqual([3, 4]);
  });

  it("never flags other ecosystems' entries", () => {
    const strays = findLikelyStrays(table);
    expect(strays.some(entry => entry.label === "Apple Home")).toBe(false);
  });

  it("flags nothing when our identity is not in the table", () => {
    const withoutOurs: FabricTable = {
      ...table,
      entries: table.entries.map(entry => ({ ...entry, isOurs: false })),
    };
    expect(findLikelyStrays(withoutOurs)).toEqual([]);
  });
});

describe("formatFabricTable", () => {
  const output = formatFabricTable(1n, table);

  it("shows slot usage and marks our entry", () => {
    expect(output).toContain("4 of 5 slots used");
    expect(output).toContain("[this controller]");
  });

  it("names known vendors and lists stale indices", () => {
    expect(output).toContain("(Apple)");
    expect(output).toContain("index 3, index 4");
  });
});