import { test } from "node:test";
import assert from "node:assert/strict";
import { buildReadKeys } from "../whatsappReadKeys.ts";

test("buildReadKeys creates DM read keys without participants", () => {
  assert.deepEqual(buildReadKeys("123@s.whatsapp.net", [{ id: "m1", sender: "123@s.whatsapp.net" }]), [{
    remoteJid: "123@s.whatsapp.net",
    id: "m1",
    fromMe: false,
    participant: undefined,
  }]);
});

test("buildReadKeys uses sender as group participant", () => {
  assert.deepEqual(buildReadKeys("123@g.us", [{ id: "m1", sender: "456@s.whatsapp.net" }]), [{
    remoteJid: "123@g.us",
    id: "m1",
    fromMe: false,
    participant: "456@s.whatsapp.net",
  }]);
});

test("buildReadKeys explicit messageIds bypass stored rows", () => {
  assert.deepEqual(buildReadKeys("123@g.us", [{ id: "old", sender: "456@s.whatsapp.net" }], ["m2"]), [{
    remoteJid: "123@g.us",
    id: "m2",
    fromMe: false,
    participant: undefined,
  }]);
});

test("buildReadKeys returns no keys for empty input", () => {
  assert.deepEqual(buildReadKeys("123@s.whatsapp.net", []), []);
});
