-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost-repository.test.ts
More file actions
178 lines (147 loc) · 5.07 KB
/
Copy pathpost-repository.test.ts
File metadata and controls
178 lines (147 loc) · 5.07 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
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
// @vitest-environment node
import fs from 'fs';
import path from 'path';
import { describe, expect, it, vi } from 'vitest';
import {
calculateReadingTime,
getFeedData,
getFolderSlug,
getSortedFeedData,
getAllFeedSlugs,
} from './post-repository';
const postsDirectory = path.join(process.cwd(), 'posts');
describe('post repository utilities', () => {
it('ignores fenced code blocks and hidden details when calculating reading time', () => {
const proseOnly = '가'.repeat(1400);
const content = `${proseOnly}
\`\`\`text
${'x'.repeat(5000)}
\`\`\`
<details>
<summary>hidden</summary>
${'y'.repeat(5000)}
</details>`;
expect(calculateReadingTime(content)).toBe(2);
});
it('weights markdown tables lighter than prose when calculating reading time', () => {
const proseContent = '가'.repeat(1000);
const tableContent = `| column |
| --- |
| ${'가'.repeat(1000)} |`;
expect(calculateReadingTime(proseContent)).toBe(2);
expect(calculateReadingTime(tableContent)).toBe(1);
});
it('returns posts sorted by date in descending order', () => {
const posts = getSortedFeedData();
expect(posts.length).toBeGreaterThan(0);
for (let index = 1; index < posts.length; index += 1) {
expect(posts[index - 1].date >= posts[index].date).toBe(true);
}
});
it('fills image metadata from mdx content when meta image is missing', () => {
const posts = getSortedFeedData();
const hasExtractedImage = posts.some(
(post) => typeof post.image === 'string' && post.image.length > 0
);
expect(hasExtractedImage).toBe(true);
});
it('returns slug list with all feed entries', () => {
const slugs = getAllFeedSlugs();
expect(slugs.length).toBeGreaterThan(0);
expect(slugs.every((item) => item.slug.length > 0)).toBe(true);
});
it('filters private posts out of the default listing and static params', () => {
const publicPosts = getSortedFeedData();
const allPosts = getSortedFeedData({ includePrivate: true });
const publicSlugs = new Set(publicPosts.map((post) => post.slug));
const privatePosts = allPosts.filter(
(post) => post.visibility === 'private'
);
expect(privatePosts.length).toBeGreaterThan(0);
const publicStaticSlugs = new Set(
getAllFeedSlugs().map((item) => item.slug)
);
for (const post of privatePosts) {
expect(publicSlugs.has(post.slug), `${post.slug} leaked to listing`).toBe(
false
);
expect(
publicStaticSlugs.has(post.slug),
`${post.slug} leaked to static params`
).toBe(false);
}
});
it('returns private posts only when includePrivate is true', async () => {
const privateSlug = 'fixed-ai-dev-environment';
expect(getFolderSlug(privateSlug)).not.toBeNull();
expect(await getFeedData(privateSlug)).toBeNull();
});
it('resolves percent-encoded slugs', () => {
const slug = '말하는-구조를-잃어버린-것-같았다';
expect(getFolderSlug(encodeURIComponent(slug))).toBe(slug);
});
it('returns null for non-existent posts folder slug', () => {
expect(getFolderSlug('missing-folder-slug')).toBeNull();
});
it('returns null for unknown feed detail path', async () => {
const feed = await getFeedData('does-not-exist');
expect(feed).toBeNull();
});
it('returns empty feed list when reading posts directory fails', () => {
const readdirSpy = vi.spyOn(fs, 'readdirSync').mockImplementation(() => {
throw new Error('Posts directory unavailable');
});
try {
expect(getAllFeedSlugs()).toEqual([]);
expect(getSortedFeedData()).toEqual([]);
} finally {
readdirSpy.mockRestore();
}
});
it('skips invalid metadata entries and keeps parser failures from breaking list', () => {
const readdirSpy = vi
.spyOn(fs, 'readdirSync')
.mockImplementation((target) => {
const targetPath = String(target);
if (targetPath === postsDirectory) {
return ['broken-folder'];
}
if (targetPath === path.join(postsDirectory, 'broken-folder')) {
return [];
}
return [];
});
const existsSpy = vi
.spyOn(fs, 'existsSync')
.mockImplementation((target) => {
const targetPath = String(target);
return (
targetPath ===
path.join(postsDirectory, 'broken-folder', 'index.mdx') ||
targetPath === path.join(postsDirectory, 'broken-folder', 'meta.json')
);
});
const statSpy = vi
.spyOn(fs, 'statSync')
.mockImplementation(() => ({ isDirectory: () => true }) as fs.Stats);
const readFileSpy = vi
.spyOn(fs, 'readFileSync')
.mockImplementation((target) => {
const targetPath = String(target);
if (
targetPath === path.join(postsDirectory, 'broken-folder', 'meta.json')
) {
return '{invalid-json';
}
return '';
});
try {
expect(getSortedFeedData()).toEqual([]);
} finally {
readdirSpy.mockRestore();
existsSpy.mockRestore();
statSpy.mockRestore();
readFileSpy.mockRestore();
}
});
});