~james/core

core/web/src/lib/components/timeline/group.test.ts -rw-r--r-- 7.7 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { describe, expect, it } from "vitest";
import { groupTimeline } from "#lib/components/timeline/group.ts";
import type * as OrgTangledFeedGetTimeline from "#lib/api/lexicons/types/org/tangled/feed/getTimeline.ts";

type TimelineItem = OrgTangledFeedGetTimeline.$output["feed"][number];

const alice = { did: "did:plc:alice", handle: "alice.test" };
const bob = { did: "did:plc:bob", handle: "bob.test" };
const charlie = { did: "did:plc:charlie", handle: "charlie.test" };
const dan = { did: "did:plc:dan", handle: "dan.test" };

let rkey = 0;

const repo = (owner: typeof alice, slug: string) => ({ did: owner.did, slug, owner });

const follow = (actor: typeof alice, subject: string, eventAt: string) =>
	({
		event: {
			$type: "org.tangled.feed.getTimeline#followEvent",
			uri: `at://${actor.did}/org.tangled.graph.follow/${rkey++}`,
			actor,
			subject: { did: `did:plc:${subject}`, handle: subject }
		},
		eventAt
	}) as unknown as TimelineItem;

const star = (actor: typeof alice, slug: string, eventAt: string) =>
	({
		event: {
			$type: "org.tangled.feed.getTimeline#starEvent",
			uri: `at://${actor.did}/org.tangled.feed.star/${rkey++}`,
			actor,
			repo: repo(bob, slug)
		},
		eventAt
	}) as unknown as TimelineItem;

const create = (owner: typeof alice, slug: string, eventAt: string) =>
	({
		event: {
			$type: "org.tangled.feed.getTimeline#repoEvent",
			uri: `at://${owner.did}/sh.tangled.repo/${rkey++}`,
			repo: repo(owner, slug)
		},
		eventAt
	}) as unknown as TimelineItem;

const fork = (owner: typeof alice, slug: string, eventAt: string) =>
	({
		event: {
			$type: "org.tangled.feed.getTimeline#repoEvent",
			uri: `at://${owner.did}/sh.tangled.repo/${rkey++}`,
			repo: repo(owner, slug),
			source: repo(bob, slug)
		},
		eventAt
	}) as unknown as TimelineItem;

const label = (item: TimelineItem) => {
	const event = item.event as {
		actor?: { handle: string };
		subject?: { handle: string };
		repo: { slug: string; owner: { handle: string } };
	};
	return event.subject?.handle ?? event.repo.slug;
};

const actorLabel = (item: TimelineItem) => {
	const event = item.event as { actor?: { handle: string }; repo: { owner: { handle: string } } };
	return event.actor?.handle ?? event.repo.owner.handle;
};

const shape = (feed: TimelineItem[]) =>
	groupTimeline(feed).map((entry) =>
		"actor" in entry
			? [entry.kind, entry.actor.handle, ...entry.items.map((i) => label(i as TimelineItem))]
			: [
					entry.kind,
					label(entry.items[0] as TimelineItem),
					...entry.items.map((i) => actorLabel(i as TimelineItem))
				]
	);

describe("groupTimeline", () => {
	it("merges consecutive follows by the same actor", () => {
		expect(
			shape([
				follow(alice, "a", "2026-01-01T12:00:00Z"),
				follow(alice, "b", "2026-01-01T11:30:00Z"),
				follow(alice, "c", "2026-01-01T11:00:01Z")
			])
		).toEqual([["follow", "alice.test", "a", "b", "c"]]);
	});

	it("merges stars, creations and forks the same way", () => {
		expect(
			shape([
				star(alice, "a", "2026-01-01T12:00:00Z"),
				star(alice, "b", "2026-01-01T11:59:00Z"),
				create(alice, "c", "2026-01-01T11:58:00Z"),
				create(alice, "d", "2026-01-01T11:57:00Z"),
				fork(alice, "e", "2026-01-01T11:56:00Z"),
				fork(alice, "f", "2026-01-01T11:55:00Z")
			])
		).toEqual([
			["star", "alice.test", "a", "b"],
			["create", "alice.test", "c", "d"],
			["fork", "alice.test", "e", "f"]
		]);
	});

	it("attributes repo events to the repo owner", () => {
		expect(
			shape([
				create(alice, "a", "2026-01-01T12:00:00Z"),
				create(bob, "b", "2026-01-01T11:59:00Z")
			])
		).toEqual([
			["create", "alice.test", "a"],
			["create", "bob.test", "b"]
		]);
	});

	it("merges an uninterrupted run by one actor regardless of time", () => {
		expect(
			shape([
				star(alice, "a", "2026-01-01T12:00:00Z"),
				star(alice, "b", "2026-01-01T08:00:00Z")
			])
		).toEqual([["star", "alice.test", "a", "b"]]);
	});

	it("merges across another actor's event within an hour", () => {
		expect(
			shape([
				star(alice, "a", "2026-01-01T12:00:00Z"),
				star(bob, "b", "2026-01-01T11:50:00Z"),
				star(alice, "c", "2026-01-01T11:30:00Z")
			])
		).toEqual([
			["star", "alice.test", "a", "c"],
			["star", "bob.test", "b"]
		]);
	});

	it("splits across another actor's event after an hour or more", () => {
		expect(
			shape([
				star(alice, "a", "2026-01-01T12:00:00Z"),
				star(bob, "b", "2026-01-01T11:30:00Z"),
				star(alice, "c", "2026-01-01T11:00:00Z")
			])
		).toEqual([
			["star", "alice.test", "a"],
			["star", "bob.test", "b"],
			["star", "alice.test", "c"]
		]);
	});

	it("merges across the same actor's events of another kind", () => {
		expect(
			shape([
				follow(alice, "a", "2026-01-01T12:00:00Z"),
				star(alice, "b", "2026-01-01T09:00:00Z"),
				follow(alice, "c", "2026-01-01T08:00:00Z"),
				star(alice, "d", "2026-01-01T07:00:00Z"),
				star(alice, "e", "2026-01-01T06:00:00Z")
			])
		).toEqual([
			["follow", "alice.test", "a", "c"],
			["star", "alice.test", "b", "d", "e"]
		]);
	});

	it("measures the window between neighbours, not from the first event", () => {
		expect(
			shape([
				follow(alice, "a", "2026-01-01T12:00:00Z"),
				follow(alice, "b", "2026-01-01T11:20:00Z"),
				follow(alice, "c", "2026-01-01T10:40:00Z")
			])
		).toEqual([["follow", "alice.test", "a", "b", "c"]]);
	});

	it("splits on a different actor", () => {
		expect(
			shape([
				follow(alice, "a", "2026-01-01T12:00:00Z"),
				follow(bob, "b", "2026-01-01T11:59:00Z")
			])
		).toEqual([
			["follow", "alice.test", "a"],
			["follow", "bob.test", "b"]
		]);
	});

	it("keys each entry by its first event uri", () => {
		const feed = [
			follow(alice, "a", "2026-01-01T12:00:00Z"),
			follow(alice, "b", "2026-01-01T11:59:00Z")
		];
		expect(groupTimeline(feed).map((e) => e.key)).toEqual([feed[0].event.uri]);
	});

	it("groups three or more actors on the same repo into one stargazers card", () => {
		expect(
			shape([
				star(alice, "core", "2026-01-01T12:00:00Z"),
				create(dan, "x", "2026-01-01T11:00:00Z"),
				star(bob, "core", "2026-01-01T10:00:00Z"),
				star(charlie, "core", "2026-01-01T09:00:00Z")
			])
		).toEqual([
			["stargazers", "core", "alice.test", "bob.test", "charlie.test"],
			["create", "dan.test", "x"]
		]);
	});

	it("leaves a repo starred by fewer than three actors to actor grouping", () => {
		expect(
			shape([
				star(alice, "core", "2026-01-01T12:00:00Z"),
				star(bob, "core", "2026-01-01T11:59:00Z")
			])
		).toEqual([
			["star", "alice.test", "core"],
			["star", "bob.test", "core"]
		]);
	});

	it("counts distinct actors towards the threshold", () => {
		expect(
			shape([
				star(alice, "core", "2026-01-01T12:00:00Z"),
				star(alice, "core", "2026-01-01T11:59:00Z"),
				star(bob, "core", "2026-01-01T11:58:00Z")
			])
		).toEqual([
			["star", "alice.test", "core", "core"],
			["star", "bob.test", "core"]
		]);
	});

	it("takes a popular target out of an actor burst without breaking it", () => {
		expect(
			shape([
				star(alice, "a", "2026-01-01T12:00:00Z"),
				star(alice, "core", "2026-01-01T11:59:00Z"),
				star(bob, "core", "2026-01-01T11:00:00Z"),
				star(alice, "b", "2026-01-01T09:00:00Z"),
				star(charlie, "core", "2026-01-01T08:00:00Z")
			])
		).toEqual([
			["star", "alice.test", "a", "b"],
			["stargazers", "core", "alice.test", "bob.test", "charlie.test"]
		]);
	});

	it("groups popular follow and fork targets", () => {
		expect(
			shape([
				follow(alice, "hub", "2026-01-01T12:00:00Z"),
				fork(alice, "core", "2026-01-01T11:59:00Z"),
				follow(bob, "hub", "2026-01-01T11:58:00Z"),
				fork(bob, "core", "2026-01-01T11:57:00Z"),
				follow(charlie, "hub", "2026-01-01T11:56:00Z"),
				fork(charlie, "core", "2026-01-01T11:55:00Z")
			])
		).toEqual([
			["followers", "hub", "alice.test", "bob.test", "charlie.test"],
			["forkers", "core", "alice.test", "bob.test", "charlie.test"]
		]);
	});
});