diff --git a/src/discord/targets.test.ts b/src/discord/targets.test.ts index e30b6a69bef..d3d4d3935ec 100644 --- a/src/discord/targets.test.ts +++ b/src/discord/targets.test.ts @@ -10,43 +10,33 @@ vi.mock("./directory-live.js", () => ({ describe("parseDiscordTarget", () => { it("parses user mention and prefixes", () => { - expect(parseDiscordTarget("<@123>")).toMatchObject({ - kind: "user", - id: "123", - normalized: "user:123", - }); - expect(parseDiscordTarget("<@!456>")).toMatchObject({ - kind: "user", - id: "456", - normalized: "user:456", - }); - expect(parseDiscordTarget("user:789")).toMatchObject({ - kind: "user", - id: "789", - normalized: "user:789", - }); - expect(parseDiscordTarget("discord:987")).toMatchObject({ - kind: "user", - id: "987", - normalized: "user:987", - }); + const cases = [ + { input: "<@123>", id: "123", normalized: "user:123" }, + { input: "<@!456>", id: "456", normalized: "user:456" }, + { input: "user:789", id: "789", normalized: "user:789" }, + { input: "discord:987", id: "987", normalized: "user:987" }, + ] as const; + for (const testCase of cases) { + expect(parseDiscordTarget(testCase.input), testCase.input).toMatchObject({ + kind: "user", + id: testCase.id, + normalized: testCase.normalized, + }); + } }); it("parses channel targets", () => { - expect(parseDiscordTarget("channel:555")).toMatchObject({ - kind: "channel", - id: "555", - normalized: "channel:555", - }); - expect(parseDiscordTarget("general")).toMatchObject({ - kind: "channel", - id: "general", - normalized: "channel:general", - }); - }); - - it("rejects ambiguous numeric ids without a default kind", () => { - expect(() => parseDiscordTarget("123")).toThrow(/Ambiguous Discord recipient/); + const cases = [ + { input: "channel:555", id: "555", normalized: "channel:555" }, + { input: "general", id: "general", normalized: "channel:general" }, + ] as const; + for (const testCase of cases) { + expect(parseDiscordTarget(testCase.input), testCase.input).toMatchObject({ + kind: "channel", + id: testCase.id, + normalized: testCase.normalized, + }); + } }); it("accepts numeric ids when a default kind is provided", () => { @@ -57,8 +47,16 @@ describe("parseDiscordTarget", () => { }); }); - it("rejects non-numeric @ mentions", () => { - expect(() => parseDiscordTarget("@bob")).toThrow(/Discord DMs require a user id/); + it("rejects invalid parse targets", () => { + const cases = [ + { input: "123", expectedMessage: /Ambiguous Discord recipient/ }, + { input: "@bob", expectedMessage: /Discord DMs require a user id/ }, + ] as const; + for (const testCase of cases) { + expect(() => parseDiscordTarget(testCase.input), testCase.input).toThrow( + testCase.expectedMessage, + ); + } }); }); diff --git a/src/slack/targets.test.ts b/src/slack/targets.test.ts index a15906884cb..5b56a5bd0da 100644 --- a/src/slack/targets.test.ts +++ b/src/slack/targets.test.ts @@ -4,39 +4,44 @@ import { parseSlackTarget, resolveSlackChannelId } from "./targets.js"; describe("parseSlackTarget", () => { it("parses user mentions and prefixes", () => { - expect(parseSlackTarget("<@U123>")).toMatchObject({ - kind: "user", - id: "U123", - normalized: "user:u123", - }); - expect(parseSlackTarget("user:U456")).toMatchObject({ - kind: "user", - id: "U456", - normalized: "user:u456", - }); - expect(parseSlackTarget("slack:U789")).toMatchObject({ - kind: "user", - id: "U789", - normalized: "user:u789", - }); + const cases = [ + { input: "<@U123>", id: "U123", normalized: "user:u123" }, + { input: "user:U456", id: "U456", normalized: "user:u456" }, + { input: "slack:U789", id: "U789", normalized: "user:u789" }, + ] as const; + for (const testCase of cases) { + expect(parseSlackTarget(testCase.input), testCase.input).toMatchObject({ + kind: "user", + id: testCase.id, + normalized: testCase.normalized, + }); + } }); it("parses channel targets", () => { - expect(parseSlackTarget("channel:C123")).toMatchObject({ - kind: "channel", - id: "C123", - normalized: "channel:c123", - }); - expect(parseSlackTarget("#C999")).toMatchObject({ - kind: "channel", - id: "C999", - normalized: "channel:c999", - }); + const cases = [ + { input: "channel:C123", id: "C123", normalized: "channel:c123" }, + { input: "#C999", id: "C999", normalized: "channel:c999" }, + ] as const; + for (const testCase of cases) { + expect(parseSlackTarget(testCase.input), testCase.input).toMatchObject({ + kind: "channel", + id: testCase.id, + normalized: testCase.normalized, + }); + } }); it("rejects invalid @ and # targets", () => { - expect(() => parseSlackTarget("@bob-1")).toThrow(/Slack DMs require a user id/); - expect(() => parseSlackTarget("#general-1")).toThrow(/Slack channels require a channel id/); + const cases = [ + { input: "@bob-1", expectedMessage: /Slack DMs require a user id/ }, + { input: "#general-1", expectedMessage: /Slack channels require a channel id/ }, + ] as const; + for (const testCase of cases) { + expect(() => parseSlackTarget(testCase.input), testCase.input).toThrow( + testCase.expectedMessage, + ); + } }); });