import { describe, expect, it, vi } from "vitest";
import type { BobbinContext } from "#lib/api/client.ts";
import { loadTickets, recommendUsers } from "#lib/components/ui/markdownSuggestions.ts";
const record = (collection: string, rkey: string, createdAt: string) => ({
uri: `at://did:plc:owner/${collection}/${rkey}`,
state: "open",
value: { title: { text: `ticket ${rkey}` }, createdAt }
});
vi.mock("#lib/api/issue.ts", () => ({
listIssues: vi.fn(async () => ({
items: [record("sh.tangled.repo.issue", "12", "2026-01-01T00:00:00Z")]
}))
}));
vi.mock("#lib/api/records.ts", () => ({
listPulls: vi.fn(async () => ({
items: [record("sh.tangled.repo.pull", "34", "2026-01-02T00:00:00Z")]
}))
}));
const person = (n: number) => ({ did: `did:plc:${n}`, handle: `user${n}.test` });
describe("recommendUsers", () => {
it("keeps the first five distinct people in order", () => {
const people = [
person(1),
person(2),
person(1),
person(3),
person(4),
person(5),
person(6)
];
expect(recommendUsers(people, undefined).map((user) => user.handle)).toEqual([
"user1.test",
"user2.test",
"user3.test",
"user4.test",
"user5.test"
]);
});
it("skips the viewer and people without a usable handle", () => {
const people = [
person(1),
{ did: "did:plc:x" },
{ did: "did:plc:y", handle: "handle.invalid" },
person(2)
];
expect(recommendUsers(people, "did:plc:1").map((user) => user.handle)).toEqual([
"user2.test"
]);
});
it("inserts a mention with a trailing space", () => {
expect(recommendUsers([person(7)], undefined)[0].insert).toBe("@user7.test ");
});
});
describe("loadTickets", () => {
it("links each ticket by its number", async () => {
const tickets = await loadTickets({} as BobbinContext, {
did: "did:plc:repo",
url: "/alice.test/repo"
});
expect(tickets.map((ticket) => ticket.insert)).toEqual([
"[#34](/alice.test/repo/pulls/34) ",
"[#12](/alice.test/repo/issues/12) "
]);
});
});