File size: 2,836 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { describe, expect, it, vi } from "vitest";
import { issuePairingChallenge } from "./pairing-challenge.js";

describe("issuePairingChallenge", () => {
  it("creates and sends a pairing reply when request is newly created", async () => {
    const sent: string[] = [];

    const result = await issuePairingChallenge({
      channel: "telegram",
      senderId: "123",
      senderIdLine: "Your Telegram user id: 123",
      upsertPairingRequest: async () => ({ code: "ABCD", created: true }),
      sendPairingReply: async (text) => {
        sent.push(text);
      },
    });

    expect(result).toEqual({ created: true, code: "ABCD" });
    expect(sent).toHaveLength(1);
    expect(sent[0]).toContain("ABCD");
  });

  it("does not send a reply when request already exists", async () => {
    const sendPairingReply = vi.fn(async () => {});

    const result = await issuePairingChallenge({
      channel: "telegram",
      senderId: "123",
      senderIdLine: "Your Telegram user id: 123",
      upsertPairingRequest: async () => ({ code: "ABCD", created: false }),
      sendPairingReply,
    });

    expect(result).toEqual({ created: false });
    expect(sendPairingReply).not.toHaveBeenCalled();
  });

  it("supports custom reply text builder", async () => {
    const sent: string[] = [];

    await issuePairingChallenge({
      channel: "line",
      senderId: "u1",
      senderIdLine: "Your line id: u1",
      upsertPairingRequest: async () => ({ code: "ZXCV", created: true }),
      buildReplyText: ({ code }) => `custom ${code}`,
      sendPairingReply: async (text) => {
        sent.push(text);
      },
    });

    expect(sent).toEqual(["custom ZXCV"]);
  });

  it("calls onCreated and forwards meta to upsert", async () => {
    const onCreated = vi.fn();
    const upsert = vi.fn(async () => ({ code: "1111", created: true }));

    await issuePairingChallenge({
      channel: "discord",
      senderId: "42",
      senderIdLine: "Your Discord user id: 42",
      meta: { name: "alice" },
      upsertPairingRequest: upsert,
      onCreated,
      sendPairingReply: async () => {},
    });

    expect(upsert).toHaveBeenCalledWith({ id: "42", meta: { name: "alice" } });
    expect(onCreated).toHaveBeenCalledWith({ code: "1111" });
  });

  it("captures reply errors through onReplyError", async () => {
    const onReplyError = vi.fn();

    const result = await issuePairingChallenge({
      channel: "signal",
      senderId: "+1555",
      senderIdLine: "Your Signal sender id: +1555",
      upsertPairingRequest: async () => ({ code: "9999", created: true }),
      sendPairingReply: async () => {
        throw new Error("send failed");
      },
      onReplyError,
    });

    expect(result).toEqual({ created: true, code: "9999" });
    expect(onReplyError).toHaveBeenCalledTimes(1);
  });
});