test(targets): table-drive slack and discord parse cases

This commit is contained in:
Peter Steinberger
2026-02-21 23:23:15 +00:00
parent 98790339ef
commit 7c248cca4a
2 changed files with 66 additions and 63 deletions

View File

@@ -10,43 +10,33 @@ vi.mock("./directory-live.js", () => ({
describe("parseDiscordTarget", () => { describe("parseDiscordTarget", () => {
it("parses user mention and prefixes", () => { it("parses user mention and prefixes", () => {
expect(parseDiscordTarget("<@123>")).toMatchObject({ const cases = [
kind: "user", { input: "<@123>", id: "123", normalized: "user:123" },
id: "123", { input: "<@!456>", id: "456", normalized: "user:456" },
normalized: "user:123", { input: "user:789", id: "789", normalized: "user:789" },
}); { input: "discord:987", id: "987", normalized: "user:987" },
expect(parseDiscordTarget("<@!456>")).toMatchObject({ ] as const;
kind: "user", for (const testCase of cases) {
id: "456", expect(parseDiscordTarget(testCase.input), testCase.input).toMatchObject({
normalized: "user:456", kind: "user",
}); id: testCase.id,
expect(parseDiscordTarget("user:789")).toMatchObject({ normalized: testCase.normalized,
kind: "user", });
id: "789", }
normalized: "user:789",
});
expect(parseDiscordTarget("discord:987")).toMatchObject({
kind: "user",
id: "987",
normalized: "user:987",
});
}); });
it("parses channel targets", () => { it("parses channel targets", () => {
expect(parseDiscordTarget("channel:555")).toMatchObject({ const cases = [
kind: "channel", { input: "channel:555", id: "555", normalized: "channel:555" },
id: "555", { input: "general", id: "general", normalized: "channel:general" },
normalized: "channel:555", ] as const;
}); for (const testCase of cases) {
expect(parseDiscordTarget("general")).toMatchObject({ expect(parseDiscordTarget(testCase.input), testCase.input).toMatchObject({
kind: "channel", kind: "channel",
id: "general", id: testCase.id,
normalized: "channel:general", normalized: testCase.normalized,
}); });
}); }
it("rejects ambiguous numeric ids without a default kind", () => {
expect(() => parseDiscordTarget("123")).toThrow(/Ambiguous Discord recipient/);
}); });
it("accepts numeric ids when a default kind is provided", () => { it("accepts numeric ids when a default kind is provided", () => {
@@ -57,8 +47,16 @@ describe("parseDiscordTarget", () => {
}); });
}); });
it("rejects non-numeric @ mentions", () => { it("rejects invalid parse targets", () => {
expect(() => parseDiscordTarget("@bob")).toThrow(/Discord DMs require a user id/); 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,
);
}
}); });
}); });

View File

@@ -4,39 +4,44 @@ import { parseSlackTarget, resolveSlackChannelId } from "./targets.js";
describe("parseSlackTarget", () => { describe("parseSlackTarget", () => {
it("parses user mentions and prefixes", () => { it("parses user mentions and prefixes", () => {
expect(parseSlackTarget("<@U123>")).toMatchObject({ const cases = [
kind: "user", { input: "<@U123>", id: "U123", normalized: "user:u123" },
id: "U123", { input: "user:U456", id: "U456", normalized: "user:u456" },
normalized: "user:u123", { input: "slack:U789", id: "U789", normalized: "user:u789" },
}); ] as const;
expect(parseSlackTarget("user:U456")).toMatchObject({ for (const testCase of cases) {
kind: "user", expect(parseSlackTarget(testCase.input), testCase.input).toMatchObject({
id: "U456", kind: "user",
normalized: "user:u456", id: testCase.id,
}); normalized: testCase.normalized,
expect(parseSlackTarget("slack:U789")).toMatchObject({ });
kind: "user", }
id: "U789",
normalized: "user:u789",
});
}); });
it("parses channel targets", () => { it("parses channel targets", () => {
expect(parseSlackTarget("channel:C123")).toMatchObject({ const cases = [
kind: "channel", { input: "channel:C123", id: "C123", normalized: "channel:c123" },
id: "C123", { input: "#C999", id: "C999", normalized: "channel:c999" },
normalized: "channel:c123", ] as const;
}); for (const testCase of cases) {
expect(parseSlackTarget("#C999")).toMatchObject({ expect(parseSlackTarget(testCase.input), testCase.input).toMatchObject({
kind: "channel", kind: "channel",
id: "C999", id: testCase.id,
normalized: "channel:c999", normalized: testCase.normalized,
}); });
}
}); });
it("rejects invalid @ and # targets", () => { it("rejects invalid @ and # targets", () => {
expect(() => parseSlackTarget("@bob-1")).toThrow(/Slack DMs require a user id/); const cases = [
expect(() => parseSlackTarget("#general-1")).toThrow(/Slack channels require a channel id/); { 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,
);
}
}); });
}); });