|
1 | 1 | import test from "node:test"; |
2 | 2 | import assert from "node:assert/strict"; |
| 3 | +import fs from "node:fs"; |
3 | 4 | import path from "node:path"; |
4 | 5 | import { |
5 | 6 | markdownToSearchText, |
6 | 7 | routePathFromContentFile, |
7 | 8 | searchFiltersFromContentFile, |
8 | 9 | } from "./build-docs-search-index.mjs"; |
9 | 10 |
|
| 11 | +/** |
| 12 | + * @typedef {{ scripts: { build: string, postbuild?: string } }} WebsitePackageJson |
| 13 | + * @typedef {{ tasks: { build: { outputs: string[] } } }} TurboJson |
| 14 | + */ |
| 15 | + |
| 16 | +/** |
| 17 | + * @param {unknown} value |
| 18 | + * @returns {value is Record<string, unknown>} |
| 19 | + */ |
| 20 | +const isRecord = (value) => |
| 21 | + typeof value === "object" && value !== null && !Array.isArray(value); |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {unknown} value |
| 25 | + * @returns {value is string[]} |
| 26 | + */ |
| 27 | +const isStringArray = (value) => |
| 28 | + Array.isArray(value) && value.every((item) => typeof item === "string"); |
| 29 | + |
| 30 | +/** |
| 31 | + * @param {string} filePath |
| 32 | + * @returns {unknown} |
| 33 | + */ |
| 34 | +const readJsonFile = (filePath) => |
| 35 | + /** @type {unknown} */ (JSON.parse(fs.readFileSync(filePath, "utf8"))); |
| 36 | + |
| 37 | +/** |
| 38 | + * @param {unknown} value |
| 39 | + * @returns {WebsitePackageJson} |
| 40 | + */ |
| 41 | +const assertWebsitePackageJson = (value) => { |
| 42 | + assert.ok(isRecord(value)); |
| 43 | + assert.ok(isRecord(value.scripts)); |
| 44 | + assert.equal(typeof value.scripts.build, "string"); |
| 45 | + assert.ok( |
| 46 | + value.scripts.postbuild === undefined || |
| 47 | + typeof value.scripts.postbuild === "string", |
| 48 | + ); |
| 49 | + |
| 50 | + return /** @type {WebsitePackageJson} */ (value); |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * @param {unknown} value |
| 55 | + * @returns {TurboJson} |
| 56 | + */ |
| 57 | +const assertTurboJson = (value) => { |
| 58 | + assert.ok(isRecord(value)); |
| 59 | + assert.ok(isRecord(value.tasks)); |
| 60 | + assert.ok(isRecord(value.tasks.build)); |
| 61 | + assert.ok(isStringArray(value.tasks.build.outputs)); |
| 62 | + |
| 63 | + return /** @type {TurboJson} */ (value); |
| 64 | +}; |
| 65 | + |
10 | 66 | void test("routePathFromContentFile maps content files to canonical docs routes", () => { |
11 | 67 | assert.equal( |
12 | 68 | routePathFromContentFile( |
@@ -78,3 +134,25 @@ void test("searchFiltersFromContentFile scopes docs records by platform", () => |
78 | 134 | undefined, |
79 | 135 | ); |
80 | 136 | }); |
| 137 | + |
| 138 | +void test("build configuration includes generated Pagefind assets", () => { |
| 139 | + const packageJson = assertWebsitePackageJson(readJsonFile("package.json")); |
| 140 | + assert.match( |
| 141 | + packageJson.scripts.build, |
| 142 | + /build-docs-search-index\.mjs/u, |
| 143 | + "website build must generate the Pagefind index when run by Turbo", |
| 144 | + ); |
| 145 | + assert.doesNotMatch( |
| 146 | + packageJson.scripts.postbuild ?? "", |
| 147 | + /build-docs-search-index\.mjs/u, |
| 148 | + "Turbo does not run package postbuild hooks for this task", |
| 149 | + ); |
| 150 | + |
| 151 | + const turboJson = assertTurboJson( |
| 152 | + readJsonFile(path.join("..", "..", "turbo.json")), |
| 153 | + ); |
| 154 | + assert.ok( |
| 155 | + turboJson.tasks.build.outputs.includes("public/_pagefind/**"), |
| 156 | + "Turbo must cache and restore the generated Pagefind assets", |
| 157 | + ); |
| 158 | +}); |
0 commit comments