-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.test.js
More file actions
238 lines (205 loc) · 6.56 KB
/
Copy pathbuild.test.js
File metadata and controls
238 lines (205 loc) · 6.56 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
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
import { describe, test, expect, beforeAll, afterAll } from 'bun:test';
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
const TEST_DIR = path.join(import.meta.dir, 'test-fixtures');
const TEST_BUILD_DIR = path.join(TEST_DIR, 'build');
describe('Build Script', () => {
beforeAll(() => {
// Create test fixtures
if (!fs.existsSync(TEST_DIR)) {
fs.mkdirSync(TEST_DIR, { recursive: true });
}
// Create test content
fs.mkdirSync(path.join(TEST_DIR, 'content'), { recursive: true });
fs.writeFileSync(
path.join(TEST_DIR, 'content', 'test.md'),
`---
title: Test Page
date: 2024-01-01
template: default
---
# Hello World
This is a test.`
);
fs.writeFileSync(
path.join(TEST_DIR, 'content', 'data-test.md'),
`---
title: Data Test
template: data-page
---
Using site data.`
);
// Create test template
fs.mkdirSync(path.join(TEST_DIR, 'templates'), { recursive: true });
fs.writeFileSync(
path.join(TEST_DIR, 'templates', 'default.hbs'),
`<!DOCTYPE html>
<html>
<head><title>{{title}}</title></head>
<body>{{{content}}}</body>
</html>`
);
fs.writeFileSync(
path.join(TEST_DIR, 'templates', 'data-page.hbs'),
`<!DOCTYPE html>
<html>
<head><title>{{title}}</title></head>
<body>
<div class="site-name">{{site.data.site.name}}</div>
{{{content}}}
</body>
</html>`
);
fs.writeFileSync(
path.join(TEST_DIR, 'templates', 'report.hbs'),
`<!DOCTYPE html>
<html>
<head><title>{{item.title}}</title></head>
<body>
<h1>{{item.title}}</h1>
<div class="slug">{{slug}}</div>
<div class="site-name">{{site.data.site.name}}</div>
</body>
</html>`
);
fs.mkdirSync(path.join(TEST_DIR, 'data', 'reports'), { recursive: true });
fs.writeFileSync(
path.join(TEST_DIR, 'data', 'site.json'),
JSON.stringify({ name: 'Fixture Site' }, null, 2)
);
fs.writeFileSync(
path.join(TEST_DIR, 'data', 'reports', 'songs.json'),
JSON.stringify({
'song-a': { title: 'Song A' },
'song-b': { title: 'Song B' }
}, null, 2)
);
fs.mkdirSync(path.join(TEST_DIR, 'collections'), { recursive: true });
fs.writeFileSync(
path.join(TEST_DIR, 'collections', 'reports.json'),
JSON.stringify({
source: 'reports.songs',
template: 'report',
output: 'reports/:slug.html'
}, null, 2)
);
// Create test static file
fs.mkdirSync(path.join(TEST_DIR, 'static'), { recursive: true });
fs.writeFileSync(
path.join(TEST_DIR, 'static', 'style.css'),
'body { margin: 0; }'
);
});
afterAll(() => {
// Clean up test fixtures
if (fs.existsSync(TEST_DIR)) {
fs.rmSync(TEST_DIR, { recursive: true });
}
});
test('should create build directory', () => {
// Run build with test fixtures
const buildScript = path.join(import.meta.dir, 'build.js');
const originalCwd = process.cwd();
try {
process.chdir(TEST_DIR);
execSync(`node ${buildScript}`, {
env: { ...process.env },
cwd: TEST_DIR
});
} finally {
process.chdir(originalCwd);
}
expect(fs.existsSync(TEST_BUILD_DIR)).toBe(true);
});
test('should process markdown files', () => {
const outputFile = path.join(TEST_BUILD_DIR, 'test.html');
expect(fs.existsSync(outputFile)).toBe(true);
const content = fs.readFileSync(outputFile, 'utf8');
expect(content).toContain('<title>Test Page</title>');
expect(content).toContain('<h1>Hello World</h1>');
});
test('should copy static files', () => {
const staticFile = path.join(TEST_BUILD_DIR, 'style.css');
expect(fs.existsSync(staticFile)).toBe(true);
const content = fs.readFileSync(staticFile, 'utf8');
expect(content).toBe('body { margin: 0; }');
});
test('should generate sitemap', () => {
const sitemapFile = path.join(TEST_BUILD_DIR, 'sitemap.xml');
expect(fs.existsSync(sitemapFile)).toBe(true);
const content = fs.readFileSync(sitemapFile, 'utf8');
expect(content).toContain('<?xml version="1.0"');
expect(content).toContain('<loc>');
});
test('should register partials from subdirectories', () => {
// Create test partial in subdirectory
const partialsDir = path.join(TEST_DIR, 'templates', 'partials');
const blogDir = path.join(partialsDir, 'blog');
fs.mkdirSync(blogDir, { recursive: true });
fs.writeFileSync(
path.join(blogDir, 'header.hbs'),
'<header>Blog Header</header>'
);
fs.writeFileSync(
path.join(partialsDir, 'footer.hbs'),
'<footer>Footer</footer>'
);
// Create content that uses the partial
fs.writeFileSync(
path.join(TEST_DIR, 'content', 'partial-test.md'),
`---
title: Partial Test
template: with-partial
---
Test content`
);
// Create template that uses partials
fs.writeFileSync(
path.join(TEST_DIR, 'templates', 'with-partial.hbs'),
`<!DOCTYPE html>
<html>
<head><title>{{title}}</title></head>
<body>
{{> blog/header}}
{{{content}}}
{{> footer}}
</body>
</html>`
);
// Run build
const buildScript = path.join(import.meta.dir, 'build.js');
const originalCwd = process.cwd();
try {
process.chdir(TEST_DIR);
execSync(`node ${buildScript}`, {
env: { ...process.env },
cwd: TEST_DIR
});
} finally {
process.chdir(originalCwd);
}
// Check output includes both partials
const outputFile = path.join(TEST_BUILD_DIR, 'partial-test.html');
expect(fs.existsSync(outputFile)).toBe(true);
const content = fs.readFileSync(outputFile, 'utf8');
expect(content).toContain('<header>Blog Header</header>');
expect(content).toContain('<footer>Footer</footer>');
});
test('should expose data files to templates under site.data', () => {
const outputFile = path.join(TEST_BUILD_DIR, 'data-test.html');
expect(fs.existsSync(outputFile)).toBe(true);
const content = fs.readFileSync(outputFile, 'utf8');
expect(content).toContain('<div class="site-name">Fixture Site</div>');
});
test('should generate collection-driven pages from structured data', () => {
const outputA = path.join(TEST_BUILD_DIR, 'reports', 'song-a.html');
const outputB = path.join(TEST_BUILD_DIR, 'reports', 'song-b.html');
expect(fs.existsSync(outputA)).toBe(true);
expect(fs.existsSync(outputB)).toBe(true);
const contentA = fs.readFileSync(outputA, 'utf8');
expect(contentA).toContain('<h1>Song A</h1>');
expect(contentA).toContain('<div class="slug">song-a</div>');
expect(contentA).toContain('<div class="site-name">Fixture Site</div>');
});
});