~james/core

core/web/src/lib/components/ui/markdownSuggestions.test.ts -rw-r--r-- 1.9 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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) "
		]);
	});
});