| 1 | import { test } from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | |
| 4 | import { mentionedHandles, mentions, mentionsColumn, plainOutside } from "./mentions.ts"; |
| 5 | |
| 6 | test("mentions are found once each, lowercased, in order", () => { |
| 7 | assert.deepEqual(mentionedHandles("@Ship can you cut it? cc @syntaqx and @ship"), ["ship", "syntaqx"]); |
| 8 | assert.deepEqual(mentionedHandles("(@review-bot) @a_b, @x."), ["review-bot", "a_b", "x"]); |
| 9 | assert.deepEqual(mentionedHandles("@ship\n@docs"), ["ship", "docs"]); |
| 10 | }); |
| 11 | |
| 12 | test("an email address or a stray @ is not a mention", () => { |
| 13 | assert.deepEqual(mentionedHandles("mail me@example.com"), []); |
| 14 | assert.deepEqual(mentionedHandles("@ alone, @@double, @-dash"), []); |
| 15 | assert.deepEqual(mentionedHandles(""), []); |
| 16 | }); |
| 17 | |
| 18 | test("a kept mention matches its whole handle only", () => { |
| 19 | const column = mentionsColumn(mentionedHandles("hey @bobby and @ship")); |
| 20 | assert.equal(column, " bobby ship "); |
| 21 | assert.equal(mentions(column, "bobby"), true); |
| 22 | assert.equal(mentions(column, "Ship"), true); |
| 23 | assert.equal(mentions(column, "bob"), false); |
| 24 | assert.equal(mentions(mentionsColumn([]), "bob"), false); |
| 25 | assert.equal(mentions(null, "bob"), false); |
| 26 | }); |
| 27 | |
| 28 | test("an agent's mention of someone outside the conversation becomes a plain name", () => { |
| 29 | const members = new Set(["syntaqx", "g1t"]); |
| 30 | assert.equal( |
| 31 | plainOutside("Ask @mike to help @syntaqx. @Triage, @bruno and @david know too; I'm @g1t.", members), |
| 32 | "Ask mike to help @syntaqx. Triage, bruno and david know too; I'm @g1t.", |
| 33 | ); |
| 34 | // Matching is by whole handle, whatever its case. |
| 35 | assert.equal(plainOutside("@SYNTAQX and @syntaqxx", members), "@SYNTAQX and syntaqxx"); |
| 36 | }); |
| 37 | |
| 38 | test("code, addresses and team mentions are left as written", () => { |
| 39 | const none = new Set<string>(); |
| 40 | assert.equal( |
| 41 | plainOutside("Use `@mike` or\n```py\n@property\ndef x(): ...\n```\nthen @mike", none), |
| 42 | "Use `@mike` or\n```py\n@property\ndef x(): ...\n```\nthen mike", |
| 43 | ); |
| 44 | assert.equal(plainOutside("mail me@example.com, ping @acme/web", none), "mail me@example.com, ping @acme/web"); |
| 45 | assert.equal(plainOutside("```\n@open fence", none), "```\n@open fence"); |
| 46 | }); |