src.nth.io/

summaryrefslogtreecommitdiff
path: root/test/commissioning.test.ts
blob: 676a5e1642977f91ef6be72c49c369714e759102 (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
import { ManualPairingCodeCodec } from "@matter/main/types";
import { describe, expect, it } from "vitest";
import { parsePairingCode } from "../src/commissioning.js";

describe("parsePairingCode", () => {
  const validCode = ManualPairingCodeCodec.encode({ discriminator: 3840, passcode: 20202021 });

  it("parses a valid 11-digit manual code", () => {
    const parsed = parsePairingCode(validCode);
    expect(parsed.passcode).toBe(20202021);
    expect(parsed.shortDiscriminator).toBe(3840 >> 8);
  });

  it("accepts hyphens and whitespace", () => {
    const withHyphens = `${validCode.slice(0, 4)}-${validCode.slice(4, 7)}-${validCode.slice(7)}`;
    expect(parsePairingCode(` ${withHyphens} `).passcode).toBe(20202021);
  });

  it("rejects empty input", () => {
    expect(() => parsePairingCode("")).toThrow(/empty/);
    expect(() => parsePairingCode("   ")).toThrow(/empty/);
  });

  it("rejects codes of the wrong length", () => {
    expect(() => parsePairingCode("12345")).toThrow(/11 or 21 digits/);
    expect(() => parsePairingCode("123456789012")).toThrow(/11 or 21 digits/);
  });

  it("rejects codes with an invalid checksum", () => {
    const corrupted = validCode.slice(0, -1) + ((Number(validCode.at(-1)) + 1) % 10).toString();
    expect(() => parsePairingCode(corrupted)).toThrow();
  });
});