|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { formatMessage } from "./message"; |
| 3 | + |
| 4 | +describe("formatMessage", () => { |
| 5 | + describe("markdown to Slack formatting", () => { |
| 6 | + test("converts markdown links to Slack format", () => { |
| 7 | + expect(formatMessage("[click here](https://example.com)")).toBe( |
| 8 | + "<https://example.com|click here>" |
| 9 | + ); |
| 10 | + }); |
| 11 | + |
| 12 | + test("converts double-star bold to single-star bold", () => { |
| 13 | + expect(formatMessage("**hello**")).toBe("*hello*"); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + describe("user ID mention wrapping", () => { |
| 18 | + test("wraps @-prefixed Slack user IDs", () => { |
| 19 | + expect(formatMessage("@U02UD2WE3HA")).toBe("<@U02UD2WE3HA>"); |
| 20 | + }); |
| 21 | + |
| 22 | + test("wraps bare Slack user IDs", () => { |
| 23 | + expect(formatMessage("user U02UD2WE3HA said")).toBe( |
| 24 | + "user <@U02UD2WE3HA> said" |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + test("wraps workspace IDs starting with W", () => { |
| 29 | + expect(formatMessage("@W01AB2CD3EF")).toBe("<@W01AB2CD3EF>"); |
| 30 | + }); |
| 31 | + |
| 32 | + test("does not double-wrap already bracketed IDs", () => { |
| 33 | + expect(formatMessage("<@U02UD2WE3HA>")).toBe("<@U02UD2WE3HA>"); |
| 34 | + }); |
| 35 | + |
| 36 | + test("removes brackets from non-ID @handles", () => { |
| 37 | + expect(formatMessage("<@john.doe>")).toBe("@john.doe"); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe("false-positive prevention", () => { |
| 42 | + test("does not match pure-alpha words like WORKSPACE", () => { |
| 43 | + const input = "CODER_WORKSPACE_IS_PREBUILD_CLAIM=true"; |
| 44 | + expect(formatMessage(input)).toBe(input); |
| 45 | + }); |
| 46 | + |
| 47 | + test("does not match UPPERCASE without digits", () => { |
| 48 | + expect(formatMessage("UNDEFINED")).toBe("UNDEFINED"); |
| 49 | + expect(formatMessage("WORKSPACEID")).toBe("WORKSPACEID"); |
| 50 | + }); |
| 51 | + |
| 52 | + test("does not match short IDs", () => { |
| 53 | + expect(formatMessage("U1234")).toBe("U1234"); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe("code block preservation", () => { |
| 58 | + test("does not format IDs inside inline code", () => { |
| 59 | + const input = "`U02UD2WE3HA`"; |
| 60 | + expect(formatMessage(input)).toBe(input); |
| 61 | + }); |
| 62 | + |
| 63 | + test("does not format IDs inside code blocks", () => { |
| 64 | + const input = "```\nU02UD2WE3HA\n```"; |
| 65 | + expect(formatMessage(input)).toBe(input); |
| 66 | + }); |
| 67 | + |
| 68 | + test("formats IDs outside code but preserves code content", () => { |
| 69 | + const input = "user U02UD2WE3HA said `U02UD2WE3HA`"; |
| 70 | + expect(formatMessage(input)).toBe( |
| 71 | + "user <@U02UD2WE3HA> said `U02UD2WE3HA`" |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + test("preserves markdown links inside code blocks", () => { |
| 76 | + const input = "```\n[text](url)\n```"; |
| 77 | + expect(formatMessage(input)).toBe(input); |
| 78 | + }); |
| 79 | + |
| 80 | + test("preserves bold syntax inside inline code", () => { |
| 81 | + const input = "`**not bold**`"; |
| 82 | + expect(formatMessage(input)).toBe(input); |
| 83 | + }); |
| 84 | + |
| 85 | + test("handles multiple code blocks", () => { |
| 86 | + const input = "`U02UD2WE3HA` and U02UD2WE3HA and ```U02UD2WE3HA```"; |
| 87 | + expect(formatMessage(input)).toBe( |
| 88 | + "`U02UD2WE3HA` and <@U02UD2WE3HA> and ```U02UD2WE3HA```" |
| 89 | + ); |
| 90 | + }); |
| 91 | + |
| 92 | + test("does not format IDs inside code blocks with more than 3 backticks", () => { |
| 93 | + const input = "````\nU02UD2WE3HA\n````"; |
| 94 | + expect(formatMessage(input)).toBe(input); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("truncation", () => { |
| 99 | + test("truncates text longer than 3000 characters", () => { |
| 100 | + const input = "a".repeat(4000); |
| 101 | + expect(formatMessage(input).length).toBe(3000); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments