Repository navigation
Expand file tree
/
Copy pathreportedPosts.test.ts
More file actions
118 lines (107 loc) · 2.9 KB
/
Copy pathreportedPosts.test.ts
File metadata and controls
118 lines (107 loc) · 2.9 KB
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
import { describe, expect, it } from "vitest";
import {
groupReportedPosts,
toLegacyReason,
type PostReportRow,
} from "./reportedPosts";
const row = (overrides: Partial<PostReportRow>): PostReportRow => ({
reportId: 1,
reason: "SPAM",
details: null,
reportCreatedAt: "2026-01-01 10:00:00+00",
postId: "post-a",
title: "Post A",
slug: "post-a-abc123",
authorId: "user-1",
authorUsername: "ada",
authorName: "Ada",
...overrides,
});
describe("toLegacyReason", () => {
it("maps a reports.reason value onto the legacy enum form", () => {
expect(toLegacyReason("off_topic")).toBe("OFF_TOPIC");
expect(toLegacyReason("SPAM")).toBe("SPAM");
});
});
describe("groupReportedPosts", () => {
it("returns nothing when neither table has open reports", () => {
expect(groupReportedPosts([], [])).toEqual([]);
});
it("surfaces a post that only has a system report", () => {
const [post] = groupReportedPosts(
[],
[row({ reportId: 7, reason: "nsfw", details: "Auto-flagged" })],
);
expect(post).toMatchObject({
id: "post-a",
title: "Post A",
authorUsername: "ada",
reportCount: 1,
reportIds: [],
systemReportIds: [7],
latestReason: "NSFW",
latestDetails: "Auto-flagged",
});
});
it("keeps colliding ids from the two tables in separate fields", () => {
const [post] = groupReportedPosts(
[row({ reportId: 5 })],
[row({ reportId: 5, reason: "spam" })],
);
expect(post.reportCount).toBe(2);
expect(post.reportIds).toEqual([5]);
expect(post.systemReportIds).toEqual([5]);
});
it("takes the headline from the newest report across both tables", () => {
const [post] = groupReportedPosts(
[
row({
reportId: 1,
reason: "HARASSMENT",
details: "member report",
reportCreatedAt: "2026-01-01 10:00:00+00",
}),
],
[
row({
reportId: 2,
reason: "off_topic",
details: "system report",
reportCreatedAt: "2026-01-02 09:00:00.5+00",
}),
],
);
expect(post.latestReason).toBe("OFF_TOPIC");
expect(post.latestDetails).toBe("system report");
expect(post.latestReportAt).toBe("2026-01-02 09:00:00.5+00");
});
it("orders posts by their most recent report", () => {
const posts = groupReportedPosts(
[
row({
reportId: 1,
postId: "post-a",
reportCreatedAt: "2026-01-03 10:00:00+00",
}),
row({
reportId: 2,
postId: "post-b",
reportCreatedAt: "2026-01-01 10:00:00+00",
}),
],
[
row({
reportId: 1,
postId: "post-c",
reason: "spam",
reportCreatedAt: "2026-01-02 10:00:00+00",
}),
],
);
expect(posts.map((post) => post.id)).toEqual([
"post-a",
"post-c",
"post-b",
]);
});
});