| 1 | import { describe, expect, test } from "bun:test"; |
| 2 | |
| 3 | import { findReferences, parseReferences |
| 4 | |
| 5 | describe("findReferences", () |
| 6 | test("finds a same-repository reference", |
| 7 | expect(findReferences("see #12 |
| 8 | { |
| 9 | kind: "issue", |
| 10 | index: 4, |
| 11 | length: 3, |
| 12 | owner: null, |
| 13 | repo: null, |
| 14 | number: 12, |
| 15 | closing: false, |
| 16 | }, |
| 17 | ]); |
| 18 | }); |
| 19 | |
| 20 | test("finds a cross-repository reference", |
| 21 | expect(findReferences("ghost/web.app#3 |
| 22 | { |
| 23 | kind: "issue", |
| 24 | index: 0, |
| 25 | length: 15, |
| 26 | owner: "ghost", |
| 27 | repo: "web.app", |
| 28 | number: 3, |
| 29 | closing: false, |
| 30 | }, |
| 31 | ]); |
| 32 | }); |
| 33 | |
| 34 | test("marks closing keywords, in any case, with or without a colon, and points at the reference only", |
| 35 | const [closes, |
| 36 | "Closes #1, fixed: #2 and RESOLVE a/b#3", |
| 37 | ); |
| 38 | expect(closes).toMatchObject |
| 39 | index: 7, |
| 40 | length: 2, |
| 41 | number: 1, |
| 42 | closing: true, |
| 43 | }); |
| 44 | expect(fixed).toMatchObject |
| 45 | expect(resolve).toMatchObject |
| 46 | owner: "a", |
| 47 | repo: "b", |
| 48 | number: 3, |
| 49 | closing: true, |
| 50 | }); |
| 51 | }); |
| 52 | |
| 53 | test("a keyword glued to a word does not close", |
| 54 | expect(findReferences("prefixes #1 |
| 55 | { number: 1, closing |
| 56 | ]); |
| 57 | }); |
| 58 | |
| 59 | test("ignores numbers that are not standalone", |
| 60 | expect(findReferences("abc#1 { a/#1 #1a #1#2 http://x.io/#4 |
| 61 | [], |
| 62 | ); |
| 63 | }); |
| 64 | |
| 65 | test("finds mentions but not email addresses, and drops a trailing dot", |
| 66 | expect(findReferences("thanks @bob.smith. mail a@b.com @jane-doe |
| 67 | [ |
| 68 | { kind: "mention", |
| 69 | { kind: "mention", |
| 70 | ], |
| 71 | ); |
| 72 | }); |
| 73 | |
| 74 | test("drops numbers no issue can have", |
| 75 | expect(findReferences("`#99999999999` |
| 76 | expect(findReferences("#2147483648 #0 |
| 77 | expect(findReferences("#2147483647 |
| 78 | { number: 2147483647 }, |
| 79 | ]); |
| 80 | }); |
| 81 | |
| 82 | test("sorts mixed kinds by position", |
| 83 | expect( |
| 84 | findReferences("@a fixes #1"). |
| 85 | ).toEqual(["mention" |
| 86 | }); |
| 87 | }); |
| 88 | |
| 89 | describe("parseReferences", () |
| 90 | test("skips code blocks, inline code and links", |
| 91 | const markdown = |
| 92 | "fixes #1 `#2`", |
| 93 | "```", |
| 94 | "#3", |
| 95 | "```", |
| 96 | " #4", |
| 97 | "[#5](https://x.io) https://x.io/a#6 <b>#7</b>", |
| 98 | ].join("\n" |
| 99 | expect( |
| 100 | parseReferences(markdown).map |
| 101 | (reference) => |
| 102 | ), |
| 103 | ).toEqual([1, |
| 104 | }); |
| 105 | }); |