// Run: npm test (Node strips the TypeScript types itself)
import { test } from "node:test";
import assert from "node:assert/strict";
import parse from "ltx/lib/parse.js";
import { parseAck, parseMessage, parseOwnDisplayed } from "../src/xmpp/parse.ts";
const me = "lukas@0ff.space";
const el = (s: string) => parse(s) as any;
test("live incoming message", () => {
const m = parseMessage(el(`
hi
`), { me })!;
assert.equal(m.peer, "anna@0ff.space");
assert.equal(m.dir, "in");
assert.equal(m.key, "anna@0ff.space|anna@0ff.space|o1");
assert.equal(m.archiveId, "A1");
assert.equal(m.receiptRequested, true);
assert.equal(m.markable, true);
});
test("stanza-id stamped by someone else is ignored", () => {
const m = parseMessage(el(`x
`), { me })!;
assert.equal(m.archiveId, undefined);
});
test("sent carbon and MAM copy of the same message share one key", () => {
const inner = `yo`;
const fwd = el(`${inner}`);
const carbon = parseMessage(el(inner), { me })!;
const mam = parseMessage(fwd.getChild("message"), { me, archiveId: "A2", forwarded: fwd })!;
assert.equal(carbon.dir, "out");
assert.equal(carbon.peer, "anna@0ff.space");
assert.equal(carbon.key, mam.key);
assert.equal(mam.archiveId, "A2");
assert.equal(mam.ts, Date.parse("2026-09-28T10:00:00Z"));
assert.equal(mam.status, "sent");
});
test("groupchat message keys by room/nick and trusts the room's stanza-id", () => {
const m = parseMessage(el(`hey
`), { me })!;
assert.equal(m.peer, "lobby@muc.0ff.space");
assert.equal(m.key, "lobby@muc.0ff.space|lobby@muc.0ff.space/anna|g1");
assert.equal(m.archiveId, "R1");
});
test("non-messages are skipped", () => {
assert.equal(parseMessage(el(``), { me }), null);
assert.equal(parseMessage(el(`b`), { me }), null);
});
test("receipts and markers point at our outgoing message", () => {
assert.deepEqual(parseAck(el(``), me),
{ key: "anna@0ff.space|lukas@0ff.space|m2", status: "delivered" });
assert.deepEqual(parseAck(el(``), me),
{ key: "anna@0ff.space|lukas@0ff.space|m2", status: "read" });
// Our own receipt echoed as a carbon is not about our messages.
assert.equal(parseAck(el(``), me), null);
});
test("our own displayed marker from another device marks the peer's message read", () => {
// Inner message of a sent carbon: Monal told anna it displayed her message m1.
assert.deepEqual(parseOwnDisplayed(el(``), me),
{ peer: "anna@0ff.space", key: "anna@0ff.space|anna@0ff.space|m1" });
// Anna's marker is not ours.
assert.equal(parseOwnDisplayed(el(``), me), null);
});
test("id-less messages get a stable key, so redelivery does not duplicate", () => {
const motd = `Welcome!`;
const a = parseMessage(el(motd), { me })!;
const b = parseMessage(el(motd), { me })!;
assert.equal(a.key, b.key);
assert.equal(a.peer, "0ff.space");
});
test("our own room message is recognised by nick and counts as delivered", () => {
const m = parseMessage(el(`hi all`), { me, nick: "lukas" })!;
assert.equal(m.dir, "out");
assert.equal(m.status, "delivered");
// Same key the client stores when sending as lukas in that room.
assert.equal(m.key, "lobby@muc.0ff.space|lobby@muc.0ff.space/lukas|g2");
});
test("mentions of our nick in rooms, whole word, any case", () => {
const msg = (body: string) => parseMessage(el(`${body}`), { me, nick: "lukas" })!;
assert.equal(msg("@Lukas can you check?").mention, true);
assert.equal(msg("lukas: ping").mention, true);
assert.equal(msg("lukasz is here").mention, undefined);
assert.equal(parseMessage(el(`lukas here`), { me, nick: "lukas" })!.mention, undefined);
});
test("private messages through a room are their own conversation", () => {
const isRoom = (j: string) => j.endsWith("@muc.0ff.space");
const incoming = parseMessage(el(`psst`), { me, isRoom })!;
assert.equal(incoming.peer, "lobby@muc.0ff.space/anna");
assert.equal(incoming.dir, "in");
// Our reply as a carbon from another device lands in the same conversation.
const out = parseMessage(el(`yes?`), { me, isRoom })!;
assert.equal(out.peer, "lobby@muc.0ff.space/anna");
assert.equal(out.key, "lobby@muc.0ff.space/anna|lukas@0ff.space|p2");
});